Strip out control codes from the logged messages (#10081)

* Strip out control codes from the logged messages

* Also strip away C1 control codes

* Add extra comments

* Clear the buffer after flushing; rename `write` to `flush`

* Move control code stripping into its own function

* Also strip out control codes from panic messages

* Also strip out Unicode left-to-right/right-to-left control codes
This commit is contained in:
Koute
2021-11-01 17:46:32 +09:00
committed by GitHub
parent 21c8d18c23
commit 8c2ea19085
5 changed files with 142 additions and 20 deletions
@@ -595,4 +595,36 @@ mod tests {
assert!(stderr.contains(&line));
}
}
#[test]
fn control_characters_are_always_stripped_out_from_the_log_messages() {
const RAW_LINE: &str = "$$START$$\x1B[1;32mIn\u{202a}\u{202e}\u{2066}\u{2069}ner\n\r\x7ftext!\u{80}\u{9f}\x1B[0m$$END$$";
const SANITIZED_LINE: &str = "$$START$$Inner\ntext!$$END$$";
let output = run_test_in_another_process(
"control_characters_are_always_stripped_out_from_the_log_messages",
|| {
std::env::set_var("RUST_LOG", "trace");
let mut builder = LoggerBuilder::new("");
builder.with_colors(true);
builder.init().unwrap();
log::error!("{}", RAW_LINE);
},
);
if let Some(output) = output {
let stderr = String::from_utf8(output.stderr).unwrap();
// The log messages should always be sanitized.
assert!(!stderr.contains(RAW_LINE));
assert!(stderr.contains(SANITIZED_LINE));
// The part where the timestamp, the logging level, etc. is printed out doesn't
// always have to be sanitized unless it's necessary, and here it shouldn't be.
assert!(stderr.contains("\x1B[31mERROR\x1B[0m"));
// Make sure the logs aren't being duplicated.
assert_eq!(stderr.find("ERROR"), stderr.rfind("ERROR"));
assert_eq!(stderr.find(SANITIZED_LINE), stderr.rfind(SANITIZED_LINE));
}
}
}