Skip to content

Keep the LoRA GGUF and compressed-tensors converters off the operator's Hugging Face token - #10263

Merged
danielhanchen merged 13 commits into
unslothai:mainfrom
Souravrajvi0:feat/hub-write-caller-boundary-fef2
Sep 7, 2026
Merged

danielhanchen merged 13 commits into
unslothai:mainfrom
Souravrajvi0:feat/hub-write-caller-boundary-fef2

Conversation

@Souravrajvi0

@Souravrajvi0 Souravrajvi0 commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

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_gguf without forwarding token, so the callee's if 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 with os.environ.copy(), and a child inherits everything, so setting no token is not the same as denying one.

Live on main and in unsloth==2026.9.2 on PyPI. Silent, no error.

Changes

unsloth/save.py only.

  • Forward token on the LoRA GGUF dispatch.
  • One _apply_token_to_child_env for both converter subprocesses. False scrubs every token alias, points HF_TOKEN_PATH at os.devnull and sets HF_HUB_DISABLE_IMPLICIT_TOKEN=1; an explicit token scrubs then grants; an ambient one is promoted exactly as before.
  • Only an explicit token may clear 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_tensors had the same env grant with no scrub and no False branch. It now uses the same helper.
  • _clean_save_token reads "" and " " as "nothing passed". They previously reached the Hub as a literal Bearer header, which huggingface_hub 1.x rejects with LocalProtocolError before sending. It also resolves True, which the Hub defines as "use the cached token".
  • _resolve_imatrix_file no longer pre-resolves with get_token() before passing the result on as an explicit string, which was the same flag bypass in-process.
  • Scrub 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 still mint a credential in CI.
  • save.py:1874 ran llama-gguf-split with text=True and no encoding, a cp1252 crash on Windows reading child output ([Bug] UnicodeDecodeError on 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.toml allows: token=False emitted 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_TOKEN x entry point, run against main and this branch. Parent os.environ was untouched in every one.

main here
token=False, LoRA save operator's credential reaches the converter denied
token=None (ordinary user) env as-is unchanged, including the operator's opt-out
token=False, compressed-tensors operator's credential reaches the subprocess denied

tests/saving: 540 passed, 0 failed, against 517 passed and 1 failed on main. 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_boundary walks the AST for os.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_IDENTITY for that, unsloth/save.py has no equivalent. Separate change.

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.
@danielhanchen

Copy link
Copy Markdown
Member

Confirmed the gap: studio/backend/routes/data_recipe/jobs.py:585 still collapses the sentinel with hf_token or None, and the export push routes hand request.hf_token straight to the worker, so an API key with no body token can write with the host credential. This does change existing API-key export pushes that relied on the ambient token, so could you confirm the worker handles the False sentinel end to end on /export/gguf before I review?

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.
@Souravrajvi0

Copy link
Copy Markdown
Contributor Author

Confirmed end-to-end on /export/gguf (and the other export push routes):

Route: allow_ambient_hf_token + hf_token_arg — API key with no body token resolves to False, not None.

Orchestrator → worker: hf_token is passed through the export command dict unchanged (cmd.get("hf_token")).

Worker → backend: _handle_export forwards hf_token=False into export_gguf. Load path no longer does config.get("hf_token") or None.

Push: _export_hub_push_token(False) is None, so push stops with "Repository ID and Hugging Face token required for Hub upload" before any push_to_hub_gguf / HfApi call — no ambient HF_TOKEN.

Publish: jobs.py uses hf_token_arg and passes the result through without hf_token or None; huggingface.py passes token=False straight to HuggingFaceHubClient.

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 False, gguf push refuses sentinel, publish sentinel not laundered.

@oobabooga

Copy link
Copy Markdown
Member

Reviewed the whole diff. The publish and export-push conversions are correct: hf_token_arg never yields "", HfApi(token=False) is a supported signature, and huggingface_hub 1.27's get_token_to_send still maps False to "no authorization header". A UI session keeps the ambient fallback on every route the diff touches; an API key with no body token gets a clean 400 on publish ("Authentication failed. Please provide a valid Hugging Face token", raised by _create_or_get_repo) and the up-front "Repository ID and Hugging Face token required for Hub upload" on the four export push routes.

One path was left open, and I pushed the fix in 9bd2479 rather than leave it for another round.

export_gguf builds local_token_kw from _export_hub_push_token(hf_token), which maps the sentinel to None, so the whole condition goes falsy and token= is dropped from the save_pretrained_gguf call. unsloth/save.py:_resolve_imatrix_file then does if token is None: token = get_token(), so the imatrix resolution reads the Hub with the operator's ambient credential. Trigger: an API key posting {"imatrix": true} to /api/export/export/gguf with no body token. push_to_hub does not need to be set; the local save at export.py:1213 is enough.

Repro against the pre-fix code, with hf_token=False, imatrix_file=True:

kwargs passed to save_pretrained_gguf: ['imatrix_file', 'quantization_method']
token forwarded? False -> <absent>

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 normalize_token and forwards anything that is not None, so False arrives as token=False and None still omits the kwarg, which is what grants a UI session the ambient token.

The same commit drops _export_load_token: it was byte-equivalent to hub.utils.hf_tokens.normalize_token, and a second copy of the sentinel rules living next to the canonical one is how these drift. _export_hub_push_token is now normalize_token plus "a write has no anonymous mode, so refuse", and worker.py uses normalize_token in place of the inline False if config_token is False else config_token. Note that branch is unreachable today: /export/load-checkpoint does not depend on allow_ambient_hf_token, so config["hf_token"] is still str | None. It is harmless either way, since "" is falsy at every consumer in utils/transformers_version.py and _token_cache_key maps "" and None to the same key.

Added test_imatrix_resolution_carries_the_caller_token, which fails on the pre-fix expression for the False case and passes for all three inputs after.

Verification: studio/backend, pytest tests/test_hub_token_caller_identity.py 96 passed; pytest tests/ -n 4 39382 passed with 13 failures and 15 errors that are identical with the change stashed (live-server, CPU-topology and offline-cache tests that do not run here); ruff check clean on every changed file.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 6, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-07T11:55:28.590319Z 1a276b6 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 3db568282c

ℹ️ 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".

@oobabooga

Copy link
Copy Markdown
Member

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 authenticated_via_api_key -> allow_ambient_hf_token chain (overriding only the bearer resolver, not the policy dependencies) across 100 combinations of caller and body shape. UI session JWT and desktop JWT resolve to None and keep the ambient fallback; sk-unsloth and keyless resolve to False; an explicit token wins for all four and is stripped. No combination changed status code, so the added Depends does not narrow who can reach these routes. That matters for the publish dialog in particular, which tells the user "Leave empty if you're already logged in via CLI" - that path is preserved, because the browser always sends a real bearer and so is never classified keyless.

What the sentinel is actually worth, measured. With an ambient HF_TOKEN on the host, HfApi receives hf_OPERATOR_HOST_TOKEN when the kwarg is omitted and False when it is passed, and build_hf_headers emits no authorization header in the second case. That is the whole mechanism, and it only works if the sentinel is forwarded rather than collapsed.

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 _unsloth_save_lora_gguf granting HF_TOKEN and HUGGING_FACE_HUB_TOKEN but leaving the operator's HUGGINGFACEHUB_API_TOKEN in place, so the child held two credentials: the caller's and ours. hub/utils/hf_tokens.py already names this exact failure in apply_token_to_child_env ("setting HF_TOKEN alone leaves an operator credential sitting in HF_HUB_TOKEN or a legacy alias, so the child holds two"), so unsloth/save.py now follows the same rule: scrub every alias first, then grant, and set HF_HUB_DISABLE_IMPLICIT_TOKEN=0 so an inherited =1 cannot void the token we just handed over. Measured after the fix, with env token, legacy alias and a cached token file all present:

token=None   ->  authorization 'Bearer hf_OPERATOR_ENV_TOKEN'   (ambient path intact)
token=False  ->  authorization None, every alias gone, implicit disabled
token='hf_caller' -> authorization 'Bearer hf_caller', operator alias gone

Also verified on real weights. unsloth/Qwen3-0.6B on an RTX 3090: load plus a merged export with hf_token=False complete normally, and push_to_hub=True with the sentinel refuses with "Repository ID and Hugging Face token required for Hub upload" without attempting a Hub call. So the three-state token reaches a real model and a real save path without breaking either.

Tests: tests/saving plus every suite that imports unsloth.save (791 passed), studio/backend export/publish/auth/keyless subset (835 passed), test_hub_token_caller_identity.py (96 passed), ruff clean. The new tests in test_export_dispatch.py each fail on the previous code.

Two things I am deliberately not touching here. /export/load-checkpoint still passes its body token straight through, so config["hf_token"] in the worker is str | None and the sentinel branch there is currently unreachable; that is a read path and belongs with the rest of #10147 item 3, not here. And unsloth/save.py:1874 has a subprocess.run(text=True) with no encoding="utf-8", which tests/saving/test_save_subprocess_utf8_encoding.py flags; it is identical on main and that test is not in the CI list, so it is pre-existing and unrelated.

The only red check is connection (openclaw, stable), where the CLI printed the correct answer and ended with stopReason=stop but did not exit inside the 1200s cap. Unrelated to this change.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: ae5d91b279

ℹ️ 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".

danielhanchen added 2 commits September 7, 2026 10:56
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.
@danielhanchen

Copy link
Copy Markdown
Member

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 HF_HUB_DISABLE_IMPLICIT_TOKEN x entry point against main and this branch, and the sentinel itself 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, and now it is measured across the supported range

token=False emitted no authorization header in 216/216 in-process cells and 90/90 at the wire (loopback HF_ENDPOINT, with an env token and a stored token file and both legacy aliases set), on every version above. It is the second check in get_token_to_send(), ahead of get_token() and ahead of the implicit-token flag, unmoved since the 2022 use_auth_token unification. pyproject.toml allows >=0.34.0 for core and >=1.23.0,<2.0 for Studio, so that is the whole declared range.

The Studio half had already landed, so I dropped it

#10142 merged as 052b2fa51 on the 6th, after this branch was cut. Merging main in conflicted in four files, and taking this branch's side would have reverted work that shipped:

  • core/export/export.py — main gained Studio: stop converting the model twice when exporting GGUF to the Hub #10317's "don't convert the model twice when pushing GGUF" branch. This branch predates it and deletes it.
  • routes/export.py — main's _resolve_export_hf_token(..., push_to_hub=...) is strictly richer than _export_hub_token(token, allow_ambient), which ignores push_to_hub.
  • core/export/worker.py — main's config.get("hf_token") or None is deliberate now, and its comment says why: _load_config_json refuses the hub cache for the sentinel, so offline a cached model falls to the default sidecar. normalize_token(...) here would pass False and break sidecar detection for cached checkpoints.
  • routes/data_recipe/jobs.py — main rejects an API-key publish with a 400 before any Hub call; threading the sentinel instead fails one round trip later with a worse message. Two of the tests here also assert 200 where main's test_hub_write_ambient_token.py asserts 400, so they contradict each other directly.

studio/ is now taken from main wholesale. The PR is MERGEABLE again, two files, +313/-14.

What was left is real, and is live on main and on PyPI today

save_pretrained_gguf(save_method="lora", token=False) leaks the operator's credential into the converter subprocess on the merge base, on current origin/main, and in unsloth==2026.9.2 (the wheel's save.py:4539 is the same token-dropping call). Silent, no crash. Reproduced end to end. _supports_kwarg does not guard it: the old signature already declares token: Optional[Union[str, bool]] = None, so it cannot tell "accepts token" from "honours token".

