Skip to content

Validate HuggingFace URL prefix correctly in HuggingFaceUrl constructor#614

Merged
haoliuu merged 3 commits into
microsoft:mainfrom
Osamaali313:fix/huggingfaceurl-prefix-validation
Jul 1, 2026
Merged

Validate HuggingFace URL prefix correctly in HuggingFaceUrl constructor#614
haoliuu merged 3 commits into
microsoft:mainfrom
Osamaali313:fix/huggingfaceurl-prefix-validation

Conversation

@Osamaali313

Copy link
Copy Markdown
Contributor

Problem

HuggingFaceUrl's constructor checks the base-URL prefix without the trailing slash, then strips a hardcoded 23 characters:

if (!modelNameOrUrl.StartsWith(BaseUrl, StringComparison.OrdinalIgnoreCase))   // BaseUrl = "https://huggingface.co" (22 chars)
{
    throw new ArgumentException("Invalid URL", nameof(modelNameOrUrl));
}
modelNameOrUrl = modelNameOrUrl[23..];

Two concrete, deterministic failures result:

  1. The exact base URL "https://huggingface.co" (22 chars) passes the loose check, then [23..] indexes past the end → ArgumentOutOfRangeException instead of the ArgumentException("Invalid URL") thrown by every other invalid-input path.
  2. A lookalike host "https://huggingface.competitor.com/evil/repo" passes the loose StartsWith check, then [23..] slices mid-domain → silently mis-parses to Organization = "petitor.com" instead of being rejected.

The sibling GitHubUrl constructor already does this correctly (StartsWith($"{BaseUrl}/") and url[(BaseUrl.Length + 1)..]).

Reproduction (real ModelUrl.cs in a console harness)

before:  new HuggingFaceUrl("https://huggingface.co")                              => THROW ArgumentOutOfRangeException
before:  new HuggingFaceUrl("https://huggingface.competitor.com/evil/repo")        => Org=petitor.com Repo=evil
after:   new HuggingFaceUrl("https://huggingface.co")                              => THROW ArgumentException
after:   new HuggingFaceUrl("https://huggingface.competitor.com/evil/repo")        => THROW ArgumentException

Valid inputs (https://huggingface.co/org/repo, org/repo) are unchanged.

Fix

Require the trailing slash and strip BaseUrl.Length + 1, mirroring GitHubUrl:

if (!modelNameOrUrl.StartsWith($"{BaseUrl}/", StringComparison.OrdinalIgnoreCase)) { ... }
modelNameOrUrl = modelNameOrUrl[(BaseUrl.Length + 1)..];

Test

Adds ConstructorWithBaseUrlOnlyShouldThrowArgumentException and ConstructorWithLookalikeDomainShouldThrowException to ModelUrlTests.cs (both Assert.ThrowsExactly<ArgumentException>).

…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.
Copilot AI review requested due to automatic review settings June 25, 2026 20:15

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI 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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comment thread AIDevGallery.Utils/ModelUrl.cs

@haoliuu haoliuu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM. Thanks for making the change.

@haoliuu haoliuu merged commit 8c29d92 into microsoft:main Jul 1, 2026
8 of 9 checks passed
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.

3 participants