-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tough: always use unix-style paths in clean_name
Using `std::path::Path` to manipulate `Url` paths only has the correct behavior on unix systems where the properties of the paths are nearly identical. This change moves to using unix-style path manipulation regardless of platform. It also adds a utility for safely creating filesystem paths based on `Url` paths for both unix and windows (which is complicated by drive lettering in absolute paths).
- Loading branch information
Showing
9 changed files
with
115 additions
and
46 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
//! This module contains utilities for mapping URL paths to local Paths. | ||
use std::path::PathBuf; | ||
use url::Url; | ||
|
||
/// Converts a file URL into a file path. | ||
/// Needed because `url.to_file_path()` will decode any percent encoding, which could restore path | ||
/// traversal characters, and `url.path()` roots paths to '/' on Windows. | ||
pub trait SafeUrlPath { | ||
/// Returns the path component of a URL as a filesystem path. | ||
fn safe_url_filepath(&self) -> PathBuf; | ||
} | ||
|
||
#[cfg(windows)] | ||
impl SafeUrlPath for Url { | ||
fn safe_url_filepath(&self) -> PathBuf { | ||
let url_path = self.path(); | ||
|
||
// Windows filepaths when written as `file://` URLs have path components prefixed with a /. | ||
PathBuf::from(if let Some(stripped) = url_path.strip_prefix('/') { | ||
stripped | ||
} else { | ||
url_path | ||
}) | ||
} | ||
} | ||
|
||
#[cfg(unix)] | ||
impl SafeUrlPath for Url { | ||
fn safe_url_filepath(&self) -> PathBuf { | ||
PathBuf::from(self.path()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
use crate::encode_filename; | ||
use std::path::PathBuf; | ||
|
||
fn manifest_dir() -> PathBuf { | ||
PathBuf::from(env!("CARGO_MANIFEST_DIR")) | ||
} | ||
|
||
#[test] | ||
fn test_safe_simple() { | ||
let cargo_toml = manifest_dir().join("Cargo.toml"); | ||
let cargo_toml_url = Url::from_file_path(&cargo_toml) | ||
.expect("Could not create URL from Cargo.toml filepath"); | ||
|
||
let safe_url_path = cargo_toml_url.safe_url_filepath(); | ||
|
||
assert_eq!(cargo_toml, safe_url_path); | ||
assert!(safe_url_path.is_absolute()); | ||
} | ||
|
||
#[test] | ||
fn test_safe_traversals() { | ||
let url_base = Url::from_directory_path(manifest_dir()) | ||
.expect("Could not create URL from CARGO_MANIFEST_DIR"); | ||
|
||
let escaped_test_path = encode_filename("a/../b/././c/.."); | ||
let traversal_url = url_base.join(&escaped_test_path).expect(&format!( | ||
"Could not create URL from unusual traveral path '{}' + '{}'", | ||
url_base.to_string(), | ||
escaped_test_path | ||
)); | ||
|
||
assert_eq!( | ||
manifest_dir().join("a%2F..%2Fb%2F.%2F.%2Fc%2F.."), | ||
traversal_url.safe_url_filepath(), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters