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
2 changes: 2 additions & 0 deletions crates/pons-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ fn run_scan(
fail_on_skips: bool,
required_languages: &[String],
) -> Result<u8> {
let absolute_path = std::path::absolute(path)?;
let path = absolute_path.as_path();
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
if !path.exists() {
bail!("no such path: {}", path.display());
}
Expand Down
109 changes: 109 additions & 0 deletions crates/pons-cli/tests/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,112 @@ fn a_finding_outside_the_scan_root_is_rejected() {
String::from_utf8_lossy(&rejected.stderr)
);
}

#[test]
fn a_relative_windows_report_validates_on_every_reader_host() {
fn rebase(value: &mut Value, root: &str) {
match value {
Value::String(text) => {
if let Some(suffix) = text.strip_prefix(root) {
*text = format!("repo{suffix}").replace('/', "\\");
}
}
Value::Array(values) => values.iter_mut().for_each(|value| rebase(value, root)),
Value::Object(values) => values.values_mut().for_each(|value| rebase(value, root)),
_ => (),
}
}
let fixture = Fixture::new();
let mut report = fixture.scan();
let root = report["scanned"]["root"].as_str().unwrap().to_owned();
rebase(&mut report, &root);
assert_eq!(report["scanned"]["root"], "repo");
let result = fixture.validate(&report);
assert!(
result.status.success(),
"{}",
String::from_utf8_lossy(&result.stderr)
);
}

#[test]
fn ambiguous_legacy_paths_require_one_consistent_record_and_fingerprint() {
use pons_core::{corroborate::fingerprint_of, finding::EvidenceClass};
fn replace_path(value: &mut Value, original: &str, replacement: &str) {
match value {
Value::String(text) if text == original => *text = replacement.into(),
Value::Array(values) => values
.iter_mut()
.for_each(|v| replace_path(v, original, replacement)),
Value::Object(values) => values
.values_mut()
.for_each(|v| replace_path(v, original, replacement)),
_ => (),
}
}
let fixture = Fixture::new();
let original = fixture.scan();
let original_file = original["findings"][0]["location"]["file"]
.as_str()
.unwrap();
for (root, file, key, conflicting_key) in [
(
"//server/share",
r"//server/share\sample.php",
"sample.php",
"server/share/sample.php",
),
(
"repo",
r"repo\sub/sample.php",
"repo/sub/sample.php",
"sub/sample.php",
),
(
r"src\sample.php",
r"src\sample.php",
"sample.php",
"src/sample.php",
),
(
r"src\sample.php",
r"src\sample.php",
"src/sample.php",
"sample.php",
),
] {
let mut report = original.clone();
replace_path(&mut report, original_file, file);
report["scanned"]["root"] = json!(root);
for finding in report["findings"].as_array_mut().unwrap() {
let evidence: EvidenceClass =
serde_json::from_value(finding["evidence"].clone()).unwrap();
let line = finding["location"]["line_start"].as_u64().unwrap() as usize;
finding["semantic_fingerprint"] = json!(fingerprint_of(key, line, evidence));
}
for record in report["evidence_contract"]["records"]
.as_array_mut()
.unwrap()
{
record["path"] = json!(key);
}
let result = fixture.validate(&report);
assert!(
result.status.success(),
"{key}: {}",
String::from_utf8_lossy(&result.stderr)
);
let frozen_result = fixture.validate(&as_0_4_0(&report));
assert!(
frozen_result.status.success(),
"frozen {key}: {}",
String::from_utf8_lossy(&frozen_result.stderr)
);
report["evidence_contract"]["records"][0]["path"] = json!(conflicting_key);
assert_eq!(
fixture.validate(&report).status.code(),
Some(2),
"mixed interpretations accepted"
);
}
}
64 changes: 64 additions & 0 deletions crates/pons-cli/tests/coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,70 @@ fn jsx_keyword_attributes_reach_analysis_and_validate() {
assert!(report["scanned"]["skipped"].as_array().unwrap().is_empty());
assert!(pons_core::report::validate::validate(&report.to_string()).is_ok());
}

