Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ You can use any of the following options:
| [`renderSingle`](#rendersingle-boolean) | If a directory only contains one file, render it |
| [`symlinks`](#symlinks-boolean) | Resolve symlinks instead of rendering a 404 error |
| [`etag`](#etag-boolean) | Calculate a strong `ETag` response header, instead of `Last-Modified` |
| [`whitelist`](#whitelist-array) | If exists, Only IPs in this list are allowed to access resources |

### public (String)

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"bytes": "3.0.0",
"content-disposition": "0.5.2",
"fast-url-parser": "1.1.3",
"ipaddr.js": "^2.0.1",
"mime-types": "2.1.18",
"minimatch": "3.0.4",
"path-is-inside": "1.0.2",
Expand Down
20 changes: 20 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const bytes = require('bytes');
const contentDisposition = require('content-disposition');
const isPathInside = require('path-is-inside');
const parseRange = require('range-parser');
const ipaddr = require('ipaddr.js');

// Other
const directoryTemplate = require('./directory');
Expand Down Expand Up @@ -545,7 +546,26 @@ const getHandlers = methods => Object.assign({
sendError
}, methods);

const whitelistApplier = (request, whitelist) => {
let ip = request.ip || request.connection.remoteAddress || request.socket.remoteAddress || request.connection.socket.remoteAddress;

if (ipaddr.isValid(ip)) {
ip = ipaddr.process(ip).toString();
}

return whitelist.includes(ip);
};

module.exports = async (request, response, config = {}, methods = {}) => {
if (config.whitelist) {
const requestIsAllowed = await whitelistApplier(request, config.whitelist);
if (!requestIsAllowed) {
const err = new Error('Forbidden');
err.statusCode = 403;
throw err;
}
}

const cwd = process.cwd();
const current = config.public ? path.resolve(cwd, config.public) : cwd;
const handlers = getHandlers(methods);
Expand Down
Loading