Four things the original commits got wrong, now fixed

The HF_HUB_DISABLE_IMPLICIT_TOKEN="0" override fired for ambient callers. get_token() does not honour that flag — 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 resulting 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. Studio's apply_token_to_child_env already drew that line; the two copies had drifted apart.

_unsloth_save_compressed_tensors had the identical env grant with no scrub and no False branch — 0 of its 126 cells changed, so the sentinel was ignored there while being honoured next door. Both converters now go through one _apply_token_to_child_env, and test_every_converter_child_env_goes_through_the_token_boundary walks the AST for os.environ.copy() and fails if a third child env is ever built without it. That test is what found this one.

"" and " " fell through every branch and reached HfApi as a literal Bearer header. On 1.x that is httpx.LocalProtocolError before the request is sent; on 0.x it is sent and earns a 401. Studio pins >=1.23.0, so it 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 became plain inheritance that an ambient =1 then voided.

_resolve_imatrix_file pre-resolved with get_token() and passed the result on as an explicit string — the same opt-out bypass, in-process, on both trees. It now hands the token through untouched.

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 still mint the operator's credential in CI. And the comment claiming all five names are read by huggingface_hub was wrong — 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

Of the tests the earlier commits added, four passed on the pre-fix tree, so they were not evidence for anything. The worst was test_export_worker_passes_false_sentinel_into_gguf_export, which exercised _handle_export — a function this PR never touched. Two more were inspect.getsource substring greps whose only failure against main was a parameter name. Those went with the Studio half.

