test(dsh): accept canonical aliases of trusted temporary roots

Signed-off-by: XiaoSeS <87064762+XiaoSeS@users.noreply.github.com>
This commit is contained in:
XiaoSeS 2026-10-29 20:28:43 +08:00
parent 095b4780af
commit 5defc7f427
2 changed files with 37 additions and 3 deletions

View file

@ -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);

View file

@ -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;
}
}
});