From 11e2acac397404bb7c721cd8a0841475172ca5bf Mon Sep 17 00:00:00 2001 From: aniruth37 Date: Wed, 13 Dec 2023 12:17:31 +0530 Subject: [PATCH] Fix for isse #63 - Incorrect parsing of PURL with leading '/' in namespace --- src/package-url.js | 4 ++-- test/data/test-suite-data.json | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/package-url.js b/src/package-url.js index 93cc59f..cdcad64 100644 --- a/src/package-url.js +++ b/src/package-url.js @@ -149,7 +149,7 @@ class PackageURL { let type [type, remainder] = remainder.split('/', 2); - if (!type || !remainder) { + if (!type) { throw new Error('purl is missing the required "type" component.'); } type = decodeURIComponent(type) @@ -207,7 +207,7 @@ class PackageURL { let nameIndex = remaining.length - 1; let namespaceComponents = remaining.slice(0, nameIndex); name = decodeURIComponent(remaining[nameIndex]); - namespace = decodeURIComponent(namespaceComponents.join('/')); + namespace = decodeURIComponent(namespaceComponents.filter(item => item.trim()).join('/')); } else if (remaining.length === 1) { name = decodeURIComponent(remaining[0]); } diff --git a/test/data/test-suite-data.json b/test/data/test-suite-data.json index 6c89085..09b99ec 100644 --- a/test/data/test-suite-data.json +++ b/test/data/test-suite-data.json @@ -430,5 +430,17 @@ "qualifiers": null, "subpath": null, "is_invalid": false + }, + { + "description": "In namespace, leading and trailing slashes '/' are not significant and should be stripped in the canonical form", + "purl": "pkg:golang//github.com/ll/xlog@2.0", + "canonical_purl": "pkg:golang/github.com/ll/xlog@2.0", + "type": "golang", + "namespace": "github.com/ll", + "name": "xlog", + "version": "2.0", + "qualifiers": null, + "subpath": null, + "is_invalid": false } ]