Validate HuggingFace URL prefix correctly in HuggingFaceUrl constructor#614
Merged
haoliuu merged 3 commits intoJul 1, 2026
Merged
Conversation
…tructor The constructor checked StartsWith(BaseUrl) without the trailing slash and then stripped a hardcoded 23 characters. BaseUrl is "https://huggingface.co" (22 chars), so: (1) the exact base URL passed the check then crashed with ArgumentOutOfRangeException on [23..] instead of the contract's ArgumentException; (2) a lookalike host such as "https://huggingface.competitor.com/evil/repo" passed the loose check and was mis-parsed (Organization became "petitor.com") instead of being rejected. Require the trailing slash and strip BaseUrl.Length + 1, mirroring the already-correct GitHubUrl constructor. Adds regression tests for the base-URL-only and lookalike-domain cases.
haoliuu
approved these changes
Jul 1, 2026
haoliuu
left a comment
Collaborator
There was a problem hiding this comment.
LGTM. Thanks for making the change.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
HuggingFaceUrl's constructor checks the base-URL prefix without the trailing slash, then strips a hardcoded 23 characters:Two concrete, deterministic failures result:
"https://huggingface.co"(22 chars) passes the loose check, then[23..]indexes past the end →ArgumentOutOfRangeExceptioninstead of theArgumentException("Invalid URL")thrown by every other invalid-input path."https://huggingface.competitor.com/evil/repo"passes the looseStartsWithcheck, then[23..]slices mid-domain → silently mis-parses toOrganization = "petitor.com"instead of being rejected.The sibling
GitHubUrlconstructor already does this correctly (StartsWith($"{BaseUrl}/")andurl[(BaseUrl.Length + 1)..]).Reproduction (real ModelUrl.cs in a console harness)
Valid inputs (
https://huggingface.co/org/repo,org/repo) are unchanged.Fix
Require the trailing slash and strip
BaseUrl.Length + 1, mirroringGitHubUrl:Test
Adds
ConstructorWithBaseUrlOnlyShouldThrowArgumentExceptionandConstructorWithLookalikeDomainShouldThrowExceptiontoModelUrlTests.cs(bothAssert.ThrowsExactly<ArgumentException>).