Host Validation Middleware

0.1.4 · active · verified Wed Apr 22

host-validation-middleware is an npm package providing Connect/Express-compatible middleware designed to protect against DNS rebinding attacks by validating the `Host` header in incoming HTTP requests. The current stable version is 0.1.4, indicating it's still in an early development phase but receives maintenance patches. It differentiates itself by offering flexible host matching, including subdomain wildcard support (e.g., `.mydomain.com`), and automatically allowing `localhost` and IP addresses which are not susceptible to DNS rebinding. While crucial for HTTP development environments, the package explicitly notes that its utility is significantly reduced for HTTPS production sites, as DNS rebinding attacks are generally ineffective against encrypted connections. Its core logic is inspired by the `allowedHosts` option found in `webpack-dev-server`.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates setting up host validation with Connect, allowing specified domains and subdomains, and customizing error responses.

import connect from 'connect';
import { hostValidationMiddleware } from 'host-validation-middleware';

const app = connect();

app.use(
  hostValidationMiddleware({
    allowedHosts: Object.freeze(['example.com', '.mydomain.com', 'localhost:3000']),
    generateErrorMessage: (hostname) => `Access denied for host: ${hostname}`,
    errorResponseContentType: 'text/plain'
  })
);

app.use((req, res) => {
  res.end('Hello, world!');
});

app.listen(3000, () => {
  console.log('Server running on http://localhost:3000');
});

view raw JSON →