Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions PROOF-NEEDS.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,15 @@ scans a Justfile and a Lua file end to end. Each test was seen to fail on
a planted fault: an unrouted analyzer, Java sent to the generic analyzer,
cross-language checks forced on, and the README's stale count of 49.

The cross-language checks blank `//` comments only in languages where `//`
is a comment (`slash_line_comments`, an exhaustive table beside `route`).
Blanking every file used to cut unquoted URLs in shell scripts and Justfile
recipes, a recall gap found while testing (c). (c)'s test now uses an
unquoted URL, and `slash_comment_tests` checks a shell script and a Rust doc
comment. Blanking every language, and blanking none, were each seen to fail
those tests. Languages whose comment syntax the table does not record keep
the earlier blanking.

Routing is not coverage: `Language::Unknown` goes to `InputAdapter`, and a
file there with no configuration adapter is marked `unsupported` in the
coverage ledger and skipped. So (a) and (b) say which analyzer each
Expand Down Expand Up @@ -300,11 +309,4 @@ tests above are exhaustive over a finite enum, but they are tests, and (a)
reads the source with a pattern match. (d) is a documentation check and
stays a test.

|Unquoted `http://` URLs are missed by the cross-language check
|Fix first, then test
|`analyze_cross_language` strips C-style `//` comments from every file
before matching, which also cuts `curl http://host` in shell. A quoted
URL is found; an unquoted one in a `.sh` file or a Justfile recipe is not.
This is a recall gap in the InsecureProtocol detector, found while testing
PA1 (c).
|===
30 changes: 18 additions & 12 deletions src/assail/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,7 @@ impl Analyzer {
&script.content,
&mut file_weak_points,
&rel_path,
script.language,
)?;
for finding in &mut file_weak_points[start..] {
// Lines are relative to the extracted block, not the
Expand Down Expand Up @@ -1071,7 +1072,7 @@ impl Analyzer {
// comments or schema identifiers as executable source creates false
// findings. Source-language adapters retain their existing checks.
if super::routing::runs_cross_language(configuration.is_some()) {
self.analyze_cross_language(&content, &mut file_weak_points, &rel_path)?;
self.analyze_cross_language(&content, &mut file_weak_points, &rel_path, file_lang)?;
}

coverage.mark(file, "analyzed", None, Some(&raw_bytes));
Expand Down Expand Up @@ -5320,20 +5321,25 @@ impl Analyzer {
content: &str,
weak_points: &mut Vec<WeakPoint>,
file_path: &str,
lang: Language,
) -> Result<()> {
// HTTP (insecure) URLs - should be HTTPS
// Count http:// URLs that are NOT localhost/127.0.0.1 (those are fine).
//
// Strip C-family line comments first: `/// example: http://example.com`
// in a doc-comment is illustrative prose, not a configured endpoint,
// and was the source of repeated FPs on JSON-LD `@type` namespace URIs
// documented in /// blocks. The same heuristic naturally handles JS,
// Java, C, C++, Go — every language whose comment syntax is `//`.
// Python `#`, Lisp `;`, etc. are not currently de-noised; the
// cross-language detector is best-effort, and a follow-up can add
// language-aware stripping per file_path extension.
let scan_content_owned = strip_c_family_line_comments(content);
let scan_content: &str = &scan_content_owned;
// Blank `//` line comments first, but only where `//` is a comment:
// `/// example: http://example.com` in a doc comment is illustrative
// prose, not a configured endpoint, and was the source of repeated FPs
// on JSON-LD `@type` namespace URIs documented in /// blocks. In a
// shell script the same blanking would cut `curl http://host`, so
// `slash_line_comments` decides per language. Python `#`, Lisp `;`,
// etc. comments are not de-noised; this detector is best-effort.
let scan_content_owned;
let scan_content: &str = if super::routing::slash_line_comments(lang) {
scan_content_owned = strip_c_family_line_comments(content);
&scan_content_owned
} else {
content
};
let http_re = RE_HTTP_URL
.get_or_init(|| Regex::new(r#"http://[a-zA-Z0-9]"#).expect("static regex is valid"));
let http_localhost_re = RE_HTTP_LOCALHOST.get_or_init(|| {
Expand Down Expand Up @@ -6809,7 +6815,7 @@ mod tests {
let analyzer = Analyzer::new(std::path::Path::new(".")).expect("analyzer construction");
let mut wp = Vec::new();
analyzer
.analyze_cross_language(content, &mut wp, "fixture.rs")
.analyze_cross_language(content, &mut wp, "fixture.rs", Language::Rust)
.expect("analyze_cross_language");
wp.iter()
.filter(|w| matches!(w.category, WeakPointCategory::InsecureProtocol))
Expand Down
123 changes: 119 additions & 4 deletions src/assail/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,73 @@ pub(crate) fn route(lang: Language) -> Route {
}
}

/// Whether `//` starts a line comment in `lang`. The cross-language checks
/// blank such comments before matching, so an illustrative URL in a doc
/// comment is not reported. In a language where `//` is not a comment,
/// blanking would cut real code, such as a `curl` of a plain-HTTP URL in a shell
/// script, so those languages are matched as written.
///
/// Languages whose comment syntax this table does not record (the nextgen
/// DSLs, Datalog, whose dialects differ, and `Unknown`) keep the earlier
/// behaviour and are blanked.
pub(crate) fn slash_line_comments(lang: Language) -> bool {
use Language as L;
match lang {
L::Rust
| L::C
| L::Cpp
| L::Go
| L::Java
| L::JavaScript
| L::Php
| L::Gleam
| L::ReScript
| L::Zig
| L::Odin
| L::Pony
| L::DLang
| L::Chapel => true,
L::Python
| L::Ruby
| L::Elixir
| L::Erlang
| L::OCaml
| L::StandardML
| L::Scheme
| L::Racket
| L::Haskell
| L::PureScript
| L::Idris
| L::Lean
| L::Agda
| L::Isabelle
| L::Coq
| L::Prolog
| L::Logtalk
| L::Ada
| L::Nim
| L::Nickel
| L::Nix
| L::Shell
| L::Julia
| L::Lua => false,
L::Datalog
| L::WokeLang
| L::Eclexia
| L::MyLang
| L::JuliaTheViper
| L::Oblibeny
| L::Anvomidav
| L::AffineScript
| L::Ephapax
| L::BetLang
| L::ErrorLang
| L::VCL
| L::FBQL
| L::Unknown => true,
}
}

impl Route {
/// The function that `analyze_inner` calls for this route.
#[cfg_attr(not(test), allow(dead_code))]
Expand Down Expand Up @@ -275,13 +342,11 @@ mod tests {
let url = |path: &str| format!("{}://example.org/{path}", "http");
let tmp = TempDir::new().unwrap();
// A Justfile is configuration. Its variable is configuration content;
// its recipe body is an embedded shell script. The recipe URL is
// quoted: the cross-language detector strips `//` comments first,
// which also cuts an unquoted URL (a separate recall gap).
// its recipe body is an embedded shell script, with an unquoted URL.
fs::write(
tmp.path().join("Justfile"),
format!(
"mirror := \"{}\"\n\nfetch:\n curl \"{}\"\n",
"mirror := \"{}\"\n\nfetch:\n curl {}\n",
url("variable"),
url("recipe")
),
Expand Down Expand Up @@ -348,3 +413,53 @@ mod tests {
}
}
}

#[cfg(test)]
mod slash_comment_tests {
use super::slash_line_comments;
use crate::assail::Analyzer;
use crate::types::{Language, WeakPointCategory};
use std::fs;
use tempfile::TempDir;

fn insecure_in(file: &str, content: &str) -> usize {
let tmp = TempDir::new().unwrap();
fs::write(tmp.path().join(file), content).unwrap();
Analyzer::new(tmp.path())
.unwrap()
.analyze()
.unwrap()
.weak_points
.iter()
.filter(|w| w.category == WeakPointCategory::InsecureProtocol)
.count()
}

#[test]
fn languages_without_slash_comments_are_matched_as_written() {
for lang in [Language::Shell, Language::Python, Language::Lua] {
assert!(!slash_line_comments(lang), "{lang:?}");
}
for lang in [Language::Rust, Language::JavaScript, Language::Go] {
assert!(slash_line_comments(lang), "{lang:?}");
}
}

/// The recall gap this table closes: an unquoted URL in a shell script
/// used to be cut at `//` before matching.
#[test]
fn an_unquoted_url_in_a_shell_script_is_reported() {
let url = format!("{}://example.org/archive.tar", "http");
assert_eq!(insecure_in("fetch.sh", &format!("curl -O {url}\n")), 1);
}

/// The precision the blanking exists for is kept where `//` is a comment.
#[test]
fn a_url_in_a_rust_doc_comment_is_still_ignored() {
let url = format!("{}://example.org/spec", "http");
assert_eq!(
insecure_in("lib.rs", &format!("/// See {url}\npub fn f() {{}}\n")),
0
);
}
}
Loading