Skip to content

fix: resolve HF symlinks in from_hub via temp local_dir - #1495

Merged
planetf1 merged 1 commit into
generative-computing:mainfrom
Disha714:bot-fix
Aug 28, 2026
Merged

planetf1 merged 1 commit into
generative-computing:mainfrom
Disha714:bot-fix

Conversation

@Disha714

@Disha714 Disha714 commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Fixes #1492.

Summary of Changes

  • from_hub downloads via huggingface_hub.snapshot_download with the default cache layout, where io_configs/*/io.yaml entries in the returned snapshot directory are symlinks into a sibling blobs/ directory outside the snapshot root. This violated from_model_directoryx27s documented self-contained-directory contract and tripped the path-escape check.
  • Rather than loosening or touching the security check in from_model_directory, this PR updates from_hub to download files into a temporary self-contained local directory via with tempfile.TemporaryDirectory() as local_dir: and passing local_dir=local_dir to snapshot_download. This populates the temporary directory with real files rather than symlinks escaping the directory.
  • Passes the return value of snapshot_download (downloaded_dir) to from_model_directory so that both runtime operations and mocked test fixtures operate reliably.

Verification & Security

  • Security Barrier Untouched: Verified locally that all security-relevant unit tests (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.
  • Regression Testing: Added a dedicated unit test test_from_hub_requests_local_dir to explicitly verify that from_hub passes local_dir to snapshot_download. Updated existing test mocks across TestFromHub and TestFromSource to expect local_dir=ANY.
  • Test Suite Results: Executed uv run --extra switch pytest test/backends/test_adapters/test_embedded_adapter.py -v; all 38 tests passed locally without errors or regressions.

@Disha714
Disha714 requested a review from a team as a code owner August 5, 2026 16:16
@Disha714 Disha714 changed the title Fix #1492: resolve HF symlinks in from_hub via temp local_dir to satisfy path-escape check fix: resolve HF symlinks in from_hub via temp local_dir Aug 5, 2026
@github-actions github-actions Bot added the bug Something isn't working label Aug 5, 2026

@AngeloDanducci AngeloDanducci left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 planetf1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

(superseded — see consolidated review below)

@planetf1 planetf1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

(superseded — see consolidated review below)

@planetf1 planetf1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

(superseded — see consolidated review below)

Comment thread mellea/backends/adapters/adapter.py Outdated
Comment thread test/backends/test_adapters/test_embedded_adapter.py Outdated
Comment thread mellea/backends/adapters/adapter.py Outdated

@planetf1 planetf1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The symlink-escape fix is correct, but cache_dir is silently defeated:

  • Cache always cold: local_dir prevents cache_dir from ever being written to; each call is ~51 HTTP round-trips with zero reuse
  • Offline broken: HF_HUB_OFFLINE=1 raises LocalEntryNotFoundError

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.

@planetf1

Copy link
Copy Markdown
Contributor

@Disha714 Hi - are you able to update the pr to address the comments & resolve conflicts
Let us know if you have any questions or need to hand over

@planetf1

Copy link
Copy Markdown
Contributor

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>
@planetf1

planetf1 commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

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:

  • 41 adapter tests
  • full non-qualitative suite: 4123 passed, 47 skipped, 128 deselected
  • ruff, mypy, pre-commit, and Hub 0.33.4 compatibility check

@planetf1
planetf1 dismissed their stale review August 27, 2026 10:55

Superseded by the updated implementation and fresh review request.

@planetf1
planetf1 requested review from a team and AngeloDanducci August 27, 2026 10:56
@planetf1
planetf1 added this pull request to the merge queue Aug 28, 2026
Merged via the queue into generative-computing:main with commit 8b3359d Aug 28, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

from_hub passes HF snapshot path to from_model_directory, breaking 0.7.0 path-escape check

4 participants