Fix Hub download token boundary#7177
Conversation
for more information, see https://pre-commit.ci
There was a problem hiding this comment.
Code Review
This pull request introduces a shared helper function hf_token_arg to sanitize and manage Hugging Face tokens, ensuring that the backend does not implicitly reuse its own ambient HF_TOKEN when fetching metadata or spawning download workers. Multiple services, utilities, and workers are updated to use this helper, and comprehensive unit tests are added to verify the security boundary. The review feedback suggests improving the hf_token_arg helper by stripping leading and trailing whitespace from the token to prevent authentication failures caused by accidental copy-paste errors.
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.
| def hf_token_arg(hf_token: Optional[str]) -> HfTokenArg: | ||
| """Return an explicit token or disable Hugging Face's ambient credentials.""" | ||
| return hf_token if hf_token else False |
There was a problem hiding this comment.
To prevent authentication failures due to accidental copy-paste errors (such as trailing newlines or spaces), it is recommended to strip leading and trailing whitespace from the Hugging Face token. Additionally, treating a token that consists only of whitespace as empty (and thus falling back to False for anonymous access) prevents invalid authentication requests.
| def hf_token_arg(hf_token: Optional[str]) -> HfTokenArg: | |
| """Return an explicit token or disable Hugging Face's ambient credentials.""" | |
| return hf_token if hf_token else False | |
| def hf_token_arg(hf_token: Optional[str]) -> HfTokenArg: | |
| """Return an explicit token or disable Hugging Face's ambient credentials.""" | |
| if hf_token: | |
| stripped = hf_token.strip() | |
| if stripped: | |
| return stripped | |
| return False |
References
- Maintain consistency with existing patterns by using standard trimming for sanitization.
Summary
Security behavior
Authenticated Studio callers can choose model and dataset repo IDs. Before this change, omitting the per-request token caused workers and metadata lookups to reuse ambient backend credentials. Anonymous requests now scrub worker credentials, disable implicit authentication, and pass token=False to Hugging Face APIs.
Validation