Skip to content

external_package_ref_validator: PURL_REGEX and re.match accept malformed purl locators (spaces, missing name, invalid type, trailing newline) #916

Backlog

Description

@trackoor

Validating an SPDX 2.2 or 2.3 document accepts malformed purl external reference locators (including unencoded spaces, missing package names, numeric package types, and trailing newlines), which allows pyspdxtools to convert an invalid JSON SBOM into a Tag-Value .spdx file that pyspdxtools itself fails to parse.

Reproduction

cat > sbom.spdx.json << 'EOF'
{
  "spdxVersion": "SPDX-2.3",
  "dataLicense": "CC0-1.0",
  "SPDXID": "SPDXRef-DOCUMENT",
  "name": "example-sbom",
  "documentNamespace": "https://example.com/spdxdocs/example-sbom-1",
  "creationInfo": {
    "created": "2025-01-15T10:00:00Z",
    "creators": ["Tool: syft-1.18.0"]
  },
  "packages": [
    {
      "SPDXID": "SPDXRef-Package-tomcat",
      "name": "apache-tomcat",
      "versionInfo": "10.1.33",
      "downloadLocation": "NOASSERTION",
      "filesAnalyzed": false,
      "externalRefs": [
        {
          "referenceCategory": "PACKAGE-MANAGER",
          "referenceType": "purl",
          "referenceLocator": "pkg:generic/Apache Tomcat@10.1.33"
        },
        {
          "referenceCategory": "PACKAGE-MANAGER",
          "referenceType": "purl",
          "referenceLocator": "pkg:pypi/@2.32.3"
        },
        {
          "referenceCategory": "PACKAGE-MANAGER",
          "referenceType": "purl",
          "referenceLocator": "pkg:123/requests@2.32.3"
        },
        {
          "referenceCategory": "PACKAGE-MANAGER",
          "referenceType": "purl",
          "referenceLocator": "pkg:pypi/requests@2.32.3\n"
        }
      ]
    }
  ],
  "relationships": [
    {
      "spdxElementId": "SPDXRef-DOCUMENT",
      "relationshipType": "DESCRIBES",
      "relatedSpdxElement": "SPDXRef-Package-tomcat"
    }
  ]
}
EOF

pyspdxtools -i sbom.spdx.json -o sbom.spdx
pyspdxtools -i sbom.spdx

Actual vs expected

Actual: pyspdxtools -i sbom.spdx.json -o sbom.spdx exits 0 with no validation errors and writes sbom.spdx, and reading sbom.spdx back exits 1 with:

ERROR:root:There have been issues while parsing the provided document:
Error while parsing Package: ["Couldn't split PackageExternalRef in category, reference_type and locator. Line: 18"]

Expected: pyspdxtools -i sbom.spdx.json -o sbom.spdx should fail validation (exit 1) before writing sbom.spdx and report all four malformed purl locators, per SPDX 2.3 Annex F.3.5 and Clause 7.21.1.

Versions

  • spdx-tools 0.8.5 (PyPI) and main (cef432adee09dbff2eedf7e33e4aa8be7c11ccfd)
  • Python 3.11.2 on Linux x86_64

Root cause

In src/spdx_tools/spdx/validation/external_package_ref_validator.py, PURL_REGEX (^pkg:.+(\/.+)?\/.+(@.+)?(\?.+)?(#.+)?$) uses greedy .+ (making its four optional component groups vacuous and matching spaces, /, @, and invalid type characters), and validate_against_regex uses re.match instead of re.fullmatch (allowing a trailing \n before $).

Credit: https://ucsbplse.github.io/

Activity

  1. francisco-carneiro commented on Dec 2, 2026

    @francisco-carneiro

    Hi @trackoor,

    Thank you for the detailed report and reproduction steps.

    I have reproduced the issue and prepared a local patch addressing the malformed PURL locators, including regression tests and end-to-end validation.

    The proposed change is limited to PURL validation, preserving the existing behavior of other external reference types.

    Validation results:

    • 63 targeted validation tests passed.
    • Full local test suite: 1,040 passed, 4 skipped.
    • Successful end-to-end verification of JSON-to-Tag-Value conversion and parsing.

    This is an incremental structural validation improvement, rather than a complete ECMA-427 implementation.

    AI-assisted tools were used during analysis, implementation, testing, and documentation.

    I would be happy to contribute the patch through a pull request and welcome any maintainer guidance regarding the proposed approach or applicable contribution requirements.

    Thank you!

  2. added a commit that references this issue on Dec 2, 2026
    1f086fe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions