fix: resolve HF symlinks in from_hub via temp local_dir - #1495
Conversation
AngeloDanducci
left a comment
There was a problem hiding this comment.
Hello, thanks for the contribution!
Overall I think this look good. One test question and I will ping @planetf1 for his 2C since he was the original issue author.
May be worth adding another test where from_hub loads adapters when snapshot_download populates local_dir with real files (as huggingface_hub does with local_dir set).
planetf1
left a comment
There was a problem hiding this comment.
The symlink-escape fix is correct, but cache_dir is silently defeated:
- Cache always cold:
local_dirpreventscache_dirfrom ever being written to; each call is ~51 HTTP round-trips with zero reuse - Offline broken:
HF_HUB_OFFLINE=1raisesLocalEntryNotFoundError
Suggested fix: Use a persistent directory under cache_dir/HF_HUB_CACHE + repo + revision instead of TemporaryDirectory(). Verified: restores caching (0.9s → 0.1s warm) and offline operation while keeping the self-contained layout.
Inline comments on test gap and docstring nit follow.
|
@Disha714 Hi - are you able to update the pr to address the comments & resolve conflicts |
|
As we've not heard, I'll push fixes to the comments above so that we can get this merged - thanks for the pr. |
Resolve Hugging Face snapshot blob symlinks through a persistent repository-and-revision local directory. This keeps from_model_directory strict while preserving repeat-download and offline behaviour. Add regression coverage for cache-style symlinks and revision-isolated materialisation directories. Assisted-by: Claude Code Assisted-by: Codex Signed-off-by: Disha714 <nsaidisha@gmail.com> Signed-off-by: Nigel Jones <jonesn@uk.ibm.com>
|
I've actioned the review fixes & done a further review -- this has been updated in b94cd9e. This is now rebased on current main. from_hub resolves through the normal Hub cache and materialises only the adapter index and referenced configs into a commit-keyed self-contained directory, keeping the local path guard strict. Validation:
|
Superseded by the updated implementation and fresh review request.
8b3359d
Fixes #1492.
Summary of Changes
from_hubdownloads viahuggingface_hub.snapshot_downloadwith the default cache layout, whereio_configs/*/io.yamlentries in the returned snapshot directory are symlinks into a siblingblobs/directory outside the snapshot root. This violatedfrom_model_directoryx27s documented self-contained-directory contract and tripped the path-escape check.from_model_directory, this PR updatesfrom_hubto download files into a temporary self-contained local directory viawith tempfile.TemporaryDirectory() as local_dir:and passinglocal_dir=local_dirtosnapshot_download. This populates the temporary directory with real files rather than symlinks escaping the directory.snapshot_download(downloaded_dir) tofrom_model_directoryso that both runtime operations and mocked test fixtures operate reliably.Verification & Security
test_path_traversal_in_io_config_raises,test_symlink_escape_in_io_config_raises) pass cleanly, confirming that the directory traversal / path-escape defenses remain 100% intact for any malicious input.test_from_hub_requests_local_dirto explicitly verify thatfrom_hubpasseslocal_dirtosnapshot_download. Updated existing test mocks acrossTestFromHubandTestFromSourceto expectlocal_dir=ANY.uv run --extra switch pytest test/backends/test_adapters/test_embedded_adapter.py -v; all 38 tests passed locally without errors or regressions.