项目文件夹

文件
Trung Huynh Chi a9228141ae fix: validate extension-enforced output paths (#2269)
Fixes output path validation so tools cannot validate one path and then
write to a different canonical target after extension enforcement.

Changes:
- Resolve dangling symlinks to their target path during
canonicalization.
- Validate the final extension-enforced output path before writing.
- Apply the same final-path validation to heap snapshots and
screencasts.
- Add regression coverage for dangling symlinks that point outside
configured roots.

Validation:
- npm run format
- npm run check-format
- npm run test tests/utils/files.test.ts
- npm run test tests/roots.test.ts
- npm run test tests/tools/memory.test.ts tests/tools/screencast.test.ts

Note: I also ran the full npm test suite locally. The targeted tests
above passed, but the full suite hit local WSL daemon/e2e startup
timeouts while waiting for daemon.pid / server_start, which appear
unrelated to this path-validation change.

---------

Co-authored-by: huynhtrungcsc <huynhtrungcsc@users.noreply.github.com>
2026-07-03 09:18:59 +00:00

96 行
3.0 KiB
TypeScript

/**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import assert from 'node:assert';
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import {describe, it} from 'node:test';
import {resolveCanonicalPath} from '../../src/utils/files.js';
describe('resolveCanonicalPath', () => {
it('should resolve an existing standard file path', async () => {
const tmpDir = await fs.mkdtemp(
path.join(os.tmpdir(), 'resolve-canonical-test-'),
);
try {
const filePath = path.join(tmpDir, 'test.txt');
await fs.writeFile(filePath, 'hello');
const resolved = await resolveCanonicalPath(filePath);
const canonicalTmpDir = await fs.realpath(tmpDir);
assert.strictEqual(resolved, path.join(canonicalTmpDir, 'test.txt'));
} finally {
await fs.rm(tmpDir, {recursive: true, force: true});
}
});
it('should resolve a non-existent file whose parent directory exists', async () => {
const tmpDir = await fs.mkdtemp(
path.join(os.tmpdir(), 'resolve-canonical-test-'),
);
try {
const filePath = path.join(tmpDir, 'non-existent.txt');
const resolved = await resolveCanonicalPath(filePath);
const canonicalTmpDir = await fs.realpath(tmpDir);
assert.strictEqual(
resolved,
path.join(canonicalTmpDir, 'non-existent.txt'),
);
} finally {
await fs.rm(tmpDir, {recursive: true, force: true});
}
});
it('should resolve a non-existent deeply nested file whose parent directories do not exist', async () => {
const tmpDir = await fs.mkdtemp(
path.join(os.tmpdir(), 'resolve-canonical-test-'),
);
try {
const filePath = path.join(
tmpDir,
'nested1',
'nested2',
'non-existent.txt',
);
const resolved = await resolveCanonicalPath(filePath);
const canonicalTmpDir = await fs.realpath(tmpDir);
assert.strictEqual(
resolved,
path.join(canonicalTmpDir, 'nested1', 'nested2', 'non-existent.txt'),
);
} finally {
await fs.rm(tmpDir, {recursive: true, force: true});
}
});
it('should resolve existing files with symlinks in path', async () => {
const tmpDir = await fs.mkdtemp(
path.join(os.tmpdir(), 'resolve-canonical-test-'),
);
try {
const targetDir = path.join(tmpDir, 'target');
await fs.mkdir(targetDir);
const targetFile = path.join(targetDir, 'file.txt');
await fs.writeFile(targetFile, 'hello');
const symlinkDir = path.join(tmpDir, 'symlink_dir');
await fs.symlink(targetDir, symlinkDir, 'dir');
const filePathWithSymlink = path.join(symlinkDir, 'file.txt');
const resolved = await resolveCanonicalPath(filePathWithSymlink);
const canonicalTargetDir = await fs.realpath(targetDir);
assert.strictEqual(resolved, path.join(canonicalTargetDir, 'file.txt'));
} finally {
await fs.rm(tmpDir, {recursive: true, force: true});
}
});
});