cleanup
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 a00259b · Browse files · 2 files·+9 added−45 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+9 added−45 removed
- other:+9 added−45 removed
Provenance
- Commit
c96398c- Tree
d90379b- Author
curious-rabbit <curious-rabbit@local>- Committer
curious-rabbit <curious-rabbit@local>- Parent
a00259b
2 files changed+9 added−45 removed
modified:src/output.rs+8 added-11 removed
5 unmodified lines6 use crate::config::Config;7 use crate::dir_entry::DirEntry;8 use crate::fmt::FormatTemplate;9 use crate::hyperlink::PathUrl;10-use crate::sanitize::{Sanitized, maybe_sanitize};10+use crate::sanitize::maybe_sanitize;11 12 fn replace_path_separator(path: &str, new_path_separator: &str) -> String {13 path.replace(std::path::MAIN_SEPARATOR, new_path_separator)14 }62 unmodified lines77 config.path_separator.as_deref(),78 );79 // TODO: support writing raw bytes on unix?80 let s = output.to_string_lossy();81- if config.interactive_terminal {82- write!(stdout, "{}", Sanitized(&s))83- } else {84- write!(stdout, "{s}")85- }81+ write!(82+ stdout,83+ "{}",84+ maybe_sanitize(&s, config.interactive_terminal)85+ )86 }87 88 // TODO: this function is performance critical and can probably be optimized89 fn print_entry_colorized<W: Write>(59 unmodified lines149 let mut path_string = path.to_string_lossy();150 if let Some(ref separator) = config.path_separator {151 *path_string.to_mut() = replace_path_separator(&path_string, separator);152 }153- if config.interactive_terminal {154- write!(stdout, "{}", Sanitized(&path_string))?;155- } else {156- write!(stdout, "{path_string}")?;157- }153+ let safe = maybe_sanitize(&path_string, config.interactive_terminal);154+ write!(stdout, "{safe}")?;155 print_trailing_slash(stdout, entry, config, None)156 }157 158 #[cfg(not(unix))]24 unmodified linesmodified:src/sanitize.rs+1 added-34 removed
1 //! TTY-output sanitization to prevent terminal escape injection via filenames.2 3 use std::borrow::Cow;4-use std::fmt::{self, Display, Formatter, Write};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]13 unmodified lines22 | '\u{E0000}'..='\u{E007F}' // language tags23 )24 }25 26-/// Streams `s` to a formatter, escaping dangerous chars as `\xNN` / `\u{NNNN}`.27-/// Allocation-free wrapper for use with `write!`, `format!`, etc.28-pub struct Sanitized<'a>(pub &'a str);29-30-impl Display for Sanitized<'_> {31- fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {32- for c in self.0.chars() {33- if needs_escape(c) {34- let v = c as u32;35- if v <= 0xFF {36- write!(f, "\\x{v:02X}")?;37- } else {38- write!(f, "\\u{{{v:04X}}}")?;39- }40- } else {41- f.write_char(c)?;42- }43- }44- Ok(())45- }46-}47-26 /// Returns a `Cow<str>` borrowing `s` when no escaping is needed, otherwise an owned27 /// escaped copy. Use this when an owned `&str`/`String` is required (e.g. ANSI paint).28 pub fn sanitize_for_terminal(s: &str) -> Cow<'_, str> {29 if !s.chars().any(needs_escape) {42 unmodified lines72 matches!(sanitize_for_terminal(s), Cow::Borrowed(_)),73 "{s:?}"74 );75 assert_eq!(sanitize_for_terminal(s), s);98- // Display matches Cow output.99- assert_eq!(Sanitized(s).to_string(), s);76 }77 }78 79 #[test]75 unmodified lines155 }156 }157 158 #[test]183- fn display_streams_without_intermediate_string() {184- // Sanitized<'_> implements Display directly; produces same bytes as Cow form.185- let attack = "x\x1byb";186- let mut out = String::new();187- write!(out, "{}", Sanitized(attack)).unwrap();188- assert_eq!(out, "x\\x1Byb");189- }190-191- #[test]159 fn maybe_sanitize_passthrough_when_not_terminal() {160 let attack = "x\x1by";161 // Pipe context: bytes pass through unchanged (zero-copy).162 let out = maybe_sanitize(attack, false);6 unmodified lines