Repository navigation
Keep the LoRA GGUF and compressed-tensors converters off the operator's Hugging Face token - #10263
Conversation
unslothai#10076 closed ambient-token leaks on Hub reads. Publish and export push still treated a missing body token as None, which huggingface_hub reads as the operator's HF_TOKEN. Resolve those writes with hf_token_arg so an API key stays anonymous unless it sends its own token, and so `hf_token or None` cannot launder the sentinel. UI sessions keep the ambient fallback.
The publish route test rejects `hf_token or None` in the function source. Mentioning that phrase in a comment made the check fail.
|
Confirmed the gap: |
Refuse Hub uploads when hf_token is the forced-anonymous sentinel instead of laundering it to None. Export worker, load_checkpoint, and push paths now use explicit helpers; add gguf worker and push coverage.
|
Confirmed end-to-end on Route: Orchestrator → worker: Worker → backend: Push: Publish: Behaviour change: yes — API-key export/publish that previously relied on the operator's ambient token now requires an explicit body token. That is intentional. Tests: route caller boundary, worker carries |
|
Reviewed the whole diff. The publish and export-push conversions are correct: One path was left open, and I pushed the fix in 9bd2479 rather than leave it for another round.
Repro against the pre-fix code, with That gap predates the PR, but the PR rewrites that exact expression while closing the same boundary next to it, so it belongs here. The fix resolves with The same commit drops Added Verification: |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9bd2479495
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| quantization_method = outtype, | ||
| # Forward the token so convert_lora_to_gguf.py can fetch a gated base's config. | ||
| token = hf_token or None, | ||
| token = normalize_token(hf_token), |
There was a problem hiding this comment.
Enforce the anonymous sentinel in LoRA GGUF conversion
When an API-key caller requests a local LoRA GGUF export without a body token and the host has an ambient HF_TOKEN, this False sentinel never reaches the converter: the save_method="lora" branch in unsloth/save.py calls _unsloth_save_lora_gguf(...) without its token argument, so that function defaults to None, calls get_token(), and exposes the operator credential to convert_lora_to_gguf.py. Consequently this path can still fetch a private base model with the host credential despite the new caller boundary. Thread the token through the local LoRA dispatch and ensure the converter environment is scrubbed when it is False.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Confirmed and fixed in 3db5682. unsloth_save_pretrained_gguf dropped its token at the save_method="lora" dispatch, so _unsloth_save_lora_gguf fell through to get_token(); it now forwards it. The second half of the report was right too: the converter env is os.environ.copy(), so token=False alone would still have leaked the inherited credential. On the sentinel it now drops every HF token alias and sets HF_HUB_DISABLE_IMPLICIT_TOKEN=1, which also covers the cached token file. Covered by test_gguf_lora_forwards_the_caller_token and test_lora_gguf_converter_is_denied_the_host_token in tests/saving/test_export_dispatch.py, both failing on the previous code.
|
Codex Review: Didn't find any major issues. Keep them coming! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
Re-reviewed this from first principles rather than from the diff, and ran the caller boundary end to end instead of trusting the route-level assertions. Two things came out of it: the boundary holds everywhere it is claimed, and one more credential leak on the LoRA GGUF path, now fixed in ae5d91b. The auth graph is correct for every caller class. I drove all five routes through the real What the sentinel is actually worth, measured. With an ambient The second leak. Running the converter as a real subprocess and asking it what credential it could reach, rather than asserting on the env dict, showed Also verified on real weights. unsloth/Qwen3-0.6B on an RTX 3090: load plus a merged export with Tests: Two things I am deliberately not touching here. The only red check is |
|
Codex Review: Didn't find any major issues. Swish! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
unslothai#10142 landed the export/publish caller boundary on main first (052b2fa), and unslothai#10317 rewrote the GGUF Hub push on top of it. This branch predates both, so its studio/backend changes were a second implementation of a landed fix and would have reverted the "don't convert twice" push branch, main's richer _resolve_export_hf_token(push_to_hub=...), and the deliberate `config.get("hf_token") or None` in the export worker, whose comment now records why the sentinel must not reach _load_config_json there. studio/ is taken from main wholesale. What remains is the part unslothai#10142 never touched and that still leaks: unsloth/save.py.
…y to both converters Three things came out of measuring this rather than reading it: 1008 cells of token shape x ambient env x HF_HUB_DISABLE_IMPLICIT_TOKEN x entry point, run against main and this branch, plus the token sentinel probed on huggingface_hub 0.34.0, 0.36.2, 1.0.0, 1.10.0, 1.23.0 and 1.30.0. The premise holds. token=False emits no authorization header in 216/216 in-process cells and 90/90 at the wire, on every one of those versions, with an env token and a stored token file and both legacy aliases all set. It is the second check in get_token_to_send(), ahead of get_token() and ahead of the implicit-token flag, and it has not moved since 2022. What did not hold: get_token() does not honour HF_HUB_DISABLE_IMPLICIT_TOKEN -- only the header builder does. So `if token is None: token = get_token()` hands back precisely the implicit token the operator switched off, and the string then satisfied `isinstance(token, str)` and cleared the flag in the child. An operator who set =1 had it overruled for a caller who passed nothing: 4 cells per LoRA entry point went from "child sends nothing" to "child sends the operator's credential". token_is_explicit is now captured before the fallback, and only an explicit token may clear the flag. An ambient caller gets the env main always gave them. _unsloth_save_compressed_tensors had the same env grant with no scrub and no False branch -- 0 of its 126 cells changed, so the sentinel was silently ignored there while being honoured next door. Both converters now go through one _apply_token_to_child_env, and a structural test fails if a third child env is ever built without it. That test is what found this one. "" and " " reached HfApi as a literal `Bearer ` header: huggingface_hub 1.x raises LocalProtocolError before sending, 0.x sends it and earns a 401. Studio pins >=1.23.0, so that is live. _clean_save_token reads them as "I passed nothing". It also resolves True, which huggingface_hub defines as "definitely use the cached token" and which previously fell through every branch into plain inheritance that an ambient =1 then voided. _resolve_imatrix_file no longer pre-resolves with get_token() before passing the result on as an explicit string, which was the same opt-out bypass in-process. Also scrubs HF_OIDC_RESOURCE / HF_OIDC_ID_TOKEN: hub >= 1.19 exchanges those inside get_token() ahead of HF_TOKEN, so a denied child that keeps them can mint the operator's credential in CI. Studio's hf_tokens.py already scrubs the first. The old comment claimed all five names were read by huggingface_hub. Only HF_TOKEN and HUGGING_FACE_HUB_TOKEN are; the rest are third-party conventions, kept because our child may run code that reads them. tests/saving: 539 passed against 517 on main, same single pre-existing failure on both. 15 of the 16 new tests fail on the previous commit.
|
Narrowed this to the part that is still open, and pushed two commits. Measured rather than read: 1008 cells of token shape x ambient env x The premise holds, and now it is measured across the supported range
The Studio half had already landed, so I dropped it#10142 merged as
What was left is real, and is live on main and on PyPI today
Four things the original commits got wrong, now fixedThe
Also scrubs TestsOf the tests the earlier commits added, four passed on the pre-fix tree, so they were not evidence for anything. The worst was The 16 new ones: 15 fail on
Scope, checked rather than assumedZero bytes under One thing I deliberately did not touch, since it is unrelated to the token boundary: |
Three follow-ups on top of the boundary work. Scrubbing the env is not the whole denial: get_token() still reads the stored token file, so a forced-anonymous child could resolve the operator's credential even with every alias removed. HF_HUB_DISABLE_IMPLICIT_TOKEN=1 stops the header being sent, but not the read, and the read is what an explicit get_token() call downstream would pick up. HF_TOKEN_PATH now points at os.devnull for that branch, so there is nothing to read and a stray write goes nowhere. save.py:1874 ran llama-gguf-split with text=True and no encoding, which is a cp1252 crash on Windows reading child output (unslothai#2660). It is the last text-mode subprocess call in the file without one, and tests/saving already had a rule for it that fails on main. All 8 such calls now declare it. Merged _HF_OIDC_ENV_KEYS into _HF_TOKEN_ENV_KEYS (one list, same scrub) and cut the comments back to what is not obvious from the code. tests/saving: 540 passed, 0 failed, against 517 passed and 1 failed on main. Ruff clean.
|
Pushed Three follow-ups on the boundary work. The token file was still readable. Scrubbing the env aliases is only half the denial: The utf-8 gap. Tidying. AST checks on the result, since most of this is structural rather than behavioural: The last one matters most for anyone already calling these: One thing still open and deliberately not in this PR: the denial is network-layer, so a private base config already sitting in the shared HF cache is readable by the child regardless. Studio has |
|
@codex review |
|
@codex security review |
|
Codex Review: Didn't find any major issues. Bravo. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Part of #10147 (item 3). Originally covered the Studio publish and export push routes too; #10142 landed those on main first, so this is now only the part still open.
The problem
save_pretrained_gguf(save_method="lora", token=False)ignores the token and uses the operator's.The LoRA branch dispatched to
_unsloth_save_lora_ggufwithout forwardingtoken, so the callee'sif token is None: token = get_token()resolved the ambient credential. The push variant already forwarded it; only the local save path was missed. Both converters then build their subprocess env withos.environ.copy(), and a child inherits everything, so setting no token is not the same as denying one.Live on
mainand inunsloth==2026.9.2on PyPI. Silent, no error.Changes
unsloth/save.pyonly.tokenon the LoRA GGUF dispatch._apply_token_to_child_envfor both converter subprocesses.Falsescrubs every token alias, pointsHF_TOKEN_PATHatos.devnulland setsHF_HUB_DISABLE_IMPLICIT_TOKEN=1; an explicit token scrubs then grants; an ambient one is promoted exactly as before.HF_HUB_DISABLE_IMPLICIT_TOKEN.get_token()does not honour that flag, so a token it returns is the implicit one the operator switched off, and clearing it for a caller who passed nothing overrode a deliberate setting._unsloth_save_compressed_tensorshad the same env grant with no scrub and noFalsebranch. It now uses the same helper._clean_save_tokenreads""and" "as "nothing passed". They previously reached the Hub as a literalBearerheader, which huggingface_hub 1.x rejects withLocalProtocolErrorbefore sending. It also resolvesTrue, which the Hub defines as "use the cached token"._resolve_imatrix_fileno longer pre-resolves withget_token()before passing the result on as an explicit string, which was the same flag bypass in-process.HF_OIDC_RESOURCE/HF_OIDC_ID_TOKEN. Hub >= 1.19 exchanges those insideget_token()ahead ofHF_TOKEN, so a denied child that keeps them can still mint a credential in CI.save.py:1874ranllama-gguf-splitwithtext=Trueand noencoding, a cp1252 crash on Windows reading child output ([Bug]UnicodeDecodeErroron Windows due to missing encoding="utf-8" in open() call #2660). All 8 text-mode subprocess calls in the file now declare it.Verification
The sentinel itself, on huggingface_hub 0.34.0, 0.36.2, 1.0.0, 1.10.0, 1.23.0 and 1.30.0, covering the whole range
pyproject.tomlallows:token=Falseemitted no authorization header in 216/216 in-process cells and 90/90 at the wire, against a loopback endpoint with an env token, a stored token file and both legacy aliases all set.Before and after, 1008 cells of token shape x ambient env x
HF_HUB_DISABLE_IMPLICIT_TOKENx entry point, run againstmainand this branch. Parentos.environwas untouched in every one.token=False, LoRA savetoken=None(ordinary user)token=False, compressed-tensorstests/saving: 540 passed, 0 failed, against 517 passed and 1 failed onmain. Ruff clean. 15 of the 16 added tests fail on the previous commit; the sixteenth is a labelled regression guard.test_every_converter_child_env_goes_through_the_token_boundarywalks the AST foros.environ.copy()and fails if a child env is built without applying the boundary. That is what found the compressed-tensors site.Not covered
The denial is network-layer. A private base config already in the shared HF cache is still readable by the child; Studio has
ANONYMOUS_CACHE_IDENTITYfor that,unsloth/save.pyhas no equivalent. Separate change.