Skip to content
Closed
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
5 changes: 4 additions & 1 deletion THREAT-MODEL.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,10 @@ provider (e.g. Ollama on `127.0.0.1`): then nothing leaves your machine
([ADR 0084](docs/adr/0084-red-risk-requires-confirm.md), §3).
- **File reads and writes by the AI** appear in the ledger as path, size, SHA-256
and (for writes) backup path only — never the file content or a diff. Paths
are redacted like command text. A SHA-256 of very short or low-entropy
are redacted like command text. A write that failed after it reached the
target (or whose outcome is unknown) is recorded the same way, marked as
failed (the file may be partly overwritten), without hashes of the new
content. A SHA-256 of very short or low-entropy
content (a PIN file, a single-token line) can be recovered by brute force,
so the hash is not a secrecy boundary: the protection is the encrypted
database, not the hash
Expand Down
2 changes: 2 additions & 0 deletions changelog.d/issue-1485-failed-file-write-record.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
### Fixed
- The activity record now notes an AI file write that failed after it started, since the file may be partly overwritten.
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,7 @@ async fn test_ledger_redacts_fake_secret_in_file_paths_and_connection_detail() {
},
LedgerEntryContent::FileWritten {
path: "/srv/app?password=hunter2geheim".to_string(),
status: Default::default(),
backup_path: None,
bytes_before: None,
bytes_after: Some(1),
Expand Down
206 changes: 206 additions & 0 deletions crates/app-logic/src/orchestration/action_exec/tests_file_ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ async fn test_file_write_records_backup_and_both_hashes_without_content() {
assert!(entries[0].meta.action_id.is_some());
let LedgerEntryContent::FileWritten {
path: p,
status: _,
backup_path,
bytes_before,
bytes_after,
Expand Down Expand Up @@ -356,3 +357,208 @@ async fn test_failed_approved_file_write_records_decision_but_no_file_written()
Some(LedgerDecisionOutcome::Confirmed)
);
}

fn failed_entry(
entries: &[persistence_sqlite::LedgerEntry],
) -> (&persistence_sqlite::LedgerEntry, &LedgerEntryContent) {
let e = entries
.iter()
.find(|e| matches!(e.content, LedgerEntryContent::FileWritten { .. }))
.unwrap_or_else(|| panic!("expected file_written: {entries:?}"));
(e, &e.content)
}

#[tokio::test]
async fn test_write_failing_mid_stream_records_failed_file_written() {
let path = "/home/deploy/app.conf";
let (entries, _t, _c) = run_write_all(
write_action(path, "NEW-MARKER-2 body"),
MockSshTransport::default(),
MockSftpSession::new()
.with_file(path, b"OLD-MARKER-1 body".to_vec())
.with_partial_write_failure(path),
false,
true,
)
.await;
let decision = entries
.iter()
.find(|e| decision_outcome(e) == Some(LedgerDecisionOutcome::Confirmed))
.unwrap_or_else(|| panic!("expected decision: {entries:?}"));
let (written, content) = failed_entry(&entries);
assert_eq!(decision.meta.action_id, written.meta.action_id);
let LedgerEntryContent::FileWritten {
path: p,
status,
backup_path,
bytes_after,
sha256_after,
..
} = content
else {
unreachable!()
};
assert_eq!(p, path);
assert_eq!(
*status,
ssh_manager_core::audit::LedgerFileWriteStatus::Failed
);
assert!(backup_path
.as_deref()
.unwrap()
.starts_with("/home/deploy/app.conf.smartssh-backup-"));
assert!(bytes_after.is_none() && sha256_after.is_none());
let json = serde_json::to_string(content).unwrap();
assert!(!json.contains("MARKER"), "no content in the record: {json}");
}

#[tokio::test]
async fn test_new_file_failing_before_open_records_no_file_written_entry() {
// The new target was never created (e.g. missing directory): no record.
let path = "/home/deploy/missing-dir/new.conf";
let (entries, _t, _c) = run_write_all(
write_action(path, "neu"),
MockSshTransport::default(),
MockSftpSession::new().with_failing_open_for_write(path),
false,
true,
)
.await;
assert!(
!entries
.iter()
.any(|e| matches!(e.content, LedgerEntryContent::FileWritten { .. })),
"{entries:?}"
);
}

#[tokio::test]
async fn test_new_file_failing_with_closed_session_records_failed_file_written() {
// The stat after the failure cannot tell whether the file was created
// (session closed): the possibly partly written file is recorded.
let path = "/home/deploy/new.conf";
let (entries, _t, _c) = run_write_all(
write_action(path, "neu und lang"),
MockSshTransport::default(),
MockSftpSession::new()
.with_failing_open_for_write(path)
.with_session_drop_on_failed_write(),
false,
true,
)
.await;
let (_, content) = failed_entry(&entries);
assert!(matches!(
content,
LedgerEntryContent::FileWritten {
status: ssh_manager_core::audit::LedgerFileWriteStatus::Failed,
..
}
));
}

#[tokio::test]
async fn test_new_file_failing_mid_stream_records_failed_file_written() {
// A new file that exists after the failure was touched: recorded.
let path = "/home/deploy/new.conf";
let (entries, _t, _c) = run_write_all(
write_action(path, "neu und lang"),
MockSshTransport::default(),
MockSftpSession::new().with_partial_write_failure(path),
false,
true,
)
.await;
let (_, content) = failed_entry(&entries);
assert!(matches!(
content,
LedgerEntryContent::FileWritten {
status: ssh_manager_core::audit::LedgerFileWriteStatus::Failed,
..
}
));
}

#[tokio::test]
async fn test_failed_file_write_path_is_redacted() {
let path = "/srv/app?password=hunter2geheim";
let normalized = normalize_lexical_path(path);
let (entries, _t, _c) = run_write(
write_action(path, "x"),
MockSshTransport::default(),
MockSftpSession::new().with_partial_write_failure(normalized),
false,
true,
)
.await;
let (_, content) = failed_entry(&entries);
let json = serde_json::to_string(content).unwrap();
assert!(!json.contains("hunter2geheim"), "{json}");
}

#[tokio::test]
async fn test_backup_failure_records_no_file_written_entry() {
// The backup copy fails before the target is touched.
let path = "/home/deploy/app.conf";
let backup_fail = MockSftpSession::new()
.with_file(path, b"alt".to_vec())
.with_permission_denied(path);
let (entries, _t, _c) = run_write(
write_action(path, "neu"),
MockSshTransport::default().with_prefix_response(
"sudo -S cp -p ",
ssh_manager_core::ssh::CommandOutput {
stdout: Vec::new(),
stderr: b"cp: failed".to_vec(),
exit_code: Some(1),
truncated: false,
},
),
backup_fail,
true,
true,
)
.await;
assert!(entries.is_empty(), "{entries:?}");
}

#[tokio::test]
async fn test_failed_sudo_install_records_failed_file_written_with_sudo() {
let path = "/etc/nginx/nginx.conf";
let transport = MockSshTransport::default()
.with_prefix_response("sudo -S cp -p '/etc/nginx/nginx.conf' ", output(""))
.with_prefix_response(
"sudo -S install -m 644 ",
ssh_manager_core::ssh::CommandOutput {
stdout: Vec::new(),
stderr: b"install: failed".to_vec(),
exit_code: Some(1),
truncated: false,
},
);
let (entries, _t, _c) = run_write(
write_action(path, "neu"),
transport,
MockSftpSession::new()
.with_file(path, b"alt".to_vec())
.with_permission_denied(path),
true,
true,
)
.await;
let (_, content) = failed_entry(&entries);
let LedgerEntryContent::FileWritten {
status,
sudo,
backup_path,
..
} = content
else {
unreachable!()
};
assert_eq!(
*status,
ssh_manager_core::audit::LedgerFileWriteStatus::Failed
);
assert!(*sudo && backup_path.is_some());
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub(super) fn redact_ledger_entry_content(
// Only the paths can carry a secret; sizes and hashes are not content.
LedgerEntryContent::FileWritten {
path,
status,
backup_path,
bytes_before,
bytes_after,
Expand All @@ -49,6 +50,7 @@ pub(super) fn redact_ledger_entry_content(
sudo,
} => LedgerEntryContent::FileWritten {
path: redactor.redact_text(&path),
status,
backup_path: backup_path.map(|b| redactor.redact_text(&b)),
bytes_before,
bytes_after,
Expand Down
Loading
Loading