Repository navigation
Conversation
… separator (GenAI-Security-Project#13, GenAI-Security-Project#14) GenAI-Security-Project#14: /generate already validates the raw input before html.escape, but the validator itself was looser than intended. Regexes anchored with `$` accept a trailing "\n" (and the URL branch accepted any query or whitespace), and "." / ".." passed as owner or repo segments. Use fullmatch per segment, reject whitespace/control characters and dot segments, and document that the function must receive unsanitized input. GenAI-Security-Project#13: generator.py and its `.replace('/', '/')` no-op were replaced by packageurl-python in AIBOMService._generate_purl, which emits pkg:huggingface/<owner>/<name>@<version>. The slash is the purl namespace delimiter and must not be encoded as %2F, so the issue's suggested replacement would produce a non-conformant purl. Add tests that pin the literal separator and the namespace/name round trip. Add tests for validation order (validator receives the raw value, invalid input never reaches the Hub or service, escaped in the error page). Fixes GenAI-Security-Project#14 Fixes GenAI-Security-Project#13 Signed-off-by: Raghuvar Karthik Durga <177662415+drk-ram@users.noreply.github.com>
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.
Summary
Fixes #14. Fixes #13 (already resolved on
main; this adds the missing regression coverage).#14 - input validation vs. sanitization
/generateinweb_controller.pyalready callsis_valid_hf_input(model_id)on the raw form value beforehtml.escape, so the ordering described in the issue is fixed. Auditing the validator itself turned up gaps that make "validate the raw input" weaker than intended:re.match(r"^...$")lets a trailing newline through ($matches before a final\n), e.g.org/model\nandhttps://huggingface.co/org/model\n..and..were accepted as owner/repo segments (org/..,../model).Changes (
src/controllers/web_controller.py):fullmatch./..segmentsValid IDs (
owner/model, Hub URLs incl./tree/main) behave exactly as before.#13 - PURL slash encoding
generator.pyand the.replace('/', '/')no-op no longer exist: PURLs are built byAIBOMService._generate_purlthroughpackageurl-python, givingpkg:huggingface/owner/model@<version>. For thehuggingfacetype the owner is the purl namespace and the repo the name, and/is the namespace delimiter, so encoding it as%2F(the issue's suggestion) would yield a non-conformant purl. No production change is needed; this PR adds tests pinning the literal separator, the namespace/name round trip, and the componentpurl/bom-ref.Tests
tests/test_input_validation.py(new): validator accept/reject cases; validator receives the raw (not HTML-escaped) value; invalid input never reachesHfApi/AIBOMService; hostile input is escaped in the error page; trailing-newline input is rejectedtests/test_purl_construction.py: no%2Fseparator, round trip throughPackageURL.from_string, componentpurl/bom-refThe newline, dot-segment and trailing-newline route tests fail against the previous validator.
Validation
pytest --no-cov(Python 3.11): 194 passed