Defensively guard against dangling ? from qualifiers

This commit is contained in:
Lane Seppala
2023-06-02 16:07:51 +00:00
parent 83edbc93dd
commit 5961fd4755
4 changed files with 22 additions and 4 deletions

View File

@@ -20,7 +20,7 @@ test("Parses CLI output", async () => {
});
describe("ComponentDetection.makePackageUrl", () => {
test("returns a valid package url", () => {
test("returns a valid package url from saturated object", () => {
const packageUrl = ComponentDetection.makePackageUrl({
Scheme: "pkg",
Type: "npm",
@@ -36,4 +36,18 @@ describe("ComponentDetection.makePackageUrl", () => {
"pkg:npm/github/component-detection-action@0.0.2?arch=amd64&os=linux"
);
});
test("returns valid package url without dangling ? with empty qualifers", () => {
const packageUrl = ComponentDetection.makePackageUrl({
Scheme: "pkg",
Type: "npm",
Namespace: "github",
Name: "component-detection-action",
Version: "0.0.2",
Qualifiers: { },
});
expect(packageUrl).toBe(
"pkg:npm/github/component-detection-action@0.0.2"
);
});
});

View File

@@ -128,7 +128,9 @@ export default class ComponentDetection {
if (packageUrlJson.Version) {
packageUrl += `@${packageUrlJson.Version}`;
}
if (packageUrlJson.Qualifiers !== null) {
if (typeof packageUrlJson.Qualifiers === "object"
&& packageUrlJson.Qualifiers !== null
&& Object.keys(packageUrlJson.Qualifiers).length > 0) {
const qualifierString = Object.entries(packageUrlJson.Qualifiers)
.map(([key, value]) => `${key}=${value}`)
.join("&");

4
dist/index.js generated vendored
View File

@@ -23425,7 +23425,9 @@ class ComponentDetection {
if (packageUrlJson.Version) {
packageUrl += `@${packageUrlJson.Version}`;
}
if (packageUrlJson.Qualifiers !== null) {
if (typeof packageUrlJson.Qualifiers === "object"
&& packageUrlJson.Qualifiers !== null
&& Object.keys(packageUrlJson.Qualifiers).length > 0) {
const qualifierString = Object.entries(packageUrlJson.Qualifiers)
.map(([key, value]) => `${key}=${value}`)
.join("&");

2
dist/index.js.map generated vendored

File diff suppressed because one or more lines are too long