|
| 1 | +import path from "node:path"; |
| 2 | +import { FsSafeError } from "./errors.js"; |
| 3 | +import { trySafeFileURLToPath } from "./local-file-access.js"; |
| 4 | + |
| 5 | +export type UnsafeDeviceReadPathReason = |
| 6 | + | "posix-device" |
| 7 | + | "posix-fd" |
| 8 | + | "windows-device"; |
| 9 | + |
| 10 | +export type UnsafeDeviceReadPathMatch = { |
| 11 | + path: string; |
| 12 | + reason: UnsafeDeviceReadPathReason; |
| 13 | +}; |
| 14 | + |
| 15 | +export type UnsafeDeviceReadPathOptions = { |
| 16 | + cwd?: string; |
| 17 | + platform?: NodeJS.Platform; |
| 18 | +}; |
| 19 | + |
| 20 | +const POSIX_BLOCKED_DEVICE_PATHS = new Set([ |
| 21 | + "/dev/zero", |
| 22 | + "/dev/random", |
| 23 | + "/dev/urandom", |
| 24 | + "/dev/full", |
| 25 | + "/dev/stdin", |
| 26 | + "/dev/stdout", |
| 27 | + "/dev/stderr", |
| 28 | + "/dev/tty", |
| 29 | + "/dev/console", |
| 30 | +]); |
| 31 | + |
| 32 | +const WINDOWS_RESERVED_DEVICE_NAMES = new Set([ |
| 33 | + "CON", |
| 34 | + "PRN", |
| 35 | + "AUX", |
| 36 | + "NUL", |
| 37 | + "CLOCK$", |
| 38 | + "CONIN$", |
| 39 | + "CONOUT$", |
| 40 | + "COM1", |
| 41 | + "COM2", |
| 42 | + "COM3", |
| 43 | + "COM4", |
| 44 | + "COM5", |
| 45 | + "COM6", |
| 46 | + "COM7", |
| 47 | + "COM8", |
| 48 | + "COM9", |
| 49 | + "LPT1", |
| 50 | + "LPT2", |
| 51 | + "LPT3", |
| 52 | + "LPT4", |
| 53 | + "LPT5", |
| 54 | + "LPT6", |
| 55 | + "LPT7", |
| 56 | + "LPT8", |
| 57 | + "LPT9", |
| 58 | +]); |
| 59 | + |
| 60 | +function candidateReadPaths(filePath: string): string[] { |
| 61 | + if (!filePath.startsWith("file://")) { |
| 62 | + return [filePath]; |
| 63 | + } |
| 64 | + const parsed = trySafeFileURLToPath(filePath); |
| 65 | + return parsed === undefined ? [filePath] : [filePath, parsed]; |
| 66 | +} |
| 67 | + |
| 68 | +function normalizePosixPath(filePath: string, cwd?: string): string { |
| 69 | + if (path.posix.isAbsolute(filePath)) { |
| 70 | + return path.posix.normalize(filePath); |
| 71 | + } |
| 72 | + const base = cwd && path.posix.isAbsolute(cwd) ? cwd : process.cwd(); |
| 73 | + return path.posix.resolve(base, filePath); |
| 74 | +} |
| 75 | + |
| 76 | +function matchPosixDeviceReadPath( |
| 77 | + filePath: string, |
| 78 | + cwd?: string, |
| 79 | +): UnsafeDeviceReadPathMatch | undefined { |
| 80 | + const normalized = normalizePosixPath(filePath, cwd); |
| 81 | + if (POSIX_BLOCKED_DEVICE_PATHS.has(normalized)) { |
| 82 | + return { path: normalized, reason: "posix-device" }; |
| 83 | + } |
| 84 | + if (normalized === "/dev/fd" || normalized.startsWith("/dev/fd/")) { |
| 85 | + return { path: normalized, reason: "posix-fd" }; |
| 86 | + } |
| 87 | + if (/^\/proc\/(?:self|thread-self|\d+)\/fd(?:\/|$)/.test(normalized)) { |
| 88 | + return { path: normalized, reason: "posix-fd" }; |
| 89 | + } |
| 90 | + return undefined; |
| 91 | +} |
| 92 | + |
| 93 | +function normalizeWindowsDeviceBaseName(filePath: string): string { |
| 94 | + const normalized = filePath.replace(/\//g, "\\").replace(/[\\]+$/g, ""); |
| 95 | + const lastSegment = normalized.split("\\").filter(Boolean).at(-1) ?? normalized; |
| 96 | + const withoutStream = lastSegment.split(":")[0] ?? lastSegment; |
| 97 | + const withoutTrailingIgnoredChars = withoutStream.replace(/[ .]+$/g, ""); |
| 98 | + return (withoutTrailingIgnoredChars.split(".")[0] ?? withoutTrailingIgnoredChars).toUpperCase(); |
| 99 | +} |
| 100 | + |
| 101 | +function matchWindowsDeviceReadPath(filePath: string): UnsafeDeviceReadPathMatch | undefined { |
| 102 | + const normalized = filePath.replace(/\//g, "\\"); |
| 103 | + if (/^\\\\\.\\/.test(normalized) || /^\\\\\?\\GLOBALROOT\\Device\\/i.test(normalized)) { |
| 104 | + return { path: normalized, reason: "windows-device" }; |
| 105 | + } |
| 106 | + const baseName = normalizeWindowsDeviceBaseName(filePath); |
| 107 | + if (WINDOWS_RESERVED_DEVICE_NAMES.has(baseName)) { |
| 108 | + return { path: normalized, reason: "windows-device" }; |
| 109 | + } |
| 110 | + return undefined; |
| 111 | +} |
| 112 | + |
| 113 | +export function matchUnsafeDeviceReadPath( |
| 114 | + filePath: string, |
| 115 | + options: UnsafeDeviceReadPathOptions = {}, |
| 116 | +): UnsafeDeviceReadPathMatch | undefined { |
| 117 | + const platform = options.platform ?? process.platform; |
| 118 | + for (const candidate of candidateReadPaths(filePath)) { |
| 119 | + const match = platform === "win32" |
| 120 | + ? matchWindowsDeviceReadPath(candidate) |
| 121 | + : matchPosixDeviceReadPath(candidate, options.cwd); |
| 122 | + if (match) { |
| 123 | + return match; |
| 124 | + } |
| 125 | + } |
| 126 | + return undefined; |
| 127 | +} |
| 128 | + |
| 129 | +export function isUnsafeDeviceReadPath( |
| 130 | + filePath: string, |
| 131 | + options?: UnsafeDeviceReadPathOptions, |
| 132 | +): boolean { |
| 133 | + return matchUnsafeDeviceReadPath(filePath, options) !== undefined; |
| 134 | +} |
| 135 | + |
| 136 | +export function assertNoUnsafeDeviceReadPath( |
| 137 | + filePath: string, |
| 138 | + options?: UnsafeDeviceReadPathOptions, |
| 139 | +): void { |
| 140 | + if (matchUnsafeDeviceReadPath(filePath, options)) { |
| 141 | + throw new FsSafeError( |
| 142 | + "device-path", |
| 143 | + `file reads from unsafe device paths are not allowed: ${filePath}`, |
| 144 | + ); |
| 145 | + } |
| 146 | +} |
0 commit comments