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/CHANGELOG.md b/CHANGELOG.md index 46904ec..4942401 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,79 @@ 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 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. 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 + 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 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); 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/src/cli.rs b/src/cli.rs index 7c84c6a..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)?; @@ -252,14 +263,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/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/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(); 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() { 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/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)] 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 { diff --git a/src/highlight.rs b/src/highlight.rs index 1b9ce79..a8f32fb 100644 --- a/src/highlight.rs +++ b/src/highlight.rs @@ -1997,8 +1997,7 @@ fn visible_byte_map_and_ansi_ranges(input: &[u8]) -> (Vec, Vec= end || end > styles.len() { continue; @@ -2505,13 +2509,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. @@ -3343,14 +3350,9 @@ struct SgrRendition { impl SgrRendition { fn apply_sequence(&mut self, bytes: &[u8]) { - let Some(body_start) = csi_introducer_len(bytes) else { + let Some(body) = sgr_parameters(bytes) else { return; }; - if !bytes.ends_with(b"m") { - return; - } - - let body = &bytes[body_start..bytes.len() - 1]; if body.is_empty() { self.reset_all(); return; @@ -3601,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() } @@ -4406,12 +4428,23 @@ 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 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) { - if let Token::Text(text) = token { - stripped.extend(text); + match token { + Token::Text(text) => stripped.extend(text), + Token::Ansi(bytes) if StringControlKind::at(&bytes, 0).is_none() => stripped.extend( + bytes + .iter() + .copied() + .filter(|byte| matches!(byte, 0x08..=0x0d)), + ), + Token::Ansi(_) => {} } } stripped diff --git a/src/profiles.rs b/src/profiles.rs index 916b36d..b0b8cfb 100644 --- a/src/profiles.rs +++ b/src/profiles.rs @@ -1220,6 +1220,8 @@ impl PromptMatcherKind { } } +/// 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; @@ -1233,15 +1235,30 @@ 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()) + && begins_a_word(haystack, needle, start) }) } +/// 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 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. 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, _)| begins_a_word(lower, needle, start)) } } @@ -1855,7 +1872,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%")); } 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/cli.rs b/tests/cli.rs index 046a129..c478093 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") @@ -130,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"); @@ -1466,6 +1571,130 @@ 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] +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:?}" + ); +} + +// 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_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| { + 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"), + ("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}")), + "{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"); diff --git a/tests/config_and_highlight.rs b/tests/config_and_highlight.rs index 958a728..3808227 100644 --- a/tests/config_and_highlight.rs +++ b/tests/config_and_highlight.rs @@ -277,6 +277,153 @@ 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() { + // 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, + 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] +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. +#[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"); + 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"); +} + #[test] fn noninteractive_stream_restores_program_sgr_set_in_an_earlier_push() { let yaml = r##" @@ -334,6 +481,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 + // 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. + ("\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"); @@ -2249,6 +2403,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(); 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`);