Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dangerous example #76

Open
bitdivine opened this issue Jan 9, 2020 · 1 comment
Open

Dangerous example #76

bitdivine opened this issue Jan 9, 2020 · 1 comment

Comments

@bitdivine
Copy link

The example server.js:

var staticHandler = function(request, response) {
  var path = request.url;

  fs.readFile(__dirname + path, function(err, content) {

doesn't validate the url, so there is nothing stopping it from being e.g. /../spec/server.key (given a few lines later). Given that people are likely to copy the example, setting a safe precedent might be a good idea! :-)

@jcoglan
Copy link
Collaborator

jcoglan commented Feb 14, 2020

That's a really good point, I don't want people to put that code in production. Is there a library for automatically sanitising the path? Otherwise I could add something like this to detect path traversal:

  if (/(^|\/)\.\.?(\/|$)/.test(path)) {
    // reject request
  }

I don't like how opaque that regex is but I also want to make this code safe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants