Skip to content

Studio: validate Hugging Face tokens before use - #7261

Merged
Imagineer99 merged 7 commits into
unslothai:mainfrom
oobabooga:fix/hf-token-validation
Jul 20, 2026
Merged

Imagineer99 merged 7 commits into
unslothai:mainfrom
oobabooga:fix/hf-token-validation

Conversation

@oobabooga

Copy link
Copy Markdown
Member

Closes #7253

Studio kept using a saved Hugging Face token even after it expired. This blocked already downloaded models, while the resulting inference error looked like an invalid model. Settings also had no direct way to clear the token.

Fix

Validate non-empty tokens through an authenticated backend endpoint using Hugging Face whoami. Definitive 401 responses open a shared warning before inference, training, or export, with options to replace the token, continue without it, or cancel.

Validation results are cached by token fingerprint. Uncached checks are limited to three per Studio user and client address per hour. Rate limits, network failures, and Hugging Face 5xx responses do not mark a token invalid.

Settings now has an explicit Clear button, keeps the masked field editable, and shows validation status. Inference preflight also reports Hugging Face authentication failures instead of a generic invalid-model error.

Behavior boundaries

  • Empty tokens skip validation.
  • Cached validations do not consume the hourly allowance.
  • Continuing without a rejected token supports public or fully downloaded assets.
  • Private and gated repositories still require a valid token and repository access.

Verification

  • pytest -q studio/backend/tests/test_hf_token_validation.py studio/backend/tests/test_utils.py -q
  • ruff check on the changed backend modules and tests
  • npm run typecheck
  • ESLint on the new hf-auth feature

@oobabooga
oobabooga requested a review from danielhanchen as a code owner July 20, 2026 13:01

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces a cached, rate-limited Hugging Face token validation mechanism. It adds a backend validation endpoint, frontend integration (including a warning dialog and settings controls), and pre-flight validation checks before starting training, loading models, or exporting checkpoints. The review feedback highlights two critical issues: a potential deadlock/indefinite blocking in the backend's single-flight logic if an exception occurs, and a missing exception handler in the frontend's token preparation logic that could crash main workflows on network failures.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@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: 9e2dfd7c25

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@Imagineer99

Copy link
Copy Markdown
Member

Ran a focused check here: https://github.com/Imagineer99/unsloth/actions/runs/29746258502
Backend tests, ruff, and frontend typecheck pass. The only failure is ESLint in use-hf-token-validation.ts: the empty-token branch calls setState directly inside an effect, triggering react-hooks/set-state-in-effect.

@oobabooga

Copy link
Copy Markdown
Member Author

The frontend lint error should be fine now @Imagineer99.

@Imagineer99
Imagineer99 merged commit 1b3bce0 into unslothai:main Jul 20, 2026
45 of 49 checks passed

@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: 28253bd431

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

model_path: payload.model_path,
native_path_lease: payload.nativePathLease ?? null,
hf_token: payload.hf_token,
hf_token: preparedToken.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.

P2 Badge Propagate anonymous token choice to follow-up scans

When an invalid saved token is rejected and the user chooses “Continue without token,” this prepared token is only used for /api/inference/validate; I checked the chat load flows in use-chat-model-runtime.ts and shared-composer.tsx, and they immediately call confirmRemoteCodeIfNeeded with their pre-validation hfToken snapshot. For a public custom-code model with medium/high scan findings, that scan runs with the token Hugging Face already rejected, falls back to an empty consent without a fingerprint, and the subsequent /load uses the anonymous choice and gets blocked because no approved fingerprint was supplied. Return the prepared token from validation or update the chat callers before running the remote-code scan.

Useful? React with 👍 / 👎.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Can't clear HF Token & Invalid HF Token results in failed model load

2 participants