snyk-docker-plugin
Version:
Snyk CLI docker plugin
59 lines • 2.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.canonicalizePath = exports.normalizeAbsolutePath = void 0;
const path_1 = require("path");
const MAX_SYMLINK_DEPTH = 40;
/**
* Normalize a filesystem path to a POSIX absolute path without resolving symlinks.
*/
function normalizeAbsolutePath(filePath) {
const normalized = path_1.posix.normalize(filePath.replace(/\\/g, "/"));
return normalized.startsWith("/") ? normalized : `/${normalized}`;
}
exports.normalizeAbsolutePath = normalizeAbsolutePath;
/**
* Resolve symlinks in a path using the extracted image symlink graph.
* Used so evidence paths like /bin/node match APK file lists recorded at /usr/bin/node.
*/
function canonicalizePath(filePath, symlinkGraph) {
let current = normalizeAbsolutePath(filePath);
const visited = new Set();
// Re-scan from the start after each substitution so that a resolved target
// whose own parent is a symlink (e.g. /lib64 -> /lib/x with /lib -> /usr/lib)
// is fully resolved. MAX_SYMLINK_DEPTH and the visited set guard against cycles.
for (let depth = 0; depth < MAX_SYMLINK_DEPTH; depth++) {
if (visited.has(current)) {
return current;
}
visited.add(current);
const segments = current.split("/").filter(Boolean);
const prefix = [];
let rewritten = false;
for (const segment of segments) {
prefix.push(segment);
const linkTarget = symlinkGraph.get(`/${prefix.join("/")}`);
if (!linkTarget) {
continue;
}
const resolvedPrefix = normalizeSymlinkTarget(`/${prefix.join("/")}`, linkTarget);
const rest = segments.slice(prefix.length).join("/");
current = normalizeAbsolutePath(rest ? `${resolvedPrefix}/${rest}` : resolvedPrefix);
rewritten = true;
break;
}
if (!rewritten) {
return current;
}
}
return current;
}
exports.canonicalizePath = canonicalizePath;
function normalizeSymlinkTarget(basePath, linkTarget) {
const target = linkTarget.replace(/\\/g, "/");
if (target.startsWith("/")) {
return path_1.posix.normalize(target);
}
const baseDir = path_1.posix.dirname(basePath);
return path_1.posix.normalize(path_1.posix.join(baseDir, target));
}
//# sourceMappingURL=path-canonicalization.js.map