GeezerVison is showing github.com — fix(sbom): percent-encode vcs_url qualifier in generated purls (#9670) · npm/cli@024e6d9
File treeExpand file treeCollapse file tree Open diff view settings tap-snapshots/test/lib/utils
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/utils/sbom-cyclonedx.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -76,7 +76,7 @@ const toCyclonedxItem = (node, { packageType }) => { | 76 | 76 | // Calculate purl from package spec | 77 | 77 | let spec = npa(node.pkgid) | 78 | 78 | spec = (spec.type === 'alias') ? spec.subSpec : spec | 79 | | - const purl = npa.toPurl(spec) + (isGitNode(node) ? `?vcs_url=${node.resolved}` : '') | | 79 | + const purl = npa.toPurl(spec) + (isGitNode(node) ? `?vcs_url=${encodeURIComponent(node.resolved)}` : '') | 80 | 80 |
| 81 | 81 | if (node.package) { | 82 | 82 | const toNormalize = new PackageJson() | |
Collapse file Copy file name to clipboardExpand all lines: lib/utils/sbom-spdx.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -109,7 +109,7 @@ const toSpdxItem = (node, { packageType }) => { | 109 | 109 | // Calculate purl from package spec | 110 | 110 | let spec = npa(node.pkgid) | 111 | 111 | spec = (spec.type === 'alias') ? spec.subSpec : spec | 112 | | - const purl = npa.toPurl(spec) + (isGitNode(node) ? `?vcs_url=${node.resolved}` : '') | | 112 | + const purl = npa.toPurl(spec) + (isGitNode(node) ? `?vcs_url=${encodeURIComponent(node.resolved)}` : '') | 113 | 113 |
| 114 | 114 | /* For workspace nodes, use the location from their linkNode */ | 115 | 115 | let location = node.location | |
Collapse file Copy file name to clipboardExpand all lines: tap-snapshots/test/lib/utils/sbom-cyclonedx.js.test.cjs +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -417,7 +417,7 @@ exports[`test/lib/utils/sbom-cyclonedx.js TAP single node - from git url > must | 417 | 417 | "version": "1.0.0", | 418 | 418 | "scope": "required", | 419 | 419 | "author": "Author", | 420 | | - | | 420 | + "purl": "pkg:npm/[email protected]?vcs_url=https%3A%2F%2Fgithub.com%2Ffoo%2Fbar%231234", | 421 | 421 | "properties": [], | 422 | 422 | "externalReferences": [ | 423 | 423 | { | |
Collapse file Copy file name to clipboardExpand all lines: tap-snapshots/test/lib/utils/sbom-spdx.js.test.cjs +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -414,7 +414,7 @@ exports[`test/lib/utils/sbom-spdx.js TAP single node - from git url > must match | 414 | 414 | { | 415 | 415 | "referenceCategory": "PACKAGE-MANAGER", | 416 | 416 | "referenceType": "purl", | 417 | | - "referenceLocator": "pkg:npm/[email protected]?vcs_url=https://github.com/foo/bar#1234" | | 417 | + "referenceLocator": "pkg:npm/[email protected]?vcs_url=https%3A%2F%2Fgithub.com%2Ffoo%2Fbar%231234" | 418 | 418 | } | 419 | 419 | ] | 420 | 420 | } | |
Collapse file Copy file name to clipboardExpand all lines: test/lib/utils/sbom-cyclonedx.js +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -270,6 +270,17 @@ t.test('single node - from git url', t => { | 270 | 270 | t.end() | 271 | 271 | }) | 272 | 272 |
| | 273 | +t.test('git url with special chars is encoded into the vcs_url qualifier', t => { | | 274 | + const node = { ...root, type: 'git', resolved: 'https://github.com/foo/bar.git?a=b&c=d#1234' } | | 275 | + const res = cyclonedxOutput({ npm, nodes: [node] }) | | 276 | + const { purl } = res.metadata.component | | 277 | + // everything after vcs_url= must be a single percent-encoded value, so the | | 278 | + // committish/query can't leak out as an extra purl qualifier or subpath | | 279 | + t.equal(purl, 'pkg:npm/[email protected]?vcs_url=https%3A%2F%2Fgithub.com%2Ffoo%2Fbar.git%3Fa%3Db%26c%3Dd%231234') | | 280 | + t.notMatch(purl.split('vcs_url=')[1], /[#&]/) | | 281 | + t.end() | | 282 | +}) | | 283 | + | 273 | 284 | t.test('single node - no package info', t => { | 274 | 285 | const node = { ...root, package: undefined } | 275 | 286 | const res = cyclonedxOutput({ npm, nodes: [node] }) | |
Collapse file Copy file name to clipboardExpand all lines: test/lib/utils/sbom-spdx.js +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -223,6 +223,17 @@ t.test('single node - from git url', t => { | 223 | 223 | t.end() | 224 | 224 | }) | 225 | 225 |
| | 226 | +t.test('git url with special chars is encoded into the vcs_url qualifier', t => { | | 227 | + const node = { ...root, type: 'git', resolved: 'https://github.com/foo/bar.git?a=b&c=d#1234' } | | 228 | + const res = spdxOutput({ npm, nodes: [node] }) | | 229 | + const purl = res.packages | | 230 | + .find(p => p.SPDXID === 'SPDXRef-Package-root-1.0.0') | | 231 | + .externalRefs.find(r => r.referenceType === 'purl').referenceLocator | | 232 | + t.equal(purl, 'pkg:npm/[email protected]?vcs_url=https%3A%2F%2Fgithub.com%2Ffoo%2Fbar.git%3Fa%3Db%26c%3Dd%231234') | | 233 | + t.notMatch(purl.split('vcs_url=')[1], /[#&]/) | | 234 | + t.end() | | 235 | +}) | | 236 | + | 226 | 237 | t.test('single node - linked', t => { | 227 | 238 | const node = { ...root, isLink: true, target: { edgesOut: [] } } | 228 | 239 | const res = spdxOutput({ npm, nodes: [node] }) | |
|