/** * Files this computer may send to Melaya for a connector call: the runner side * of `local:` arguments (server services/files/runnerFileStage.ts). * * The server asks; THIS module decides, on the user's own computer, whether the * file may leave it. Same consent model as the other files the runner hands * over (stepPlans.ts for ~/.melaya/plans, assistantFiles.ts for turn outputs): * a file leaves only from a place the user chose, never by a path the server or * a model made up. On the server, a call carrying `local:` is a write that a * human approved, after seeing the name and size this module reported. * * runner:file-check {path} -> {ok, name, size} | {ok:false, code, message} * runner:file-stage {path, uploadUrl, maxBytes, tool} -> {ok, name, size} | {ok:false, code, message} * * - Shared places: ~/.melaya/files/ (always; the user puts a file there to * send it) and the folders the user shared ON THIS COMPUTER with * `melaya-runner files allow ` (kept in ~/.melaya/file-access.json, * 0600). Nothing on the socket can add a folder: the server can only ask. * - The asked path is checked AS TEXT against the shared places before any * file-system call: a path outside them is refused without touching the * disk (no existence oracle), and a network or device path (`//host/x`, * `\\host\x`, `\\?\…`, `\\.\…`) is never even resolved (on Windows it would * reach another machine with the user's credentials). * - Then the REAL path must be inside a shared place too: no `..` escape, no * symbolic link (the file itself, or a folder resolving elsewhere), no * hard-linked file. The file is opened with O_NOFOLLOW, the open file is * compared (device, inode, link count, size) with what was checked, its * real path is checked again after the open, and the bytes are streamed * from that open file: swapping the file between check and send fails. * - Never a secret (keys, tokens, .env, SSH / cloud credentials: the coding * policy's isSecretPath), nothing under a hidden folder (~/.ssh, * ~/.melaya-runner, ~/.config, …) or the system's application data * (~/Library, AppData), never a program, script, disk image or macro * document, never an empty file, at most `maxBytes` (the server's cap). * - The bytes go only to the upload URL on the server this runner is * connected to (same host, https), streamed, and each send is logged. * * The desktop app runs this same runner, so the app answers too. */ import type { FileHandle } from "node:fs/promises"; export declare const FILE_STAGE_CAPABILITY = "file_stage_v1"; /** runner:file-check: name and size for the approval card, nothing sent. */ export declare const FILE_CHECK_CAPABILITY = "file_check_v1"; /** Ceiling of one file; the server's maxBytes (50 MB by default) wins when smaller. */ export declare const SHARE_MAX_BYTES: number; /** Never sent: programs, scripts, installers, disk images, macro documents, * shortcuts (server STAGED_BLOCKED_EXT). */ export declare const BLOCKED_EXT: ReadonlySet; export type ShareCode = "bad_path" | "not_found" | "not_allowed" | "secret" | "blocked_type" | "too_large" | "not_a_file" | "read_failed" | "upload_failed"; export type ShareCheck = { ok: true; path: string; name: string; size: number; } | { ok: false; code: ShareCode; message: string; }; /** ~/.melaya/files: the always-shared drop folder. */ export declare function filesDir(home?: string): string; /** ~/.melaya/file-access.json: the folders the user shared on this computer. */ export declare function accessFile(home?: string): string; /** The folders the user shared (absolute, as written), never throwing. */ export declare function readSharedFolders(home?: string): Promise; /** A network or device path, refused before anything resolves it. */ export declare function isNetworkOrDevicePath(p: string): boolean; /** Expand `~`, map a bare file name to ~/.melaya/files/; null for a * relative, network or device path. Pure: no file-system call. */ export declare function expandRequestedPath(raw: string, home?: string): string | null; type Opened = { ok: true; fh: FileHandle; path: string; name: string; size: number; } | { ok: false; code: ShareCode; message: string; }; /** Check `raw` and open it for sending (the caller closes `fh`). */ export declare function openShareable(raw: unknown, opts?: { home?: string; maxBytes?: number; }): Promise; /** May `raw` leave this computer? The answer names the rule that refused it. */ export declare function checkShareable(raw: unknown, opts?: { home?: string; maxBytes?: number; }): Promise; /** The upload URL must be on the server this runner talks to, over https * (http only for a localhost development server). */ export declare function uploadUrlAllowed(uploadUrl: string, serverHttpBase: string): boolean; /** How long one upload may take: 20 s plus 600 KB/s (about 5 Mbps), so a * 50 MB file gets about 107 s. The server waits 10 s longer for the answer * (runnerFileStage.stageAckTimeoutMs). */ export declare function uploadAbortMs(size: number): number; export type FetchLike = (url: string, init: Record) => Promise<{ status: number; text(): Promise; }>; /** Stream one opened, checked file to its upload URL (from the open file: * the bytes sent are the bytes checked). */ export declare function uploadSharedFile(file: { fh: FileHandle; size: number; }, uploadUrl: string, fetchFn?: FetchLike): Promise<{ ok: true; } | { ok: false; message: string; }>; /** The subset of a socket.io-client Socket used here (a fake in tests). */ export interface FileShareSocketLike { on(event: string, cb: (...args: any[]) => void): unknown; } export interface FileShareOptions { serverHttpBase: string; home?: () => string; fetch?: FetchLike; log?: (line: string) => void; } /** runner:file-check {path} and runner:file-stage {path, uploadUrl, maxBytes, tool}. */ export declare function attachFileShareHandlers(socket: FileShareSocketLike, o: FileShareOptions): void; /** Folders that may never be shared whole: the file system root, the home * folder or a folder containing it, hidden and app-data folders, secret folders. */ export declare function folderShareRefusal(dir: string, home?: string): string | null; /** `files list | allow | revoke `. Returns the process exit code. */ export declare function runFilesCommand(args: string[], out?: (l: string) => void, home?: string): Promise; export {};