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
241 changes: 239 additions & 2 deletions crates/ripr/src/agent/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,15 @@ pub(crate) fn repo_exposure_artifact_metadata(
"profile": context.mode,
"worktree": status,
},
"snapshot_identity": format!("snapshot:{}", context.input_identity),
"snapshot_identity": repo_exposure_snapshot_identity(&context.input_identity, &head),
Comment thread
EffortlessSteven marked this conversation as resolved.
Resolved
"content_sha256": content_sha256,
}))
}

fn repo_exposure_snapshot_identity(input_identity: &str, repository_head: &str) -> String {
format!("snapshot:{input_identity};revision:{repository_head}")
}
Comment thread
EffortlessSteven marked this conversation as resolved.
Resolved
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

pub(crate) fn validate_repo_exposure_artifact(
root: &Path,
raw: &str,
Expand Down Expand Up @@ -181,7 +185,6 @@ pub(crate) fn validate_repo_exposure_artifact(
|| identity.analysis.input_identity.trim().is_empty()
|| identity.analysis.command != "ripr check --format repo-exposure-json"
|| identity.analysis.profile != identity.analysis.mode
|| !identity.snapshot_identity.starts_with("snapshot:input:")
|| !matches!(identity.analysis.worktree.as_str(), "clean" | "dirty")
{
return Err(format!(
Expand All @@ -203,6 +206,15 @@ pub(crate) fn validate_repo_exposure_artifact(
identity.repository.head
));
}
let expected_snapshot = repo_exposure_snapshot_identity(
&identity.analysis.input_identity,
&identity.repository.head,
);
if identity.snapshot_identity != expected_snapshot {
return Err(format!(
"agent verify {label} artifact snapshot identity does not match the declared analysis input identity and repository head"
));
}
if !identity.content_sha256.starts_with("sha256:") {
return Err(format!(
"agent verify {label} artifact is missing a sha256 content commitment"
Expand Down Expand Up @@ -404,6 +416,33 @@ fn content_sha256_with_placeholder(raw: &str) -> Result<String, String> {
mod tests {
use super::*;

fn run_git(root: &Path, args: &[&str]) -> Result<String, String> {
git_output(root, args)
}

fn temporary_git_root() -> Result<PathBuf, String> {
let stamp = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_err(|error| format!("clock error: {error}"))?
.as_nanos();
let root = std::env::temp_dir().join(format!("ripr-artifact-identity-{stamp}"));
std::fs::create_dir_all(&root).map_err(|error| format!("create temp root: {error}"))?;
let init = (|| -> Result<(), String> {
run_git(&root, &["init", "--quiet"])?;
run_git(&root, &["config", "user.name", "RIPR test"])?;
run_git(
&root,
&["config", "user.email", "ripr-test@example.invalid"],
)?;
Ok(())
})();
if let Err(error) = init {
let _ = std::fs::remove_dir_all(&root);
return Err(error);
}
Ok(root)
}
Comment thread
EffortlessSteven marked this conversation as resolved.
Resolved

#[test]
fn content_placeholder_is_fixed_width() {
assert_eq!(CONTENT_SHA256_PLACEHOLDER.len(), 71);
Expand All @@ -422,4 +461,202 @@ mod tests {
let result = content_sha256_with_placeholder(raw);
assert!(matches!(result, Err(error) if error.contains("sha256:<64 hex>")));
}

#[test]
fn repo_exposure_identity_changes_with_controlled_git_revision() -> Result<(), String> {
let root = temporary_git_root()?;
let result = (|| -> Result<(), String> {
std::fs::write(root.join("Cargo.toml"), "[workspace]\n")
.map_err(|error| format!("write Cargo.toml: {error}"))?;
run_git(&root, &["add", "Cargo.toml"])?;
run_git(
&root,
&[
"-c",
"commit.gpgSign=false",
"commit",
"--quiet",
"-m",
"before",
],
)?;
let before = RepoExposureArtifactContext::for_repo_exposure(
root.clone(),
"draft".to_string(),
None,
)?;
let before_artifact = repo_exposure_artifact_metadata(&before, "sha256:test")?;

run_git(
&root,
&[
"-c",
"commit.gpgSign=false",
"commit",
"--quiet",
"--allow-empty",
"-m",
"after",
],
)?;
let after = RepoExposureArtifactContext::for_repo_exposure(
root.clone(),
"draft".to_string(),
None,
)?;
let after_artifact = repo_exposure_artifact_metadata(&after, "sha256:test")?;

if before.input_identity != after.input_identity {
return Err(
"controlled Git revisions must preserve the comparable input identity"
.to_string(),
);
}
let before_snapshot = before_artifact["snapshot_identity"]
.as_str()
.ok_or_else(|| "before artifact omitted snapshot identity".to_string())?;
let after_snapshot = after_artifact["snapshot_identity"]
.as_str()
.ok_or_else(|| "after artifact omitted snapshot identity".to_string())?;
let before_head = before_artifact["repository"]["head"]
.as_str()
.ok_or_else(|| "before artifact omitted repository head".to_string())?;
let after_head = after_artifact["repository"]["head"]
.as_str()
.ok_or_else(|| "after artifact omitted repository head".to_string())?;
if before_snapshot
!= repo_exposure_snapshot_identity(&before.input_identity, before_head)
|| after_snapshot
!= repo_exposure_snapshot_identity(&after.input_identity, after_head)
{
return Err(
"snapshot identity must use the production snapshot identity builder"
.to_string(),
);
}
if before_snapshot == after_snapshot {
return Err(
"controlled Git revisions must produce distinct snapshot identities"
.to_string(),
);
}
Ok(())
})();
let cleanup =
std::fs::remove_dir_all(&root).map_err(|error| format!("remove temp root: {error}"));
result?;
cleanup?;
Ok(())
}

fn commit_fixture_file(root: &Path) -> Result<(), String> {
std::fs::write(root.join("Cargo.toml"), "[workspace]\n")
.map_err(|error| format!("write Cargo.toml: {error}"))?;
run_git(root, &["add", "Cargo.toml"])?;
run_git(
root,
&[
"-c",
"commit.gpgSign=false",
"commit",
"--quiet",
"-m",
"fixture",
],
)?;
Ok(())
}

/// Render a canonical artifact document with the content commitment left
/// at the fixed placeholder so variants can be recommitted after edits.
fn repo_exposure_raw_with_placeholder(root: &Path) -> Result<String, String> {
let context = RepoExposureArtifactContext::for_repo_exposure(
root.to_path_buf(),
"draft".to_string(),
None,
)?;
let identity = repo_exposure_artifact_metadata(&context, CONTENT_SHA256_PLACEHOLDER)?;
let document = json!({
"schema_version": crate::output::repo_exposure::REPO_EXPOSURE_SCHEMA_VERSION,
"scope": "repo",
"run_status": "complete",
"artifact": identity,
"seams": [],
});
serde_json::to_string_pretty(&document)
.map_err(|error| format!("render artifact document: {error}"))
}

fn commit_content(raw_with_placeholder: &str) -> Result<String, String> {
let digest = content_sha256_with_placeholder(raw_with_placeholder)?;
Ok(raw_with_placeholder.replace(CONTENT_SHA256_PLACEHOLDER, &digest))
}

#[test]
fn repo_exposure_validation_requires_exact_snapshot_identity() -> Result<(), String> {
let root = temporary_git_root()?;
let result = (|| -> Result<(), String> {
commit_fixture_file(&root)?;
let placeholder_raw = repo_exposure_raw_with_placeholder(&root)?;
let raw = commit_content(&placeholder_raw)?;
let validated = validate_repo_exposure_artifact(&root, &raw, "test before")
.map_err(|error| format!("canonical artifact must validate: {error}"))?;
if validated.currentness != ArtifactCurrentness::Current {
return Err("fresh canonical artifact must be current".to_string());
}

let head = validated.repository_head.clone();
let input = validated.input_identity.clone();
let valid_snapshot = validated.snapshot_identity.clone();
let wrong_head = "0".repeat(40);
let cases = [
(
"wrong revision component",
repo_exposure_snapshot_identity(&input, &wrong_head),
),
(
"wrong input component",
repo_exposure_snapshot_identity("input:0000000000000000", &head),
),
(
"prefix-compatible legacy shape",
format!("snapshot:{input}"),
),
(
"arbitrary prefix-compatible text",
"snapshot:input:arbitrary".to_string(),
),
(
"reordered components",
format!("revision:{head};snapshot:{input}"),
),
];
for (case, snapshot) in cases {
if snapshot == valid_snapshot {
return Err(format!(
"{case} fixture must differ from the valid snapshot identity"
));
}
let tampered =
commit_content(&placeholder_raw.replace(&valid_snapshot, &snapshot))?;
match validate_repo_exposure_artifact(&root, &tampered, "test before") {
Err(error) if error.contains("snapshot identity does not match") => {}
Err(error) => {
return Err(format!("{case}: unexpected rejection reason: {error}"));
}
Ok(_) => {
return Err(format!(
"{case}: tampered snapshot identity must be rejected"
));
}
}
}
Ok(())
})();
let cleanup =
std::fs::remove_dir_all(&root).map_err(|error| format!("remove temp root: {error}"));
result?;
cleanup?;
Ok(())
}
}
8 changes: 6 additions & 2 deletions crates/ripr/tests/cli_smoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ fn write_bound_repo_exposure_fixture(
"producer": {{"tool": "ripr", "version": "0.10.0"}},
"repository": {{"root": "{root_identity}", "head": "{head}"}},
"analysis": {{"format": "repo-exposure-json", "mode": "draft", "base_revision": null, "input_identity": "input:fixture", "command": "ripr check --format repo-exposure-json", "profile": "draft", "worktree": "clean"}},
"snapshot_identity": "snapshot:input:fixture",
"snapshot_identity": "snapshot:input:fixture;revision:{head}",
"content_sha256": "{placeholder}"
}},
"scope": "repo",
Expand Down Expand Up @@ -349,7 +349,7 @@ fn bind_repo_exposure_fixture_with_worktree(
"producer": {"tool": "ripr", "version": "0.10.0"},
"repository": {"root": root_identity, "head": head},
"analysis": {"format": "repo-exposure-json", "mode": "draft", "base_revision": null, "input_identity": "input:fixture", "command": "ripr check --format repo-exposure-json", "profile": "draft", "worktree": worktree},
"snapshot_identity": "snapshot:input:fixture",
"snapshot_identity": format!("snapshot:input:fixture;revision:{head}"),
"content_sha256": placeholder,
});
let raw = serde_json::to_string_pretty(&value)?;
Expand Down Expand Up @@ -2954,6 +2954,10 @@ fn agent_receipt_rejects_incomparable_analysis_inputs() -> Result<(), Box<dyn st
"\"input_identity\": \"input:fixture\"",
"\"input_identity\": \"input:other\"",
);
// Keep the tampered artifact internally consistent (input identity and
// snapshot identity must agree under exact snapshot validation) so the
// pair comparison — not single-artifact validation — rejects it.
let altered = altered.replace("snapshot:input:fixture", "snapshot:input:other");
std::fs::write(&after, recommit_repo_exposure_json(altered))?;
let verify = root.join("fabricated-agent-verify.json");
write_fabricated_agent_verify_json(&verify, &before, &after)?;
Expand Down
2 changes: 1 addition & 1 deletion docs/OUTPUT_SCHEMA.md
Original file line number Diff line number Diff line change
Expand Up @@ -2128,7 +2128,7 @@ additive top-level `artifact` envelope before it is suitable for
"profile": "draft",
"worktree": "clean"
},
"snapshot_identity": "snapshot:input:<bounded-fingerprint>",
"snapshot_identity": "snapshot:input:<bounded-fingerprint>;revision:<full-head-sha>",
"content_sha256": "sha256:<64-hex-digest>"
}
```
Expand Down
2 changes: 1 addition & 1 deletion xtask/src/reports/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1858,7 +1858,7 @@ fn bind_repo_exposure_fixture(
"profile": "draft",
"worktree": worktree,
},
"snapshot_identity": "snapshot:input:fixture",
"snapshot_identity": format!("snapshot:input:fixture;revision:{head}"),
"content_sha256": "sha256:0000000000000000000000000000000000000000000000000000000000000000",
});
let raw = serde_json::to_string_pretty(&value).map_err(|err| {
Expand Down
Loading