package-url / packageurl-js

JavaScript implementation of the package url spec
https://www.npmjs.com/package/packageurl-js
MIT License
26 stars 21 forks source link

Unable to construct PURL containing percent signs #75

Closed matt-phylum closed 2 months ago

matt-phylum commented 3 months ago
> const { PackageURL } = await import("packageurl-js");
undefined
> new PackageURL("generic", "100%", "test");
Uncaught URIError: URI malformed
    at decodeURIComponent (<anonymous>)
    at Object.normalizeNamespace [as normalize] (/purl/repo/src/normalize.js:16:25)
    at new PackageURL (/purl/repo/src/package-url.js:50:39)
> new PackageURL("generic", "test", "100%");
Uncaught URIError: URI malformed
    at decodeURIComponent (<anonymous>)
    at Object.normalizeName [as normalize] (/purl/repo/src/normalize.js:10:11)
    at new PackageURL (/purl/repo/src/package-url.js:55:34)
> new PackageURL("generic", "test", "test", "100%");
Uncaught URIError: URI malformed
    at decodeURIComponent (<anonymous>)
    at Object.normalizeVersion [as normalize] (/purl/repo/src/normalize.js:91:11)
    at new PackageURL (/purl/repo/src/package-url.js:60:37)
> new PackageURL("generic", "test", "test", "1.0", {}, "100%");
Uncaught URIError: URI malformed
    at decodeURIComponent (<anonymous>)
    at Object.normalizeSubpath [as normalize] (/purl/repo/src/normalize.js:77:25)
    at new PackageURL (/purl/repo/src/package-url.js:71:37)

Or worse if the percent sign is followed by two or more hex characters:

> new PackageURL("generic", "", "%21").toString();
'pkg:generic/!'

It only works correctly for qualifier values:

> new PackageURL("generic", "test", "test", "1.0", {"a": "100%"}).toString();
'pkg:generic/test/test@1.0?a=100%25'
jdalton commented 3 months ago

@matt-phylum Thanks for the find! This happens because in v2 normalization is consistent across constructor and fromString. In v1.x PackageURL.fromString("pkg:general/100%/test") would throw a similar error (because of decodeURIComponent use).

jdalton commented 3 months ago

PR at #76.

jdalton commented 2 months ago

Closed by #76

steven-esser commented 2 months ago

v2.0.1 published to npmjs: https://www.npmjs.com/package/packageurl-js/v/2.0.1 with this fix included.