A file reader accepts notes.txt inside a workspace. It should reject a file in a sibling directory, even if that directory’s name starts with the same text. A symlink inside the workspace needs another check: its target might be outside.
This local exercise demonstrates those distinctions with disposable fixtures. It does not create a production sandbox. Run it only in an environment you control; every file it creates lives under a new temporary directory.
1. Create the isolated exercise
Use Node.js 22 or newer on Linux or macOS, where creating these file symlinks normally needs no extra privileges. Save the code as file-boundaries.mjs. Windows symlink creation may require Developer Mode or suitable privileges; a setup error must be treated as a test that did not run.
The helper accepts a relative filename, resolves its actual target, and compares path components with the workspace root. The sibling fixture deliberately shares the root’s name prefix.
import assert from 'node:assert/strict';
import { mkdtempSync, mkdirSync, writeFileSync, realpathSync, readFileSync, symlinkSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import path from 'node:path';
const temporary = mkdtempSync(path.join(tmpdir(), 'linmas-files-'));
try {
const root = path.join(temporary, 'workspace');
const sibling = path.join(temporary, 'workspace-backup');
mkdirSync(root);
mkdirSync(sibling);
writeFileSync(path.join(root, 'notes.txt'), 'allowed fixture');
writeFileSync(path.join(sibling, 'private.txt'), 'synthetic private fixture');
symlinkSync(path.join(sibling, 'private.txt'), path.join(root, 'shortcut.txt'));
const canonicalRoot = realpathSync(root);
function readWorkspaceFile(name) {
assert(typeof name === 'string' && name.length > 0 && !name.includes('\0'), 'invalid path');
assert(!path.isAbsolute(name), 'absolute path denied');
const target = realpathSync(path.resolve(canonicalRoot, name));
const relative = path.relative(canonicalRoot, target);
assert(relative !== '..' && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative), 'outside workspace');
// ponytail: immutable local fixtures; hostile concurrent writes need OS-enforced containment.
return readFileSync(target, 'utf8');
}
assert.equal(readWorkspaceFile('notes.txt'), 'allowed fixture');
assert.throws(() => readWorkspaceFile('../workspace-backup/private.txt'), /outside workspace/);
assert.throws(() => readWorkspaceFile('shortcut.txt'), /outside workspace/);
assert.throws(() => readWorkspaceFile(path.join(sibling, 'private.txt')), /absolute path denied/);
assert.throws(() => readWorkspaceFile(null), /invalid path/);
assert.throws(() => readWorkspaceFile('bad\0name'), /invalid path/);
assert.throws(() => readWorkspaceFile('missing.txt'), { code: 'ENOENT' });
assert.equal(readFileSync(path.join(sibling, 'private.txt'), 'utf8'), 'synthetic private fixture');
console.log('PASS: allowed file read; sibling, symlink, absolute, invalid, and missing paths rejected.');
} finally {
rmSync(temporary, { recursive: true, force: true });
}
2. Run and inspect the result
node file-boundaries.mjs
The program prints one PASS: line and removes its temporary fixtures, including the symlink. It never follows the link to delete the target separately. The sibling file is checked before cleanup to establish that the reader did not alter it.
If symlink setup or a filesystem operation fails, Node exits with an error. Do not interpret the absence of a leaked file as a successful security test when setup failed.
3. Check that the assertion matters
In a disposable copy, remove the assertion beginning with assert(relative !== '..'. Run it again. The sibling-path test should fail with Missing expected exception: the unprotected reader returned data from outside its root. Restore the assertion before using the original exercise again.
This mutation checks the observation, not just the function’s happy path. The test knows what an unauthorized read looks like because the entire environment is synthetic and controlled.
4. Keep the claim narrow
There is a gap between realpathSync and readFileSync. Another process that can alter the filesystem could change a component during that gap. This example also does not implement file-type restrictions, output-size limits, or a general policy for writable paths.
Use it to learn and regression-test path handling in static fixtures. For an agent that can modify directories or run other tools, enforce containment through the operating system and review every alternative route to the same file. The file-access guide explains why tool-level checks and process-level isolation are different claims.
