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/proud-planets-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nodesecure/scanner": major
---

Move payload integrity into rootDependency
24 changes: 16 additions & 8 deletions workspaces/scanner/src/depWalker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ type WalkerOptions = Omit<Options, "registry"> & {
npmRcConfig?: Config;
};

type InitialPayload =
Partial<Payload> &
{
rootDependency: Payload["rootDependency"];
metadata: Payload["metadata"];
};

export async function depWalker(
manifest: PackageJSON | WorkspacesPackageJSON | ManifestVersion,
options: WalkerOptions,
Expand All @@ -112,11 +119,12 @@ export async function depWalker(

const dependencyConfusionWarnings: DependencyConfusionWarning[] = [];

const payload: Partial<Payload> = {
const payload: InitialPayload = {
id: tempDir.id,
rootDependency: {
name: manifest.name ?? "workspace",
version: manifest.version ?? "0.0.0"
version: manifest.version ?? "0.0.0",
integrity: null
},
scannerVersion: packageVersion,
vulnerabilityStrategy,
Expand Down Expand Up @@ -189,12 +197,14 @@ export async function depWalker(

const isRoot = current.id === kRootDependencyId;

if (isRoot && payload.integrity) {
payload.integrity = integrity;
if (isRoot && payload.rootDependency.integrity) {
payload.rootDependency.integrity = integrity;
}
else if (isRoot) {
const isWorkspace = options.location && "workspaces" in manifest;
payload.integrity = isWorkspace ? null : fromData(JSON.stringify(manifest), { algorithms: ["sha512"] }).toString();
payload.rootDependency.integrity = isWorkspace ?
null :
fromData(JSON.stringify(manifest), { algorithms: ["sha512"] }).toString();
}

// If the dependency is a DevDependencies we ignore it.
Expand Down Expand Up @@ -331,9 +341,7 @@ export async function depWalker(
contacts: illuminated
};
payload.dependencies = Object.fromEntries(dependencies);

const metadata = payload.metadata!;
metadata.executionTime = Date.now() - startedAt;
payload.metadata.executionTime = Date.now() - startedAt;

return payload as Payload;
}
Expand Down
5 changes: 2 additions & 3 deletions workspaces/scanner/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ export interface Payload {
rootDependency: {
name: string;
version: string;
/** The integrity of the scanned package */
integrity: string | null;
};
/** Global warnings list */
warnings: GlobalWarning[];
Expand All @@ -204,9 +206,6 @@ export interface Payload {
/** Vulnerability strategy name (npm, snyk, node) */
vulnerabilityStrategy: Vulnera.Kind;

/** The integrity of the scanned package */
integrity: string | null;

metadata: {
/**
* UNIX Timestamp when the scan started
Expand Down
12 changes: 5 additions & 7 deletions workspaces/scanner/test/depWalker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,10 @@ test("fetch payload of pacote on the npm registry", async() => {
"vulnerabilityStrategy",
"warnings",
"metadata",
"integrity",
"highlighted",
"dependencies"
]);
assert.strictEqual(typeof result.integrity, "string");
assert.strictEqual(typeof result.rootDependency.integrity, "string");
});

test("fetch payload of pacote on the gitlab registry", async() => {
Expand All @@ -194,11 +193,10 @@ test("fetch payload of pacote on the gitlab registry", async() => {
"vulnerabilityStrategy",
"warnings",
"metadata",
"integrity",
"highlighted",
"dependencies"
]);
assert.strictEqual(typeof result.integrity, "string");
assert.strictEqual(typeof result.rootDependency.integrity, "string");
});

test("highlight contacts from a remote package", async() => {
Expand Down Expand Up @@ -244,7 +242,7 @@ describe("scanner.cwd()", () => {
name: "NodeSecure"
});
assert.strictEqual(dep.metadata.homepage, "https://nodesecure.com");
assert.strictEqual(typeof result.integrity, "string");
assert.strictEqual(typeof result.rootDependency.integrity, "string");
});

test("should parse local manifest author field without throwing when attempting to highlight contacts", async() => {
Expand All @@ -266,9 +264,9 @@ describe("scanner.cwd()", () => {

assert.deepStrictEqual(result.rootDependency, {
name: "workspace",
version: "0.0.0"
version: "0.0.0",
integrity: null
});
assert.strictEqual(result.integrity, null);
});
});