From 1f086fe55328c021c6c6a1a09aee23ab2e691ebc Mon Sep 17 00:00:00 2001 From: Francisco Carlos Sant'Ana Carneiro Date: Wed, 2 Dec 2026 12:49:36 -0300 Subject: [PATCH] fix(validation): reject malformed purl external references The purl validator used a greedy regex and re.match, accepting raw spaces, missing names, numeric types, trailing newlines, empty namespace segments, and empty qualifier keys/values. Tighten the structural validation and apply re.fullmatch only to purl references, so non-purl reference validation is unchanged. Add regression and boundary tests. This is an incremental structural improvement, not a full ECMA-427 ABNF implementation; type-specific purl rules remain out of scope. Fixes #916 Signed-off-by: Francisco Carlos Sant'Ana Carneiro --- .../external_package_ref_validator.py | 12 +++- .../test_external_package_ref_validator.py | 65 +++++++++++++++++++ 2 files changed, 75 insertions(+), 2 deletions(-) diff --git a/src/spdx_tools/spdx/validation/external_package_ref_validator.py b/src/spdx_tools/spdx/validation/external_package_ref_validator.py index 7dd316354..61ef0bc91 100644 --- a/src/spdx_tools/spdx/validation/external_package_ref_validator.py +++ b/src/spdx_tools/spdx/validation/external_package_ref_validator.py @@ -21,7 +21,11 @@ NPM_REGEX = r"^[^@]+@[^@]+$" NUGET_REGEX = r"^[^/]+/[^/]+$" BOWER_REGEX = r"^[^#]+#[^#]+$" -PURL_REGEX = r"^pkg:.+(\/.+)?\/.+(@.+)?(\?.+)?(#.+)?$" +PURL_REGEX = ( + r"^pkg:[A-Za-z][A-Za-z0-9.+-]*/(?:[^/@?#\s]+/)*[^/@?#\s]+" + r"(?:@[^?#\s]+)?(?:\?[A-Za-z][A-Za-z0-9._-]*=[^#&\s]+" + r"(?:&[A-Za-z][A-Za-z0-9._-]*=[^#&\s]+)*)?(?:#[^\s]+)?$" +) SWH_REGEX = r"^swh:1:(snp|rel|rev|dir|cnt):[0-9a-fA-F]{40}$" GITOID_REGEX = r"^gitoid:(blob|tree|commit|tag):(sha1:[0-9a-fA-F]{40}|sha256:[0-9a-fA-F]{64})$" @@ -109,7 +113,11 @@ def validate_against_regex( string_to_validate: str, reference_type: str, context: ValidationContext ) -> List[ValidationMessage]: regex = TYPE_TO_REGEX[reference_type] - if not re.match(regex, string_to_validate): + # PURL locators are validated against the whole string so a trailing + # newline cannot satisfy the end anchor. Other reference types retain + # their existing re.match semantics to avoid unrelated behaviour changes. + matcher = re.fullmatch if reference_type == "purl" else re.match + if not matcher(regex, string_to_validate): return [ ValidationMessage( f'externalPackageRef locator of type "{reference_type}" must conform with the regex {regex}, ' diff --git a/tests/spdx/validation/test_external_package_ref_validator.py b/tests/spdx/validation/test_external_package_ref_validator.py index eafa31941..f279b9ff3 100644 --- a/tests/spdx/validation/test_external_package_ref_validator.py +++ b/tests/spdx/validation/test_external_package_ref_validator.py @@ -75,6 +75,8 @@ ), (ExternalPackageRefCategory.PACKAGE_MANAGER, "purl", "pkg:npm/%40angular/animation@12.3.1"), (ExternalPackageRefCategory.PACKAGE_MANAGER, "purl", "pkg:nuget/EnterpriseLibrary.Common@6.0.1304"), + (ExternalPackageRefCategory.PACKAGE_MANAGER, "purl", "pkg:generic/foo"), + (ExternalPackageRefCategory.PACKAGE_MANAGER, "purl", "pkg:npm/%40angular/animation"), ( ExternalPackageRefCategory.PACKAGE_MANAGER, "purl", @@ -226,6 +228,69 @@ def test_invalid_external_package_ref_types(category, reference_type, locator, e f'externalPackageRef locator of type "purl" must conform with the regex {PURL_REGEX}, ' f"but is: pkg:npm@12.3.1", ), + ( + ExternalPackageRefCategory.PACKAGE_MANAGER, + "purl", + "pkg:generic/Apache Tomcat@10.1.33", + f'externalPackageRef locator of type "purl" must conform with the regex {PURL_REGEX}, ' + f"but is: pkg:generic/Apache Tomcat@10.1.33", + ), + ( + ExternalPackageRefCategory.PACKAGE_MANAGER, + "purl", + "pkg:pypi/@2.32.3", + f'externalPackageRef locator of type "purl" must conform with the regex {PURL_REGEX}, ' + f"but is: pkg:pypi/@2.32.3", + ), + ( + ExternalPackageRefCategory.PACKAGE_MANAGER, + "purl", + "pkg:123/requests@2.32.3", + f'externalPackageRef locator of type "purl" must conform with the regex {PURL_REGEX}, ' + f"but is: pkg:123/requests@2.32.3", + ), + ( + ExternalPackageRefCategory.PACKAGE_MANAGER, + "purl", + "pkg:generic/", + f'externalPackageRef locator of type "purl" must conform with the regex {PURL_REGEX}, ' + f"but is: pkg:generic/", + ), + ( + ExternalPackageRefCategory.PACKAGE_MANAGER, + "purl", + "pkg:generic//foo", + f'externalPackageRef locator of type "purl" must conform with the regex {PURL_REGEX}, ' + f"but is: pkg:generic//foo", + ), + ( + ExternalPackageRefCategory.PACKAGE_MANAGER, + "purl", + "pkg:generic/foo?=bar", + f'externalPackageRef locator of type "purl" must conform with the regex {PURL_REGEX}, ' + f"but is: pkg:generic/foo?=bar", + ), + ( + ExternalPackageRefCategory.PACKAGE_MANAGER, + "purl", + "pkg:generic/foo?arch=", + f'externalPackageRef locator of type "purl" must conform with the regex {PURL_REGEX}, ' + f"but is: pkg:generic/foo?arch=", + ), + ( + ExternalPackageRefCategory.PACKAGE_MANAGER, + "purl", + "pkg:generic/foo ", + f'externalPackageRef locator of type "purl" must conform with the regex {PURL_REGEX}, ' + f"but is: pkg:generic/foo ", + ), + ( + ExternalPackageRefCategory.PACKAGE_MANAGER, + "purl", + "pkg:pypi/requests@2.32.3\n", + f'externalPackageRef locator of type "purl" must conform with the regex {PURL_REGEX}, ' + f"but is: pkg:pypi/requests@2.32.3\n", + ), ( ExternalPackageRefCategory.PERSISTENT_ID, "swh",