diff --git a/docs/api.md b/docs/api.md index d1c0a873..013babd3 100644 --- a/docs/api.md +++ b/docs/api.md @@ -327,6 +327,7 @@ Returns an `http.Server` instance (got from calling `http.createServer`). If ```js { origin: String // Allow requests from specific origin. `false` for same-origin. [default: '*'] + hostname: String // If specified, only allow requests whose `Host` header matches this hostname. Note that you should not specify the port since this is automatically determined by the server. Ex: `localhost` [default: `undefined`] } ``` diff --git a/lib/server.js b/lib/server.js index be45ddc1..5b241816 100644 --- a/lib/server.js +++ b/lib/server.js @@ -51,6 +51,13 @@ function Server (torrent, opts) { // deny them if (req.headers.origin == null) return false + // If a 'hostname' string is specified, deny requests with a 'Host' + // header that does not match the origin of the torrent server to prevent + // DNS rebinding attacks. + if (opts.hostname && req.headers.host !== `${opts.hostname}:${server.address().port}`) { + return false + } + // The user allowed all origins if (opts.origin === '*') return true