Skip to content
Open
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
20 changes: 10 additions & 10 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@
"snyk-gradle-plugin": "7.0.0",
"snyk-module": "3.1.0",
"snyk-mvn-plugin": "4.8.0",
"snyk-nodejs-lockfile-parser": "2.8.1",
"snyk-nodejs-plugin": "^2.0.1",
"snyk-nodejs-lockfile-parser": "2.9.0",
"snyk-nodejs-plugin": "^2.1.0",
"snyk-nuget-plugin": "4.2.3",
"snyk-php-plugin": "1.12.1",
"snyk-policy": "^4.1.6",
Expand Down
2 changes: 2 additions & 0 deletions src/lib/plugins/nodejs-plugin/npm-lock-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export async function parse(
pruneCycles: true,
honorAliases: true,
showNpmScope: options.showNpmScope,
includeComponentMetadata: options.includeComponentMetadata || false,
},
);
}
Expand All @@ -89,6 +90,7 @@ export async function parse(
strictOutOfSync,
true,
options.showNpmScope,
options.includeComponentMetadata || false,
Comment on lines 78 to +93

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Optional] instead of setting false as default in both of these includeComponentMetadata, I would suggest saving to a constant such as defaultIncludeComponentMetadata

);
} finally {
await spinner.clear<void>(resolveModuleSpinnerLabel)();
Expand Down
1 change: 1 addition & 0 deletions src/lib/plugins/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface Options {
scanAllUnmanaged?: boolean;
showNpmScope?: boolean;
allProjects?: boolean;
includeComponentMetadata?: boolean;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this hold values such as null and undefined? Just a sanity question

}

export interface Plugin {
Expand Down

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "npm-component-metadata-v1",
"version": "1.0.0",
"dependencies": {
"lodash": "4.17.15"
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "npm-component-metadata-v2",
"version": "1.0.0",
"dependencies": {
"lodash": "4.17.15"
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "npm-component-metadata-v3",
"version": "1.0.0",
"dependencies": {
"lodash": "4.17.15"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { createProjectFromFixture } from '../../util/createProject';
import { runSnykCLI } from '../../util/runSnykCLI';

jest.setTimeout(1000 * 60);

// `--include-component-metadata` makes the npm plugin forward the flag to
// snyk-nodejs-lockfile-parser, which reads the install-time `integrity` and
// `resolved` fields already recorded in the lockfile and surfaces them as
// `hash:<algorithm>` and `distribution:url` labels on the dep-graph nodes.
// Unlike maven there is nothing to resolve first — the metadata lives in the
// lockfile — so these fixtures need no `npm install`.
//
// Covers npm lockfile v1 (legacy depTree path, converted to a dep-graph for
// printing) and v2/v3 (native dep-graph path).
describe('`snyk test --include-component-metadata` (npm)', () => {
const labelKeys = (printGraphStdout: string, prefix: string): string[] => {
const jsonDG = JSON.parse(
printGraphStdout.split('DepGraph data:')[1].split('DepGraph target:')[0],
);
return jsonDG.graph.nodes
.flatMap((node) => Object.keys(node.info?.labels ?? {}))
.filter((key) => key.startsWith(prefix));
};

const fixtures = [
['v1', 'npm-include-component-metadata/lock-v1'],
['v2', 'npm-include-component-metadata/lock-v2'],
['v3', 'npm-include-component-metadata/lock-v3'],
];

describe.each(fixtures)('lockfile %s', (_version, fixture) => {
it('attaches hash and distribution:url labels with the flag', async () => {
const project = await createProjectFromFixture(fixture);

const { code, stdout } = await runSnykCLI(
'test --include-component-metadata --print-graph --file=package-lock.json',
{ cwd: project.path() },
);

expect(code).toEqual(0);
expect(stdout).toContain('DepGraph data:');
expect(labelKeys(stdout, 'hash:').length).toBeGreaterThan(0);
expect(labelKeys(stdout, 'distribution:url').length).toBeGreaterThan(0);
});

// Control: without the flag the same project must not produce the labels,
// proving they are driven by `--include-component-metadata`.
it('does not attach the labels without the flag', async () => {
const project = await createProjectFromFixture(fixture);

const { code, stdout } = await runSnykCLI(
'test --print-graph --file=package-lock.json',
{ cwd: project.path() },
);
Comment on lines +48 to +54

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this test is in a loop, I'm wondering if we should add some timeout here or if default is enough


expect(code).toEqual(0);
expect(stdout).toContain('DepGraph data:');
expect(labelKeys(stdout, 'hash:')).toHaveLength(0);
expect(labelKeys(stdout, 'distribution:url')).toHaveLength(0);
Comment on lines +56 to +59

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any chance that changes in the responses from runSnykCLI would cause these values to fail the test?
Maybe a better question is: are we testing what we want here?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(maybe my comment is hard to understand the way I wrote - just wondering if this could be flaky)

});
});
});