The 16 new ones: 15 fail on ae5d91b. The one that does not is the "" env case, which is a regression guard and labelled as one.

tests/saving: 539 passed on this branch vs 517 on main, with the same single pre-existing failure on both (test_save_subprocess_utf8_encoding at save.py:1874, unrelated and identical with the change stashed). Ruff clean.

Scope, checked rather than assumed

Zero bytes under studio/frontend/ or src-tauri/, no .ts/.tsx/.rs, no kernel, dtype, device or dispatch code — so there is no browser or GPU-vendor surface here. The OS axis does apply, because the mechanism is os.environ.copy() + popping keys + Popen(env=...), and it is sound: CPython's nt branch upper-cases env keys at import and os._Environ.copy() is dict(self), so a Windows user's hf_token is already HF_TOKEN in the copy and the scrub removes it. On POSIX a lower-case alias was never live, since hub reads only the upper-case names. The full copy keeps SYSTEMROOT/PATH/COMSPEC, and every value assigned is a str.

One thing I deliberately did not touch, since it is unrelated to the token boundary: save.py:1874 has a subprocess.run(text=True) with no encoding="utf-8", which the repo's own test_save_subprocess_utf8_encoding flags. It is identical on main and fails there too, so it wants its own PR.

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.
@danielhanchen danielhanchen changed the title Studio: apply Hub caller boundary to publish and export push Keep the LoRA GGUF and compressed-tensors converters off the operator's Hugging Face token Sep 7, 2026
@danielhanchen

