Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion cli/module_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1908,6 +1909,10 @@ impl<TGraphContainer: ModuleGraphContainer> ModuleLoader
) -> Option<Cow<'_, [u8]>> {
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
Expand Down Expand Up @@ -1959,7 +1964,13 @@ impl<TGraphContainer: ModuleGraphContainer> 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
Expand Down Expand Up @@ -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<String> {
// Do NOT use .lines(): it skips the terminating empty line.
Expand Down
12 changes: 12 additions & 0 deletions tests/specs/permission/source_map_no_read/__test__.jsonc
Original file line number Diff line number Diff line change
@@ -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
}
}
}
2 changes: 2 additions & 0 deletions tests/specs/permission/source_map_no_read/main.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
error: Uncaught (in promise) Error: boom
at [WILDLINE]secret.txt:1:1
36 changes: 36 additions & 0 deletions tests/specs/permission/source_map_no_read/main.ts
Original file line number Diff line number Diff line change
@@ -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));
1 change: 1 addition & 0 deletions tests/specs/permission/source_map_no_read/secret.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SECRET_CANARY_LEAK_abc123
Loading