Skip to content

Studio: hide host cache inventory from API keys - #10265

Closed
Souravrajvi0 wants to merge 7 commits into
unslothai:mainfrom
Souravrajvi0:feat/hub-cached-inventory-caller-boundary-fef2
Closed

Souravrajvi0 wants to merge 7 commits into
unslothai:mainfrom
Souravrajvi0:feat/hub-cached-inventory-caller-boundary-fef2

Conversation

@Souravrajvi0

Copy link
Copy Markdown
Contributor

Summary

Part of #10147 (item 2 — /api/hub/cached-models and /api/hub/cached-gguf).

These routes enumerate cached repo ids, sizes, capabilities and absolute cache paths. #10076 only changed the token annotation; they still answered any authenticated caller, including an API key denied the ambient token. They never claimed Hub authorization: they are a local filesystem inventory.

This draws the same caller boundary as the other #10147 slices:

  • UI session (allow_ambient_hf_token): full host scan, unchanged
  • API key: empty {cached: [], scan_confirmed: true} — even if the key sends X-Unsloth-HF-Token

An HF token is not a license to inventory this machine. Empty list rather than 403 so existing clients that iterate cached keep working.

The in-process CLI catalog (unsloth_cli/_model_catalog.py) still scans disk directly and is unchanged. Legacy /api/models/cached-models is unchanged in this PR (no token, out of the issue's named scope).

Test plan

  • python -m pytest tests/test_hub_token_caller_identity.py -k hub_cache_inventory (4 passed)

/api/hub/cached-models and /cached-gguf list repo ids, sizes,
capabilities and absolute cache paths. After unslothai#10076 they take a token
but still answered any authenticated caller, including an API key
denied the ambient credential.

UI sessions keep the full local scan. API keys get an empty listing so
they cannot inventory this host. The in-process CLI catalog still
walks disk directly and is unchanged.
@danielhanchen

Copy link
Copy Markdown
Member

allow_ambient_hf_tokenresolves throughauthenticated_via_api_key, which also returns True for a keyless caller, so /api/hub/cached-modelsand/api/hub/cached-gguf` would return an empty list to local CLI and API consumers that enumerate downloaded models today. Could you confirm that case and say whether an empty list rather than a 403 is the behaviour you want there before I review?

Use authenticated_with_sk_unsloth_key instead of allow_ambient_hf_token
on /api/hub/cached-models and /cached-gguf. Keyless local CLI consumers
still enumerate downloaded models; only sk-unsloth API keys get an empty
list (not a 403, to avoid leaking that the host has cached repos).
The keyless cached-models/cached-gguf test 401ed because
get_request_hf_token still depends on allow_ambient_hf_token/HTTPBearer.
Override that dependency and unit-test the sk-unsloth helper itself.
@Souravrajvi0

Copy link
Copy Markdown
Contributor Author

Good catch — allow_ambient_hf_token was the wrong dependency here because authenticated_via_api_key also returns true for keyless callers.

Change: /api/hub/cached-models and /api/hub/cached-gguf now gate on authenticated_with_sk_unsloth_key instead. Only a real sk-unsloth-* bearer gets an empty list; keyless local CLI/API consumers and UI sessions still get the full host scan.

Empty list vs 403: still an empty list for API keys — a 403 would confirm that the host has cached repos. scan_confirmed: true keeps the response shape stable for consumers that only look at cached.

Tests: inventory hidden from sk-unsloth key; UI session and keyless caller still scan; direct unit test on the new helper.

Fix import-hoist lint: module-level hoist looked unused to the verifier.
@danielhanchen

Copy link
Copy Markdown
Member

Thanks for switching to the sk-unsloth-only helper, the keyless CLI case I raised is covered by the new tests. One thing before I review: an integration using an API key to list downloaded models now silently gets an empty cached with scan_confirmed: true, and legacy /api/models/cached-models still returns the full scan, so could you confirm that split is intended?

Resolve test_hub_token_caller_identity merge and assert legacy
/api/models/cached-models stays ungated for sk-unsloth keys.
@Souravrajvi0

Copy link
Copy Markdown
Contributor Author

@danielhanchen — yes, the split is intentional.

/api/hub/cached-models and /api/hub/cached-gguf: callers presenting an sk-unsloth-* API key get cached: [] with scan_confirmed: true so integrations cannot enumerate host cache paths, repo ids, or sizes. UI sessions and keyless local CLI callers still run the disk scan (covered by test_hub_cache_inventory_still_scans_for_a_keyless_caller).

GET /api/models/cached-models (legacy): unchanged — subject-scoped listing for in-app / compatibility callers, not treated as external host inventory. An API key hitting that route still gets the normal scan; only the Hub inventory routes apply the sk-unsloth guard. Docstring + test_legacy_models_cached_listing_is_not_hub_inventory_gated pin that.

Merged latest main and pushed on feat/hub-cached-inventory-caller-boundary-fef2.

@danielhanchen

Copy link
Copy Markdown
Member

Thanks for the PR and for the follow-up on the keyless CLI case! Closing this one since it has been superseded by #11026, which landed on main and covers the same two routes (/api/hub/cached-models and /api/hub/cached-gguf).

#11026 took a different approach to the same concern: the inventory still answers every authenticated caller with repo ids, sizes, capabilities and partial state, but for an sk-unsloth API key every host path in the payload is replaced by an opaque reference (redact_host_paths), and the scan roots are dropped. That removes the actual host detail (absolute cache root, home directory, account name) without breaking integrations that use an API key to list downloaded models, which was the concern raised earlier in this thread. Returning an empty list for API keys on top of that would reverse that decision, and the branch now conflicts with the rewritten handlers anyway.

Item 2 of #10147 is addressed by #11026. Thanks again for working through the caller boundary slices!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants