Skip to content

Commit

Permalink
Prevent file traversals (#6833)
Browse files Browse the repository at this point in the history
* prevent file traversal

* fix

* add changeset

* unit test

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
  • Loading branch information
abidlabs and gradio-pr-bot committed Dec 18, 2023
1 parent 992d540 commit 1b9d423
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/tough-zoos-move.md
@@ -0,0 +1,5 @@
---
"gradio": minor
---

feat:Prevent file traversals
2 changes: 1 addition & 1 deletion gradio/utils.py
Expand Up @@ -932,7 +932,7 @@ def is_in_or_equal(path_1: str | Path, path_2: str | Path):
"""
path_1, path_2 = abspath(path_1), abspath(path_2)
try:
if str(path_1.relative_to(path_2)).startswith(".."): # prevent path traversal
if ".." in str(path_1.relative_to(path_2)): # prevent path traversal
return False
except ValueError:
return False
Expand Down
1 change: 1 addition & 0 deletions test/test_utils.py
Expand Up @@ -403,6 +403,7 @@ def test_is_in_or_equal():
assert is_in_or_equal("/home/usr/notes.txt", "/home/usr/")
assert not is_in_or_equal("/home/usr/subdirectory", "/home/usr/notes.txt")
assert not is_in_or_equal("/home/usr/../../etc/notes.txt", "/home/usr/")
assert not is_in_or_equal("/safe_dir/subdir/../../unsafe_file.txt", "/safe_dir/")


@pytest.mark.parametrize(
Expand Down

0 comments on commit 1b9d423

Please sign in to comment.