Skip to content

Commit

Permalink
fix(runtime): allow nul device on windows (#23741)
Browse files Browse the repository at this point in the history
Fixes [23721](#23721)
  • Loading branch information
mmastrac committed May 8, 2024
1 parent 547ce6c commit 9f7f681
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
15 changes: 11 additions & 4 deletions ext/fs/std_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -890,16 +890,24 @@ fn open_with_access_check(
access_check: Option<AccessCheckCb>,
) -> FsResult<std::fs::File> {
if let Some(access_check) = access_check {
let path = if path.is_absolute() {
let path_bytes = path.as_os_str().as_encoded_bytes();
let is_windows_device_path = cfg!(windows)
&& path_bytes.starts_with(br"\\.\")
&& !path_bytes.contains(&b':');
let path = if is_windows_device_path {
// On Windows, normalize_path doesn't work with device-prefix-style
// paths. We pass these through.
path.to_owned()
} else if path.is_absolute() {
normalize_path(path)
} else {
let cwd = current_dir()?;
normalize_path(cwd.join(path))
};
(*access_check)(false, &path, &options)?;
// On Linux, /proc may contain magic links that we don't want to resolve
let needs_canonicalization =
!cfg!(target_os = "linux") || path.starts_with("/proc");
let needs_canonicalization = !is_windows_device_path
&& (!cfg!(target_os = "linux") || path.starts_with("/proc"));
let path = if needs_canonicalization {
match path.canonicalize() {
Ok(path) => path,
Expand All @@ -916,7 +924,6 @@ fn open_with_access_check(
} else {
path
};

(*access_check)(true, &path, &options)?;

// For windows
Expand Down
6 changes: 6 additions & 0 deletions runtime/permissions/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1696,6 +1696,12 @@ impl PermissionsContainer {
self.check_was_allow_all_flag_passed().map_err(error_all)?;
}
} else if cfg!(target_os = "windows") {
// \\.\nul is allowed
let s = path.as_os_str().as_encoded_bytes();
if s.eq_ignore_ascii_case(br#"\\.\nul"#) {
return Ok(());
}

fn is_normalized_windows_drive_path(path: &Path) -> bool {
let s = path.as_os_str().as_encoded_bytes();
// \\?\X:\
Expand Down
2 changes: 2 additions & 0 deletions tests/specs/permission/special/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const testCases = [
// Allowed, safe
[["darwin", "linux"], null, "/dev/null"],
[["darwin", "linux"], null, "/etc/passwd"],
[["windows"], null, "\\\\.\\nul"],
// Denied, requires `--allow-all`
[["darwin", "linux"], /PermissionDenied/, "/dev/ptmx"],
[["linux"], /PermissionDenied/, "/proc/self/environ"],
Expand Down Expand Up @@ -40,6 +41,7 @@ for (const [oses, error, file] of testCases) {
console.log(`Got an error (expected) for ${file}: ${e}`);
} else {
console.log(`*** Got an unexpected error for ${file}: ${e}`);
failed = true;
}
}
}
Expand Down

0 comments on commit 9f7f681

Please sign in to comment.