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",