-
-
Notifications
You must be signed in to change notification settings - Fork 6
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
Add support for escaping OsStr
#9
Open
aalekhpatel07
wants to merge
17
commits into
sfackler:master
Choose a base branch
from
aalekhpatel07:master
base: master
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 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
28e8445
Add an implementation of escape for OsStr
aalekhpatel07 a32705d
Clippy fix.
aalekhpatel07 7d5ca97
Rustfmt fix.
aalekhpatel07 ebef84f
Reorder the implementation to be above tests.
aalekhpatel07 143f0c3
Make escape_os_str tests more readable.
aalekhpatel07 791a4aa
Make escape tests more readable.
aalekhpatel07 3b964b4
Use test_case in windows escape tests as well.
aalekhpatel07 ae6c50c
Add an `escape_os_str` for Windows.
aalekhpatel07 4e148e2
Apply rustfmt fixes.
aalekhpatel07 934b6f1
Refactor escape impls between two modules.
aalekhpatel07 37d4fc8
Make `escape` for windows more readable.
aalekhpatel07 60219aa
Reserve more bytes.
aalekhpatel07 e94b26f
Apply some suggestions from the review.
aalekhpatel07 13def30
Apply clippy fixes.
aalekhpatel07 c0d2a8b
Rustfmt fixes.
aalekhpatel07 b0bc985
Fix doctest import.
aalekhpatel07 8b281f2
Make the implementation of escape uniform across both platforms.
CdnCentreForChildProtection 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 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,168 @@ | ||
//! Unix-specific escaping. | ||
//! | ||
use std::{ | ||
borrow::Cow, | ||
ffi::{OsStr, OsString}, | ||
os::unix::ffi::{OsStrExt, OsStringExt}, | ||
}; | ||
|
||
fn non_whitelisted(ch: char) -> bool { | ||
!matches!(ch, 'a'..='z' | 'A'..='Z' | '0'..='9' | '-' | '_' | '=' | '/' | ',' | '.' | '+') | ||
} | ||
|
||
/// Escape characters that may have special meaning in a shell, including spaces. | ||
pub fn escape(s: Cow<str>) -> Cow<str> { | ||
if !s.is_empty() && !s.contains(non_whitelisted) { | ||
return s; | ||
} | ||
|
||
let mut es = String::with_capacity(s.len() + 2); | ||
es.push('\''); | ||
for ch in s.chars() { | ||
match ch { | ||
'\'' | '!' => { | ||
es.push_str("'\\"); | ||
es.push(ch); | ||
es.push('\''); | ||
} | ||
_ => es.push(ch), | ||
} | ||
} | ||
es.push('\''); | ||
es.into() | ||
} | ||
|
||
fn allowed(byte: u8) -> bool { | ||
matches!(byte, b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'-' | b'_' | b'=' | b'/' | b',' | b'.' | b'+') | ||
} | ||
|
||
/// Escape characters that may have special meaning in a shell, including spaces. | ||
/// Work with `OsStr` instead of `str`. | ||
pub fn escape_os_str(s: &OsStr) -> Cow<'_, OsStr> { | ||
let as_bytes = s.as_bytes(); | ||
let all_whitelisted = as_bytes.iter().copied().all(allowed); | ||
|
||
if !as_bytes.is_empty() && all_whitelisted { | ||
return Cow::Borrowed(s); | ||
} | ||
|
||
let mut escaped = Vec::with_capacity(as_bytes.len() + 2); | ||
escaped.push(b'\''); | ||
|
||
for &b in as_bytes { | ||
match b { | ||
b'\'' | b'!' => { | ||
escaped.reserve(4); | ||
escaped.push(b'\''); | ||
escaped.push(b'\\'); | ||
escaped.push(b); | ||
escaped.push(b'\''); | ||
} | ||
_ => escaped.push(b), | ||
} | ||
} | ||
escaped.push(b'\''); | ||
OsString::from_vec(escaped).into() | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
extern crate test_case; | ||
|
||
use super::{escape, escape_os_str}; | ||
use std::ffi::OsStr; | ||
use std::os::unix::ffi::OsStrExt; | ||
|
||
#[test_case::test_case( | ||
" ", | ||
r#"' '"# | ||
; "Space is escaped by wrapping it in single quotes." | ||
)] | ||
#[test_case::test_case( | ||
"", | ||
r#"''"# | ||
; "Empty string is escaped by wrapping it in single quotes." | ||
)] | ||
#[test_case::test_case( | ||
r#"'!\$`\\\n "#, | ||
r#"''\'''\!'\$`\\\n '"# | ||
; "Text with a mix of characters that require escaping are individually escaped as well as wrapping the whole thing in single quotes." | ||
)] | ||
#[test_case::test_case( | ||
r#"--features="default""#, | ||
r#"'--features="default"'"# | ||
; "Text with a double quote is escaped by wrapping it all in single quotes." | ||
)] | ||
#[test_case::test_case( | ||
"linker=gcc -L/foo -Wl,bar", | ||
r#"'linker=gcc -L/foo -Wl,bar'"# | ||
; "Text with a slash is escaped by wrapping it all in single quotes." | ||
)] | ||
#[test_case::test_case( | ||
"--aaa=bbb-ccc", | ||
"--aaa=bbb-ccc" | ||
; "a flag built up entirely of allowed characters is not escaped." | ||
)] | ||
#[test_case::test_case( | ||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_=/,.+", | ||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_=/,.+" | ||
; "all allowed characters that do not require escaping are not escaped" | ||
)] | ||
fn test_escape(input: &str, expected: &str) { | ||
assert_eq!(escape(input.into()), expected); | ||
} | ||
|
||
#[test_case::test_case( | ||
" ", | ||
r#"' '"# | ||
; "Space is escaped by wrapping it in single quotes." | ||
)] | ||
#[test_case::test_case( | ||
"", | ||
r#"''"# | ||
; "Empty string is escaped by wrapping it in single quotes." | ||
)] | ||
#[test_case::test_case( | ||
r#"'!\$`\\\n "#, | ||
r#"''\'''\!'\$`\\\n '"# | ||
; "Text with a mix of characters that require escaping are individually escaped as well as wrapping the whole thing in single quotes." | ||
)] | ||
#[test_case::test_case( | ||
r#"--features="default""#, | ||
r#"'--features="default"'"# | ||
; "Text with a double quote is escaped by wrapping it all in single quotes." | ||
)] | ||
#[test_case::test_case( | ||
"linker=gcc -L/foo -Wl,bar", | ||
r#"'linker=gcc -L/foo -Wl,bar'"# | ||
; "Text with a slash is escaped by wrapping it all in single quotes." | ||
)] | ||
#[test_case::test_case( | ||
"--aaa=bbb-ccc", | ||
"--aaa=bbb-ccc" | ||
; "a flag built up entirely of allowed characters is not escaped." | ||
)] | ||
#[test_case::test_case( | ||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_=/,.+", | ||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_=/,.+" | ||
; "all allowed characters that do not require escaping are not escaped" | ||
)] | ||
fn test_escape_os_str(input: &str, expected: &str) { | ||
let input_os_str = OsStr::from_bytes(input.as_bytes()); | ||
let observed_os_str = escape_os_str(input_os_str); | ||
let expected_os_str = OsStr::from_bytes(expected.as_bytes()); | ||
assert_eq!(observed_os_str, expected_os_str); | ||
} | ||
|
||
#[test_case::test_case( | ||
&[0x66, 0x6f, 0x80, 0x6f], | ||
&[b'\'', 0x66, 0x6f, 0x80, 0x6f, b'\''] | ||
; "Bytes that are not valid UTF-8 are escaped by wrapping them in single quotes." | ||
)] | ||
fn test_escape_os_str_from_bytes(input: &[u8], expected: &[u8]) { | ||
let input_os_str = OsStr::from_bytes(input); | ||
let observed_os_str = escape_os_str(input_os_str); | ||
let expected_os_str = OsStr::from_bytes(expected); | ||
assert_eq!(observed_os_str, expected_os_str); | ||
} | ||
} |
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.
Making these modules conditionally compiled is a breaking change.