Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/gold-parks-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nodesecure/scanner": minor
---

Enhance warnings extractor by adding unique kinds & refactoring response
3 changes: 3 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions workspaces/scanner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"@nodesecure/tree-walker": "^1.2.0",
"@nodesecure/vulnera": "^2.0.1",
"@openally/mutex": "^1.0.0",
"frequency-set": "^1.0.2",
"pacote": "^21.0.0",
"semver": "^7.5.4",
"type-fest": "^4.41.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
// Import Third-party Dependencies
import type { WarningDefault, Warning } from "@nodesecure/js-x-ray";
import type {
WarningDefault,
Warning,
WarningName
} from "@nodesecure/js-x-ray";
import FrequencySet from "frequency-set";

// Import Internal Dependencies
import type {
Expand All @@ -9,8 +14,11 @@ import type {
import type { DependencyVersion } from "../../types.js";

export type WarningsExtractorResult = {
warnings: Record<string, Warning<WarningDefault>[]>;
count: number;
warnings: {
count: number;
groups: Record<string, Warning<WarningDefault>[]>;
uniqueKinds: Record<WarningName, number>;
};
};

export interface WarningsExtractorOptions {
Expand All @@ -24,6 +32,7 @@ export class WarningsExtractor implements ManifestProbeExtractor<WarningsExtract
level = "manifest" as const;

#warnings: Record<string, Warning<WarningDefault>[]> = Object.create(null);
#uniqueKinds = new FrequencySet<WarningName>();
#count = 0;
#useSpecAsKey: boolean;

Expand All @@ -48,6 +57,10 @@ export class WarningsExtractor implements ManifestProbeExtractor<WarningsExtract
`${parent.name}@${version}` :
parent.name;

warnings
.map((warn) => warn.kind)
.forEach((kind) => this.#uniqueKinds.add(kind));

if (key in this.#warnings) {
this.#warnings[key].push(...warnings);
}
Expand All @@ -58,8 +71,11 @@ export class WarningsExtractor implements ManifestProbeExtractor<WarningsExtract

done() {
return {
count: this.#count,
warnings: this.#warnings
warnings: {
count: this.#count,
uniqueKinds: Object.fromEntries(this.#uniqueKinds) as any,
groups: this.#warnings
}
};
}
}
23 changes: 15 additions & 8 deletions workspaces/scanner/test/extractors/payload.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,24 @@ describe("Extractors.Probes", () => {
);

const {
count,
warnings
} = extractor.extractAndMerge();

assert.strictEqual(count, 3);
const keys = Object.keys(warnings);
assert.strictEqual(warnings.count, 3);
const keys = Object.keys(warnings.groups);
assert.deepEqual(keys, ["strnum@1.1.2"]);

const kinds = warnings["strnum@1.1.2"].map((warning) => warning.kind);
assert.deepEqual(kinds, ["unsafe-regex", "unsafe-regex", "encoded-literal"]);
assert.deepEqual(
warnings.groups["strnum@1.1.2"].map((warning) => warning.kind),
["unsafe-regex", "unsafe-regex", "encoded-literal"]
);
assert.deepEqual(
warnings.uniqueKinds,
{
"unsafe-regex": 2,
"encoded-literal": 1
}
);
});

it("should extract strnum warnings with options useSpecAsKey: false", () => {
Expand All @@ -162,12 +170,11 @@ describe("Extractors.Probes", () => {
);

const {
count,
warnings
} = extractor.extractAndMerge();

assert.strictEqual(count, 3);
const keys = Object.keys(warnings);
assert.strictEqual(warnings.count, 3);
const keys = Object.keys(warnings.groups);
assert.deepEqual(keys, ["strnum"]);
});
});
Expand Down