Skip to content

Commit dd0165b

Browse files
committed
Merge branch 'main' of github.com:package-url/packageurl-python into validate-vers
2 parents 2e48bf0 + abe3806 commit dd0165b

File tree

2 files changed

+55
-6
lines changed

2 files changed

+55
-6
lines changed

src/packageurl/__init__.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -464,12 +464,17 @@ def from_string(cls, purl: str) -> Self:
464464
url=remainder, scheme="", allow_fragments=True
465465
)
466466

467-
if scheme or authority:
468-
msg = (
469-
f'Invalid purl {purl!r} cannot contain a "user:pass@host:port" '
470-
f"URL Authority component: {authority!r}."
471-
)
472-
raise ValueError(msg)
467+
# The spec (seems) to allow colons in the name and namespace.
468+
# urllib.urlsplit splits on : considers them parts of scheme
469+
# and authority.
470+
# Other libraries do not care about this.
471+
# See https://github.com/package-url/packageurl-python/issues/152#issuecomment-2637692538
472+
# We do + ":" + to put the colon back that urlsplit removed.
473+
if authority:
474+
path = authority + ":" + path
475+
476+
if scheme:
477+
path = scheme + ":" + path
473478

474479
path = path.lstrip("/")
475480

tests/test_packageurl.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,50 @@ def test_purl_is_hashable():
334334
assert len(s) == 1
335335

336336

337+
def test_colons_in_name_are_handled_correctly() -> None:
338+
p = PackageURL.from_string(
339+
"pkg:nuget/libiconv:%20character%20set%20conversion%[email protected]?package-id=e11a609df352e292"
340+
)
341+
342+
assert p.type == "nuget"
343+
assert p.namespace is None
344+
assert p.name == "libiconv: character set conversion library"
345+
assert p.version == "1.9"
346+
assert p.qualifiers == {"package-id": "e11a609df352e292"}
347+
assert p.subpath is None
348+
349+
assert PackageURL.from_string(p.to_string()).to_string() == p.to_string()
350+
351+
352+
def test_colons_in_namespace_are_handled_correctly() -> None:
353+
p = PackageURL.from_string(
354+
"pkg:nuget/an:odd:space/libiconv:%20character%20set%20conversion%[email protected]?package-id=e11a609df352e292"
355+
)
356+
357+
assert p.type == "nuget"
358+
assert p.namespace == "an:odd:space"
359+
assert p.name == "libiconv: character set conversion library"
360+
assert p.version == "1.9"
361+
assert p.qualifiers == {"package-id": "e11a609df352e292"}
362+
assert p.subpath is None
363+
364+
assert PackageURL.from_string(p.to_string()).to_string() == p.to_string()
365+
366+
367+
def test_encoding_stuff_with_colons_correctly() -> None:
368+
p = PackageURL(
369+
type="nuget",
370+
namespace="an:odd:space",
371+
name="libiconv: character set conversion library",
372+
version="1.9",
373+
qualifiers={"package-id": "e11a609df352e292"},
374+
)
375+
assert (
376+
p.to_string()
377+
== "pkg:nuget/an:odd:space/libiconv:%20character%20set%20conversion%[email protected]?package-id=e11a609df352e292"
378+
)
379+
380+
337381
def test_vers_validation_ok():
338382
url = PackageURL.from_string("pkg:pypi/requests?vers=vers:pypi/>=2.0")
339383
assert url.qualifiers["vers"] == "vers:pypi/>=2.0"

0 commit comments

Comments
 (0)