Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing of npm purls with namespace #93 #106

Merged
merged 5 commits into from
Dec 28, 2022
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ jobs:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
python-version:
- "3.11"
- "3.10"
- "3.9"
- "3.8"
- "3.7"
- "3.6"

steps:
- name: Checkout
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

0.10.5 (2022-11-29)
-------------------

- Fixed `PackageURL.from_string` to properly handle npm purls
with namespace.

0.10.4 (2022-10-17)
-------------------

Expand Down
12 changes: 10 additions & 2 deletions src/packageurl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,15 @@ def from_string(cls, purl):
raise ValueError(msg)

path = path.lstrip("/")

namespace = ""
# NPM purl have a namespace in the path
# and the namespace in an npm purl is
# different from others because it starts with `@`
# so we need to handle this case separately
if type == "npm" and path.startswith("@"):
namespace, sep, path = path.partition("/")

remainder, sep, version = path.rpartition("@")
if not sep:
remainder = version
Expand All @@ -382,9 +391,8 @@ def from_string(cls, purl):
ns_name = remainder.strip().strip("/")
ns_name = ns_name.split("/")
ns_name = [seg for seg in ns_name if seg and seg.strip()]
namespace = ""
name = ""
if len(ns_name) > 1:
if not namespace and len(ns_name) > 1:
name = ns_name[-1]
ns = ns_name[0:-1]
namespace = "/".join(ns)
Expand Down
48 changes: 48 additions & 0 deletions tests/data/test-suite-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -298,5 +298,53 @@
"qualifiers": {"in production": "true"},
"subpath": null,
"is_invalid": true
},
{
"description": "valid npm purl without version and with subpath",
"purl": "pkg:npm/@babel/core#/googleapis/api/annotations/",
"canonical_purl": "pkg:npm/%40babel/core#googleapis/api/annotations",
"type": "npm",
"namespace": "@babel",
"name": "core",
"version": null,
"qualifiers": null,
"subpath": "googleapis/api/annotations",
"is_invalid": false
},
{
"description": "valid npm purl with namespace, version and subpath",
"purl": "pkg:npm/@babel/core@1.0.2#/googleapis/api/annotations/",
"canonical_purl": "pkg:npm/%40babel/core@1.0.2#googleapis/api/annotations",
"type": "npm",
"namespace": "@babel",
"name": "core",
"version": "1.0.2",
"qualifiers": null,
"subpath": "googleapis/api/annotations",
"is_invalid": false
},
{
"description": "valid npm purl without namespace and with subpath",
"purl": "pkg:npm/core@1.0.2#/googleapis/api/annotations/",
"canonical_purl": "pkg:npm/core@1.0.2#googleapis/api/annotations",
"type": "npm",
"namespace": null,
"name": "core",
"version": "1.0.2",
"qualifiers": null,
"subpath": "googleapis/api/annotations",
"is_invalid": false
},
{
"description": "valid npm purl without namespace, version and subpath",
"purl": "pkg:npm/core#/googleapis/api/annotations/",
"canonical_purl": "pkg:npm/core#googleapis/api/annotations",
"type": "npm",
"namespace": null,
"name": "core",
"version": null,
"qualifiers": null,
"subpath": "googleapis/api/annotations",
"is_invalid": false
}
]