Skip to content

Commit

Permalink
fix(unstable): vendor cache override should handle forbidden windows …
Browse files Browse the repository at this point in the history
…directory names (denoland#20069)

Meant to do this earlier.
  • Loading branch information
dsherret committed Aug 5, 2023
1 parent 8ae7062 commit 6b0f75d
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion cli/cache/http_cache/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,19 @@ fn url_to_local_sub_path(
static FORBIDDEN_CHARS: Lazy<HashSet<char>> = Lazy::new(|| {
HashSet::from(['?', '<', '>', ':', '*', '|', '\\', ':', '"', '\'', '/'])
});
// https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file
static FORBIDDEN_WINDOWS_NAMES: Lazy<HashSet<&'static str>> =
Lazy::new(|| {
let set = HashSet::from([
"con", "prn", "aux", "nul", "com0", "com1", "com2", "com3", "com4",
"com5", "com6", "com7", "com8", "com9", "lpt0", "lpt1", "lpt2", "lpt3",
"lpt4", "lpt5", "lpt6", "lpt7", "lpt8", "lpt9",
]);
// ensure everything is lowercase because we'll be comparing
// lowercase filenames against this
debug_assert!(set.iter().all(|s| s.to_lowercase() == *s));
set
});

fn has_forbidden_chars(segment: &str) -> bool {
segment.chars().any(|c| {
Expand Down Expand Up @@ -405,7 +418,11 @@ fn url_to_local_sub_path(
};

// the hash symbol at the start designates a hash for the url part
hash_context_specific || part.starts_with('#') || has_forbidden_chars(part)
hash_context_specific
|| part.starts_with('#')
|| has_forbidden_chars(part)
|| last_ext.is_none() && FORBIDDEN_WINDOWS_NAMES.contains(part)
|| part.ends_with('.')
}

// get the base url
Expand Down Expand Up @@ -811,6 +828,19 @@ mod test {
&[("content-type", "application/javascript")],
"deno.land/x/#mod.ts_e8c36.js",
);
run_test(
// not allowed windows folder name
"https://deno.land/x/con/con.ts",
&[],
"deno.land/x/#con_1143d/con.ts",
);
run_test(
// disallow ending a directory with a period
// https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file
"https://deno.land/x/test./main.ts",
&[],
"deno.land/x/#test._4ee3d/main.ts",
);

#[track_caller]
fn run_test(url: &str, headers: &[(&str, &str)], expected: &str) {
Expand Down

0 comments on commit 6b0f75d

Please sign in to comment.