-
Notifications
You must be signed in to change notification settings - Fork 6.8k
build(dev-app): prevent directory traversal #18494
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Mind giving context on why this came up?
res.end('Error: Detected directory traversal'); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be best to just disallow ../
in the requests? If we just want to go with that logic, then we could probably simplify it by doing it using path.relative
? WDYT?
for (const rootPath of this._rootPaths) {
const relativePath = path.relative(rootPath, path.join(rootPath, req.url));
if (relativePath.startsWith('../')) { throw }
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used the approach recommended by Google's security team. As I see it, it's safer because it ensures that, at the very end, the resolved URL didn't escape the webserver root.
My first approach wouldn't have caught all cases (https://wikipedia.org/wiki/Directory_traversal_attack#Variations_of_directory_traversal), and is more future proof in case mre ways of directory traversal are introduced.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, that sounds reasonable.
I'm unsure about path.resolve
though. The root paths are not file system paths in the dev-server, but rather Bazel manifest/runfile paths, so resolving paths from the cwd
is incorrect. It doesn't impact the URL escape check in general, but it's technically not correct. It might be better to use path.relative
to determine if the paths escapes the root path.
(cherry picked from commit f3d43fe)
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
No description provided.