#[test]
fn relative_cli_inputs_emit_absolute_roots() {
let t = Temp::new();
fs::write(t.0.join("input.js"), "const value = 1;").unwrap();
for extra in [&[][..], &["--lang", "javascript"][..]] {
let out = Command::new(env!("CARGO_BIN_EXE_pons"))
.current_dir(&t.0)
.args(["scan", "input.js", "--format", "json"])
.args(extra)
.output()
.unwrap();
assert_eq!(out.status.code(), Some(0));
let report: Value = serde_json::from_slice(&out.stdout).unwrap();
let expected = if extra.is_empty() {
t.0.join("input.js")
} else {
t.0.clone()
};
assert_eq!(
PathBuf::from(report["scanned"]["root"].as_str().unwrap()),
expected
);
assert!(pons_core::report::validate::validate(&report.to_string()).is_ok());
}
let out = Command::new(env!("CARGO_BIN_EXE_pons"))
.current_dir(&t.0)
.args(["scan", ".", "--format", "json"])
.output()
.unwrap();
assert_eq!(out.status.code(), Some(0));
let report: Value = serde_json::from_slice(&out.stdout).unwrap();
assert_eq!(
PathBuf::from(report["scanned"]["root"].as_str().unwrap()),
t.0
);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

#[cfg(unix)]
#[test]
fn relative_posix_colon_and_backslash_filename_keeps_its_evidence_boundary() {
let t = Temp::new();
let file = r"a:b\c.php";
fs::write(
t.0.join(file),
"<?php function render() { echo $_GET['x']; }\n",
)
.unwrap();
let out = Command::new(env!("CARGO_BIN_EXE_pons"))
.current_dir(&t.0)
.args(["scan", file, "--format", "json"])
.output()
.unwrap();
assert_eq!(out.status.code(), Some(0));
let report: Value = serde_json::from_slice(&out.stdout).unwrap();
assert!(!report["evidence_contract"]["records"]
.as_array()
.unwrap()
.is_empty());
for record in report["evidence_contract"]["records"].as_array().unwrap() {
assert_eq!(record["path"], "a:b/c.php");
}
assert!(pons_core::report::validate::validate(&report.to_string()).is_ok());
}
#[test]
fn absent_required_language_cannot_be_suppressed() {
let t = Temp::new();
Expand Down
9 changes: 7 additions & 2 deletions crates/pons-core/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ pub struct ScanReport {
pub exit_code: Option<u8>,
pub anchors: std::collections::BTreeMap<String, String>,
pub coverage_failures: Vec<String>,
/// The root exactly as the caller gave it — never canonicalised, so
/// relative invocations stay relative in the output.
/// Normal scans use an absolute root without resolving symbolic links.
/// Explicit `scan_files` callers retain their supplied label. Absolute
/// roots may contain checkout details; exchange keys are root-relative.
pub root: String,
pub findings: Vec<Finding>,
pub files_scanned: usize,
Expand Down Expand Up @@ -197,6 +198,10 @@ impl Engine {
/// splitting them by which layer noticed would only invite a consumer to
/// handle one and miss the others.
pub fn scan(&self, root: &Path) -> anyhow::Result<ScanReport> {
// Absolute roots disambiguate POSIX filenames that resemble Windows
// drive paths without requiring access to the producer's filesystem.
let absolute_root = std::path::absolute(root)?;
let root = absolute_root.as_path();
let discovery = source::discover(root)?;
let mut report = self.scan_files(root, &discovery.files);
report.inventory.extend(discovery.inventory);
Expand Down
89 changes: 66 additions & 23 deletions crates/pons-core/src/evidence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,22 @@ use crate::{
use serde::{Deserialize, Serialize};
pub(crate) const ASSUMPTION: &str = "Only registered rule semantics and declared parser/model coverage; no runtime safety certification";

// Writers retain the native path semantics of the historical format.
pub(crate) fn relative_path(file: &str, root: &str) -> String {
relative_path_details(file, root).0
let path = std::path::Path::new(file);
let root = std::path::Path::new(root);
let base = if path == root {
root.parent().unwrap_or(root)
} else {
root
};
path.strip_prefix(base)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Align validator candidates with native path stripping

When the public scan_files API receives a directory root and an equivalent file spelling containing normalized components—for example, root repo and file repo/./a.php on POSIX—Path::strip_prefix emits a.php here, but path_candidates performs a raw string prefix removal and derives only ./a.php. Consequently, the report's evidence record and semantic fingerprint cannot pass the validator even though the file is inside the supplied root; either preserve the writer's lexical spelling or add the component-normalized form to the reader candidates.

Useful? React with 👍 / 👎.

.unwrap_or(path)
.to_string_lossy()
.replace('\\', "/")
}

pub(crate) fn relative_path_details(file: &str, root: &str) -> (String, bool) {
let windows = root.as_bytes().get(1) == Some(&b':')
|| root.starts_with("\\\\")
|| (cfg!(windows) && !root.starts_with('/'));
fn path_for_style(file: &str, root: &str, windows: bool) -> (String, bool) {
let file = if windows {
file.replace('\\', "/")
} else {
Expand All @@ -26,8 +34,6 @@ pub(crate) fn relative_path_details(file: &str, root: &str) -> (String, bool) {
} else {
root.into()
};
// A file scan has root == file. Do not consult the validator's filesystem:
// the original checkout may no longer exist where evidence is consumed.
let base = if file == root {
root.rsplit_once('/').map_or("", |(parent, _)| parent)
} else {
Expand All @@ -45,36 +51,73 @@ pub(crate) fn relative_path_details(file: &str, root: &str) -> (String, bool) {
)
}

// Historical relative roots do not identify the producer's platform. Readers
// enumerate both lexical interpretations and require the record and fingerprint
// to agree on ONE of them; they never consult their own filesystem or host OS.
pub(crate) fn path_candidates(file: &str, root: &str) -> Vec<(String, bool)> {
let drive_absolute = root.as_bytes().first().is_some_and(u8::is_ascii_alphabetic)
&& root.as_bytes().get(1) == Some(&b':')
&& root
.as_bytes()
.get(2)
.is_some_and(|c| matches!(c, b'/' | b'\\'));
if drive_absolute || root.starts_with("\\\\") {
return vec![path_for_style(file, root, true)];
Comment thread
hyperpolymath marked this conversation as resolved.
Resolved
}
let posix = path_for_style(file, root, false);
if root.starts_with('/') && !root.starts_with("//") {
return vec![posix];
}
let windows = path_for_style(file, root, true);
if windows == posix {
vec![posix]
} else {
vec![posix, windows]
}
}

#[cfg(test)]
mod portable_paths {
use super::path_candidates;
#[test]
fn windows_reports_have_the_same_keys_on_every_reader_host() {
assert_eq!(
super::relative_path(r"C:\repo\sample.php", r"C:\repo"),
"sample.php"
path_candidates(r"C:\repo\sample.php", r"C:\repo"),
vec![("sample.php".into(), true)]
);
assert_eq!(
super::relative_path(r"C:\repo\sample.php", r"C:\repo\sample.php"),
"sample.php"
path_candidates(r"C:\repo\sample.php", r"C:\repo\sample.php"),
vec![("sample.php".into(), true)]
);
assert_eq!(
super::relative_path(r"C:\outside\sample.php", r"C:\repo"),
"C:/outside/sample.php"
path_candidates(r"C:\outside\sample.php", r"C:\repo"),
vec![("C:/outside/sample.php".into(), false)]
);
assert!(path_candidates(r"repo\a.py", "repo").contains(&("a.py".into(), true)));
assert!(path_candidates(r".\a.py", ".").contains(&("a.py".into(), true)));
assert!(
path_candidates(r"//server/share\sample.php", "//server/share")
.contains(&("sample.php".into(), true))
);
}
#[test]
fn posix_filename_boundaries_survive_single_file_scans() {
for file in [r"/tmp/a\b.py", "/tmp/a:b.py"] {
assert_eq!(
super::relative_path(file, file),
super::relative_path(file, "/tmp")
);
assert!(super::relative_path_details(file, "/tmp").1);
}
fn posix_boundaries_and_ambiguous_historical_keys_are_preserved() {
assert_eq!(
super::relative_path(r"/tmp/a\b.py", r"/tmp/a\b.py"),
"a/b.py"
path_candidates(r"/tmp/a\b.py", r"/tmp/a\b.py"),
vec![("a/b.py".into(), true)]
);
assert!(!path_candidates(r"/tmp/repo\outside/a.py", "/tmp/repo")[0].1);
for (file, root, expected) in [
(r"repo\sub/a.py", "repo", "repo/sub/a.py"),
(r"repo\sub/a.py", "repo", "sub/a.py"),
(r"src\a.py", r"src\a.py", "a.py"),
(r"src\a.py", r"src\a.py", "src/a.py"),
(r"a:b\c.py", r"a:b\c.py", "a:b/c.py"),
] {
assert!(path_candidates(file, root)
.iter()
.any(|(path, _)| path == expected));
}
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
Expand Down
27 changes: 18 additions & 9 deletions crates/pons-core/src/report/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,11 @@ fn validate_value(v: Value) -> Result<String> {
// is right for rendering and wrong for a join key, so it is refused.
if has_corroboration {
let file = location["file"].as_str().context("missing finding file")?;
let (relative, stripped) = crate::evidence::relative_path_details(file, root);
let candidates = crate::evidence::path_candidates(file, root);
ensure!(
is_root_relative(&relative, stripped),
candidates
.iter()
.any(|(path, stripped)| is_root_relative(path, *stripped)),
"finding path is not inside the scan root"
);
}
Expand All @@ -255,13 +257,12 @@ fn validate_value(v: Value) -> Result<String> {
let line = location["line_start"]
.as_u64()
.context("missing finding line")? as usize;
let expected = crate::corroborate::fingerprint_of(
&crate::evidence::relative_path(file, root),
line,
evidence,
);
ensure!(
f["semantic_fingerprint"] == expected.as_str(),
crate::evidence::path_candidates(file, root)
.iter()
.any(|(path, stripped)| is_root_relative(path, *stripped)
&& f["semantic_fingerprint"]
== crate::corroborate::fingerprint_of(path, line, evidence)),
"semantic fingerprint mismatch"
);
}
Expand Down Expand Up @@ -316,7 +317,15 @@ fn validate_value(v: Value) -> Result<String> {
r.rule == f["rule_id"]
&& serde_json::to_value(r.tier)? == f["tier"]
&& serde_json::to_value(r.evidence)? == f["evidence"]
&& r.path == crate::evidence::relative_path(file, root)
&& crate::evidence::path_candidates(file, root)
.iter()
.any(|(path, stripped)| r.path == *path
&& (!has_corroboration
|| (is_root_relative(path, *stripped)
&& f["semantic_fingerprint"]
== crate::corroborate::fingerprint_of(
path, r.line, r.evidence
))))
&& Some(r.line as u64) == f["location"]["line_start"].as_u64()
&& r.trace == f["evidence_note"]
&& r.counterconditions == counterconditions
Expand Down
Loading
Loading