From 011eef8f0e3dba1c19dbfbaf0f709d9eb5c40c0a Mon Sep 17 00:00:00 2001 From: CLAUDE <355856132+HoTT-stuff@users.noreply.github.com> Date: Thu, 12 Nov 2026 22:49:43 +0000 Subject: [PATCH 1/3] fix(assail): shell /tmp/ rule ignores comments and reports the line The rule matched /tmp/ and mktemp as whole-file substrings, so a /tmp/ mentioned only in a # comment fired, and mktemp in a comment silenced a real use. Both tests now run over comment-stripped code (quote-, $#-, ${#v}- and heredoc-aware; doubt is treated as code) and the finding carries the first offending line. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0136P7ZkERqsQWMogHRpM2z8 --- CHANGELOG.adoc | 11 +++ src/assail/analyzer.rs | 179 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 187 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 6c93c73..59cb7d9 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -2,6 +2,17 @@ === [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, while `+#+` inside quotes, `+$#+`, `+${#var}+` and +heredoc bodies are kept as code, so any doubt still fires the rule. The finding +now carries the first offending line number. + + ==== 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..6217d02 100644 --- a/src/assail/analyzer.rs +++ b/src/assail/analyzer.rs @@ -320,6 +320,100 @@ pub struct Analyzer { browser_extension: bool, } +/// Split shell `content` into `(1-based line number, code text)` pairs with +/// comments removed. A `#` starts a comment only when it is unquoted and at +/// the start of a word (line start or after whitespace), so `$#`, `${#var}` +/// and `#` inside quotes are kept. Quote state carries across lines, and +/// heredoc bodies are returned verbatim as code. 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 out = Vec::new(); + let mut quote: Option = None; + let mut heredocs: std::collections::VecDeque<(String, bool)> = Default::default(); + for (idx, line) in content.lines().enumerate() { + if quote.is_none() { + if let Some((tag, strip_tabs)) = heredocs.front() { + let candidate = if *strip_tabs { + line.trim_start_matches('\t') + } else { + line + }; + if candidate == tag.as_str() { + heredocs.pop_front(); + } + out.push((idx + 1, line.to_string())); + continue; + } + } + let chars: Vec = line.chars().collect(); + let mut code = String::new(); + let mut i = 0; + while i < chars.len() { + let c = chars[i]; + match quote { + Some(q) => { + if q == '"' && c == '\\' && i + 1 < chars.len() { + code.push(c); + i += 1; + code.push(chars[i]); + } else { + if c == q { + quote = None; + } + code.push(c); + } + } + None => { + if c == '\\' && i + 1 < chars.len() { + code.push(c); + i += 1; + code.push(chars[i]); + } else if c == '\'' || c == '"' { + quote = Some(c); + code.push(c); + } else if c == '#' && (i == 0 || chars[i - 1].is_whitespace()) { + break; + } else if c == '<' + && chars.get(i + 1) == Some(&'<') + && chars.get(i + 2) != Some(&'<') + { + // Heredoc introducer: <<[-] ['"]TAG['"] + let mut j = i + 2; + 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 q = chars.get(j).copied().filter(|ch| *ch == '\'' || *ch == '"'); + if q.is_some() { + j += 1; + } + let start = j; + while chars + .get(j) + .is_some_and(|ch| ch.is_alphanumeric() || *ch == '_') + { + j += 1; + } + if j > start { + heredocs.push_back((chars[start..j].iter().collect(), strip_tabs)); + } + code.push_str("<<"); + i += 1; + } else { + code.push(c); + } + } + } + i += 1; + } + out.push((idx + 1, code)); + } + out +} + impl Analyzer { pub fn new(target: &Path) -> Result { Self::build(target, false, false) @@ -4708,11 +4802,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 +6463,77 @@ 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 < Date: Fri, 13 Nov 2026 00:02:33 +0000 Subject: [PATCH 2/3] fix(assail): harden shell comment scanner; fix readiness binary lookup Review findings on the shell /tmp rule: - ANSI-C $'...' strings keep escaped \' (tracked as its own quote state) - '#' starts a comment only at an unquoted word boundary: not after an escaped space, but yes after an operator such as ';' - here-strings (<<<) are consumed whole and never open a heredoc - heredoc delimiters are parsed whole ('---', EOF-TEXT) - findings from embedded scripts carry line = None (block-relative lines would point at unrelated code in the enclosing file) Each has a unit test. CI: tests/readiness.rs derived the binary path from current_exe, which breaks when Cargo places test binaries under build///out/ (15 of 21 readiness tests failed; coverage and ledger run the same tests). Use CARGO_BIN_EXE_panic-attack instead. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0136P7ZkERqsQWMogHRpM2z8 --- CHANGELOG.adoc | 17 +++- src/assail/analyzer.rs | 219 +++++++++++++++++++++++++++++++++-------- tests/readiness.rs | 19 +--- 3 files changed, 195 insertions(+), 60 deletions(-) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 59cb7d9..6c24c61 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -8,9 +8,20 @@ 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, while `+#+` inside quotes, `+$#+`, `+${#var}+` and -heredoc bodies are kept as code, so any doubt still fires the rule. The finding -now carries the first offending line number. +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. + +==== 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 diff --git a/src/assail/analyzer.rs b/src/assail/analyzer.rs index 6217d02..b63f182 100644 --- a/src/assail/analyzer.rs +++ b/src/assail/analyzer.rs @@ -320,18 +320,37 @@ 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, ';' | '&' | '|' | '(' | ')' | '<' | '>') +} + /// Split shell `content` into `(1-based line number, code text)` pairs with -/// comments removed. A `#` starts a comment only when it is unquoted and at -/// the start of a word (line start or after whitespace), so `$#`, `${#var}` -/// and `#` inside quotes are kept. Quote state carries across lines, and -/// heredoc bodies are returned verbatim as code. Conservative by design: any +/// 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 state carries across lines. +/// 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 out = Vec::new(); - let mut quote: Option = None; + let mut quote = ShellQuote::None; let mut heredocs: std::collections::VecDeque<(String, bool)> = Default::default(); for (idx, line) in content.lines().enumerate() { - if quote.is_none() { + if quote == ShellQuote::None { if let Some((tag, strip_tabs)) = heredocs.front() { let candidate = if *strip_tabs { line.trim_start_matches('\t') @@ -347,63 +366,109 @@ fn shell_code_lines(content: &str) -> Vec<(usize, String)> { } let chars: Vec = line.chars().collect(); let mut code = String::new(); + let mut word_start = true; let mut i = 0; while i < chars.len() { let c = chars[i]; match quote { - Some(q) => { - if q == '"' && c == '\\' && i + 1 < chars.len() { + ShellQuote::Single => { + if c == '\'' { + quote = ShellQuote::None; + } + code.push(c); + } + ShellQuote::Double | ShellQuote::AnsiC => { + if c == '\\' && i + 1 < chars.len() { code.push(c); i += 1; code.push(chars[i]); } else { - if c == q { - quote = None; + let closer = if quote == ShellQuote::Double { + '"' + } else { + '\'' + }; + if c == closer { + quote = ShellQuote::None; } code.push(c); } } - None => { + ShellQuote::None => { if c == '\\' && i + 1 < chars.len() { code.push(c); i += 1; code.push(chars[i]); - } else if c == '\'' || c == '"' { - quote = Some(c); + word_start = false; + } else if c == '$' && chars.get(i + 1) == Some(&'\'') { + quote = ShellQuote::AnsiC; + code.push_str("$'"); + i += 1; + word_start = false; + } else if c == '\'' { + quote = ShellQuote::Single; + code.push(c); + word_start = false; + } else if c == '"' { + quote = ShellQuote::Double; code.push(c); - } else if c == '#' && (i == 0 || chars[i - 1].is_whitespace()) { + word_start = false; + } else if c == '#' && word_start { break; - } else if c == '<' - && chars.get(i + 1) == Some(&'<') - && chars.get(i + 2) != Some(&'<') - { - // Heredoc introducer: <<[-] ['"]TAG['"] - let mut j = i + 2; - 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 q = chars.get(j).copied().filter(|ch| *ch == '\'' || *ch == '"'); - if q.is_some() { - j += 1; - } - let start = j; - while chars - .get(j) - .is_some_and(|ch| ch.is_alphanumeric() || *ch == '_') - { - j += 1; - } - if j > start { - heredocs.push_back((chars[start..j].iter().collect(), strip_tabs)); + } else if c == '<' && chars.get(i + 1) == Some(&'<') { + if chars.get(i + 2) == Some(&'<') { + // Here-string: consume all three, no heredoc. + code.push_str("<<<"); + i += 2; + } else { + // Heredoc introducer: <<[-] WORD (quotes allowed). + let mut j = i + 2; + 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 { + '\'' | '"' => { + // Quoted run: contents literal, quotes removed. + 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; + } + } + } + if !tag.is_empty() { + heredocs.push_back((tag, strip_tabs)); + } + // Delimiter text is plain code; resume after it. + let end = j.max(start); + code.push_str("<<"); + code.extend(&chars[i + 2..end]); + i = end.saturating_sub(1).max(i + 1); } - code.push_str("<<"); - i += 1; + word_start = true; } else { code.push(c); + word_start = c.is_whitespace() || is_shell_operator(c); } } } @@ -660,6 +725,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 @@ -6534,6 +6602,73 @@ mod tests { assert_eq!(f[0].line, Some(2)); } + #[test] + fn shell_ansi_c_escaped_quote_keeps_hash_literal() { + let f = shell_tmp_findings("printf '%s\\n' $'it\\' # literal'; rm -f /tmp/x\n"); + assert_eq!(f.len(), 1); + assert_eq!(f[0].line, Some(1)); + } + + #[test] + fn shell_escaped_space_does_not_start_comment() { + let f = shell_tmp_findings("rm -f name\\ #suffix /tmp/x\n"); + assert_eq!(f.len(), 1); + } + + #[test] + fn shell_hash_after_operator_starts_comment() { + // `;#` begins a comment, so the `mktemp` inside it must not silence. + let f = shell_tmp_findings("rm -f /tmp/x;# mktemp\n"); + assert_eq!(f.len(), 1); + // And a `/tmp/` only inside such a comment is not flagged. + assert!(shell_tmp_findings("echo ok;# /tmp/x\n").is_empty()); + } + + #[test] + fn shell_here_string_is_not_a_heredoc() { + let f = shell_tmp_findings("cat <<< data\n# mktemp\nrm -f /tmp/x\n"); + assert_eq!(f.len(), 1); + assert_eq!(f[0].line, Some(3)); + } + + #[test] + fn shell_heredoc_quoted_punctuation_delimiter() { + let f = shell_tmp_findings("cat <<'---'\n# /tmp/x\n---\n"); + assert_eq!(f.len(), 1); + assert_eq!(f[0].line, Some(2)); + } + + #[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). From c5b48f0caa8273ef1c58f7fcaadca4284af62d63 Mon Sep 17 00:00:00 2001 From: CLAUDE <355856132+HoTT-stuff@users.noreply.github.com> Date: Fri, 13 Nov 2026 00:34:29 +0000 Subject: [PATCH 3/3] fix(assail): carry shell word state across lines; split shell_code_lines into ShellScanner A '#' directly after a multiline quoted word ("ab"# /tmp/x) or a backslash-newline continuation is part of the word, not a comment. Refactor the scanner into a ShellScanner struct with per-state methods (behaviour otherwise identical) to address the Sonar cognitive-complexity finding. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0136P7ZkERqsQWMogHRpM2z8 --- CHANGELOG.adoc | 4 +- src/assail/analyzer.rs | 369 +++++++++++++++++++++++++++-------------- 2 files changed, 246 insertions(+), 127 deletions(-) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 6c24c61..36725dc 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -15,7 +15,9 @@ 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. +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 diff --git a/src/assail/analyzer.rs b/src/assail/analyzer.rs index b63f182..7c376d6 100644 --- a/src/assail/analyzer.rs +++ b/src/assail/analyzer.rs @@ -337,146 +337,245 @@ fn is_shell_operator(c: char) -> bool { matches!(c, ';' | '&' | '|' | '(' | ')' | '<' | '>') } -/// 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 state carries across lines. -/// 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 out = Vec::new(); - let mut quote = ShellQuote::None; - let mut heredocs: std::collections::VecDeque<(String, bool)> = Default::default(); - for (idx, line) in content.lines().enumerate() { - if quote == ShellQuote::None { - if let Some((tag, strip_tabs)) = heredocs.front() { - let candidate = if *strip_tabs { - line.trim_start_matches('\t') - } else { - line - }; - if candidate == tag.as_str() { - heredocs.pop_front(); - } - out.push((idx + 1, line.to_string())); - continue; - } +/// 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(); - let mut word_start = true; + // 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]; - match quote { + let stop = match self.quote { ShellQuote::Single => { - if c == '\'' { - quote = ShellQuote::None; - } - code.push(c); + self.step_single(c, &mut code); + false } ShellQuote::Double | ShellQuote::AnsiC => { - if c == '\\' && i + 1 < chars.len() { - code.push(c); - i += 1; - code.push(chars[i]); - } else { - let closer = if quote == ShellQuote::Double { - '"' - } else { - '\'' - }; - if c == closer { - quote = ShellQuote::None; - } - code.push(c); - } - } - ShellQuote::None => { - if c == '\\' && i + 1 < chars.len() { - code.push(c); - i += 1; - code.push(chars[i]); - word_start = false; - } else if c == '$' && chars.get(i + 1) == Some(&'\'') { - quote = ShellQuote::AnsiC; - code.push_str("$'"); - i += 1; - word_start = false; - } else if c == '\'' { - quote = ShellQuote::Single; - code.push(c); - word_start = false; - } else if c == '"' { - quote = ShellQuote::Double; - code.push(c); - word_start = false; - } else if c == '#' && word_start { - break; - } else if c == '<' && chars.get(i + 1) == Some(&'<') { - if chars.get(i + 2) == Some(&'<') { - // Here-string: consume all three, no heredoc. - code.push_str("<<<"); - i += 2; - } else { - // Heredoc introducer: <<[-] WORD (quotes allowed). - let mut j = i + 2; - 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 { - '\'' | '"' => { - // Quoted run: contents literal, quotes removed. - 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; - } - } - } - if !tag.is_empty() { - heredocs.push_back((tag, strip_tabs)); - } - // Delimiter text is plain code; resume after it. - let end = j.max(start); - code.push_str("<<"); - code.extend(&chars[i + 2..end]); - i = end.saturating_sub(1).max(i + 1); - } - word_start = true; - } else { - code.push(c); - word_start = c.is_whitespace() || is_shell_operator(c); - } + 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; } - out.push((idx + 1, code)); + code } - out + + /// 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 { @@ -6638,6 +6737,24 @@ mod tests { assert_eq!(f[0].line, Some(2)); } + #[test] + fn shell_hash_after_multiline_quote_is_not_a_comment() { + // The `#` follows the closing quote with no space, so it is part of + // the same word (`end"#`), not a comment: `/tmp/x` must be flagged. + let f = shell_tmp_findings("echo \"start\nend\"# /tmp/x\n"); + assert_eq!(f.len(), 1); + assert_eq!(f[0].line, Some(2)); + } + + #[test] + fn shell_hash_after_backslash_newline_word_is_not_a_comment() { + // `foo\#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 <