Skip to content

Commit

Permalink
security fix: Under Windows, you can jump out of the serve dir to acc…
Browse files Browse the repository at this point in the history
…ess any file on the server
  • Loading branch information
chrislearn committed Sep 19, 2024
1 parent e5c8551 commit d5ceccd
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
4 changes: 4 additions & 0 deletions crates/serve-static/src/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,10 @@ impl Handler for StaticDir {
if self.include_dot_files || !is_dot_file {
for root in &self.roots {
let raw_path = join_path!(root, &rel_path);
// Security check to ensure that the accessed path is a subpath of the current root path.
if !Path::new(&raw_path).starts_with(root) {
continue;
}
for filter in &self.exclude_filters {
if filter(&raw_path) {
continue;
Expand Down
2 changes: 1 addition & 1 deletion crates/serve-static/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub(crate) fn format_url_path_safely(path: &str) -> String {
let final_slash = if path.ends_with('/') { "/" } else { "" };
let mut used_parts = Vec::with_capacity(8);
for part in path.split(['/', '\\']) {
if part.is_empty() || part == "." {
if part.is_empty() || part == "." || (cfg!(windows) && part.contains(':')) {
continue;
} else if part == ".." {
used_parts.pop();
Expand Down

0 comments on commit d5ceccd

Please sign in to comment.