fix(html-view): refuse /tmp, /var/tmp and directories above a broad root

os.tmpdir() returns $TMPDIR when it is set, and macOS sets it in every
terminal session, so with TMPDIR set /tmp, /private/tmp and /var/tmp were
never treated as broad and /tmp/report.html got a capability over all of
/tmp. The broad-root list now names /tmp and /var/tmp literally (through
canonical(), so macOS gets /private/tmp), refuses any directory that is an
ancestor of a broad root (/home, /Users, /private), and treats a drive's
mount point (/mnt/d on WSL, /media/x, /Volumes/x) as broad as well.

The new test sets TMPDIR elsewhere and still gets /tmp refused.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Codeman maintainer committed 2026-10-13 17:07:36 +02:00
1 parent 2797098aa5
commit ded7911d1e
4 files changed
+54 -7

No files matched your search

+26
View File
@@ -163,6 +163,32 @@ describe('html-view routes', () => {
expect(isBroadHtmlViewRoot(workspace)).toBe(false);
});
it('treats /tmp, /var/tmp and every directory above a broad root as broad, whatever TMPDIR says', async () => {
// macOS sets TMPDIR to a per-user /var/folders dir in every terminal, which
// os.tmpdir() returns, so the literal shared temp dirs must not depend on it.
const savedTmpdir = process.env.TMPDIR;
process.env.TMPDIR = join(root, 'private-tmp');
mkdirSync(process.env.TMPDIR);
const page = join('/tmp', `html-view-broad-literal-${process.pid}.html`);
writeFileSync(page, '<p>broad</p>');
try {
expect(tmpdir()).toBe(process.env.TMPDIR);
expect(isBroadHtmlViewRoot('/tmp')).toBe(true);
expect(isBroadHtmlViewRoot('/var/tmp')).toBe(true);
expect((await open(page)).statusCode).toBe(403);
} finally {
rmSync(page, { force: true });
if (savedTmpdir === undefined) delete process.env.TMPDIR;
else process.env.TMPDIR = savedTmpdir;
}
// Above the home directory (`/home`, `/Users`): serving it would serve home too.
expect(isBroadHtmlViewRoot(join(homedir(), '..'))).toBe(true);
// A drive's mount point and the mount parent itself.
expect(isBroadHtmlViewRoot('/mnt')).toBe(true);
expect(isBroadHtmlViewRoot('/mnt/d')).toBe(true);
expect(isBroadHtmlViewRoot('/mnt/d/projects/report')).toBe(false);
});
it('refuses a page sitting directly in the temp dir', async () => {
const page = join(tmpdir(), `html-view-broad-${process.pid}.html`);
writeFileSync(page, '<p>broad</p>');