Skip to content

Commit

Permalink
Test docker extract bugs
Browse files Browse the repository at this point in the history
Signed-off-by: Prabhu Subramanian <prabhu@appthreat.com>
  • Loading branch information
prabhu committed Dec 24, 2024
1 parent 59e97b7 commit 3f6bc32
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions lib/managers/docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
homedir,
tmpdir,
} from "node:os";
import { basename, join, resolve } from "node:path";
import { basename, join, resolve, win32 } from "node:path";
import process from "node:process";
import stream from "node:stream/promises";
import { parse } from "node:url";
Expand Down Expand Up @@ -50,6 +50,20 @@ if (
isContainerd = true;
}

// Taken from https://github.com/isaacs/node-tar/blob/main/src/strip-absolute-path.ts
export const stripAbsolutePath = (path) => {
let parsed = win32.parse(path);
while (win32.isAbsolute(path) || parsed.root) {
// windows will think that //x/y/z has a "root" of //x/y/
// but strip the //?/C:/ off of //?/C:/path
const root =
path.charAt(0) === "/" && path.slice(0, 4) !== "//?/" ? "/" : parsed.root;
path = path.slice(root.length);
parsed = win32.parse(path);
}
return path;
};

/**
* Detect if Rancher desktop is running on a mac.
*/
Expand Down Expand Up @@ -745,6 +759,18 @@ export const getImage = async (fullImageName) => {
return localData;
};

/**
* Warnings such as TAR_ENTRY_INFO are treated as errors in strict mode. While this is mostly desired, we can relax this
* requirement for one particular warning related to absolute paths.
*
* @param entry {tar.ReadEntry} ReadEntry object from node-tar
*/
function handleAbsolutePath(entry) {
if (entry.path === "/" || win32.isAbsolute(entry.path)) {
entry.path = stripAbsolutePath(entry.path);
}
}

export const extractTar = async (fullImageName, dir) => {
try {
await stream.pipeline(
Expand All @@ -760,9 +786,10 @@ export const extractTar = async (fullImageName, dir) => {
onwarn: () => {
// ignore
},
onReadEntry: handleAbsolutePath,
filter: (path, entry) => {
// Some files are known to cause issues with extract
if (
return !(
path.includes("etc/machine-id") ||
path.includes("etc/gshadow") ||
path.includes("etc/shadow") ||
Expand Down Expand Up @@ -790,10 +817,7 @@ export const extractTar = async (fullImageName, dir) => {
"RenamedOrSymlinked",
"HardLink",
].includes(entry.type)
) {
return false;
}
return true;
);
},
}),
);
Expand Down Expand Up @@ -834,6 +858,8 @@ export const extractTar = async (fullImageName, dir) => {
*/
} else if (!["TAR_ENTRY_INFO", "TAR_ENTRY_INVALID"].includes(err.code)) {
console.log(err);
} else if (DEBUG_MODE) {
console.log(err.code, "is not handled yet.");
}
return false;
}
Expand Down Expand Up @@ -1084,6 +1110,7 @@ export const exportImage = async (fullImageName) => {
onwarn: () => {
// ignore
},
onReadEntry: handleAbsolutePath,
}),
);
} catch (err) {
Expand All @@ -1103,6 +1130,7 @@ export const exportImage = async (fullImageName) => {
onwarn: () => {
// ignore
},
onReadEntry: handleAbsolutePath,
}),
);
} catch (err) {
Expand Down

0 comments on commit 3f6bc32

Please sign in to comment.