Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/spdx_tools/spdx/validation/external_package_ref_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})$"

Expand Down Expand Up @@ -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}, '
Expand Down
65 changes: 65 additions & 0 deletions tests/spdx/validation/test_external_package_ref_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down