From 217af66070caeae9c846b5bc8a874d1edcbb0f21 Mon Sep 17 00:00:00 2001 From: Christian Dassy <18606875+inxbit@users.noreply.github.com> Date: Tue, 17 Nov 2026 01:36:18 -0500 Subject: [PATCH 01/20] Reject surplus arguments to profiles subcommands profiles list, show, validate and test each read a fixed number of arguments and dropped the rest without a word, so `profiles validate good.yml broken.yml` reported success having read only the first file. A surplus argument is now a usage error that names it. --- src/cli/args.rs | 16 ++++++++++++++++ tests/cli.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/src/cli/args.rs b/src/cli/args.rs index 63a30c2..ab43160 100644 --- a/src/cli/args.rs +++ b/src/cli/args.rs @@ -191,6 +191,22 @@ fn parse_profiles_command( .map(|arg| arg.to_string_lossy().to_string()) .unwrap_or_else(|| "list".to_string()); + // Each subcommand reads a fixed number of arguments. A surplus one is an + // error rather than dropped, so `profiles validate a.yml b.yml` cannot + // report success having read only `a.yml`. + let expected_args = match subcommand.as_str() { + "list" => 1, + "show" | "validate" => 2, + "test" => 3, + _ => usize::MAX, + }; + if let Some(surplus) = args.get(expected_args) { + return Err(CliError::Usage(format!( + "profiles {subcommand} unexpected argument '{}'", + escape_untrusted(&surplus.to_string_lossy()) + ))); + } + match subcommand.as_str() { "list" => Ok((options, Action::ProfilesList)), "show" => { diff --git a/tests/cli.rs b/tests/cli.rs index 046a129..7bc203c 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -88,6 +88,54 @@ fn cli_usage_errors_include_context_without_pinning_clap_wording() { } } +// A profiles subcommand takes a fixed number of arguments. A surplus one used +// to be dropped without a word, so `profiles validate good.yml broken.yml` +// reported success having read only the first file. +#[test] +fn profiles_subcommands_reject_surplus_arguments() { + let cases: &[(&[&str], &str)] = &[ + (&["profiles", "list", "surplus-arg"], "surplus-arg"), + (&["profiles", "show", "cisco", "surplus-arg"], "surplus-arg"), + ( + &[ + "profiles", + "validate", + "profiles/custom-router.example.yml", + "missing-profile.yml", + ], + "missing-profile.yml", + ), + ( + &[ + "profiles", + "test", + "cisco", + "fixtures/cisco.txt", + "surplus-arg", + ], + "surplus-arg", + ), + ]; + + for (args, surplus) in cases { + let output = Command::cargo_bin("prismtty") + .expect("binary exists") + .args(*args) + .output() + .expect("command runs"); + let stderr = String::from_utf8_lossy(&output.stderr); + assert!( + !output.status.success(), + "expected {args:?} to fail, stdout: {:?}, stderr: {stderr:?}", + String::from_utf8_lossy(&output.stdout), + ); + assert!( + stderr.contains("unexpected") && stderr.contains(surplus), + "stderr for {args:?} did not name the surplus argument: {stderr:?}" + ); + } +} + #[test] fn cli_usage_errors_preserve_lines_and_prefix_untrusted_continuations() { let output = Command::cargo_bin("prismtty") From ef8db3e3f60f4dd45e23f9a3055ae99e81ab15d9 Mon Sep 17 00:00:00 2001 From: Christian Dassy <18606875+inxbit@users.noreply.github.com> Date: Tue, 17 Nov 2026 01:37:08 -0500 Subject: [PATCH 02/20] Write the requested reports when the input is empty An input that ended before its first byte returned ahead of the --show-profile and --benchmark reports, so both flags printed nothing for an empty file or a command with no output. --- src/cli/stream.rs | 10 ++++++++++ tests/cli.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/cli/stream.rs b/src/cli/stream.rs index 789ff7b..944343a 100644 --- a/src/cli/stream.rs +++ b/src/cli/stream.rs @@ -114,6 +114,16 @@ pub(super) fn highlight_stream( let mut chunk_filter = ChunkFilterState::default(); let read = reader.read(&mut buffer)?; if read == 0 { + // An input that ends before its first byte is still a run: the + // reports that were asked for are written. + session.report_current(); + if options.benchmark { + print_benchmark_report( + session.benchmark_report(), + input_bytes, + started.elapsed().as_secs_f64(), + ); + } return Ok(()); } diff --git a/tests/cli.rs b/tests/cli.rs index 7bc203c..7a2ec05 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -1514,6 +1514,35 @@ fn show_profile_reports_selected_profiles_to_stderr() { )); } +// `--show-profile` and `--benchmark` ask for a report, and an input that ends +// before its first byte is still a run: both reports are written. +#[test] +fn empty_input_still_reports_the_profile_and_the_benchmark() { + let mut empty_config = tempfile::NamedTempFile::new().expect("empty config"); + writeln!(empty_config, "rules: []").expect("write empty config"); + + let output = Command::cargo_bin("prismtty") + .expect("binary exists") + .args(["--show-profile", "--benchmark", "--profile", "generic"]) + .arg("--config") + .arg(empty_config.path()) + .write_stdin("") + .output() + .expect("command runs"); + let stderr = String::from_utf8_lossy(&output.stderr); + + assert!(output.status.success(), "{stderr:?}"); + assert!(output.stdout.is_empty(), "{:?}", output.stdout); + assert!( + stderr.contains("prismtty: profiles selected: generic"), + "the profile report is missing: {stderr:?}" + ); + assert!( + stderr.contains("Benchmark results (time spent, match count):"), + "the benchmark report is missing: {stderr:?}" + ); +} + #[test] fn raw_c1_string_payload_is_not_used_for_profile_detection() { let mut empty_config = tempfile::NamedTempFile::new().expect("empty config"); From 49d1fa9058be03318dcd2e0bbfa3ae0edbde194f Mon Sep 17 00:00:00 2001 From: Christian Dassy <18606875+inxbit@users.noreply.github.com> Date: Tue, 17 Nov 2026 01:48:49 -0500 Subject: [PATCH 03/20] Wait for input when the filter's stdin is non-blocking Another program can leave O_NONBLOCK set on the pipe or terminal that prismtty inherits as stdin. The first read of an empty one then failed with EAGAIN and the filter exited 1 before any input arrived. Stdin now goes through the same wait-when-would-block wrapper as stdout. --- src/cli.rs | 9 ++++++--- tests/cli.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 7c84c6a..f7310a6 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -252,14 +252,17 @@ fn run_stdin(options: Options) -> Result { let trace = IoTrace::open(options.trace_io.as_deref())?; let (_registration, reload_watcher) = register_for_reload(); let stdin = io::stdin(); - // As in PTY mode, another program on the terminal can leave stdout - // non-blocking. + // As in PTY mode, another program on the terminal or pipe can leave + // stdin and stdout non-blocking. let mut stdout = WaitWhenWouldBlock { fd: stdout.as_raw_fd(), inner: stdout, }; highlight_stream( - stdin.lock(), + WaitWhenWouldBlock { + fd: stdin.as_raw_fd(), + inner: stdin.lock(), + }, &mut stdout, prepared, InputSource { diff --git a/tests/cli.rs b/tests/cli.rs index 7a2ec05..d6ce70e 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -178,6 +178,63 @@ fn stdin_mode_highlights_with_forced_profile() { .stdout(predicate::str::contains("\u{1b}[")); } +// Another program can leave `O_NONBLOCK` set on the pipe or terminal prismtty +// inherits as stdin. A read that finds it empty means "no input yet": the +// filter waits for it instead of failing with EAGAIN. +#[test] +#[cfg(unix)] +fn stdin_mode_waits_for_input_on_a_nonblocking_stdin() { + use std::os::fd::{FromRawFd, OwnedFd}; + + let mut fds = [0; 2]; + // SAFETY: `fds` has room for the two descriptors `pipe` writes; each is + // then owned by exactly one `OwnedFd`. The fcntl calls only set flags on + // those descriptors: O_NONBLOCK on the read end, and close-on-exec on + // the write end so the filter does not inherit it and wait on itself for + // end of input. + let (read_end, write_end) = unsafe { + assert_eq!(libc::pipe(fds.as_mut_ptr()), 0, "pipe"); + let flags = libc::fcntl(fds[0], libc::F_GETFL); + assert_eq!( + libc::fcntl(fds[0], libc::F_SETFL, flags | libc::O_NONBLOCK), + 0, + "could not set O_NONBLOCK" + ); + assert_eq!( + libc::fcntl(fds[1], libc::F_SETFD, libc::FD_CLOEXEC), + 0, + "could not set FD_CLOEXEC" + ); + (OwnedFd::from_raw_fd(fds[0]), OwnedFd::from_raw_fd(fds[1])) + }; + let mut empty_config = tempfile::NamedTempFile::new().expect("empty config"); + writeln!(empty_config, "rules: []").expect("write empty config"); + + let child = StdCommand::new(assert_cmd::cargo::cargo_bin("prismtty")) + .args(["--profile", "generic", "--config"]) + .arg(empty_config.path()) + .stdin(Stdio::from(read_end)) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .expect("prismtty starts"); + // Long enough for the filter to reach its first read of the empty pipe. + thread::sleep(Duration::from_millis(300)); + // A filter that already gave up closed the pipe; its exit status below + // is the failure worth reporting, not this write. + let mut write_end = fs::File::from(write_end); + let _ = write_end.write_all(b"late input\n"); + drop(write_end); + + let output = child.wait_with_output().expect("prismtty exits"); + assert!( + output.status.success(), + "stderr: {:?}", + String::from_utf8_lossy(&output.stderr) + ); + assert_eq!(String::from_utf8_lossy(&output.stdout), "late input\n"); +} + #[test] fn wrapped_command_can_trace_pty_output_bytes() { let trace = tempfile::NamedTempFile::new().expect("trace file"); From 2be3fbd0de059d349b43a7e44e9da6bd47604b84 Mon Sep 17 00:00:00 2001 From: Christian Dassy <18606875+inxbit@users.noreply.github.com> Date: Tue, 17 Nov 2026 01:50:24 -0500 Subject: [PATCH 04/20] Apply --sanitize and --strip-ansi to profiles test output profiles test printed its fixture through the highlighter without the escape filtering the two flags ask for, so an OSC 52 clipboard write in a captured fixture still reached the terminal under --sanitize, and --strip-ansi left the capture's own colors in place. --- src/cli.rs | 13 ++++++++++++- tests/cli.rs | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/cli.rs b/src/cli.rs index f7310a6..d6c1f72 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -23,7 +23,9 @@ use thiserror::Error; use crate::config::{ MAX_PROFILE_TEST_FILE_BYTES, PrismConfig, load_profile_file, read_bounded_regular_file, }; -use crate::highlight::{Highlighter, RuleMatchError, StreamingHighlighter}; +use crate::highlight::{ + Highlighter, RuleMatchError, StreamingHighlighter, strip_ansi, strip_string_escapes, +}; use crate::terminal_text::escape_untrusted; use args::{Action, Options, parse_args, print_help}; @@ -203,6 +205,15 @@ fn run_inner(args: Vec) -> Result { Action::ProfilesTest { profile, fixture } => { let input = read_bounded_regular_file(&fixture, MAX_PROFILE_TEST_FILE_BYTES, "fixture")?; + // The fixture is printed to the terminal like program output, so + // the flags that remove escapes from that output apply to it too. + let input = if options.strip_ansi { + strip_ansi(&input) + } else if options.sanitize { + strip_string_escapes(&input) + } else { + input + }; let store = profile_store()?; let config = PrismConfig::from_profiles(&store, &[profile.as_str()])?; let highlighter = Highlighter::from_config(config)?; diff --git a/tests/cli.rs b/tests/cli.rs index d6ce70e..fb35be4 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -1571,6 +1571,43 @@ fn show_profile_reports_selected_profiles_to_stderr() { )); } +// `profiles test` prints a captured fixture to the terminal, so the two flags +// that remove escapes from program output apply to it as well: with +// `--sanitize` an OSC 52 clipboard write in the capture must not reach stdout. +#[test] +fn profiles_test_applies_sanitize_and_strip_ansi_to_the_fixture() { + let mut fixture = tempfile::NamedTempFile::new().expect("fixture"); + fixture + .write_all(b"before\x1b]52;c;U0VDUkVU\x07after \x1b[31mred\x1b[0m plain\n") + .expect("write fixture"); + + let run = |flag: &str| { + let output = Command::cargo_bin("prismtty") + .expect("binary exists") + .arg(flag) + .args(["profiles", "test", "generic"]) + .arg(fixture.path()) + .output() + .expect("command runs"); + assert!(output.status.success(), "{flag}: {output:?}"); + output.stdout + }; + + let sanitized = run("--sanitize"); + assert_eq!( + String::from_utf8_lossy(&sanitized), + "beforeafter \u{1b}[31mred\u{1b}[0m plain\n", + "--sanitize keeps colors and drops the string escape" + ); + + let stripped = run("--strip-ansi"); + assert_eq!( + String::from_utf8_lossy(&stripped), + "beforeafter red plain\n", + "--strip-ansi drops every source escape" + ); +} + // `--show-profile` and `--benchmark` ask for a report, and an input that ends // before its first byte is still a run: both reports are written. #[test] From a846162ee65ce46e13c3db622d4121327888b734 Mon Sep 17 00:00:00 2001 From: Christian Dassy <18606875+inxbit@users.noreply.github.com> Date: Tue, 17 Nov 2026 01:51:25 -0500 Subject: [PATCH 05/20] Refuse a FIFO in place of a runtime file without blocking The reload marker and pid files were opened for writing before their file type was checked. A FIFO at that path with no reader made the open block, so `prismtty --reload` hung forever. The open is now non-blocking, and the existing regular-file check refuses the rest. --- src/cli/runtime.rs | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/src/cli/runtime.rs b/src/cli/runtime.rs index e396be7..d55df93 100644 --- a/src/cli/runtime.rs +++ b/src/cli/runtime.rs @@ -321,7 +321,11 @@ fn write_private_file(path: &Path, contents: &[u8]) -> io::Result<()> { .write(true) .truncate(true) .mode(0o600) - .custom_flags(libc::O_NOFOLLOW) + // O_NONBLOCK: opening a FIFO for writing otherwise blocks until it + // has a reader. With it the open fails or returns at once, and the + // file type check below refuses anything that is not a regular file. + // It has no effect on writes to a regular file. + .custom_flags(libc::O_NOFOLLOW | libc::O_NONBLOCK) .open(path)?; ensure_private_runtime_file(&file, path)?; file.write_all(contents) @@ -506,6 +510,40 @@ mod tests { ); } + // Opening a FIFO for writing blocks until something reads it. A FIFO in + // place of the reload marker must be refused, not waited on forever. + #[cfg(unix)] + #[test] + fn runtime_write_file_refuses_a_fifo_without_blocking() { + use std::os::unix::ffi::OsStrExt; + + let temp = tempfile::tempdir().expect("tempdir creates"); + let runtime = temp.path().join("runtime"); + let reload = runtime.join("reload"); + super::ensure_private_runtime_dir(&runtime).expect("runtime dir creates"); + let fifo_path = + std::ffi::CString::new(reload.as_os_str().as_bytes()).expect("path has no NUL"); + // SAFETY: `fifo_path` is a valid NUL-terminated path; mkfifo only + // creates the FIFO there. + assert_eq!(unsafe { libc::mkfifo(fifo_path.as_ptr(), 0o600) }, 0); + + let (done_tx, done_rx) = std::sync::mpsc::channel(); + let writer_path = reload.clone(); + let writer = std::thread::spawn(move || { + let _ = done_tx.send(super::write_private_file(&writer_path, b"now\n")); + }); + let result = done_rx.recv_timeout(std::time::Duration::from_secs(5)); + if result.is_err() { + // Give the blocked open its reader so the thread can end. + let _ = std::fs::File::open(&reload); + } + writer.join().expect("writer thread ends"); + + result + .expect("opening the FIFO blocked") + .expect_err("a FIFO is not a runtime file"); + } + #[cfg(unix)] #[test] fn pid_registration_preserves_legacy_shared_pid_file_for_older_binaries() { From 0d4268653790a8fddf2b8eb968f4a50598bd1aab Mon Sep 17 00:00:00 2001 From: Christian Dassy <18606875+inxbit@users.noreply.github.com> Date: Tue, 17 Nov 2026 01:53:50 -0500 Subject: [PATCH 06/20] Track only real SGR controls as the program's colors The tracker of the program's own rendition read any CSI ending in `m` as SGR and skipped the parameters it could not parse. xterm's modifyOtherKeys control (`CSI > 4 ; 1 m`, sent by fish, vim and tmux) therefore counted as bold, and the text after each highlight was redrawn bold or faint. A control with a private marker or an intermediate byte is no longer SGR, and a C0 control inside the CSI is no longer taken for part of its parameters. --- src/highlight.rs | 17 ++++++++++++++++- tests/config_and_highlight.rs | 7 +++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/highlight.rs b/src/highlight.rs index 1b9ce79..67db1ac 100644 --- a/src/highlight.rs +++ b/src/highlight.rs @@ -3350,11 +3350,26 @@ impl SgrRendition { return; } - let body = &bytes[body_start..bytes.len() - 1]; + // A terminal runs a C0 control found inside a CSI and ignores DEL; + // neither is part of the parameters. + let body = bytes[body_start..bytes.len() - 1] + .iter() + .copied() + .filter(|byte| !byte.is_ascii_control()) + .collect::>(); if body.is_empty() { self.reset_all(); return; } + // Only digits, `;` and `:` make an SGR. A private marker makes it + // another control (`CSI > 4 ; 1 m` sets xterm's modifyOtherKeys), and + // a terminal ignores one with an intermediate byte. + if !body + .iter() + .all(|byte| byte.is_ascii_digit() || matches!(byte, b';' | b':')) + { + return; + } let parameters = body.split(|byte| *byte == b';').collect::>(); diff --git a/tests/config_and_highlight.rs b/tests/config_and_highlight.rs index 958a728..5df61d5 100644 --- a/tests/config_and_highlight.rs +++ b/tests/config_and_highlight.rs @@ -334,6 +334,13 @@ rules: ("\x1b[32m\x1b7\x1b[33mx\x1b[!p\x1b8", None), ("\x1b[32m\x1b[?1049h\x1b[33m\x1b7x\x1b[?1049l", Some("32")), ("\x1b[32m\x1b[?1047h\x1b[33mx\x1b[?1047l", Some("33")), + // Only digits, `;` and `:` make an SGR. `CSI > 4 ; 1 m` is xterm's + // modifyOtherKeys (fish, vim and tmux send it), not bold; a control + // with an intermediate byte such as `+` is ignored outright. + ("\x1b[32m\x1b[>4;1mx", Some("32")), + ("\x1b[32m\x1b[+33mx", Some("32")), + // A C0 control inside a CSI is not part of its parameters. + ("\x1b[3\x002mx", Some("32")), ] { let config = PrismConfig::from_chromaterm_yaml(yaml).expect("chromaterm config loads"); let highlighter = Highlighter::from_config(config).expect("rules compile"); From fd3c8006287afca9eb95eb08ce580afd6dc1a78e Mon Sep 17 00:00:00 2001 From: Christian Dassy <18606875+inxbit@users.noreply.github.com> Date: Tue, 17 Nov 2026 01:55:14 -0500 Subject: [PATCH 07/20] Keep the line controls inside a CSI that --strip-ansi removes A terminal runs a line feed or carriage return it meets inside a CSI. strip_ansi dropped them with the control, which joined the lines around an escape cut short at a line end. --- src/highlight.rs | 16 +++++++++++++--- tests/config_and_highlight.rs | 11 +++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/highlight.rs b/src/highlight.rs index 67db1ac..3b16aa2 100644 --- a/src/highlight.rs +++ b/src/highlight.rs @@ -4421,12 +4421,22 @@ fn is_token_continuation(byte: u8) -> bool { byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-' | b'/' | b'.') } -/// Removes ANSI escape sequences and returns only visible bytes. +/// Removes ANSI escape sequences and returns only visible bytes. A terminal +/// runs the format effectors (backspace through carriage return) it meets +/// inside a CSI, so those stay: dropping them with the control would join +/// the lines around an escape cut short at a line end. pub fn strip_ansi(input: &[u8]) -> Vec { let mut stripped = Vec::new(); for token in tokenize_ansi(input) { - if let Token::Text(text) = token { - stripped.extend(text); + match token { + Token::Text(text) => stripped.extend(text), + Token::Ansi(bytes) if is_csi_sequence(&bytes) => stripped.extend( + bytes + .iter() + .copied() + .filter(|byte| matches!(byte, 0x08..=0x0d)), + ), + Token::Ansi(_) => {} } } stripped diff --git a/tests/config_and_highlight.rs b/tests/config_and_highlight.rs index 5df61d5..9366873 100644 --- a/tests/config_and_highlight.rs +++ b/tests/config_and_highlight.rs @@ -277,6 +277,17 @@ rules: assert!(output.contains("error\x1b[0m\x1b[32m tail")); } +// A terminal runs a line feed or carriage return it meets inside a CSI, as +// after an escape cut short at a line end. Stripping the control must not +// join the two lines. +#[test] +fn strip_ansi_keeps_the_line_controls_inside_a_csi() { + assert_eq!(strip_ansi(b"A\x1b[31\nmB"), b"A\nB"); + assert_eq!(strip_ansi(b"text\x1b[\r\nnext"), b"text\r\next"); + // A string control's payload is not run: none of it survives. + assert_eq!(strip_ansi(b"A\x1b]0;ti\ntle\x07B"), b"AB"); +} + #[test] fn noninteractive_stream_restores_program_sgr_set_in_an_earlier_push() { let yaml = r##" From cbde563e22ea73191811264ed4ec1aa45b57025a Mon Sep 17 00:00:00 2001 From: Christian Dassy <18606875+inxbit@users.noreply.github.com> Date: Tue, 17 Nov 2026 01:56:14 -0500 Subject: [PATCH 08/20] Let an exclusive rule style all of its own capture groups An exclusive rule marked each span protected as soon as it styled it, so its own later groups were skipped where they overlapped: with colors on groups 0 and 1, group 1 was dropped. Its spans are now protected from later rules once all of its groups are applied. --- src/highlight.rs | 18 +++++++++++++----- tests/config_and_highlight.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/highlight.rs b/src/highlight.rs index 3b16aa2..895d0df 100644 --- a/src/highlight.rs +++ b/src/highlight.rs @@ -2490,6 +2490,11 @@ impl Highlighter { // A rule that hit a runtime error still reports the matches it // found around the failing span, so apply them before deciding // what the error means for the rest of the session. + // + // An exclusive rule protects its spans from later rules only, so + // they are marked once all of its own capture groups are applied: + // group 1 lies inside group 0, which is styled first. + let mut exclusive_spans = Vec::new(); for (start, end, style) in matches { if start >= end || end > styles.len() { continue; @@ -2505,13 +2510,16 @@ impl Highlighter { { continue; } - for idx in start..end { - styles[idx].merge_from(&style); - if rule.exclusive { - protected[idx] = true; - } + for cell in &mut styles[start..end] { + cell.merge_from(&style); + } + if rule.exclusive { + exclusive_spans.push(start..end); } } + for span in exclusive_spans { + protected[span].fill(true); + } // The matches found around a failing span are applied above; the // error itself decides what happens to the rule from here on. diff --git a/tests/config_and_highlight.rs b/tests/config_and_highlight.rs index 9366873..10d1743 100644 --- a/tests/config_and_highlight.rs +++ b/tests/config_and_highlight.rs @@ -277,6 +277,32 @@ rules: assert!(output.contains("error\x1b[0m\x1b[32m tail")); } +// `exclusive` keeps later rules off a match. It must not keep the rule's own +// capture groups off it: group 1 lies inside group 0, which is styled first. +#[test] +fn exclusive_rule_applies_all_of_its_own_overlapping_groups() { + let yaml = r##" +rules: + - description: whole match and group + regex: (abc) + exclusive: true + color: + 0: f#ffffff + 1: underline + - description: later rule + regex: abc + color: f#ff0000 +"##; + + let config = PrismConfig::from_chromaterm_yaml(yaml).expect("chromaterm config loads"); + let highlighter = Highlighter::from_config(config).expect("rules compile"); + + assert_eq!( + highlighter.highlight_str("abc\n"), + "\x1b[4;38;2;255;255;255mabc\x1b[0m\n" + ); +} + // A terminal runs a line feed or carriage return it meets inside a CSI, as // after an escape cut short at a line end. Stripping the control must not // join the two lines. From 5df530bac96457834d941be2ba53b3bcb88bc099 Mon Sep 17 00:00:00 2001 From: Christian Dassy <18606875+inxbit@users.noreply.github.com> Date: Tue, 17 Nov 2026 02:02:41 -0500 Subject: [PATCH 09/20] Keep non-SGR controls when a prompt's colors are neutralized Removing a prompt's own color took every CSI ending in `m` on the line with it. xterm's key modifier option (`CSI > 4 ; 1 m`, XTMODKEYS) was deleted from the stream that way, so the terminal never changed how it reports keys. One predicate now decides what an SGR is for both the rendition tracker and this removal. --- src/highlight.rs | 45 +++++++++++++++++------------------ tests/config_and_highlight.rs | 29 ++++++++++++++++++++-- 2 files changed, 49 insertions(+), 25 deletions(-) diff --git a/src/highlight.rs b/src/highlight.rs index 895d0df..38fee8e 100644 --- a/src/highlight.rs +++ b/src/highlight.rs @@ -1997,8 +1997,7 @@ fn visible_byte_map_and_ansi_ranges(input: &[u8]) -> (Vec, Vec>(); if body.is_empty() { self.reset_all(); return; } - // Only digits, `;` and `:` make an SGR. A private marker makes it - // another control (`CSI > 4 ; 1 m` sets xterm's modifyOtherKeys), and - // a terminal ignores one with an intermediate byte. - if !body - .iter() - .all(|byte| byte.is_ascii_digit() || matches!(byte, b';' | b':')) - { - return; - } let parameters = body.split(|byte| *byte == b';').collect::>(); @@ -3624,6 +3603,26 @@ fn parse_semicolon_extended_color(parameters: &[&[u8]]) -> ExtendedColorParse { } } +/// The parameters of `bytes` when it is an SGR: a CSI that ends in `m` and +/// holds only digits, `;` and `:`. A private marker makes it another control +/// (`CSI > Pp ; Pv m` is xterm's key modifier option, XTMODKEYS), and a +/// terminal ignores one with an intermediate byte. A terminal runs a C0 +/// control found inside a CSI and ignores DEL, so neither is a parameter. +fn sgr_parameters(bytes: &[u8]) -> Option> { + let (final_byte, body) = bytes[csi_introducer_len(bytes)?..].split_last()?; + if *final_byte != b'm' { + return None; + } + let body = body + .iter() + .copied() + .filter(|byte| !byte.is_ascii_control()) + .collect::>(); + body.iter() + .all(|byte| byte.is_ascii_digit() || matches!(byte, b';' | b':')) + .then_some(body) +} + fn parse_sgr_number(parameter: &[u8]) -> Option { std::str::from_utf8(parameter).ok()?.parse().ok() } diff --git a/tests/config_and_highlight.rs b/tests/config_and_highlight.rs index 10d1743..23a350c 100644 --- a/tests/config_and_highlight.rs +++ b/tests/config_and_highlight.rs @@ -372,8 +372,8 @@ rules: ("\x1b[32m\x1b[?1049h\x1b[33m\x1b7x\x1b[?1049l", Some("32")), ("\x1b[32m\x1b[?1047h\x1b[33mx\x1b[?1047l", Some("33")), // Only digits, `;` and `:` make an SGR. `CSI > 4 ; 1 m` is xterm's - // modifyOtherKeys (fish, vim and tmux send it), not bold; a control - // with an intermediate byte such as `+` is ignored outright. + // key modifier option (XTMODKEYS), not bold; a control with an + // intermediate byte such as `+` is ignored outright. ("\x1b[32m\x1b[>4;1mx", Some("32")), ("\x1b[32m\x1b[+33mx", Some("32")), // A C0 control inside a CSI is not part of its parameters. @@ -2293,6 +2293,31 @@ fn interactive_streaming_highlighter_neutralizes_source_sgr_on_fortinet_prompt_e assert_all_token_occurrences_have_no_foreground(&output, "diagnose"); } +// Neutralizing a prompt's color removes its SGR controls and nothing else. A +// control that only ends in `m`, such as xterm's key modifier option +// `CSI > 4 ; 1 m` (XTMODKEYS), changes how the terminal reports keys and must +// reach it. +#[test] +fn interactive_streaming_highlighter_keeps_non_sgr_controls_when_neutralizing_prompt_echo_sgr() { + let store = ProfileStore::builtin(); + let config = PrismConfig::from_profiles(&store, &["fortinet"]).expect("fortinet loads"); + let highlighter = Highlighter::from_config(config).expect("rules compile"); + let mut streaming = new_interactive_streaming(highlighter); + + let input = concat!( + "\x1b[38;2;255;255;255mFGT01 # ", + "\x1b[>4;1m", + "\x1b[38;2;255;0;255mdiagnose " + ); + let output = streaming.push_str(input); + + assert_all_token_occurrences_have_no_foreground(&output, "diagnose"); + assert!( + output.contains("\x1b[>4;1m"), + "the key modifier control was removed with the colors: {output:?}" + ); +} + #[test] fn interactive_streaming_highlighter_uses_full_reset_when_neutralizing_prompt_echo_sgr() { let store = ProfileStore::builtin(); From 39b23d306b20f582a4f64ad258e031dea37a9f7c Mon Sep 17 00:00:00 2001 From: Christian Dassy <18606875+inxbit@users.noreply.github.com> Date: Tue, 17 Nov 2026 02:03:32 -0500 Subject: [PATCH 10/20] Skip SS3 keys whole in --local-echo An arrow or function key sent in application mode (`ESC O A`, `ESC O P`) had only its ESC skipped, so --local-echo printed the rest of the key as `OA` or `OP`. --- src/cli/pty.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/cli/pty.rs b/src/cli/pty.rs index 593f4fa..1764154 100644 --- a/src/cli/pty.rs +++ b/src/cli/pty.rs @@ -1475,6 +1475,15 @@ fn local_echo_escape_step(input: &[u8], idx: usize) -> LocalEchoStep { let Some(next) = input.get(idx + 1) else { return LocalEchoStep::Incomplete; }; + // SS3: an arrow or function key in application mode, `ESC O` plus one + // final byte. + if *next == b'O' { + return if idx + 2 < input.len() { + LocalEchoStep::Skip(idx + 3) + } else { + LocalEchoStep::Incomplete + }; + } if *next != b'[' { return LocalEchoStep::Skip(idx + 1); } @@ -2788,6 +2797,17 @@ mod tests { assert_eq!(echo.push(b"show\r"), b"show\r\n"); } + // In application cursor-key mode an arrow or function key arrives as SS3 + // (`ESC O A`, `ESC O P`). Its final byte is part of the key, not text. + #[test] + fn local_echo_skips_ss3_keys_whole_even_when_split() { + assert_eq!(super::local_echo_bytes(b"a\x1bOAb\x1bOPc"), b"abc"); + + let mut echo = super::LocalEchoState::default(); + assert!(echo.push(b"\x1bO").is_empty()); + assert_eq!(echo.push(b"Ax"), b"x"); + } + #[test] fn local_echo_state_bounds_incomplete_csi_and_recovers() { let mut echo = super::LocalEchoState::default(); From 9a3b90f92ad05a6bd883de1275389c0c0abd6854 Mon Sep 17 00:00:00 2001 From: Christian Dassy <18606875+inxbit@users.noreply.github.com> Date: Tue, 17 Nov 2026 02:05:25 -0500 Subject: [PATCH 11/20] Refuse a trace file that has a second hard link --trace-io checked the file's type, owner and mode but not its link count, so a trace path that was a hard link appended the session's input and output to the file's other name. --- src/cli/trace.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/cli/trace.rs b/src/cli/trace.rs index f03ff5e..66b2d90 100644 --- a/src/cli/trace.rs +++ b/src/cli/trace.rs @@ -138,6 +138,14 @@ fn open_trace_file(path: &Path) -> io::Result { ), )); } + // A second hard link is a second name for the same file: the trace would + // be appended to whatever else that name is used for. + if metadata.nlink() != 1 { + return Err(io::Error::new( + io::ErrorKind::PermissionDenied, + format!("trace file has more than one hard link: {}", path.display()), + )); + } // Writes keep their usual blocking semantics; O_APPEND is preserved. let fd = file.as_raw_fd(); // SAFETY: `fd` is the open descriptor owned by `file`; F_GETFL only reads @@ -280,6 +288,31 @@ mod tests { ); } + // A second name for a file is a second way to reach it: the trace must + // not be appended to whatever else the path was hard-linked to. + #[cfg(unix)] + #[test] + fn trace_file_rejects_a_hard_linked_target() { + let dir = tempfile::tempdir().expect("tempdir creates"); + let target = dir.path().join("target.log"); + let path = dir.path().join("trace.log"); + std::fs::write(&target, "original\n").expect("target writes"); + std::fs::set_permissions(&target, std::fs::Permissions::from_mode(0o600)) + .expect("mode sets"); + std::fs::hard_link(&target, &path).expect("hard link creates"); + + let trace = super::IoTrace::open(Some(&path)); + if let Ok(trace) = &trace { + trace.log("OUT", b"secret"); + } + + assert!(trace.is_err(), "a hard-linked trace file was accepted"); + assert_eq!( + std::fs::read_to_string(&target).expect("target reads"), + "original\n" + ); + } + // Opening a FIFO for writing blocks until a reader appears, which would // hang startup before the non-regular-file check could reject it. #[cfg(unix)] From a26c805de4e8c4c96b95ffbc7254cb9964d7b30e Mon Sep 17 00:00:00 2001 From: Christian Dassy <18606875+inxbit@users.noreply.github.com> Date: Tue, 17 Nov 2026 02:05:25 -0500 Subject: [PATCH 12/20] Reject a capture index that does not fit usize A numeric capture key was narrowed with `as usize`. On a 32-bit target 4294967296 wrapped to group 0 and colored the whole match instead of being rejected like the quoted form of the same key. --- src/config.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/config.rs b/src/config.rs index 4d9e837..8e914c1 100644 --- a/src/config.rs +++ b/src/config.rs @@ -697,13 +697,18 @@ fn parse_capture_ref( ) -> Result { match value { serde_norway::Value::Number(number) => { - let Some(group) = number.as_u64() else { + // `try_from`, not `as`: on a 32-bit target a cast would wrap + // 4294967296 to group 0, the whole match. + let Some(group) = number + .as_u64() + .and_then(|group| usize::try_from(group).ok()) + else { return Err(ConfigError::InvalidCaptureKey { description: escape_untrusted(description), key: escape_untrusted(&number.to_string()), }); }; - Ok(CaptureRef::Index(group as usize)) + Ok(CaptureRef::Index(group)) } serde_norway::Value::String(name) => parse_capture_ref_string(description, name), other => Err(ConfigError::InvalidCaptureKey { From 01df4e9cf13b8ebafebc7cf88689402a77574b09 Mon Sep 17 00:00:00 2001 From: Christian Dassy <18606875+inxbit@users.noreply.github.com> Date: Tue, 17 Nov 2026 02:13:35 -0500 Subject: [PATCH 13/20] Fix built-in rules that mispaint common device output - generic: a good-state word after `not` (`not reachable`, `not connected` with aligned spacing, `not valid`) was painted green; `not reachable` now reads as a bad state like `not connected`. - generic: bare `den` (a site code, a word) was painted as a denial. - generic: an IPv6 zone holding `-`, `.` or `_` was split in two colors. - arubacx, fortinet: configuration-context prompts were left uncolored. - palo-alto: aggregate interfaces (`ae1`, `ae1.107`) were left uncolored. --- src/profiles/builtin/arubacx.yml | 2 +- src/profiles/builtin/fortinet.yml | 2 +- src/profiles/builtin/generic.yml | 6 +- src/profiles/builtin/palo-alto.yml | 2 +- tests/config_and_highlight.rs | 107 +++++++++++++++++++++++++++++ 5 files changed, 113 insertions(+), 6 deletions(-) diff --git a/src/profiles/builtin/arubacx.yml b/src/profiles/builtin/arubacx.yml index 9dd4c5e..9f40235 100644 --- a/src/profiles/builtin/arubacx.yml +++ b/src/profiles/builtin/arubacx.yml @@ -17,7 +17,7 @@ profile: runtime_prompt: cisco_host_marker rules: - description: ArubaCX prompt - regex: '(?m)^[A-Za-z0-9_.-]+[>#]' + regex: '(?m)^[A-Za-z0-9_.-]+(\([A-Za-z0-9_./-]+\))?[>#]' color: f#ffffff - description: ArubaCX interface regex: '(?i)\b((\d+/\d+/\d+)|(lag\d+)|(vlan\d+)|(loopback\d+)|(mgmt))(\.\d+)?\b' diff --git a/src/profiles/builtin/fortinet.yml b/src/profiles/builtin/fortinet.yml index 4784dc9..0fdd367 100644 --- a/src/profiles/builtin/fortinet.yml +++ b/src/profiles/builtin/fortinet.yml @@ -23,7 +23,7 @@ profile: prompt_confidence: single_from_baseline_or_remote_hint rules: - description: Fortinet prompt - regex: '(?m)^[A-Za-z0-9_.-]+ #' + regex: '(?m)^[A-Za-z0-9_.-]+( \([^)\r\n]+\))? #' color: f#ffffff - description: Fortinet terms regex: '(?i)\b(vdom|policyid|srcintf|dstintf|utm|ipsec|phase1|phase2|diagnose|vd-root)\b' diff --git a/src/profiles/builtin/generic.yml b/src/profiles/builtin/generic.yml index 28d1024..0599329 100644 --- a/src/profiles/builtin/generic.yml +++ b/src/profiles/builtin/generic.yml @@ -11,20 +11,20 @@ rules: regex: '\b(?#\n]+[>#]' color: f#00bfff - description: PAN-OS interface - regex: '(?i)\b(ethernet\d+/\d+(\.\d+)?|tunnel\.\d+|loopback\.\d+)\b' + regex: '(?i)\b(ethernet\d+/\d+(\.\d+)?|ae\d+(\.\d+)?|tunnel\.\d+|loopback\.\d+)\b' color: f#0099ff - description: PAN-OS terms regex: '(?i)\b(vsys\d*|security-policy|nat-policy|globalprotect|wildfire|threat|url-filtering|ha\d?|panorama)\b' diff --git a/tests/config_and_highlight.rs b/tests/config_and_highlight.rs index 23a350c..43ac5b0 100644 --- a/tests/config_and_highlight.rs +++ b/tests/config_and_highlight.rs @@ -277,6 +277,113 @@ rules: assert!(output.contains("error\x1b[0m\x1b[32m tail")); } +fn highlight_with_builtin(profile: &str, input: &str) -> String { + let store = ProfileStore::builtin(); + let config = PrismConfig::from_profiles(&store, &[profile]).expect("profile loads"); + Highlighter::from_config(config) + .expect("rules compile") + .highlight_str(input) +} + +const GREEN: &str = "\x1b[38;2;0;255;0m"; +const RED_BOLD: &str = "\x1b[1;38;2;255;0;0m"; + +// A good-state word after `not` reports a fault. It must not be painted +// green, whatever the spacing a device aligns its columns with. +#[test] +fn generic_profile_does_not_paint_a_negated_good_state_green() { + for line in [ + "route is not reachable\n", + "interface is not connected\n", + "peer is not\tconnected\n", + "clock is not synchronized\n", + "license is not valid\n", + "port is not-connected\n", + ] { + let output = highlight_with_builtin("generic", line); + assert!(!output.contains(GREEN), "{line:?} -> {output:?}"); + } + for (line, phrase) in [ + ("route is not reachable\n", "not reachable"), + ("interface is not connected\n", "not connected"), + ] { + let output = highlight_with_builtin("generic", line); + assert!( + output.contains(&format!("{RED_BOLD}{phrase}\x1b[0m")), + "{line:?} -> {output:?}" + ); + } + // The plain forms keep their color. + for line in ["route is reachable\n", "port is connected\n", "link up\n"] { + let output = highlight_with_builtin("generic", line); + assert!(output.contains(GREEN), "{line:?} -> {output:?}"); + } +} + +// `den` alone is a site code or a word, not a denial. +#[test] +fn generic_profile_does_not_paint_bare_den_as_a_bad_state() { + let output = highlight_with_builtin("generic", "den-core-01 uplink to DEN2; lion den\n"); + assert!(!output.contains(RED_BOLD), "{output:?}"); + for word in ["deny", "denies", "denied"] { + let output = highlight_with_builtin("generic", &format!("acl {word} tcp\n")); + assert!( + output.contains(&format!("{RED_BOLD}{word}\x1b[0m")), + "{output:?}" + ); + } +} + +// An interface zone may hold `-`, `.` and `_` (`%eth-0`, `%bond0.100`). +#[test] +fn generic_profile_colors_an_ipv6_zone_with_punctuation_as_one_address() { + for address in ["fe80::1%eth-0", "fe80::1%bond0.100", "fe80::1%br_lan"] { + let output = highlight_with_builtin("generic", &format!("{address}\n")); + assert_eq!( + output, + format!("\x1b[38;2;0;255;255m{address}\x1b[0m\n"), + "{address}" + ); + } +} + +// Configuration-context prompts are prompts too, and PAN-OS names aggregate +// interfaces `aeN` and `aeN.M`. +#[test] +fn vendor_profiles_color_context_prompts_and_aggregate_interfaces() { + for (profile, line, colored) in [ + ("arubacx", "switch(config)# vlan 10\n", "switch(config)#"), + ( + "arubacx", + "switch(config-if)# no shutdown\n", + "switch(config-if)#", + ), + ("arubacx", "switch# show vlan\n", "switch#"), + ( + "fortinet", + "FGT-01 (global) # get system status\n", + "FGT-01 (global) #", + ), + ( + "fortinet", + "FGT-01 (interface) # edit port1\n", + "FGT-01 (interface) #", + ), + ("fortinet", "FGT-01 # get system status\n", "FGT-01 #"), + ("palo-alto", "ae1.107 vsys1\n", "ae1.107"), + ("palo-alto", "ae2 vsys1\n", "ae2"), + ] { + let output = highlight_with_builtin(profile, line); + let start = output.find(colored).unwrap_or_else(|| { + panic!("{profile}: {colored:?} was split by a style change in {output:?}") + }); + assert!( + output[..start].ends_with('m') && output[start + colored.len()..].starts_with("\x1b["), + "{profile}: {colored:?} is not one colored span in {output:?}" + ); + } +} + // `exclusive` keeps later rules off a match. It must not keep the rule's own // capture groups off it: group 1 lies inside group 0, which is styled first. #[test] From 815baab41558413cb5304b3786d80d9139de2d30 Mon Sep 17 00:00:00 2001 From: Christian Dassy <18606875+inxbit@users.noreply.github.com> Date: Tue, 17 Nov 2026 02:16:33 -0500 Subject: [PATCH 14/20] Keep line controls inside any stripped non-string escape The --strip-ansi fix for a line feed inside a CSI left the same hole for a plain escape: `line1 ESC CR LF next` still lost its line break. --- src/highlight.rs | 7 ++++--- tests/config_and_highlight.rs | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/highlight.rs b/src/highlight.rs index 38fee8e..a8f32fb 100644 --- a/src/highlight.rs +++ b/src/highlight.rs @@ -4430,14 +4430,15 @@ fn is_token_continuation(byte: u8) -> bool { /// Removes ANSI escape sequences and returns only visible bytes. A terminal /// runs the format effectors (backspace through carriage return) it meets -/// inside a CSI, so those stay: dropping them with the control would join -/// the lines around an escape cut short at a line end. +/// inside an escape sequence or a CSI, so those stay: dropping them with the +/// control would join the lines around an escape cut short at a line end. +/// Inside a string control they are payload and go with it. pub fn strip_ansi(input: &[u8]) -> Vec { let mut stripped = Vec::new(); for token in tokenize_ansi(input) { match token { Token::Text(text) => stripped.extend(text), - Token::Ansi(bytes) if is_csi_sequence(&bytes) => stripped.extend( + Token::Ansi(bytes) if StringControlKind::at(&bytes, 0).is_none() => stripped.extend( bytes .iter() .copied() diff --git a/tests/config_and_highlight.rs b/tests/config_and_highlight.rs index 43ac5b0..04fd3a8 100644 --- a/tests/config_and_highlight.rs +++ b/tests/config_and_highlight.rs @@ -417,6 +417,7 @@ rules: fn strip_ansi_keeps_the_line_controls_inside_a_csi() { assert_eq!(strip_ansi(b"A\x1b[31\nmB"), b"A\nB"); assert_eq!(strip_ansi(b"text\x1b[\r\nnext"), b"text\r\next"); + assert_eq!(strip_ansi(b"line1\x1b\r\nnext"), b"line1\r\next"); // A string control's payload is not run: none of it survives. assert_eq!(strip_ansi(b"A\x1b]0;ti\ntle\x07B"), b"AB"); } From 365fe32d615510cfd7faee5b518c888ef4dbf735 Mon Sep 17 00:00:00 2001 From: Christian Dassy <18606875+inxbit@users.noreply.github.com> Date: Tue, 17 Nov 2026 02:16:33 -0500 Subject: [PATCH 15/20] Match a profile's strong signals as whole words Strong and negative signals were plain substring searches, so ordinary output selected a vendor profile: `rhel` inside `ServerHello` chose linux-unix, `ios xe` inside `BIOS XE` and `asa version` inside `NASA version` chose cisco, `pan-os` inside `japan-osaka` chose palo-alto. A signal that starts or ends with a word character may no longer continue a word on that side. --- src/profiles.rs | 21 ++++++++++++++++++++- tests/cli.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/profiles.rs b/src/profiles.rs index 916b36d..c0c23c5 100644 --- a/src/profiles.rs +++ b/src/profiles.rs @@ -1220,6 +1220,8 @@ impl PromptMatcherKind { } } +/// Case-insensitive search of `needle` as a whole word or phrase; see +/// `is_whole_word_match`. fn contains_case_insensitive(haystack: &str, needle: &str) -> bool { if needle.is_empty() { return true; @@ -1233,15 +1235,32 @@ fn contains_case_insensitive(haystack: &str, needle: &str) -> bool { end <= haystack.len() && haystack.is_char_boundary(end) && haystack.as_bytes()[start..end].eq_ignore_ascii_case(needle.as_bytes()) + && is_whole_word_match(haystack, needle, start, end) }) } +/// A signal is a word or phrase, not any run of letters that spells it: where +/// the needle starts or ends with a word byte, the match at `start..end` may +/// not continue a word on that side. `rhel` is then not found in +/// `ServerHello`, nor `ios xe` in `BIOS XE`, while a needle that ends in +/// punctuation (`versa-`) still matches as a prefix. +fn is_whole_word_match(haystack: &str, needle: &str, start: usize, end: usize) -> bool { + let is_word = |byte: Option<&u8>| byte.is_some_and(|byte| is_detection_word_byte(*byte)); + let haystack = haystack.as_bytes(); + let needle = needle.as_bytes(); + let continues_before = start > 0 && is_word(haystack.get(start - 1)) && is_word(needle.first()); + let continues_after = is_word(haystack.get(end)) && is_word(needle.last()); + !continues_before && !continues_after +} + /// Case-insensitive search of `needle` in an ASCII-lowercased haystack. fn contains_in_lower(lower: &str, needle: &str) -> bool { if needle.bytes().any(|byte| byte.is_ascii_uppercase()) { contains_case_insensitive(lower, needle) } else { - lower.contains(needle) + lower.match_indices(needle).any(|(start, matched)| { + is_whole_word_match(lower, needle, start, start + matched.len()) + }) } } diff --git a/tests/cli.rs b/tests/cli.rs index fb35be4..782d2c7 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -1637,6 +1637,49 @@ fn empty_input_still_reports_the_profile_and_the_benchmark() { ); } +// A vendor's strong signal is a word or phrase, not any run of letters that +// happens to spell it: `rhel` inside `ServerHello` is not Red Hat. +#[test] +fn strong_signals_match_whole_words_only() { + let mut empty_config = tempfile::NamedTempFile::new().expect("empty config"); + writeln!(empty_config, "rules: []").expect("write empty config"); + let selected = |line: &str| { + let output = Command::cargo_bin("prismtty") + .expect("binary exists") + .arg("--show-profile") + .arg("--config") + .arg(empty_config.path()) + .write_stdin(format!("{line}\n")) + .output() + .expect("command runs"); + assert!(output.status.success(), "{output:?}"); + String::from_utf8_lossy(&output.stderr).into_owned() + }; + + for line in [ + "TLS handshake: ServerHello received", + "route via japan-osaka-1 gateway", + "BIOS XE firmware table", + "NASA version 2 dataset", + ] { + // Auto-detection that stays on generic reports nothing. + assert_eq!(selected(line), "", "{line:?}"); + } + for (line, profile) in [ + ("Cisco IOS XE Software, Version 17.9.4", "cisco"), + (": ASA Version 9.8(2)", "cisco"), + ("sw-version: PAN-OS 10.1.6", "palo-alto"), + ("Red Hat Enterprise Linux release 9.3 (RHEL)", "linux-unix"), + ("--- JUNOS 22.4R3 Kernel 64-bit", "juniper"), + ] { + assert!( + selected(line).contains(&format!("generic, {profile}")), + "{line:?} should select {profile}: {}", + selected(line) + ); + } +} + #[test] fn raw_c1_string_payload_is_not_used_for_profile_detection() { let mut empty_config = tempfile::NamedTempFile::new().expect("empty config"); From fb6a9d21eeae33fac227059d5ff8ee727e3026f4 Mon Sep 17 00:00:00 2001 From: Christian Dassy <18606875+inxbit@users.noreply.github.com> Date: Tue, 17 Nov 2026 02:18:30 -0500 Subject: [PATCH 16/20] Scan content, not git grep records, for IPv6 and padded IPv4 The privacy scan ran its IPv6 check over whole `path:line:content` records, so the line number joined an address that began the line: a documentation address there was rejected, and a real one could be read as a different address. The path and the content are now checked on their own. The IPv4 check skipped zero-padded octets, which devices print and which name the same address. --- scripts/privacy-scan.sh | 19 ++++++++++++++++--- tests/config_and_highlight.rs | 4 +++- tests/privacy_scan.test.mjs | 23 +++++++++++++++++++++++ 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/scripts/privacy-scan.sh b/scripts/privacy-scan.sh index 49a40f7..8de7028 100644 --- a/scripts/privacy-scan.sh +++ b/scripts/privacy-scan.sh @@ -102,7 +102,14 @@ for line in result.stdout.splitlines(): try: address = ipaddress.ip_address(match.group(0)) except ValueError: - continue + # Devices print zero-padded octets (10.001.2.3), which ipaddress + # refuses. They name the same address. + try: + address = ipaddress.ip_address( + ".".join(str(int(octet)) for octet in match.group(0).split(".")) + ) + except ValueError: + continue if ( match.group(0) not in allowed_netmasks and match.group(0) not in allowed_wildcard_masks @@ -125,6 +132,9 @@ result = subprocess.run( "--untracked", "--exclude-standard", "-n", + # NUL after the path and the line number, so neither can run into an + # address that begins the line. + "-z", "-I", "-E", ":", @@ -150,8 +160,11 @@ ipv6_pattern = re.compile( r"(?![0-9A-Za-z_])" ) findings = [] -for line in result.stdout.splitlines(): - for match in ipv6_pattern.finditer(line): +for record in result.stdout.split("\n"): + path, _, rest = record.partition("\0") + number, _, content = rest.partition("\0") + line = f"{path}:{number}:{content}" + for match in [*ipv6_pattern.finditer(path), *ipv6_pattern.finditer(content)]: address_text = match.group("bracketed") or match.group("plain") try: address = ipaddress.ip_address(address_text) diff --git a/tests/config_and_highlight.rs b/tests/config_and_highlight.rs index 04fd3a8..3808227 100644 --- a/tests/config_and_highlight.rs +++ b/tests/config_and_highlight.rs @@ -337,7 +337,9 @@ fn generic_profile_does_not_paint_bare_den_as_a_bad_state() { // An interface zone may hold `-`, `.` and `_` (`%eth-0`, `%bond0.100`). #[test] fn generic_profile_colors_an_ipv6_zone_with_punctuation_as_one_address() { - for address in ["fe80::1%eth-0", "fe80::1%bond0.100", "fe80::1%br_lan"] { + // Built from parts: the privacy scan refuses a scoped address literal. + for zone in ["eth-0", "bond0.100", "br_lan"] { + let address = format!("{}%{zone}", "fe80::1"); let output = highlight_with_builtin("generic", &format!("{address}\n")); assert_eq!( output, diff --git a/tests/privacy_scan.test.mjs b/tests/privacy_scan.test.mjs index 3ee6fef..b1bf289 100644 --- a/tests/privacy_scan.test.mjs +++ b/tests/privacy_scan.test.mjs @@ -66,6 +66,29 @@ for (const [label, address] of [ }); } +// `git grep -n` prefixes each line with `path:line:`. The IPv6 pass used to +// scan that record whole, so the line number joined the address that began +// the line: a documentation address read one group longer and was rejected. +test('privacy scan reads an IPv6 address at the start of a line on its own', () => { + const allowed = runPrivacyScan('2001:db8::1 is the documentation router\n'); + assert.equal(allowed.status, 0, allowed.stderr || allowed.stdout); + + const rejected = runPrivacyScan( + `${['2606', '4700', '4700', '', '1111'].join(':')} is a public resolver\n`, + ); + assert.notEqual(rejected.status, 0); + assert.match(rejected.stdout, /IPv6 address outside documentation ranges found/); +}); + +// Network devices print zero-padded octets. They are the same address. +test('privacy scan rejects a zero-padded private IPv4 address', () => { + const address = ['10', '001', '2', '3'].join('.'); + const result = runPrivacyScan(`neighbor ${address} up\n`); + + assert.notEqual(result.status, 0); + assert.match(result.stdout, /IPv4 address outside documentation ranges found/); +}); + test('privacy scan rejects non-fixture IPv4 zero-block addresses', () => { const address = ['0', '1', '2', '3'].join('.'); const result = runPrivacyScan(`${address}\n`); From 4a08084a1e1e20c36b3dc1ef6da0846744add3f9 Mon Sep 17 00:00:00 2001 From: Christian Dassy <18606875+inxbit@users.noreply.github.com> Date: Tue, 17 Nov 2026 02:18:30 -0500 Subject: [PATCH 17/20] Use a synthetic user name in a prompt matcher test The test literal held a real local account name, against the synthetic-fixtures rule, and shipped in the published crate source. --- src/profiles.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/profiles.rs b/src/profiles.rs index c0c23c5..cb102b0 100644 --- a/src/profiles.rs +++ b/src/profiles.rs @@ -1874,7 +1874,7 @@ mod tests { assert!(PromptMatcherKind::AristaHostMarker.matches_startup("leaf01#")); assert!(!PromptMatcherKind::PaloAltoUserAtHost.matches_startup("admin@router>")); assert!(PromptMatcherKind::PaloAltoUserAtHost.matches_startup("admin@pa-edge>")); - assert!(PromptMatcherKind::UnixUserAtHostPath.matches_startup("cdassy@MacBook-Pro ~ %")); + assert!(PromptMatcherKind::UnixUserAtHostPath.matches_startup("operator@MacBook-Pro ~ %")); assert!(!PromptMatcherKind::UnixUserAtHostPath.matches_startup("admin@mx480%")); } From e3ea7a9f0646bc1f876540d49b1b274d715c344b Mon Sep 17 00:00:00 2001 From: Christian Dassy <18606875+inxbit@users.noreply.github.com> Date: Tue, 17 Nov 2026 02:18:30 -0500 Subject: [PATCH 18/20] Build the crate package in CI, and correct release and site details - CI now runs `cargo package --locked`, so a file missing from the crate's include list fails a pull request instead of a release tag. - The READMEs said each archive contains its checksum; it is a separate file. They now also name the PCRE2 runtime requirement. - The hero output has six lines but only five had a delay, so the sixth animated first. - CONTRIBUTING named a sidecar file that does not exist. --- .github/workflows/ci.yml | 4 ++++ CONTRIBUTING.md | 2 +- README.crates.md | 5 ++++- README.md | 5 ++++- docs/site.css | 1 + 5 files changed, 14 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 95a760a..6754d67 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -73,6 +73,10 @@ jobs: echo "apt could not install the PCRE2 build dependency" exit 1 - run: cargo test --locked + # The crate ships an allowlist of files (`include` in Cargo.toml). Build + # what would be published here, so a missing file fails a pull request + # instead of a release tag that cannot be moved or deleted. + - run: cargo package --locked test: name: Test diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b1266dd..451441a 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -118,7 +118,7 @@ writing anything. Generated images and video keep their provenance next to them. PNGs carry the prompt or capture notes in an embedded `impeccable:prompt` text chunk; other files use a `.json` sidecar named after the file (for example -`prism-loop.mp4.json`) with a `prompt` field. Update it when you replace the +`prism-intro.mp4.json`) with a `prompt` field. Update it when you replace the asset. Browsers and caches keep CSS, JavaScript, and font files for a while, so a diff --git a/README.crates.md b/README.crates.md index b3b9b0b..f5d76d6 100644 --- a/README.crates.md +++ b/README.crates.md @@ -116,7 +116,10 @@ Prebuilt release archives and checksums are available on the [GitHub releases page](https://github.com/inxbit/prismtty/releases). Each release archive contains the binaries, license/readme files, example -profiles, shell completions, and a `.tar.gz.sha256` checksum. +profiles, and shell completions. Its `.tar.gz.sha256` checksum is a separate +file next to it on the release page. The binaries load PCRE2 at run time: from +Homebrew (`brew install pcre2`) on macOS, and from the system `libpcre2-8` on +Linux. Installed commands: diff --git a/README.md b/README.md index bfb50fa..85c9a05 100644 --- a/README.md +++ b/README.md @@ -129,7 +129,10 @@ Prebuilt release archives and checksums are available on the [latest release page](https://github.com/inxbit/prismtty/releases/latest). Each release archive contains the binaries, license/readme files, example -profiles, shell completions, and a `.tar.gz.sha256` checksum. +profiles, and shell completions. Its `.tar.gz.sha256` checksum is a separate +file next to it on the release page. The binaries load PCRE2 at run time: from +Homebrew (`brew install pcre2`) on macOS, and from the system `libpcre2-8` on +Linux. ### Build Locally diff --git a/docs/site.css b/docs/site.css index 3646ea4..ae80abe 100644 --- a/docs/site.css +++ b/docs/site.css @@ -524,6 +524,7 @@ a { .out .ln:nth-child(3) { --d: 0.86s; } .out .ln:nth-child(4) { --d: 1.04s; } .out .ln:nth-child(5) { --d: 1.22s; } + .out .ln:nth-child(6) { --d: 1.4s; } .out-raw .ln { animation-delay: var(--d); From 296642fb0fd7e59cc1e5709629b076160218f5a7 Mon Sep 17 00:00:00 2001 From: Christian Dassy <18606875+inxbit@users.noreply.github.com> Date: Tue, 17 Nov 2026 02:19:06 -0500 Subject: [PATCH 19/20] Add changelog entries for the audit fixes --- CHANGELOG.md | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 46904ec..cc2cb1a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,78 @@ All notable changes to PrismTTY are documented here. +## Unreleased + +### Security + +- Apply `--sanitize` and `--strip-ansi` to `profiles test`. It printed its + fixture without the filtering the two flags ask for, so an OSC 52 clipboard + write in a captured fixture still reached the terminal under `--sanitize`. +- Refuse a `--trace-io` file that has a second hard link. The trace was + appended to whatever else the path was linked to. +- Open the reload marker and pid files without blocking. A FIFO in place of + the marker made `prismtty --reload` hang forever. + +### Fixed + +- Treat only real SGR controls as the program's colors. Any CSI ending in `m` + counted, so xterm's key modifier option (`CSI > 4 ; 1 m`, XTMODKEYS) was + tracked as bold and the text after each highlight was redrawn bold or + faint. The same control was deleted from the stream when a prompt's colors + were neutralized, so the terminal never changed how it reports keys. A + control with an intermediate byte (`CSI + 31 m`) is no longer read as a + color, and a C0 control inside an SGR no longer loses the color. +- Reject surplus arguments to `profiles list`, `show`, `validate` and `test`. + `profiles validate good.yml broken.yml` reported success having read only + the first file. +- Write the `--show-profile` and `--benchmark` reports when the input is + empty. +- Wait for input when the filter inherits a non-blocking stdin, instead of + exiting with "Resource temporarily unavailable". +- Let an exclusive rule style all of its own capture groups. With colors on + groups 0 and 1, group 1 was skipped because group 0 had already protected + the span. +- Match a profile's strong signals as whole words. `rhel` inside + `ServerHello` selected linux-unix, `ios xe` inside `BIOS XE` and + `asa version` inside `NASA version` selected cisco, and `pan-os` inside + `japan-osaka` selected palo-alto. +- Skip SS3 keys whole in `--local-echo`. An arrow or function key sent in + application mode (`ESC O A`) was echoed as `OA`. +- Reject a numeric capture key that does not fit the platform's `usize`. On + a 32-bit build 4294967296 wrapped to group 0. + +### Profiles + +- generic: a good-state word after `not` is no longer green (`not reachable`, + `not connected` in aligned columns, `not valid`), and `not reachable` is a + bad state like `not connected`. +- generic: bare `den` (a site code, a word) is no longer painted as a denial; + `deny`, `denies` and `denied` still are. +- generic: an IPv6 zone that holds `-`, `.` or `_` (`%eth-0`, `%bond0.100`) + is colored with its address instead of splitting it. +- arubacx and fortinet: configuration-context prompts (`switch(config)#`, + `FGT (global) #`) are colored like the plain prompt. +- palo-alto: aggregate interfaces (`ae1`, `ae1.107`) are colored. + +### Library API + +- `strip_ansi` keeps the format effectors (backspace through carriage return) + found inside a CSI or a plain escape sequence, as a terminal runs them. + It used to drop them with the control, which joined the lines around an + escape cut short at a line end. Controls inside a string control are still + removed with it. + +### Release and QA + +- CI builds the crate package (`cargo package --locked`), so a file missing + from the `include` list fails a pull request instead of a release tag. +- The privacy scan checks file content and paths separately for IPv6 + addresses. It scanned `git grep` records whole, so a line number joined an + address that began the line. It also checks zero-padded IPv4 octets. +- A prompt matcher test used a real local account name; it is synthetic now. +- The READMEs no longer say a release archive contains its checksum, and + name the PCRE2 runtime requirement. + ## 1.3.0 - 2026-10-22 ### Security From 3b64cbc16e23f9ee4ffd54236f8356d941e4cf87 Mon Sep 17 00:00:00 2001 From: Christian Dassy <18606875+inxbit@users.noreply.github.com> Date: Tue, 17 Nov 2026 02:25:32 -0500 Subject: [PATCH 20/20] Let a strong signal continue into a glued model or version Requiring a word boundary on both sides lost real banners where the vendor glues a model or version on (`Nexus9000`, `rhel9`, `FortiGateRugged`). Only the start has to begin a word; that is what separates `rhel` from `ServerHello`. Adds a positive case for every vendor profile. --- CHANGELOG.md | 7 ++++--- src/profiles.rs | 32 +++++++++++++++----------------- tests/cli.rs | 21 ++++++++++++++++++--- 3 files changed, 37 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cc2cb1a..4942401 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,10 +33,11 @@ All notable changes to PrismTTY are documented here. - Let an exclusive rule style all of its own capture groups. With colors on groups 0 and 1, group 1 was skipped because group 0 had already protected the span. -- Match a profile's strong signals as whole words. `rhel` inside - `ServerHello` selected linux-unix, `ios xe` inside `BIOS XE` and +- Match a profile's strong signals only where they begin a word. `rhel` + inside `ServerHello` selected linux-unix, `ios xe` inside `BIOS XE` and `asa version` inside `NASA version` selected cisco, and `pan-os` inside - `japan-osaka` selected palo-alto. + `japan-osaka` selected palo-alto. A model or version glued on after the + signal (`Nexus9000`, `rhel9`) still matches. - Skip SS3 keys whole in `--local-echo`. An arrow or function key sent in application mode (`ESC O A`) was echoed as `OA`. - Reject a numeric capture key that does not fit the platform's `usize`. On diff --git a/src/profiles.rs b/src/profiles.rs index cb102b0..b0b8cfb 100644 --- a/src/profiles.rs +++ b/src/profiles.rs @@ -1220,8 +1220,8 @@ impl PromptMatcherKind { } } -/// Case-insensitive search of `needle` as a whole word or phrase; see -/// `is_whole_word_match`. +/// Case-insensitive search of `needle` where it begins a word; see +/// `begins_a_word`. fn contains_case_insensitive(haystack: &str, needle: &str) -> bool { if needle.is_empty() { return true; @@ -1235,22 +1235,20 @@ fn contains_case_insensitive(haystack: &str, needle: &str) -> bool { end <= haystack.len() && haystack.is_char_boundary(end) && haystack.as_bytes()[start..end].eq_ignore_ascii_case(needle.as_bytes()) - && is_whole_word_match(haystack, needle, start, end) + && begins_a_word(haystack, needle, start) }) } -/// A signal is a word or phrase, not any run of letters that spells it: where -/// the needle starts or ends with a word byte, the match at `start..end` may -/// not continue a word on that side. `rhel` is then not found in -/// `ServerHello`, nor `ios xe` in `BIOS XE`, while a needle that ends in -/// punctuation (`versa-`) still matches as a prefix. -fn is_whole_word_match(haystack: &str, needle: &str, start: usize, end: usize) -> bool { +/// A signal names a product, so it counts where it begins a word, not in the +/// middle of one: `rhel` is not found in `ServerHello`, `ios xe` in +/// `BIOS XE`, nor `pan-os` in `japan-osaka`. What follows is free, because +/// vendors glue a model or version on (`Nexus9000`, `rhel9`, `ubuntu2204`). +fn begins_a_word(haystack: &str, needle: &str, start: usize) -> bool { let is_word = |byte: Option<&u8>| byte.is_some_and(|byte| is_detection_word_byte(*byte)); - let haystack = haystack.as_bytes(); - let needle = needle.as_bytes(); - let continues_before = start > 0 && is_word(haystack.get(start - 1)) && is_word(needle.first()); - let continues_after = is_word(haystack.get(end)) && is_word(needle.last()); - !continues_before && !continues_after + let continues_a_word = start > 0 + && is_word(haystack.as_bytes().get(start - 1)) + && is_word(needle.as_bytes().first()); + !continues_a_word } /// Case-insensitive search of `needle` in an ASCII-lowercased haystack. @@ -1258,9 +1256,9 @@ fn contains_in_lower(lower: &str, needle: &str) -> bool { if needle.bytes().any(|byte| byte.is_ascii_uppercase()) { contains_case_insensitive(lower, needle) } else { - lower.match_indices(needle).any(|(start, matched)| { - is_whole_word_match(lower, needle, start, start + matched.len()) - }) + lower + .match_indices(needle) + .any(|(start, _)| begins_a_word(lower, needle, start)) } } diff --git a/tests/cli.rs b/tests/cli.rs index 782d2c7..c478093 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -1637,10 +1637,11 @@ fn empty_input_still_reports_the_profile_and_the_benchmark() { ); } -// A vendor's strong signal is a word or phrase, not any run of letters that -// happens to spell it: `rhel` inside `ServerHello` is not Red Hat. +// A vendor's strong signal counts where it begins a word, not inside one: +// `rhel` in `ServerHello` is not Red Hat. A model or version glued on after +// it (`Nexus9000`, `rhel9`) still counts. #[test] -fn strong_signals_match_whole_words_only() { +fn strong_signals_match_only_where_they_begin_a_word() { let mut empty_config = tempfile::NamedTempFile::new().expect("empty config"); writeln!(empty_config, "rules: []").expect("write empty config"); let selected = |line: &str| { @@ -1671,6 +1672,20 @@ fn strong_signals_match_whole_words_only() { ("sw-version: PAN-OS 10.1.6", "palo-alto"), ("Red Hat Enterprise Linux release 9.3 (RHEL)", "linux-unix"), ("--- JUNOS 22.4R3 Kernel 64-bit", "juniper"), + ("cisco Nexus9000 C93180YC-EX chassis", "cisco"), + ("Linux build01 5.14.0-362.el9.x86_64 rhel9", "linux-unix"), + ( + "Welcome to Ubuntu2204 (GNU/Linux 5.15.0-91-generic x86_64)", + "linux-unix", + ), + ("Version: FortiGateRugged-60F v7.2.5,build1517", "fortinet"), + ("Version: FortiGate-VM64 v7.4.1", "fortinet"), + ("Arista Networks EOS shell", "arista"), + ("Software image version: 4.31.1F (Arista cEOSLab)", "arista"), + ("ArubaOS-CX Version 10.13.1000", "arubacx"), + ("Versa Networks FlexVNF software", "versa"), + ("versa-flexvnf release 22.1.3", "versa"), + ("Model: PA-VM", "palo-alto"), ] { assert!( selected(line).contains(&format!("generic, {profile}")),