/** * Session-directory validation shared by the Web session-creation routes. * * RCS grants machine-level access: a user who can use an environment may * start a session in ANY directory that the worker's OS user can reach. The * server therefore only rejects clearly malformed input (relative paths, * NUL bytes, absurd length) and leaves existence/permission checks to the * worker, which is the only side that can stat the remote filesystem. * * `~`-prefixed paths are accepted verbatim — the worker expands them against * its own OS user's home directory. Everything else must be absolute * (POSIX or Windows) and is lexically normalised (`.`/`..` collapsed). */ import { posix, win32 } from 'node:path' const MAX_PATH_LENGTH = 4096 /** POSIX absolute, Windows drive absolute, or UNC. */ export function isAbsolutePath(path: string): boolean { if (!path) return false if (path.startsWith('/')) return true if (/^[A-Za-z]:[\\/]/.test(path)) return true return path.startsWith('\\\\') } /** Absolute, or a `~`/`~/...` path the worker will expand. */ export function isAbsoluteOrHomePath(path: string): boolean { return path === '~' || path.startsWith('~/') || path.startsWith('~\\') ? true : isAbsolutePath(path) } function stripTrailingSeparators(path: string): string { // Keep filesystem roots (`/`, `C:\`, UNC roots) intact. if (path.length <= 1) return path if (/^[A-Za-z]:[\\/]$/.test(path)) return path const stripped = path.replace(/[\\/]+$/, '') return stripped || path.charAt(0) } function normalizeAbsolutePath(path: string): string { const normalized = /^[A-Za-z]:[\\/]/.test(path) || path.startsWith('\\\\') ? win32.normalize(path) : posix.normalize(path) return stripTrailingSeparators(normalized) } export type CwdValidation = | { ok: true; cwd: string } | { ok: false; message: string } /** * Validate a user-supplied session working directory. * Empty input is the caller's responsibility (it means "use the default"). */ export function normalizeSessionCwd(raw: unknown): CwdValidation { if (typeof raw !== 'string') { return { ok: false, message: 'cwd must be a string' } } const trimmed = raw.trim() if (!trimmed) { return { ok: false, message: 'cwd must not be empty' } } if (trimmed.length > MAX_PATH_LENGTH) { return { ok: false, message: `cwd must be at most ${MAX_PATH_LENGTH} characters`, } } if (trimmed.includes('\0')) { return { ok: false, message: 'cwd contains an invalid character' } } if (isAbsoluteOrHomePath(trimmed)) { // Home-relative paths stay verbatim; the worker expands them. return { ok: true, cwd: trimmed.startsWith('~') ? trimmed : normalizeAbsolutePath(trimmed), } } return { ok: false, message: 'cwd must be an absolute path (or start with ~/)', } } /** * Normalise the worker's quick-pick directory list. Advisory only: the Web * UI uses it for shortcuts, never as an access-control list. Falls back to * the worker's startup directory for legacy workers. */ export function normalizeWorkspaceDirs( raw: unknown, directory: string | null | undefined, ): string[] { const out: string[] = [] const seen = new Set() const push = (value: string): void => { const trimmed = value.trim() if (!trimmed || !isAbsolutePath(trimmed) || trimmed.includes('\0')) return const normalized = normalizeAbsolutePath(trimmed) if (seen.has(normalized)) return seen.add(normalized) out.push(normalized) } if (Array.isArray(raw)) { for (const entry of raw) { if (typeof entry === 'string') push(entry) } } if (out.length === 0 && directory) push(directory) return out }