apply strict filter
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by curious-rabbit · committed by curious-rabbit · parent 825f308 · Browse files · 3 files·+52 added−11 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+52 added−11 removed
- other:+52 added−11 removed
Provenance
- Commit
aeecb5d- Tree
5450beb- Author
curious-rabbit <curious-rabbit@local>- Committer
curious-rabbit <curious-rabbit@local>- Parent
825f308
3 files changed+52 added−11 removed
modified:src/exec/mod.rs+2 added
240 unmodified lines241 if args.is_empty() {242 bail!("No executable provided for --exec or --exec-batch");243 }244 245+ // Reject placeholder-as-executable for both --exec and --exec-batch246+ // (was previously checked only for --exec-batch).247 if args[0].has_tokens() {248 bail!(249 "First argument of --exec/--exec-batch must be a fixed executable, not a placeholder"250 );235 unmodified linesmodified:src/dir_entry.rs+3 added-3 removed
53 unmodified lines54 }55 }56 57 /// Returns the path as it should be presented to the user.58- /// When stripping `./` would leave the path starting with `-`, keep the `./` so59- /// downstream tools don't interpret the filename as an option.58+ /// When stripping `./` would leave the path starting with `-`, keep the original59+ /// (with `./`) so downstream tools don't interpret it as an option.60 pub fn stripped_path(&self, config: &Config) -> Cow<'_, Path> {61 let path = self.path();62 if config.strip_cwd_prefix {63 let stripped = strip_current_dir(path);64 if starts_with_dash(stripped) {65- Cow::Owned(Path::new(".").join(stripped))65+ Cow::Borrowed(path)66 } else {67 Cow::Borrowed(stripped)68 }69 } else {121 unmodified linesmodified:src/sanitize.rs+47 added-8 removed
1 unmodified line2 3 use std::borrow::Cow;4 use std::fmt::Write;5 6+/// True for any char that is neither printable nor permitted whitespace (only HT).7+/// Covers C0/C1/DEL, bidi overrides, zero-width and format chars, and tag chars.8 #[inline]7-fn is_dangerous_control(c: char) -> bool {8- // C0 (except HT), DEL, and C1 controls (U+0080..=U+009F can act as9- // single-byte CSI/OSC initiators on 8-bit-control terminals).10- matches!(c, '\x00'..='\x08' | '\x0A'..='\x1F' | '\x7F' | '\u{80}'..='\u{9F}')9+fn needs_escape(c: char) -> bool {10+ if c == '\t' {11+ return false;12+ }13+ c.is_control()14+ || matches!(c,15+ '\u{00AD}' // soft hyphen (invisible)16+ | '\u{180E}' // Mongolian vowel separator17+ | '\u{200B}'..='\u{200F}' // zero-width + LRM/RLM18+ | '\u{202A}'..='\u{202E}' // bidi embedding/override19+ | '\u{2060}'..='\u{206F}' // word joiner, invisibles, deprecated formats20+ | '\u{FEFF}' // BOM / zero-width no-break space21+ | '\u{FFF9}'..='\u{FFFB}' // interlinear annotation22+ | '\u{E0000}'..='\u{E007F}' // language tags23+ )24 }25 13-/// Replace control characters with `\xNN` so the original filename remains recoverable.26+/// Replace dangerous chars with `\xNN` / `\u{NNNN}` so the original is recoverable.27 pub fn sanitize_for_terminal(s: &str) -> Cow<'_, str> {15- if !s.chars().any(is_dangerous_control) {28+ if !s.chars().any(needs_escape) {29 return Cow::Borrowed(s);30 }31 let mut out = String::with_capacity(s.len());32 for c in s.chars() {20- if is_dangerous_control(c) {21- let _ = write!(out, "\\x{:02X}", c as u32);33+ if needs_escape(c) {34+ let v = c as u32;35+ if v <= 0xFF {36+ let _ = write!(out, "\\x{:02X}", v);37+ } else {38+ let _ = write!(out, "\\u{{{:04X}}}", v);39+ }40 } else {41 out.push(c);42 }43 }71 unmodified lines115 sanitize_for_terminal("\u{9d}0;pwned\u{9c}"),116 "\\x9D0;pwned\\x9C"117 );118 }119+120+ #[test]121+ fn strips_bidi_overrides_and_zero_width() {122+ // Trojan-Source style RLO/LRO that flip rendered order of filename text.123+ assert_eq!(124+ sanitize_for_terminal("safe\u{202E}fil\u{202D}gnp.exe"),125+ "safe\\u{202E}fil\\u{202D}gnp.exe"126+ );127+ // Zero-width space and BOM are also format chars used to disguise filenames.128+ assert_eq!(sanitize_for_terminal("a\u{200B}b"), "a\\u{200B}b");129+ assert_eq!(sanitize_for_terminal("\u{FEFF}name"), "\\u{FEFF}name");130+ }131+132+ #[test]133+ fn keeps_legitimate_unicode_features() {134+ // Variation selectors (U+FE0F, U+E0100..) modify preceding glyphs in CJK/emoji135+ // and are legitimate in filenames. Private-use chars are used by icon fonts.136+ for s in ["heart\u{2764}\u{FE0F}.txt", "icon\u{E000}.cfg", "cjk\u{6F22}\u{E0101}.txt"] {137+ assert!(matches!(sanitize_for_terminal(s), Cow::Borrowed(_)), "{s:?}");138+ }139+ }140 }