diff --git a/plugins/dsh/scripts/smoke-native-host.mjs b/plugins/dsh/scripts/smoke-native-host.mjs index 83d622e10..e32d92911 100644 --- a/plugins/dsh/scripts/smoke-native-host.mjs +++ b/plugins/dsh/scripts/smoke-native-host.mjs @@ -214,9 +214,12 @@ export async function prepareDownloadCache(path) { const target = resolve(path); for (const base of bases) { const basePath = resolve(base); - const suffix = relative(basePath, target); - if (!suffix || suffix === '..' || suffix.startsWith('..' + sep) || isAbsolute(suffix)) continue; - let current = await realpath(basePath); + const canonicalBase = await realpath(basePath); + // Only the trusted anchor may be canonicalized: never resolve unverified descendants. + const suffix = [basePath, canonicalBase].map(anchor => relative(anchor, target)) + .find(value => value && value !== '..' && !value.startsWith('..' + sep) && !isAbsolute(value)); + if (!suffix) continue; + let current = canonicalBase; const parts = suffix.split(sep); for (const [index, part] of parts.entries()) { current = join(current, part); diff --git a/plugins/dsh/test/smoke-native-host.test.ts b/plugins/dsh/test/smoke-native-host.test.ts index 27592763c..397e8f77d 100644 --- a/plugins/dsh/test/smoke-native-host.test.ts +++ b/plugins/dsh/test/smoke-native-host.test.ts @@ -252,3 +252,34 @@ test.skipIf(process.platform === 'win32')('download cache rejects writable inter await expect(access(join(parent, 'cache'))).rejects.toThrow(); expect((await stat(parent)).mode & 0o777).toBe(0o777); }); + + +test('trusted temporary base aliases accept lexical and canonical children but never anchors or descendant links', async () => { + const directory = await root(); + const actual = join(directory, 'actual-temp'); + const alias = join(directory, 'temp-alias'); + await mkdir(actual); + await symlink(actual, alias, process.platform === 'win32' ? 'junction' : 'dir'); + const previous = { CI: process.env.CI, RUNNER_TEMP: process.env.RUNNER_TEMP, TMPDIR: process.env.TMPDIR, TEMP: process.env.TEMP, TMP: process.env.TMP }; + try { + process.env.TMPDIR = alias; process.env.TEMP = alias; process.env.TMP = alias; + delete process.env.RUNNER_TEMP; + expect((await prepareDownloadCache(join(actual, 'canonical'))).path).toBe(join(actual, 'canonical')); + expect((await prepareDownloadCache(join(alias, 'lexical'))).path).toBe(join(actual, 'lexical')); + await expect(prepareDownloadCache(alias)).rejects.toThrow(); + await expect(prepareDownloadCache(actual)).rejects.toThrow(); + const descendant = join(actual, 'linked'); + await symlink(directory, descendant, process.platform === 'win32' ? 'junction' : 'dir'); + await expect(prepareDownloadCache(join(alias, 'linked', 'escape'))).rejects.toThrow(); + await expect(prepareDownloadCache(join(actual, 'linked', 'escape'))).rejects.toThrow(); + const unrelated = join(directory, 'other-temp'); await mkdir(unrelated); + process.env.TMPDIR = unrelated; process.env.TEMP = unrelated; process.env.TMP = unrelated; + process.env.CI = 'true'; process.env.RUNNER_TEMP = alias; + expect((await prepareDownloadCache(join(actual, 'runner'))).path).toBe(join(actual, 'runner')); + await expect(prepareDownloadCache(actual)).rejects.toThrow(); + } finally { + for (const [key, value] of Object.entries(previous)) { + if (value === undefined) delete process.env[key]; else process.env[key] = value; + } + } +});