Skip to content

Commit d782b53

Browse files
fix: snyk will now scan vscode project (#1136)
1 parent 9d87150 commit d782b53

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@
6464
"check-vsix-size": "ts-node ./scripts/check-vsix-size.ts",
6565
"release-draft": "node ./scripts/release-draft.js",
6666
"reformat": "prettier --write .",
67+
"presnyk-test": "echo \"Creating backup for package-lock.json.\"; cp package-lock.json original-package-lock.json",
6768
"snyk-test": "node scripts/snyk-test.js",
69+
"postsnyk-test": "echo \"Restoring original package-lock.json.\"; mv original-package-lock.json package-lock.json",
6870
"generate-icon-font": "ts-node ./scripts/generate-icon-font.ts",
6971
"generate-vulnerability-report": "mongodb-sbom-tools generate-vulnerability-report --snyk-reports=.sbom/snyk-test-result.json --dependencies=.sbom/dependencies.json --fail-on=high",
7072
"create-vulnerability-tickets": "mongodb-sbom-tools generate-vulnerability-report --snyk-reports=.sbom/snyk-test-result.json --dependencies=.sbom/dependencies.json --create-jira-issues",

scripts/snyk-test.js

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,56 @@ const { glob } = require('glob');
66
const { promisify } = require('util');
77
const execFile = promisify(childProcess.execFile);
88

9+
const PACKAGE_LOCK_PATH = path.join(__dirname, '..', 'package-lock.json');
10+
11+
/**
12+
* "node_modules/@vscode/vsce-sign" package which is a dev dependency used for
13+
* publishing extension declares platform specific optionalDependencies, namely
14+
* the following:
15+
* - "@vscode/vsce-sign-alpine-arm64"
16+
* - "@vscode/vsce-sign-alpine-x64"
17+
* - "@vscode/vsce-sign-darwin-arm64"
18+
* - "@vscode/vsce-sign-darwin-x64"
19+
* - "@vscode/vsce-sign-linux-arm"
20+
* - "@vscode/vsce-sign-linux-arm64"
21+
* - "@vscode/vsce-sign-linux-x64"
22+
* - "@vscode/vsce-sign-win32-arm64"
23+
* - "@vscode/vsce-sign-win32-x64"
24+
*
25+
* Snyk requires what is declared in package-lock.json to be also present in
26+
* installed node_modules but this will never happen because for any platform,
27+
* other platform specific deps will always be missing which means Snyk will
28+
* always fail in this case.
29+
*
30+
* Because we always install with `npm ci --omit=optional`, with this method we
31+
* try to remove these identified problematic optionalDependencies before
32+
* running the Snyk tests and once the tests are finished, we restore the
33+
* original state back using npm hooks.
34+
*/
35+
async function removeProblematicOptionalDepsFromPackageLock() {
36+
const packageLockContent = JSON.parse(
37+
await fs.readFile(PACKAGE_LOCK_PATH, 'utf-8'),
38+
);
39+
40+
const vsceSignPackage =
41+
packageLockContent.packages?.['node_modules/@vscode/vsce-sign'];
42+
43+
if (!vsceSignPackage || !vsceSignPackage.optionalDependencies) {
44+
console.info('No problematic optional dependencies to fix');
45+
return;
46+
}
47+
48+
// Temporarily remove the optional dependencies
49+
vsceSignPackage['optionalDependencies'] = {};
50+
51+
// We write the actual package-lock path but restoring of the original file is
52+
// handled by npm hooks.
53+
await fs.writeFile(
54+
PACKAGE_LOCK_PATH,
55+
JSON.stringify(packageLockContent, null, 2),
56+
);
57+
}
58+
959
async function snykTest(cwd) {
1060
const tmpPath = path.join(os.tmpdir(), 'tempfile-' + Date.now());
1161

@@ -17,9 +67,8 @@ async function snykTest(cwd) {
1767
await execFile(
1868
'npx',
1969
[
20-
'snyk',
70+
'snyk@latest',
2171
'test',
22-
'--all-projects',
2372
'--severity-threshold=low',
2473
'--dev',
2574
`--json-file-output=${tmpPath}`,
@@ -47,6 +96,7 @@ async function snykTest(cwd) {
4796
async function main() {
4897
const rootPath = path.resolve(__dirname, '..');
4998
await fs.mkdir(path.join(rootPath, `.sbom`), { recursive: true });
99+
await removeProblematicOptionalDepsFromPackageLock();
50100
const results = await snykTest(rootPath);
51101

52102
await fs.writeFile(

0 commit comments

Comments
 (0)