Copy link
Copy Markdown
Member

Pushed 92a274e and rewrote the title and description, which still described the Studio scope that #10142 took over.

Three follow-ups on the boundary work.

The token file was still readable. Scrubbing the env aliases is only half the denial: get_token() reads the stored token file next, so a forced-anonymous child could resolve the operator's credential with every alias removed. HF_HUB_DISABLE_IMPLICIT_TOKEN=1 stops the header being sent but not the read, and the read is exactly what an explicit get_token() call downstream would pick up. That is the same shape as the _resolve_imatrix_file bug fixed in the previous commit, so it is not hypothetical. HF_TOKEN_PATH now points at os.devnull on that branch: nothing to read, and a stray write goes nowhere rather than erroring.

The utf-8 gap. save.py:1874 ran llama-gguf-split with text=True and no encoding, which is a cp1252 crash on Windows reading child output (#2660). tests/saving/test_save_subprocess_utf8_encoding.py already had a rule for it and fails on main today. It was the last text-mode subprocess call in the file without one; all 8 now declare it, so tests/saving is 540 passed and 0 failed here against 517 passed and 1 failed on main.

Tidying. _HF_OIDC_ENV_KEYS folded into _HF_TOKEN_ENV_KEYS (one list, same scrub), and the comments cut back to what is not already obvious from the code.

AST checks on the result, since most of this is structural rather than behavioural:

child-env builders applying the boundary:  _unsloth_save_lora_gguf, _unsloth_save_compressed_tensors  (2/2)
text-mode subprocess calls missing encoding: []            (8 such calls)
_clean_save_token / _apply_token_to_child_env: identity tests only, no truthiness on the sentinel
public signatures: unchanged, no new required arguments

The last one matters most for anyone already calling these: save_pretrained_gguf, push_to_hub_gguf and both private helpers keep the same required-argument counts, so nothing existing has to change.

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 ANONYMOUS_CACHE_IDENTITY for that; unsloth/save.py has no equivalent and giving it one is its own change.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 7, 2026
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@danielhanchen

Copy link
Copy Markdown
Member

@codex security review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: 1a276b6a56

ℹ️ 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".

@danielhanchen
danielhanchen merged commit 5ae462d into unslothai:main Sep 7, 2026
37 checks passed
oobabooga pushed a commit to oobabooga/unsloth-test that referenced this pull request Sep 7, 2026
oobabooga pushed a commit to oobabooga/unsloth-test that referenced this pull request Sep 7, 2026
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.

3 participants