Skip to content

Commit 6b228ef

Browse files
committed
chore: merge branch 'main' into fix/dockerfile-attribution
2 parents 40390ea + c9cbe4e commit 6b228ef

File tree

13 files changed

+273
-99
lines changed

13 files changed

+273
-99
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# This is a comment.
22
# Each line is a file pattern followed by one or more owners.
33

4-
* @snyk/infrasec_container
4+
* @snyk/infrasec_container @snyk/container_container
55

lib/analyzer/applications/node-modules-utils.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as Debug from "debug";
22
import { mkdir, mkdtemp, rm, stat, writeFile } from "fs/promises";
3+
import * as os from "os";
34
import * as path from "path";
45
import { FilePathToContent, FilesByDirMap } from "./types";
56
const debug = Debug("snyk");
@@ -22,7 +23,7 @@ interface ScanPaths {
2223
async function createTempProjectDir(
2324
projectDir: string,
2425
): Promise<{ tmpDir: string; tempProjectRoot: string }> {
25-
const tmpDir = await mkdtemp("snyk");
26+
const tmpDir = await mkdtemp(path.join(os.tmpdir(), "snyk-"));
2627

2728
const tempProjectRoot = path.join(tmpDir, projectDir);
2829

@@ -76,20 +77,23 @@ async function persistNodeModules(
7677
fileNamesGroupedByDirectory: FilesByDirMap,
7778
): Promise<ScanPaths> {
7879
const modules = fileNamesGroupedByDirectory.get(project);
79-
const tmpDir: string = "";
80-
const tempProjectRoot: string = "";
8180

8281
if (!modules || modules.size === 0) {
8382
debug(`Empty application directory tree.`);
84-
85-
return {
86-
tempDir: tmpDir,
87-
tempProjectPath: tempProjectRoot,
88-
};
83+
return { tempDir: "", tempProjectPath: "" };
8984
}
9085

86+
// Create the temp directory first so we can return it in the catch block
87+
// for cleanup. Previously, the outer tmpDir/tempProjectRoot were always
88+
// empty strings, meaning any temp directory created before a failure in
89+
// saveOnDisk or later steps would be leaked (caller couldn't clean it up).
90+
let tmpDir = "";
91+
let tempProjectRoot = "";
92+
9193
try {
92-
const { tmpDir, tempProjectRoot } = await createTempProjectDir(project);
94+
const created = await createTempProjectDir(project);
95+
tmpDir = created.tmpDir;
96+
tempProjectRoot = created.tempProjectRoot;
9397

9498
await saveOnDisk(tmpDir, modules, filePathToContent);
9599

@@ -122,7 +126,10 @@ async function persistNodeModules(
122126
}
123127
}
124128

125-
async function createFile(filePath, fileContent): Promise<void> {
129+
async function createFile(
130+
filePath: string,
131+
fileContent: string,
132+
): Promise<void> {
126133
try {
127134
await mkdir(path.dirname(filePath), { recursive: true });
128135
await writeFile(filePath, fileContent, "utf-8");

lib/analyzer/applications/node.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ async function depGraphFromNodeModules(
136136
}
137137

138138
const depGraph = await legacy.depTreeToGraph(
139-
pkgTree,
139+
pkgTree as any,
140140
pkgTree.type || "npm",
141141
);
142142

@@ -417,7 +417,7 @@ function stripUndefinedLabels(
417417
parserResult: lockFileParser.PkgTree,
418418
): lockFileParser.PkgTree {
419419
const optionalLabels = parserResult.labels;
420-
const mandatoryLabels: Record<string, string> = {};
420+
const mandatoryLabels: Record<string, any> = {};
421421
if (optionalLabels) {
422422
for (const currentLabelName of Object.keys(optionalLabels)) {
423423
if (optionalLabels[currentLabelName] !== undefined) {
@@ -428,7 +428,7 @@ function stripUndefinedLabels(
428428
const parserResultWithProperLabels = Object.assign({}, parserResult, {
429429
labels: mandatoryLabels,
430430
});
431-
return parserResultWithProperLabels;
431+
return parserResultWithProperLabels as lockFileParser.PkgTree;
432432
}
433433

434434
async function buildDepGraph(
@@ -513,7 +513,10 @@ async function buildDepGraphFromDepTree(
513513
// Don't provide a default manifest file name, prefer the parser to infer it.
514514
);
515515
const strippedLabelsParserResult = stripUndefinedLabels(parserResult);
516-
return await legacy.depTreeToGraph(strippedLabelsParserResult, lockfileType);
516+
return await legacy.depTreeToGraph(
517+
strippedLabelsParserResult as any,
518+
lockfileType,
519+
);
517520
}
518521

519522
export function getLockFileVersion(

lib/analyzer/image-inspector.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as Debug from "debug";
22
import * as fs from "fs";
3-
import * as mkdirp from "mkdirp";
43
import * as path from "path";
54

65
import { Docker, DockerOptions } from "../docker";
@@ -200,7 +199,7 @@ async function getImageArchive(
200199
platform?: string,
201200
): Promise<ArchiveResult> {
202201
const docker = new Docker();
203-
mkdirp.sync(imageSavePath);
202+
fs.mkdirSync(imageSavePath, { recursive: true });
204203
const destination: DestinationDir = {
205204
name: imageSavePath,
206205
removeCallback: cleanupCallback(imageSavePath, "image.tar"),

lib/image-save-path.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
import * as crypto from "crypto";
2+
import * as os from "os";
13
import * as path from "path";
2-
import * as tmp from "tmp";
3-
import { v4 as uuidv4 } from "uuid";
44

55
export function fullImageSavePath(imageSavePath: string | undefined): string {
6-
let imagePath = tmp.dirSync().name;
6+
let imagePath = os.tmpdir();
77
if (imageSavePath) {
88
imagePath = path.normalize(imageSavePath);
99
}
1010

11-
return path.join(imagePath, uuidv4());
11+
return path.join(imagePath, crypto.randomUUID());
1212
}

lib/scan.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ export async function extractContent(
259259
switch (imageType) {
260260
case ImageType.DockerArchive:
261261
case ImageType.OciArchive:
262+
case ImageType.KanikoArchive:
262263
imagePath = getAndValidateArchivePath(targetImage);
263264
break;
264265
case ImageType.Identifier:

package-lock.json

Lines changed: 9 additions & 55 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,14 @@
4646
"fzstd": "^0.1.1",
4747
"gunzip-maybe": "^1.4.2",
4848
"minimatch": "^9.0.0",
49-
"mkdirp": "^1.0.4",
5049
"packageurl-js": "1.2.0",
5150
"semver": "^7.7.3",
5251
"shescape": "^2.1.7",
53-
"snyk-nodejs-lockfile-parser": "^2.2.2",
52+
"snyk-nodejs-lockfile-parser": "^2.7.0",
5453
"snyk-poetry-lockfile-parser": "1.9.1",
5554
"snyk-resolve-deps": "^4.9.1",
5655
"tar-stream": "^2.2.0",
57-
"tmp": "^0.2.5",
5856
"tslib": "^1",
59-
"uuid": "^8.2.0",
6057
"varint": "^6.0.0"
6158
},
6259
"devDependencies": {
@@ -70,7 +67,6 @@
7067
"@types/jest": "^29.5.5",
7168
"@types/node": "^20.19.1",
7269
"@types/tar-stream": "^1.6.1",
73-
"@types/tmp": "^0.2.0",
7470
"jest": "^29.7.0",
7571
"npm-run-all": "^4.1.5",
7672
"prettier": "^2.7.1",

0 commit comments

Comments
 (0)