diff --git a/lib/managers/docker.js b/lib/managers/docker.js index 4b1a53267..01ae3fc05 100644 --- a/lib/managers/docker.js +++ b/lib/managers/docker.js @@ -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"; @@ -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. */ @@ -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( @@ -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") || @@ -790,10 +817,7 @@ export const extractTar = async (fullImageName, dir) => { "RenamedOrSymlinked", "HardLink", ].includes(entry.type) - ) { - return false; - } - return true; + ); }, }), ); @@ -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; } @@ -1084,6 +1110,7 @@ export const exportImage = async (fullImageName) => { onwarn: () => { // ignore }, + onReadEntry: handleAbsolutePath, }), ); } catch (err) { @@ -1103,6 +1130,7 @@ export const exportImage = async (fullImageName) => { onwarn: () => { // ignore }, + onReadEntry: handleAbsolutePath, }), ); } catch (err) {