Something went wrong. Try again.
A reusable desktop shell: your sidecar, your index, safe APIs, installers for every platform
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748import { test } from 'node:test';import assert from 'node:assert/strict';import { mkdtempSync, mkdirSync, realpathSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';import { tmpdir } from 'node:os';import { join } from 'node:path';import { allowedPath, expandHome, realish, within } from '../shell/paths.js';
test('expandHome', () => { assert.equal(expandHome('~', '/h'), '/h'); assert.equal(expandHome('~/x', '/h'), '/h/x'); assert.equal(expandHome('/~/x', '/h'), '/~/x');});
test('within', () => { assert.ok(within('/a/b', '/a')); assert.ok(within('/a', '/a')); assert.ok(!within('/ab', '/a')); assert.ok(within('/a/b', '/a/'));});
test('allowedPath: roots, grants, symlinks, missing files', () => { const dir = realpathSync(mkdtempSync(join(tmpdir(), 'mollusc-paths-'))); try { mkdirSync(join(dir, 'root', 'sub'), { recursive: true }); mkdirSync(join(dir, 'outside')); writeFileSync(join(dir, 'root', 'sub', 'f.txt'), 'x'); writeFileSync(join(dir, 'outside', 'secret'), 'x'); symlinkSync(join(dir, 'outside'), join(dir, 'root', 'link')); const roots = [join(dir, 'root')]; assert.equal(allowedPath(join(dir, 'root', 'sub', 'f.txt'), { roots }), join(dir, 'root', 'sub', 'f.txt')); assert.equal(allowedPath(join(dir, 'root', 'sub', '..', 'sub', 'f.txt'), { roots }), join(dir, 'root', 'sub', 'f.txt')); // A file that does not exist yet, under a root, is fine to write. assert.equal(allowedPath(join(dir, 'root', 'new', 'file.txt'), { roots }), join(dir, 'root', 'new', 'file.txt')); assert.equal(realish(join(dir, 'root', 'new', 'file.txt')), join(dir, 'root', 'new', 'file.txt')); assert.throws(() => allowedPath(join(dir, 'outside', 'secret'), { roots }), /Not allowed/); assert.throws(() => allowedPath(join(dir, 'root', '..', 'outside', 'secret'), { roots }), /Not allowed/); // Through the symlink is still outside. assert.throws(() => allowedPath(join(dir, 'root', 'link', 'secret'), { roots }), /Not allowed/); // Unless the user picked it. assert.equal(allowedPath(join(dir, 'outside', 'secret'), { roots, grants: [join(dir, 'outside', 'secret')] }), join(dir, 'outside', 'secret')); assert.equal(allowedPath(join(dir, 'outside', 'secret'), { roots, grants: [join(dir, 'outside')] }), join(dir, 'outside', 'secret')); assert.throws(() => allowedPath(join(dir, 'outside', 'other'), { roots, grants: [join(dir, 'outside', 'secret')] }), /Not allowed/); assert.throws(() => allowedPath('', { roots }), TypeError); assert.throws(() => allowedPath(42, { roots }), TypeError); } finally { rmSync(dir, { recursive: true, force: true }); }});