diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 6c93c73..36725dc 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -2,6 +2,30 @@ === [Unreleased] +==== Fixed — the shell `/tmp/` rule ignores comments and reports a line + +* `+analyze_shell+` raised "Hardcoded /tmp/ path without mktemp" on a +whole-file substring match, so a `+/tmp/+` mentioned only in a `+#+` comment +fired, and the word `+mktemp+` in a comment silenced a real `+/tmp/+` use. Both +tests now look only at code: full-line comments and trailing `+ #+` comments +outside quotes are skipped. A `+#+` starts a comment only at an unquoted word +boundary (line start, after whitespace, or after an operator such as `+;+`), so +`+#+` inside quotes (including ANSI-C `+$'...\'...'+`), `+$#+`, `+${#var}+`, after an +escaped space and in heredoc bodies is kept as code, and any doubt still fires +the rule. Here-strings (`+<<<+`) are not heredocs, and heredoc delimiters are +parsed whole (`+'---'+`, `+EOF-TEXT+`). The finding carries the first offending +line number; findings from embedded scripts carry none, because their lines are +relative to the extracted block. Word state carries across line breaks inside a +quote and over a backslash-newline continuation, so `+"ab"# /tmp/x+` +is not mistaken for a comment. + +==== Fixed — readiness tests find the binary under Cargo's split build directory + +* `+tests/readiness.rs+` derived the binary path from `+current_exe+`, which +fails when Cargo places test binaries under `+build///out/+` (15 of +21 tests failed in CI). It now uses `+CARGO_BIN_EXE_panic-attack+`. + + ==== Fixed — the Idris2 ABI proofs are type-checked, and the stripping proof is re-proved * No CI job had ever type-checked `+src/abi/+`, and as committed none of its diff --git a/src/assail/analyzer.rs b/src/assail/analyzer.rs index 6698052..7c376d6 100644 --- a/src/assail/analyzer.rs +++ b/src/assail/analyzer.rs @@ -320,6 +320,264 @@ pub struct Analyzer { browser_extension: bool, } +/// Quote state tracked by [`shell_code_lines`]. +#[derive(Clone, Copy, PartialEq, Eq)] +enum ShellQuote { + None, + /// `'...'`: no escapes. + Single, + /// `"..."`: backslash escapes the next character. + Double, + /// `$'...'`: backslash escapes the next character, including `\'`. + AnsiC, +} + +/// True for the shell operator characters that end a word. +fn is_shell_operator(c: char) -> bool { + matches!(c, ';' | '&' | '|' | '(' | ')' | '<' | '>') +} + +/// Line-by-line shell scanner behind [`shell_code_lines`]. All state that +/// survives a line break lives here: the open quote, pending heredoc +/// delimiters, and whether the next character begins a word. +struct ShellScanner { + quote: ShellQuote, + /// Pending heredoc delimiters: `(tag, strip_leading_tabs)`. + heredocs: std::collections::VecDeque<(String, bool)>, + /// True when the next character begins a shell word. Carried across line + /// breaks inside a quote and over a backslash-newline continuation. + word_start: bool, + /// The previous line ended in an unescaped backslash outside any quote. + continued: bool, +} + +impl ShellScanner { + fn new() -> Self { + Self { + quote: ShellQuote::None, + heredocs: Default::default(), + word_start: true, + continued: false, + } + } + + /// If `line` is part of a heredoc body, consume it (closing the heredoc + /// when it is the delimiter) and return true. + fn heredoc_body_line(&mut self, line: &str) -> bool { + if self.quote != ShellQuote::None { + return false; + } + let Some((tag, strip_tabs)) = self.heredocs.front() else { + return false; + }; + let candidate = if *strip_tabs { + line.trim_start_matches('\t') + } else { + line + }; + if candidate == tag.as_str() { + self.heredocs.pop_front(); + } + true + } + + /// Scan one non-heredoc line, returning it with any comment removed. + fn scan_line(&mut self, line: &str) -> String { + let chars: Vec = line.chars().collect(); + let mut code = String::new(); + // A new line starts a new word, unless we are inside a quote or the + // previous line ended in a backslash continuation. + if self.quote == ShellQuote::None && !self.continued { + self.word_start = true; + } + self.continued = false; + let mut i = 0; + while i < chars.len() { + let c = chars[i]; + let stop = match self.quote { + ShellQuote::Single => { + self.step_single(c, &mut code); + false + } + ShellQuote::Double | ShellQuote::AnsiC => { + self.step_escaping_quote(&chars, &mut i, &mut code); + false + } + ShellQuote::None => self.step_unquoted(&chars, &mut i, &mut code), + }; + if stop { + break; + } + i += 1; + } + code + } + + /// Inside `'...'`: no escapes, only the closing quote matters. + fn step_single(&mut self, c: char, code: &mut String) { + if c == '\'' { + self.quote = ShellQuote::None; + } + code.push(c); + } + + /// Inside `"..."` or `$'...'`: a backslash escapes the next character. + fn step_escaping_quote(&mut self, chars: &[char], i: &mut usize, code: &mut String) { + let c = chars[*i]; + if c == '\\' && *i + 1 < chars.len() { + code.push(c); + *i += 1; + code.push(chars[*i]); + return; + } + let closer = if self.quote == ShellQuote::Double { + '"' + } else { + '\'' + }; + if c == closer { + self.quote = ShellQuote::None; + } + code.push(c); + } + + /// Unquoted character at `chars[*i]`. Returns true when a comment starts + /// and the rest of the line must be dropped. + fn step_unquoted(&mut self, chars: &[char], i: &mut usize, code: &mut String) -> bool { + let c = chars[*i]; + if c == '\\' { + self.step_backslash(chars, i, code); + } else if c == '$' && chars.get(*i + 1) == Some(&'\'') { + self.open_quote(ShellQuote::AnsiC, code); + code.push('\''); + *i += 1; + } else if c == '\'' || c == '"' { + let q = if c == '\'' { + ShellQuote::Single + } else { + ShellQuote::Double + }; + self.open_quote(q, code); + } else if c == '#' && self.word_start { + return true; + } else if c == '<' && chars.get(*i + 1) == Some(&'<') { + self.step_heredoc_or_here_string(chars, i, code); + } else { + code.push(c); + self.word_start = c.is_whitespace() || is_shell_operator(c); + } + false + } + + /// Open a quote; the opening delimiter itself is pushed by the caller for + /// `$'` and here for plain quotes. + fn open_quote(&mut self, quote: ShellQuote, code: &mut String) { + self.quote = quote; + match quote { + ShellQuote::Single => code.push('\''), + ShellQuote::Double => code.push('"'), + ShellQuote::AnsiC => code.push('$'), + ShellQuote::None => {} + } + self.word_start = false; + } + + /// Unquoted backslash: escapes the next character, or, at end of line, + /// is a line continuation that leaves the word state untouched. + fn step_backslash(&mut self, chars: &[char], i: &mut usize, code: &mut String) { + code.push('\\'); + if *i + 1 < chars.len() { + *i += 1; + code.push(chars[*i]); + self.word_start = false; + } else { + self.continued = true; + } + } + + /// `<<<` here-string (consumed whole) or `<<[-] WORD` heredoc introducer. + fn step_heredoc_or_here_string(&mut self, chars: &[char], i: &mut usize, code: &mut String) { + if chars.get(*i + 2) == Some(&'<') { + code.push_str("<<<"); + *i += 2; + } else { + let (end, tag, strip_tabs) = parse_heredoc_delimiter(chars, *i + 2); + if !tag.is_empty() { + self.heredocs.push_back((tag, strip_tabs)); + } + // Delimiter text is plain code; resume after it. + code.push_str("<<"); + code.extend(&chars[*i + 2..end]); + *i = end.saturating_sub(1).max(*i + 1); + } + self.word_start = true; + } +} + +/// Parse a heredoc delimiter starting just after `<<` at `chars[from]`. +/// Returns `(end index, tag, strip_tabs)`. Quoted runs contribute their +/// contents literally; a backslash escapes the next character. +fn parse_heredoc_delimiter(chars: &[char], from: usize) -> (usize, String, bool) { + let mut j = from; + let strip_tabs = chars.get(j) == Some(&'-'); + if strip_tabs { + j += 1; + } + while chars.get(j).is_some_and(|ch| *ch == ' ' || *ch == '\t') { + j += 1; + } + let start = j; + let mut tag = String::new(); + while let Some(&ch) = chars.get(j) { + if ch.is_whitespace() || is_shell_operator(ch) { + break; + } + match ch { + '\'' | '"' => { + let mut k = j + 1; + while k < chars.len() && chars[k] != ch { + tag.push(chars[k]); + k += 1; + } + j = (k + 1).min(chars.len()); + } + '\\' if j + 1 < chars.len() => { + tag.push(chars[j + 1]); + j += 2; + } + _ => { + tag.push(ch); + j += 1; + } + } + } + (j.max(start), tag, strip_tabs) +} + +/// Split shell `content` into `(1-based line number, code text)` pairs with +/// comments removed. A `#` starts a comment only when it is unquoted and +/// begins a word: at line start, after unescaped whitespace, or after an +/// operator such as `;`. So `$#`, `${#var}`, `name\ #x` and `#` inside quotes +/// (including `$'...\'...'`) are kept. Quote and word state carry across +/// lines (so `"ab"# x` and `foo\#x` are not comments). +/// Heredoc bodies are returned verbatim as code; a here-string `<<<` is +/// consumed whole and never opens a heredoc. Conservative by design: any +/// doubt leaves text in place as code so rules still fire. +fn shell_code_lines(content: &str) -> Vec<(usize, String)> { + let mut scanner = ShellScanner::new(); + content + .lines() + .enumerate() + .map(|(idx, line)| { + if scanner.heredoc_body_line(line) { + (idx + 1, line.to_string()) + } else { + (idx + 1, scanner.scan_line(line)) + } + }) + .collect() +} + impl Analyzer { pub fn new(target: &Path) -> Result { Self::build(target, false, false) @@ -566,6 +824,9 @@ impl Analyzer { &rel_path, )?; for finding in &mut file_weak_points[start..] { + // Lines are relative to the extracted block, not the + // enclosing file; do not report a misleading one. + finding.line = None; finding.description.push_str(&format!( " (embedded {:?} input at {})", script.language, script.origin @@ -4708,11 +4969,19 @@ impl Analyzer { }); } - // Unsafe temp files - if content.contains("/tmp/") && !content.contains("mktemp") { + // Unsafe temp files. Comments are ignored for both the `/tmp/` and the + // `mktemp` test (see `shell_code_lines`); the first offending code line + // is reported. + let code_lines = shell_code_lines(content); + let first_tmp_line = code_lines + .iter() + .find(|(_, code)| code.contains("/tmp/")) + .map(|(n, _)| *n); + let has_mktemp = code_lines.iter().any(|(_, code)| code.contains("mktemp")); + if let (Some(tmp_line), false) = (first_tmp_line, has_mktemp) { weak_points.push(WeakPoint { file: None, - line: None, + line: u32::try_from(tmp_line).ok(), category: WeakPointCategory::PathTraversal, location: Some(file_path.to_string()), severity: Severity::Medium, @@ -6361,6 +6630,162 @@ mod tests { use std::fs; use tempfile::TempDir; + // --------------------------------------------------------------- + // Shell /tmp rule is comment-aware + // --------------------------------------------------------------- + + fn shell_tmp_findings(content: &str) -> Vec { + let analyzer = Analyzer::new(std::path::Path::new(".")).expect("analyzer construction"); + let mut stats = ProgramStatistics::default(); + let mut wp = Vec::new(); + analyzer + .analyze_shell(content, &mut stats, &mut wp, "fixture.sh") + .expect("analyze_shell"); + wp.into_iter() + .filter(|w| matches!(w.category, WeakPointCategory::PathTraversal)) + .collect() + } + + #[test] + fn shell_tmp_in_comment_only_is_not_flagged() { + let src = "#!/bin/sh\n# In memory, not /tmp/mint-tokens-answers.$$: keep it\necho ok # see /tmp/x\n"; + assert!(shell_tmp_findings(src).is_empty()); + } + + #[test] + fn shell_tmp_in_code_is_flagged_with_line() { + let src = "#!/bin/sh\necho hi\nrm -f /tmp/foo\n"; + let f = shell_tmp_findings(src); + assert_eq!(f.len(), 1); + assert_eq!(f[0].line, Some(3)); + } + + #[test] + fn shell_mktemp_only_in_comment_does_not_silence() { + let src = "# use mktemp instead\nrm -f /tmp/foo\n"; + let f = shell_tmp_findings(src); + assert_eq!(f.len(), 1); + assert_eq!(f[0].line, Some(2)); + } + + #[test] + fn shell_mktemp_in_code_silences() { + assert!(shell_tmp_findings("t=$(mktemp /tmp/x.XXXXXX)\n").is_empty()); + } + + #[test] + fn shell_tmp_in_quoted_string_is_flagged() { + let f = shell_tmp_findings("echo \"# not a comment /tmp/x\"\n"); + assert_eq!(f.len(), 1); + assert_eq!(f[0].line, Some(1)); + } + + #[test] + fn shell_hash_inside_quotes_arg_does_not_hide_tmp() { + let f = shell_tmp_findings("echo \"#\" /tmp/x\n"); + assert_eq!(f.len(), 1); + assert_eq!(f[0].line, Some(1)); + } + + #[test] + fn shell_param_length_hash_is_not_a_comment() { + let f = shell_tmp_findings("n=${#v}; echo $# /tmp/x\n"); + assert_eq!(f.len(), 1); + } + + #[test] + fn shell_heredoc_body_is_kept_as_code() { + let src = "cat <#bar` joins into the single word `foo#bar`. + let f = shell_tmp_findings("echo foo\\\n# /tmp/x\n"); + assert_eq!(f.len(), 1); + assert_eq!(f[0].line, Some(2)); + // After whitespace the continuation still starts a new word: comment. + assert!(shell_tmp_findings("echo foo \\\n# /tmp/x\n").is_empty()); + } + #[test] + fn shell_heredoc_hyphenated_delimiter() { + let src = "cat < = report + .weak_points + .iter() + .filter(|w| w.description.contains("embedded") && w.description.contains("/tmp/")) + .collect(); + assert!(!embedded.is_empty(), "expected an embedded /tmp/ finding"); + assert!(embedded.iter().all(|w| w.line.is_none())); + } + // --------------------------------------------------------------- // 0b. JSON-LD / JSON-Schema identifier exemption (cross-lang URLs) // --------------------------------------------------------------- diff --git a/tests/readiness.rs b/tests/readiness.rs index 9de3355..8062f85 100644 --- a/tests/readiness.rs +++ b/tests/readiness.rs @@ -28,22 +28,11 @@ use tempfile::TempDir; /// Path to the compiled panic-attack binary. /// -/// This is structurally infallible in a cargo test context: `current_exe` -/// only fails if the OS cannot locate the test binary, which would mean -/// cargo test itself is broken. +/// Resolved through `CARGO_BIN_EXE_*`, which Cargo sets for integration tests. +/// Deriving it from `current_exe` broke when Cargo moved test binaries under +/// `target//build///out/`. fn binary() -> PathBuf { - let mut p = std::env::current_exe() - .expect("test runtime: current_exe should always succeed in cargo test"); - p.pop(); // remove test binary name - p.pop(); // remove deps/ - p.push("panic-attack"); - if !p.exists() { - p.pop(); - p.pop(); - p.push("release"); - p.push("panic-attack"); - } - p + PathBuf::from(env!("CARGO_BIN_EXE_panic-attack")) } /// Run a panic-attack subcommand and return (success, stdout, stderr).