Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
import { describe, it, expect, beforeAll, afterAll, beforeEach } from 'bun:test';
import { mkdtempSync, rmSync, writeFileSync, symlinkSync, mkdirSync, realpathSync } from 'fs';
import { join, win32 } from 'path';
import { tmpdir } from 'os';
import {
validateUploadPath,
validatePageSlug,
validateFilename,
OperationError,
} from '../src/core/operations.ts';
import { uploadPathInsideRoot } from '../src/core/ops/context.ts';
// --- validateUploadPath ---
describe('validateUploadPath', () => {
let sandbox: string;
let root: string;
let outside: string;
beforeAll(() => {
sandbox = mkdtempSync(join(tmpdir(), 'gbrain-upload-'));
root = realpathSync(sandbox);
outside = mkdtempSync(join(tmpdir(), 'gbrain-outside-'));
});
afterAll(() => {
rmSync(sandbox, { recursive: true, force: true });
rmSync(outside, { recursive: true, force: true });
});
it('allows a regular file inside the confinement root', () => {
const p = join(root, 'photo.jpg');
writeFileSync(p, 'binary');
expect(() => validateUploadPath(p, root)).not.toThrow();
});
it('allows a nested file inside the confinement root', () => {
const sub = join(root, 'sub');
mkdirSync(sub, { recursive: true });
const p = join(sub, 'note.txt');
writeFileSync(p, 'hi');
expect(() => validateUploadPath(p, root)).not.toThrow();
});
it('rejects a path outside the confinement root', () => {
const p = join(outside, 'secret.txt');
writeFileSync(p, 'x');
expect(() => validateUploadPath(p, root)).toThrow(OperationError);
try { validateUploadPath(p, root); } catch (e) {
expect((e as OperationError).code).toBe('invalid_params');
expect((e as Error).message).toMatch(/within the working directory/i);
}
});
it('rejects ../ traversal above the root', () => {
const p = join(root, '..', 'escaped.txt');
writeFileSync(p, 'nope');
try {
expect(() => validateUploadPath(p, root)).toThrow(OperationError);
} finally {
rmSync(p, { force: true });
}
});
it('allows a file under a folder whose name starts with two dots', () => {
// `..drafts` is an ordinary name, not a parent-dir step: the confinement
// check must compare path components, not a `..` string prefix.
const dir = join(root, '..drafts');
mkdirSync(dir, { recursive: true });
const p = join(dir, 'report.pdf');
writeFileSync(p, 'pdf');
try {
expect(validateUploadPath(p, root)).toBe(realpathSync(p));
expect(validateUploadPath(p, root, true)).toBe(realpathSync(p));
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it('allows a file under a folder named exactly ... (three dots)', () => {
const dir = join(root, '...');
mkdirSync(dir, { recursive: true });
const p = join(dir, 'note.txt');
writeFileSync(p, 'hi');
try {
expect(validateUploadPath(p, root)).toBe(realpathSync(p));
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it('rejects the confinement root itself', () => {
expect(() => validateUploadPath(root, root)).toThrow(/within the working directory/i);
});
it('rejects a sibling directory that shares the root name as a prefix', () => {
const sibling = `${root}-sibling`;
mkdirSync(sibling, { recursive: true });
const p = join(sibling, 'x.txt');
writeFileSync(p, 'x');
try {
expect(() => validateUploadPath(p, root)).toThrow(/within the working directory/i);
} finally {
rmSync(sibling, { recursive: true, force: true });
}
});
it('rejects a ..-named folder reached through a parent-dir step', () => {
// `<root>/../..drafts-escape/x` leaves the root even though the folder
// name itself only starts with two dots.
const dir = join(root, '..', `..drafts-escape-${process.pid}`);
mkdirSync(dir, { recursive: true });
const p = join(dir, 'x.txt');
writeFileSync(p, 'x');
try {
expect(() => validateUploadPath(p, root)).toThrow(/within the working directory/i);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it('rejects /etc/passwd (absolute path outside root)', () => {
expect(() => validateUploadPath('/etc/passwd', root)).toThrow(OperationError);
});
it('rejects a symlink whose final component points outside root (B5 regression)', () => {
const target = join(outside, 'target.txt');
writeFileSync(target, 'secret');
const link = join(root, 'link-to-outside.txt');
symlinkSync(target, link);
try {
expect(() => validateUploadPath(link, root)).toThrow(OperationError);
} finally {
rmSync(link, { force: true });
}
});
it('rejects a symlink whose parent dir points outside root (B5 parent-symlink regression)', () => {
const linkDir = join(root, 'link-dir');
symlinkSync(outside, linkDir);
const p = join(linkDir, 'secret.txt');
writeFileSync(join(outside, 'secret.txt'), 'secret');
try {
expect(() => validateUploadPath(p, root)).toThrow(OperationError);
} finally {
rmSync(linkDir, { force: true });
rmSync(join(outside, 'secret.txt'), { force: true });
}
});
it('rejects non-existent paths with a clear error', () => {
const p = join(root, 'never-created.txt');
try {
validateUploadPath(p, root);
throw new Error('expected throw');
} catch (e) {
expect(e).toBeInstanceOf(OperationError);
expect((e as OperationError).code).toBe('invalid_params');
expect((e as Error).message).toMatch(/File not found/i);
}
});
it('handles relative paths via resolve', () => {
const p = join(root, 'rel.txt');
writeFileSync(p, 'hi');
const prevCwd = process.cwd();
process.chdir(root);
try {
expect(() => validateUploadPath('./rel.txt', root)).not.toThrow();
} finally {
process.chdir(prevCwd);
}
});
});
// --- validatePageSlug (H5 allowlist) ---
describe('validatePageSlug', () => {
it('accepts clean slugs', () => {
expect(() => validatePageSlug('people/alice-smith')).not.toThrow();
expect(() => validatePageSlug('concepts/ai')).not.toThrow();
expect(() => validatePageSlug('a')).not.toThrow();
expect(() => validatePageSlug('a/b/c/d')).not.toThrow();
});
it('accepts sync-slugifier dot/underscore continuation slugs (#4665)', () => {
// slugifySegment deliberately preserves '.' and '_' (v1.0.0, my_file_name);
// the op-boundary validator must round-trip them.
expect(() => validatePageSlug('notes/v1.0.0')).not.toThrow();
expect(() => validatePageSlug('people/my_file_name')).not.toThrow();
});
it('accepts colon-separated namespace parts (#5032)', () => {
expect(() => validatePageSlug('calendar:abc123def456')).not.toThrow();
expect(() => validatePageSlug('integrations/calendar:event-123')).not.toThrow();
});
it('rejects empty or dot-led colon namespace parts (#5032)', () => {
expect(() => validatePageSlug('calendar:')).toThrow(OperationError);
expect(() => validatePageSlug(':event')).toThrow(OperationError);
expect(() => validatePageSlug('calendar::event')).toThrow(OperationError);
expect(() => validatePageSlug('calendar:../event')).toThrow(OperationError);
});
it('accepts underscore-LED segments the slugifier produces (Hugo _index shape, #4665)', () => {
// slugifySegment('_index') === '_index' — leading underscores survive
// sync, so the op boundary must round-trip them too. Dots stay
// continuation-only (the traversal fence is unchanged).
expect(() => validatePageSlug('notes/_index')).not.toThrow();
expect(() => validatePageSlug('_scratch/notes')).not.toThrow();
expect(() => validatePageSlug('notes/.hidden')).toThrow(OperationError);
});
it('rejects dot-LED segments (dots are continuation-only)', () => {
expect(() => validatePageSlug('notes/.hidden')).toThrow(OperationError);
expect(() => validatePageSlug('.git/config')).toThrow(OperationError);
});
it('rejects ../ traversal', () => {
expect(() => validatePageSlug('../etc/passwd')).toThrow(OperationError);
expect(() => validatePageSlug('pages/../../etc')).toThrow(OperationError);
});
it('rejects URL-encoded traversal (not in allowlist)', () => {
expect(() => validatePageSlug('%2e%2e%2fetc%2fpasswd')).toThrow(OperationError);
});
it('rejects absolute paths', () => {
expect(() => validatePageSlug('/etc/passwd')).toThrow(OperationError);
});
it('rejects backslash (Windows separator)', () => {
expect(() => validatePageSlug('people\\alice')).toThrow(OperationError);
});
it('rejects leading/trailing slash', () => {
expect(() => validatePageSlug('/people/alice')).toThrow(OperationError);
expect(() => validatePageSlug('people/alice/')).toThrow(OperationError);
});
it('rejects consecutive slashes', () => {
expect(() => validatePageSlug('people//alice')).toThrow(OperationError);
});
it('rejects empty or too-long', () => {
expect(() => validatePageSlug('')).toThrow(OperationError);
expect(() => validatePageSlug('a'.repeat(256))).toThrow(OperationError);
});
it('rejects NUL and control chars', () => {
expect(() => validatePageSlug('people\x00alice')).toThrow(OperationError);
expect(() => validatePageSlug('people\nalice')).toThrow(OperationError);
});
it('rejects spaces', () => {
expect(() => validatePageSlug('people/alice smith')).toThrow(OperationError);
});
});
// --- validateFilename (M4 allowlist) ---
describe('validateFilename', () => {
it('accepts clean filenames with extensions', () => {
expect(() => validateFilename('photo.jpg')).not.toThrow();
expect(() => validateFilename('report-2026.pdf')).not.toThrow();
expect(() => validateFilename('v1.0.0_release.md')).not.toThrow();
});
it('rejects control chars', () => {
expect(() => validateFilename('file\nwith\nnewlines.txt')).toThrow(OperationError);
expect(() => validateFilename('file\x00nul.txt')).toThrow(OperationError);
});
it('rejects backslash', () => {
expect(() => validateFilename('file\\win.txt')).toThrow(OperationError);
});
it('rejects RTL override and other Unicode injection', () => {
expect(() => validateFilename('file\u202E.exe')).toThrow(OperationError);
});
it('rejects leading dash (CLI flag confusion)', () => {
expect(() => validateFilename('-rf.txt')).toThrow(OperationError);
});
it('rejects leading dot (hidden files)', () => {
expect(() => validateFilename('.htaccess')).toThrow(OperationError);
});
it('rejects empty and too-long', () => {
expect(() => validateFilename('')).toThrow(OperationError);
expect(() => validateFilename('x'.repeat(256))).toThrow(OperationError);
});
it('rejects path separators in filename', () => {
expect(() => validateFilename('foo/bar.txt')).toThrow(OperationError);
});
});
// --- strict upload containment, Windows path shapes ---
// No Windows runner exists, and validateUploadPath's realpathSync never returns
// backslash paths on POSIX, so these drive the pure comparison it uses with
// path.win32 shapes. The old check (relative + round trip) accepted another
// drive and a UNC share, because path.win32.relative across drives returns an
// absolute path that does not start with '..'.
describe('uploadPathInsideRoot — win32 shapes', () => {
const root = 'C:\\work';
const inside = (p: string) => uploadPathInsideRoot(p, root, win32.sep);
it('the old relative-path check accepted another drive and a UNC share', () => {
const oldCheck = (real: string) => {
const rel = win32.relative(root, real);
return !(rel === '' || rel.startsWith('..') || win32.resolve(root, rel) !== real);
};
expect(oldCheck('D:\\x.txt')).toBe(true);
expect(oldCheck('\\\\srv\\share\\x.txt')).toBe(true);
});
it('refuses a file on another drive', () => {
expect(inside('D:\\x.txt')).toBe(false);
expect(inside('D:\\work\\x.txt')).toBe(false);
});
it('refuses a file on a UNC share', () => {
expect(inside('\\\\srv\\share\\x.txt')).toBe(false);
});
it('refuses a sibling folder sharing the root name, and the root itself', () => {
expect(inside('C:\\work-sibling\\x.txt')).toBe(false);
expect(inside('C:\\work')).toBe(false);
});
it('accepts a file under a folder whose name starts with two dots', () => {
expect(inside('C:\\work\\..drafts\\report.pdf')).toBe(true);
expect(inside('C:\\work\\a.txt')).toBe(true);
});
});