Skip to content

Commit 5961fd4

Browse files
committed
Defensively guard against dangling ? from qualifiers
1 parent 83edbc9 commit 5961fd4

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

componentDetection.test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ test("Parses CLI output", async () => {
2020
});
2121

2222
describe("ComponentDetection.makePackageUrl", () => {
23-
test("returns a valid package url", () => {
23+
test("returns a valid package url from saturated object", () => {
2424
const packageUrl = ComponentDetection.makePackageUrl({
2525
Scheme: "pkg",
2626
Type: "npm",
@@ -36,4 +36,18 @@ describe("ComponentDetection.makePackageUrl", () => {
3636
"pkg:npm/github/component-detection-action@0.0.2?arch=amd64&os=linux"
3737
);
3838
});
39+
40+
test("returns valid package url without dangling ? with empty qualifers", () => {
41+
const packageUrl = ComponentDetection.makePackageUrl({
42+
Scheme: "pkg",
43+
Type: "npm",
44+
Namespace: "github",
45+
Name: "component-detection-action",
46+
Version: "0.0.2",
47+
Qualifiers: { },
48+
});
49+
expect(packageUrl).toBe(
50+
"pkg:npm/github/component-detection-action@0.0.2"
51+
);
52+
});
3953
});

componentDetection.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ export default class ComponentDetection {
128128
if (packageUrlJson.Version) {
129129
packageUrl += `@${packageUrlJson.Version}`;
130130
}
131-
if (packageUrlJson.Qualifiers !== null) {
131+
if (typeof packageUrlJson.Qualifiers === "object"
132+
&& packageUrlJson.Qualifiers !== null
133+
&& Object.keys(packageUrlJson.Qualifiers).length > 0) {
132134
const qualifierString = Object.entries(packageUrlJson.Qualifiers)
133135
.map(([key, value]) => `${key}=${value}`)
134136
.join("&");

dist/index.js

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)