diff --git a/cli/module_loader.rs b/cli/module_loader.rs index fab7a31e8895b9..8ae47a2a47555f 100644 --- a/cli/module_loader.rs +++ b/cli/module_loader.rs @@ -74,6 +74,7 @@ use deno_runtime::deno_node::NodeRequireLoader; use deno_runtime::deno_node::create_host_defined_options; use deno_runtime::deno_node::ops::require::UnableToGetCwdError; use deno_runtime::deno_permissions::CheckSpecifierKind; +use deno_runtime::deno_permissions::PermissionState; use deno_runtime::deno_permissions::PermissionsContainer; use deno_runtime::deno_web::Blob; use deno_runtime::tokio_util::create_basic_runtime; @@ -1908,6 +1909,10 @@ impl ModuleLoader ) -> Option> { let specifier = resolve_url(source_map_url).ok()?; + if !source_map_file_read_allowed(&self.0.permissions, &specifier) { + return None; + } + if let Ok(Some(file)) = self .0 .shared @@ -1959,7 +1964,13 @@ impl ModuleLoader return None; } None => { - // Not in graph, try to read from file system (for source-mapped original files) + // Not in graph, try to read from file system (for source-mapped + // original files). Gate the disk read on read permission so a source + // map can't be used to read arbitrary files and leak their contents + // through a stack trace's source line. + if !source_map_file_read_allowed(&self.0.permissions, &specifier) { + return None; + } if let Ok(Some(file)) = self .0 .shared @@ -2024,6 +2035,32 @@ fn pick_hook_module_type( } } +/// Whether a specifier may be read from disk to resolve source maps / source +/// lines. +/// +/// Non-`file:` specifiers are served from the in-memory module cache, so they +/// are always safe. A `file:` specifier is read only if the program already +/// holds read permission for it (queried without prompting). This stops a +/// source map — e.g. an inline one on a `data:` module declaring +/// `"sources": ["file:///etc/passwd"]` — from being used to read arbitrary +/// files off disk and leak their contents through a stack trace's source line, +/// with no `--allow-read` grant. +fn source_map_file_read_allowed( + permissions: &PermissionsContainer, + specifier: &ModuleSpecifier, +) -> bool { + if specifier.scheme() != "file" { + return true; + } + let Ok(path) = deno_path_util::url_to_file_path(specifier) else { + return false; + }; + matches!( + permissions.query_read(Some(&path.to_string_lossy())), + Ok(PermissionState::Granted) + ) +} + /// Extracts a specific line from source code text. fn extract_source_line(text: &str, line_number: usize) -> Option { // Do NOT use .lines(): it skips the terminating empty line. diff --git a/tests/specs/permission/source_map_no_read/__test__.jsonc b/tests/specs/permission/source_map_no_read/__test__.jsonc new file mode 100644 index 00000000000000..37c5b0a4fef67f --- /dev/null +++ b/tests/specs/permission/source_map_no_read/__test__.jsonc @@ -0,0 +1,12 @@ +{ + // Regression test: an inline source map must not read a local file without + // read permission. The secret file's contents must not leak into the stack + // trace under --no-prompt. + "tests": { + "source_map_no_read": { + "args": "run --no-prompt main.ts", + "output": "main.out", + "exitCode": 1 + } + } +} diff --git a/tests/specs/permission/source_map_no_read/main.out b/tests/specs/permission/source_map_no_read/main.out new file mode 100644 index 00000000000000..07b71468903403 --- /dev/null +++ b/tests/specs/permission/source_map_no_read/main.out @@ -0,0 +1,2 @@ +error: Uncaught (in promise) Error: boom + at [WILDLINE]secret.txt:1:1 diff --git a/tests/specs/permission/source_map_no_read/main.ts b/tests/specs/permission/source_map_no_read/main.ts new file mode 100644 index 00000000000000..32b20043b7effe --- /dev/null +++ b/tests/specs/permission/source_map_no_read/main.ts @@ -0,0 +1,36 @@ +// Regression test: an inline source map must not be able to read a local file +// the program has no read permission for. +// +// The `data:` module below carries an inline source map whose `sources` points +// at `./secret.txt`. When the module throws, resolving the source-mapped source +// line used to read that file off disk with no permission check and print its +// contents in the stack trace. Run with `--no-prompt` (no `--allow-read`), the +// file must not be read, so its contents must not appear in the output. + +const B = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; +const enc = (n: number) => { + let v = n < 0 ? ((-n) << 1) | 1 : (n << 1); + let o = ""; + do { + let x = v & 31; + v >>>= 5; + if (v > 0) x |= 32; + o += B[x]; + } while (v > 0); + return o; +}; +const vlq = (a: number, b: number, c: number, d: number) => + enc(a) + enc(b) + enc(c) + enc(d); + +const secret = new URL("./secret.txt", import.meta.url).href; +const map = { + version: 3, + sources: [secret], + names: [], + mappings: vlq(0, 0, 0, 0), +}; +const code = "throw new Error('boom');\n" + + "//# sourceMappingURL=data:application/json;base64," + + btoa(JSON.stringify(map)) + "\n"; + +await import("data:application/javascript;base64," + btoa(code)); diff --git a/tests/specs/permission/source_map_no_read/secret.txt b/tests/specs/permission/source_map_no_read/secret.txt new file mode 100644 index 00000000000000..6b21edf38cfd8d --- /dev/null +++ b/tests/specs/permission/source_map_no_read/secret.txt @@ -0,0 +1 @@ +SECRET_CANARY_LEAK_abc123