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

[Dashboard] Prevent Directory Traversal #39018

Merged
merged 10 commits into from Sep 26, 2023

Conversation

ijrsvt
Copy link
Contributor

@ijrsvt ijrsvt commented Aug 28, 2023

Why are these changes needed?

  • Ensure we can only go to subdirectories of logs and static resources.

Related issue number

Checks

  • I've signed off every commit(by using the -s flag, i.e., git commit -s) in this PR.
  • I've run scripts/format.sh to lint the changes in this PR.
  • I've included any doc changes needed for https://docs.ray.io/en/master/.
    • I've added any new APIs to the API Reference. For example, if I added a
      method in Tune, I've added it in doc/source/tune/api/ under the
      corresponding .rst file.
  • I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/
  • Testing Strategy
    • Unit tests
    • Release tests
    • This PR is not tested :(


# If the destination is not relative to the expected directoy, then the user is attempting
# path traversal, so deny the request
if not pathlib.Path(os.path.abspath(request.path)).is_relative_to(parent):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if not pathlib.Path(os.path.abspath(request.path)).is_relative_to(parent):
if not pathlib.Path(request.path).absolute().is_relative_to(parent):

Mixing path libraries for a particular reason? (If its load bearing can you explain it in a comment?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to resolve()

@@ -128,6 +129,17 @@ def get_address(self):
assert self.http_host and self.http_port
return self.http_host, self.http_port

@aiohttp.web.middleware
async def path_clean_middleware(self, request, handler):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to normalized all path values before passing it forward in the routing handler? (can we even do that in a middleware?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand what you mean?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking that since these APIs lean on the request path itself and not a parameter or data field; if we could normalize the request.Path field itself before it was handled by the router/mux, then this would 404 instead of getting into the final handler.

That said, that almost certainly isn't something we can do from within Starlette middleware? Noop this idea xD

Signed-off-by: Ian Rodney <ian.rodney@gmail.com>
Signed-off-by: Ian Rodney <ian.rodney@gmail.com>
Signed-off-by: Ian Rodney <ian.rodney@gmail.com>
Signed-off-by: Ian Rodney <ian.rodney@gmail.com>
Signed-off-by: Ian Rodney <ian.rodney@gmail.com>
@ijrsvt ijrsvt force-pushed the fix-some-dashboard-path-traversals branch from 2f2f980 to 2e72429 Compare August 29, 2023 15:24
dashboard/http_server_head.py Outdated Show resolved Hide resolved
Signed-off-by: Ian Rodney <ian.rodney@gmail.com>
Signed-off-by: Ian Rodney <ian.rodney@gmail.com>
@ijrsvt
Copy link
Contributor Author

ijrsvt commented Sep 26, 2023

Failing test is unrelated--merging

@ijrsvt ijrsvt merged commit 542ca64 into ray-project:master Sep 26, 2023
23 of 24 checks passed
@ijrsvt ijrsvt deleted the fix-some-dashboard-path-traversals branch September 26, 2023 05:05
simonsays1980 pushed a commit to simonsays1980/ray that referenced this pull request Sep 26, 2023
Ensure we can only go to subdirectories of logs and static resources.
Signed-off-by: Simon Zehnder <simon.zehnder@gmail.com>
vymao pushed a commit to vymao/ray that referenced this pull request Oct 11, 2023
Ensure we can only go to subdirectories of logs and static resources.

Signed-off-by: Victor <vctr.y.m@example.com>
pcmoritz pushed a commit to pcmoritz/ray-1 that referenced this pull request Nov 30, 2023
Ensure we can only go to subdirectories of logs and static resources.
aslonnie pushed a commit that referenced this pull request Nov 30, 2023
* [Dashboard] Prevent Directory Traversal (#39018)

Ensure we can only go to subdirectories of logs and static resources.

* [core][state][log] State API should not allow reading files outside of the ray log directory on all ray nodes. (#41467)

State API log retrieval has a security bug where one could pass:

relative paths like "../../../xxx" to get file outside of ray's log dir
absolute path that's refers to other files to get file somewhere else.
This PR fixes both issues such that one could only read logs under the ray logs directory.

---------

Signed-off-by: rickyyx <rickyx@anyscale.com>

* [Dashboard] Migrate Logs page to use state api. (#41474)

Migrates to use state-api, to unify behavior with CLI and UI
Delete log_proxy API, it's legacy and has some security issues.

---------

Signed-off-by: Alan Guo <aguo@anyscale.com>

* [core][state][log] Enable following symlinks that point outside of the `root_log_dir` when resolving paths (#41502)

Follow-up to: #41467. The change incidentally broke log retrieval on mac os because /tmp is a symlink to /private/tmp.

This PR avoids resolving the symlink until after we do the subdir check. This solves the mac os problem and generically enables file paths that contain symlinks outside of the root_log_dir.

---------

Signed-off-by: Edward Oakes <ed.nmi.oakes@gmail.com>

---------

Signed-off-by: rickyyx <rickyx@anyscale.com>
Signed-off-by: Alan Guo <aguo@anyscale.com>
Signed-off-by: Edward Oakes <ed.nmi.oakes@gmail.com>
Co-authored-by: Ian Rodney <ian.rodney@gmail.com>
Co-authored-by: Ricky Xu <xuchen727@hotmail.com>
Co-authored-by: Alan Guo <aguo@anyscale.com>
Co-authored-by: Edward Oakes <ed.nmi.oakes@gmail.com>
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

Successfully merging this pull request may close these issues.

None yet

2 participants