diff --git a/scripts/build-image-worker.mjs b/scripts/build-image-worker.mjs index 5ac3fff9..2126471d 100644 --- a/scripts/build-image-worker.mjs +++ b/scripts/build-image-worker.mjs @@ -164,7 +164,9 @@ export function buildImageWorker({ if (result.status !== 0) throw new Error(`cargo build failed with ${describeExit(result)}`) if (!fs.existsSync(paths.sourceBinaryPath)) throw new Error(`cargo build completed but binary was not found: ${paths.sourceBinaryPath}`) - const prepared = prepareCargoImageWorkerSource(paths.sourceBinaryPath) + const prepared = prepareCargoImageWorkerSource(paths.sourceBinaryPath, { + searchRoot: paths.cargoTargetDir + }) try { publishImageWorkerBinary( prepared.sourcePath, diff --git a/scripts/cargo-image-worker-source.mjs b/scripts/cargo-image-worker-source.mjs index f9b2740e..bdd4095d 100644 --- a/scripts/cargo-image-worker-source.mjs +++ b/scripts/cargo-image-worker-source.mjs @@ -5,11 +5,52 @@ const stable = (left, right) => ['dev', 'ino', 'size', 'mtimeMs', 'ctimeMs', 'nlink'].every((key) => left[key] === right[key]) const regular = (stat) => stat.isFile() && !stat.isSymbolicLink() -// Cargo publishes the executable as two links: release/name.exe and -// release/deps/name_with_underscores.exe. Only that exact two-link identity is -// accepted here. The existing publication gate still receives a single-link -// snapshot and continues to reject arbitrary linked source/output files. -export function prepareCargoImageWorkerSource(sourcePath) { +const MAX_SEARCH_ENTRIES = 200_000 + +const lstatOrNull = (target) => { + try { + return fs.lstatSync(target) + } catch (error) { + if (error?.code === 'ENOENT') return null + throw error + } +} + +// Newer Cargo keeps its intermediate artifacts in a build directory inside the target directory +// instead of release/deps, so the second link moves. It is looked for only inside this build's +// own target directory, without following links, and it must be the only other link there. +function findBuildDirAlias(sourcePath, searchRoot) { + // 64-bit file ids (NTFS) lose precision as numbers, so compare them as bigints. + const before = fs.lstatSync(sourcePath, { bigint: true }) + const matches = [] + const pending = [searchRoot] + let entries = 0 + while (pending.length) { + const directory = pending.pop() + for (const entry of fs.readdirSync(directory, { withFileTypes: true })) { + if (++entries > MAX_SEARCH_ENTRIES) { + throw new Error('Cargo image worker target directory is too large to search') + } + const full = path.join(directory, entry.name) + if (entry.isSymbolicLink()) continue + if (entry.isDirectory()) pending.push(full) + else if (entry.isFile() && full !== sourcePath) { + const stat = fs.lstatSync(full, { bigint: true }) + if (stat.dev === before.dev && stat.ino === before.ino) matches.push(full) + } + } + } + if (matches.length !== 1) throw new Error('Cargo image worker hard-link alias not found') + return matches[0] +} + +// Cargo publishes the executable as two links: release/name.exe and its build artifact, which +// older Cargo keeps at release/deps/name_with_underscores.exe and newer Cargo keeps in its build +// directory. Only that exact two-link identity is accepted here; with `searchRoot` (the build's +// own Cargo target directory) the build-directory link is accepted too. The existing +// publication gate still receives a single-link snapshot and continues to reject arbitrary +// linked source/output files. +export function prepareCargoImageWorkerSource(sourcePath, { searchRoot } = {}) { const before = fs.lstatSync(sourcePath) if (!regular(before)) throw new Error('Cargo image worker source must be a regular file') if (before.nlink === 1) return { sourcePath, cleanup() {} } @@ -17,8 +58,21 @@ export function prepareCargoImageWorkerSource(sourcePath) { throw new Error('Unexpected Cargo image worker hard-link layout') } const releaseDir = path.dirname(sourcePath) - const depsDir = path.join(releaseDir, 'deps') - const alias = path.join(depsDir, 'canvas_thumbnail_sidecar.exe') + const legacyAlias = path.join(releaseDir, 'deps', 'canvas_thumbnail_sidecar.exe') + let alias = legacyAlias + if (!lstatOrNull(legacyAlias)) { + if (!searchRoot) throw new Error('Cargo image worker hard-link alias not found') + const root = path.resolve(searchRoot) + const rootStat = fs.lstatSync(root) + if (!rootStat.isDirectory() || rootStat.isSymbolicLink()) { + throw new Error('Cargo image worker directories must not be symbolic links') + } + if (path.relative(root, sourcePath).startsWith('..')) { + throw new Error('Cargo image worker source is outside its target directory') + } + alias = findBuildDirAlias(sourcePath, root) + } + const depsDir = path.dirname(alias) for (const directory of [releaseDir, depsDir]) { const stat = fs.lstatSync(directory) if (!stat.isDirectory() || stat.isSymbolicLink()) { diff --git a/scripts/cargo-image-worker-source.test.js b/scripts/cargo-image-worker-source.test.js index a16126a9..af265600 100644 --- a/scripts/cargo-image-worker-source.test.js +++ b/scripts/cargo-image-worker-source.test.js @@ -50,6 +50,41 @@ describe('Cargo image worker publication source', () => { } expect(fs.readdirSync(release).sort()).toEqual(['canvas-thumbnail-sidecar.exe', 'deps']) })) + it('finds the second link in the build directory of newer Cargo, inside the target directory only', () => + fixture(({ root, release, source }) => { + fs.rmSync(path.join(release, 'deps'), { recursive: true }) + const buildDir = path.join(root, 'build', 'canvas-thumbnail-sidecar', 'abc123', 'out') + fs.mkdirSync(buildDir, { recursive: true }) + const alias = path.join(buildDir, 'canvas_thumbnail_sidecar-abc123.exe') + fs.linkSync(source, alias) + const before = fs.readFileSync(source) + expect(() => prepareCargoImageWorkerSource(source)).toThrow('alias not found') + const prepared = prepareCargoImageWorkerSource(source, { searchRoot: root }) + try { + expect(prepared.sourcePath).not.toBe(source) + expect(fs.statSync(prepared.sourcePath).nlink).toBe(1) + expect(fs.readFileSync(prepared.sourcePath)).toEqual(before) + } finally { + prepared.cleanup() + } + expect(fs.readdirSync(release)).toEqual(['canvas-thumbnail-sidecar.exe']) + })) + it('refuses a build-directory link outside the target directory', () => + fixture(({ root, release, source }) => { + fs.rmSync(path.join(release, 'deps'), { recursive: true }) + const outside = fs.mkdtempSync(path.join(os.tmpdir(), 'magicpot-cargo-outside-')) + try { + fs.linkSync(source, path.join(outside, 'canvas_thumbnail_sidecar.exe')) + expect(() => prepareCargoImageWorkerSource(source, { searchRoot: release })).toThrow( + 'alias not found' + ) + expect(() => + prepareCargoImageWorkerSource(source, { searchRoot: path.join(root, 'missing') }) + ).toThrow() + } finally { + fs.rmSync(outside, { recursive: true, force: true }) + } + })) it('accepts an existing regular single-link artifact without mutating it', () => fixture(({ source }) => { const prepared = prepareCargoImageWorkerSource(source)