/** * Path resolution and containment. * * This is the security-sensitive module: it decides where a download is allowed * to land. Two mistakes it exists to prevent, both observed in the wild: * * 1. Prefix-matched containment. `"/tmp-evil".startsWith("/tmp")` is true, so * a string-prefix allowlist happily accepts a sibling directory. Containment * must compare path *components*. * 2. Lexical-only checks. A symlink inside an allowed directory can resolve * anywhere, so containment is decided after realpath. */ /** * Expand a leading `~` to the current user's home directory. * * `~user` is deliberately NOT expanded. It means "user's home" to a shell, we * cannot resolve it, and rewriting it to the *current* user's home (which both * existing fleet implementations do, by `slice(1)` or `replace("~", home)`) * silently sends files somewhere the user did not ask for. */ export declare function expandHome(input: string): string; /** * True when `candidate` is `root` or sits beneath it. * * Component-aware: `/tmp-evil` is not inside `/tmp`. Symlinks are resolved for * whichever leading portion of the path exists, so a link cannot smuggle the * destination outside the root. */ export declare function isContained(candidate: string, root: string): boolean; export interface ResolveDirectoryOptions { /** Roots the resolved path must sit under. Default: [homedir(), tmpdir()]. */ allowedRoots?: string[]; /** * What to do when the input is missing or lands outside `allowedRoots`. * Fathom warns and falls back; iOS Apps throws. Both are correct for their * context, so this is a required decision rather than a default. */ onUnsafe?: "fallback" | "throw"; fallback?: string; /** mkdir -p the result. Default true. */ create?: boolean; /** * Called when the resolved directory is NOT the one asked for, so the caller can * say so. Without this the fallback is silent: a user whose configured directory * is rejected gets their files in ~/Downloads with no explanation, and reads it as * the preference being ignored. * * Fires only when an input was supplied and something else was chosen — never for * an absent input (nothing was attempted) and never when `onUnsafe: "throw"` (the * throw is already the signal). Throwing from this callback is the caller's own * bug, so it is invoked after the path is settled but before `mkdir`. */ onFallback?: (attempted: string, chosen: string) => void; } /** * Resolve a user-supplied directory preference to a safe absolute path. */ export declare function resolveDirectory(input: string | undefined, options?: ResolveDirectoryOptions): string; /** * Release a reservation taken by `uniquePath({reserve: true})`. * * Call this when the work that claimed the name fails before writing anything. * An abandoned zero-byte sidecar would otherwise burn that filename forever: * every later attempt skips past it to `name (2)`, `name (3)`, and so on. * * Only removes the sidecar if it is still empty, so it can never delete a * partial download that has real bytes in it and could be resumed. */ export declare function releaseReservation(outputPath: string, reserveSuffix?: string): void; /** * Write a file that only the current user can read, enforcing the mode even if * the path already exists. * * `writeFileSync(path, data, { mode })` applies the mode only when it CREATES * the file — measured: writing a secret over an existing 0644 file leaves it at * 0644, world-readable. Since these files carry signed URLs (bearer * credentials), the mode has to be enforced rather than requested. * * Order matters: chmod BEFORE writing, so the secret is never on disk under * loose permissions, even briefly. */ export declare function writeSecretFile(path: string, data: string): void; export interface UniquePathOptions { /** " (n)" (both current fleet extensions) or "-n". Default "paren". */ style?: "paren" | "dash"; /** * First suffix to try. Fetch numbers from 1, Fathom from 2 — preserved * deliberately, because unifying them renames files users already have. */ startAt?: number; /** Attempts before falling back to a timestamp. Default 1000. */ limit?: number; /** * Atomically claim the name by creating a sidecar file, closing the window * between "this name is free" and "this name is taken". * * Required when the real file appears later than this call — a download that * writes to `.part` first, for instance. Without it, two downloads * started moments apart both see a free name and one overwrites the other. */ reserve?: boolean; /** Sidecar suffix used when reserving. Default `".part"`. */ reserveSuffix?: string; } /** Find a non-colliding path in `dir` for `filename`. */ export declare function uniquePath(dir: string, filename: string, options?: UniquePathOptions): string; /** * Reduce an arbitrary string to a safe single path segment. * * Never returns an empty string — an empty filename would silently become the * directory itself at the join() call site. */ export declare function sanitizeFilename(name: string, options?: { maxLength?: number; reserveBytes?: number; }): string; /** * Roll a `.part` file back to a byte count that is known good. * * Used after an unfollowed 3xx: curl writes the redirect BODY into the partial, * and on a RESUMED transfer the bytes before it are the user's real progress, * so the tail is removed rather than the whole file. * * Returns false when the partial cannot be left in a state that is safe to * resume from, and does everything it can to make it non-resumable first. A * resume runs `curl -C -`, which appends from the file's CURRENT size, so a * partial still carrying redirect HTML gets the real recording spliced onto the * end of it and published under the user's filename. Losing progress is * recoverable; a corrupt media file is not. * * Three things here are load-bearing: * * - **Every operation is bound to ONE open file descriptor.** Doing this by * pathname across stat / truncate / re-stat is a TOCTOU: another attempt or * an external actor can replace the file mid-sequence, and the cleanup then * lands on a healthy partial belonging to someone else. * - **The size is VERIFIED after truncating.** A truncate that throws is * obvious; one that leaves the file longer than asked is not, and only the * second corrupts the next resume. * - **`keepBytes` is validated before anything destructive runs.** It is a * caller-supplied number on an exported function, and a negative value * reaches `ftruncateSync`, throws, and would otherwise fall through to the * cleanup path — turning bad input into deleted progress. */ export declare function rollbackPartial(partPath: string, keepBytes: number): boolean; //# sourceMappingURL=paths.d.ts.map