Validate the file name derived from origin in get_file - #23330
Validate the file name derived from origin in get_file#23330SABITHSAHEB wants to merge 2 commits into
origin in get_file#23330Conversation
There was a problem hiding this comment.
Code Review
This pull request enhances the security and robustness of the get_file utility in keras/src/utils/file_utils.py by validating the parsed file name from the origin URL. It prevents directory traversal and escaping of the target directory by rejecting invalid file names such as ., .., or those containing drive letters, raising a ValueError in these cases. Corresponding unit tests have been added to verify this behavior. There are no review comments, so no further feedback is provided.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #23330 +/- ##
==========================================
+ Coverage 80.46% 84.17% +3.71%
==========================================
Files 466 468 +2
Lines 70017 70501 +484
Branches 11582 11663 +81
==========================================
+ Hits 56339 59345 +3006
+ Misses 10824 8177 -2647
- Partials 2854 2979 +125
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Please fill out the contributor agreement in the PR description. |
JyotinderSingh
left a comment
There was a problem hiding this comment.
Thanks for the fix!
There was a problem hiding this comment.
Nit. There is no gap after "'{origin}'." and before "Please specific ..." in the string builder. Would be nice to fix it as part of the PR.
There was a problem hiding this comment.
Good catch, that was missing before this PR too. Added the space and pushed, the message now reads "... provided: ''. Please specify the fname argument."
|
Both of the review-commitment items are checked in the description, and I'll keep this one moving until it lands. The missing space you flagged is fixed in 103bd50. |
Description
get_filerejects separators in thefnameargument, but whenfnameis omitted the name comes frombasename(urlsplit(origin).path), which never reaches that check.basenamedrops separators and still returns..for a URL path ending in/.., so the download and extraction targets resolve one level above<cache_dir>/<cache_subdir>: withextract=Truethe archive members are written intocache_diritself, over files such askeras.json, and without itget_filereturns a directory and never downloads at all.basenamekeepsC:and the laterjointhen dropsdatadir.Treating those names as unparsable reuses the error the function already raises for a URL that carries no file name. The test covers the
/,/.and/..forms; like #23287 the Windows case is not tested since there is no Windows runner.Contributor Agreement
Please review our AI-Assisted Contribution Policy and check all boxes below before submitting your PR for review:
Note: Failing to adhere to this agreement may result in your future PRs no longer being reviewed.