Skip to content

Commit e7d6e4f

Browse files
authored
Merge pull request webtorrent#1260 from diracdeltas/fix/add-hostname-opt
Add hostname option to mitigate DNS rebinding
2 parents fb20cfd + 7c107e6 commit e7d6e4f

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

docs/api.md

+1
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ Returns an `http.Server` instance (got from calling `http.createServer`). If
327327
```js
328328
{
329329
origin: String // Allow requests from specific origin. `false` for same-origin. [default: '*']
330+
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`]
330331
}
331332
```
332333

lib/server.js

+7
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ function Server (torrent, opts) {
5151
// deny them
5252
if (req.headers.origin == null) return false
5353

54+
// If a 'hostname' string is specified, deny requests with a 'Host'
55+
// header that does not match the origin of the torrent server to prevent
56+
// DNS rebinding attacks.
57+
if (opts.hostname && req.headers.host !== `${opts.hostname}:${server.address().port}`) {
58+
return false
59+
}
60+
5461
// The user allowed all origins
5562
if (opts.origin === '*') return true
5663

0 commit comments

Comments
 (0)