Express: response.sendfile() fails with Error: Forbidden with path includes a symlink

Created on 10 Jan 2013  路  4Comments  路  Source: expressjs/express

here's the stack:

Error: Forbidden
at SendStream.error (/Users/jlage/Development/web/server/bootstrap/node_modules/express/node_modules/send/lib/send.js:145:16)
at SendStream.pipe (/Users/jlage/Development/web/server/bootstrap/node_modules/express/node_modules/send/lib/send.js:307:39)
at ServerResponse.res.sendfile (/Users/jlage/Development/web/server/bootstrap/node_modules/express/lib/response.js:336:8)
at module.exports.load_funds_list (/Users/jlage/Development/web/amber/controllers/ref-data.js:23:12)
at Object.oncomplete (fs.js:297:15)

Most helpful comment

That error comes from the path containing .. (up parent directory) and you didn't supply the root option. Try using sendfile like so:

res.sendfile(path, {'root': '/path/to/root/directory'});

The root option should be the directory you want to serve the files from. It is intended to prevent the path from containing things like .. so a user may get the server to serve a file outside that directory.

All 4 comments

That error comes from the path containing .. (up parent directory) and you didn't supply the root option. Try using sendfile like so:

res.sendfile(path, {'root': '/path/to/root/directory'});

The root option should be the directory you want to serve the files from. It is intended to prevent the path from containing things like .. so a user may get the server to serve a file outside that directory.

are you saying that it explicitly prevents you from using .. in any case?

Yes, path cannot contain .. unless you specify the root option.

yeah if you're not restricting with a root dir it's a potential security issue. You can resolve() to absolute paths to get around that but if you're accepting user input in those cases that's still a flaw, but if you're not then no big deal!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wxs77577 picture wxs77577  路  3Comments

cuni0716 picture cuni0716  路  3Comments

prashantLio picture prashantLio  路  3Comments

ER-GAIBI picture ER-GAIBI  路  3Comments

dmaks9 picture dmaks9  路  3Comments