Repository navigation
Preserve literal Unix backslashes in glob result paths (#990) #1076
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
leynos
wants to merge
4
commits into
main
Choose a base branch
from
issue-990-preserve-literal-unix-backslashes-in-glob-result-paths
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,106 @@ | ||
| //! Regression tests for the identity of Unix paths containing a literal | ||
| //! backslash. | ||
| //! | ||
| //! On Unix a backslash is an ordinary filename character, not a separator. | ||
| //! Result rebasing must therefore return the exact name that passed the | ||
| //! capability metadata check. Rewriting the backslash would report the match | ||
| //! under a different identity, and a name such as `part\..\other.txt` would | ||
| //! become the parent-directory traversal `part/../other.txt` that the checked | ||
| //! filesystem path never contained. | ||
| //! | ||
| //! Each case builds a disposable fixture and invokes the production | ||
| //! [`super::super::glob_paths`] boundary; neither the environment nor the | ||
| //! working directory of the test process is mutated. | ||
|
|
||
| #[cfg(unix)] | ||
| use super::super::glob_paths; | ||
| #[cfg(unix)] | ||
| use anyhow::{Context, Result, ensure}; | ||
| #[cfg(unix)] | ||
| use camino::Utf8Path; | ||
| #[cfg(unix)] | ||
| use tempfile::tempdir; | ||
| #[cfg(unix)] | ||
| use test_support::fs as test_fs; | ||
|
|
||
| /// A literal backslash stays in the returned name rather than addressing a | ||
| /// different file that the capability check never saw. | ||
| #[cfg(unix)] | ||
| #[test] | ||
| fn glob_paths_preserves_a_literal_backslash_filename() -> Result<()> { | ||
| let temp = tempdir()?; | ||
| test_fs::create_dir(temp.path().join("nested"))?; | ||
| test_fs::write(temp.path().join("nested").join("leaf.txt"), "decoy")?; | ||
| test_fs::write(temp.path().join(r"nested\leaf.txt"), "attacker")?; | ||
| let base = Utf8Path::from_path(temp.path()).context("temporary paths must be UTF-8")?; | ||
|
|
||
| let results = glob_paths("*.txt", Some(base))?; | ||
|
|
||
| ensure!( | ||
| results == vec![r"nested\leaf.txt".to_owned()], | ||
| "a literal backslash must be preserved, got {results:?}" | ||
| ); | ||
| let returned = results | ||
| .first() | ||
| .context("the matched literal-backslash file must be returned")?; | ||
| let contents = test_fs::read_to_string(base.join(returned))?; | ||
| ensure!( | ||
| contents == "attacker", | ||
| "the returned path must name the matched file, not the {contents:?} decoy" | ||
| ); | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// A literal `..` spelled between backslashes stays part of one filename | ||
| /// component; it must not become parent-directory traversal after rebasing. | ||
| #[cfg(unix)] | ||
| #[test] | ||
| fn glob_paths_does_not_introduce_parent_traversal() -> Result<()> { | ||
| let temp = tempdir()?; | ||
| test_fs::create_dir(temp.path().join("part"))?; | ||
| test_fs::write(temp.path().join("other.txt"), "decoy")?; | ||
| test_fs::write(temp.path().join(r"part\..\other.txt"), "attacker")?; | ||
| let base = Utf8Path::from_path(temp.path()).context("temporary paths must be UTF-8")?; | ||
|
|
||
| // Match only the literal-backslash name: the decoy that a rewritten | ||
| // separator would reach is `other.txt`, and it must not be a legitimate | ||
| // match of the pattern. | ||
| let results = glob_paths("part*", Some(base))?; | ||
|
|
||
| ensure!( | ||
| results == vec![r"part\..\other.txt".to_owned()], | ||
| "the literal backslash spelling must survive, got {results:?}" | ||
| ); | ||
| let returned = results | ||
| .first() | ||
| .context("the matched literal-backslash file must be returned")?; | ||
| ensure!( | ||
| !returned.contains("/../"), | ||
| "rebasing must not create a parent-directory component: {returned:?}" | ||
| ); | ||
| let contents = test_fs::read_to_string(base.join(returned))?; | ||
| ensure!( | ||
| contents == "attacker", | ||
| "the returned path must name the matched file, not the {contents:?} decoy" | ||
| ); | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// An absolute pattern with no injected base also keeps the literal | ||
| /// backslash, so direct callers see the identity the walk selected. | ||
| #[cfg(unix)] | ||
| #[test] | ||
| fn glob_paths_preserves_a_literal_backslash_in_an_absolute_match() -> Result<()> { | ||
| let temp = tempdir()?; | ||
| test_fs::write(temp.path().join(r"nested\leaf.txt"), "attacker")?; | ||
| let pattern = format!("{}/*.txt", temp.path().display()); | ||
|
|
||
| let results = glob_paths(&pattern, None)?; | ||
|
|
||
| let expected = format!(r"{}/nested\leaf.txt", temp.path().display()); | ||
| ensure!( | ||
| results == vec![expected], | ||
| "the absolute match must keep its literal backslash, got {results:?}" | ||
| ); | ||
| Ok(()) | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use Oxford spelling in the new module comments.
Replace
normalisationwithnormalizationat Lines 1 and 171. As per path instructions, “Comments and docs must follow en-GB-oxendict (-ize / -yse / -our) spelling and grammar.” (raw.githubusercontent.com)Also applies to: 171-171
Source: Path instructions