use std::io::{Read, Seek};
use std::path::Path;
const REDACTION_MARKER: &str = "redaction.required";
pub(crate) fn require_redaction(directory: &Path) -> Result<(), String> {
std::fs::write(directory.join(REDACTION_MARKER), []).map_err(|error| error.to_string())
}
pub(crate) fn redaction_pending(path: Option<&str>) -> bool {
path.and_then(|path| Path::new(path).parent())
.is_some_and(|directory| directory.join(REDACTION_MARKER).exists())
}
pub(crate) fn finalize_redaction(paths: &[&Path], secrets: &[&String]) -> Result<(), String> {
let Some(marker) = paths
.first()
.and_then(|path| path.parent())
.map(|directory| directory.join(REDACTION_MARKER))
else {
return Ok(());
};
if !marker.exists() {
return Ok(());
}
if paths.iter().all(|path| {
path.metadata()
.map(|metadata| metadata.is_file() && metadata.len() == 0)
.unwrap_or_else(|_| !path.exists())
}) {
std::fs::remove_file(marker).map_err(|error| error.to_string())?;
return Ok(());
}
let result = if secrets.iter().all(|secret| secret.is_empty()) {
rewrite_or_remove(paths, |_| {
Err("resolved secrets are unavailable after recovery".to_string())
})
} else {
rewrite_or_remove(paths, |path| redact(path, secrets))
};
if result.is_ok() || paths.iter().all(|path| !path.exists()) {
std::fs::remove_file(marker).map_err(|error| error.to_string())?;
}
result
}
pub(crate) fn read_tail(path: Option<&str>, bytes: usize) -> Result<(String, bool), String> {
let Some(path) = path else {
return Ok((String::new(), false));
};
let mut file = match std::fs::File::open(path) {
Ok(file) => file,
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
return Ok((String::new(), false));
}
Err(error) => return Err(error.to_string()),
};
let length = file.metadata().map_err(|error| error.to_string())?.len();
file.seek(std::io::SeekFrom::Start(
length.saturating_sub(bytes as u64),
))
.map_err(|error| error.to_string())?;
let mut value = Vec::new();
file.read_to_end(&mut value)
.map_err(|error| error.to_string())?;
Ok((
String::from_utf8_lossy(&value).into_owned(),
length > bytes as u64,
))
}
pub(crate) fn read_matches(
path: Option<&str>,
stream: &str,
pattern: ®ex::Regex,
context: usize,
max_matches: usize,
) -> Result<(Vec<crate::run::LogLine>, usize, usize), String> {
let (text, total) = read_all(path)?;
let lines = text.lines().collect::<Vec<_>>();
let mut selected = std::collections::BTreeSet::new();
let mut matches = 0;
for (index, line) in lines.iter().enumerate() {
if !pattern.is_match(line) {
continue;
}
matches += 1;
if matches > max_matches {
continue;
}
let start = index.saturating_sub(context);
let end = (index + context).min(lines.len().saturating_sub(1));
for number in start..=end {
selected.insert(number);
}
}
Ok((
selected
.into_iter()
.map(|index| crate::run::LogLine {
number: index + 1,
stream: stream.to_string(),
text: lines[index].to_string(),
})
.collect(),
matches,
total,
))
}
pub(crate) fn read_lines(
path: Option<&str>,
stream: &str,
start: usize,
end: usize,
) -> Result<(Vec<crate::run::LogLine>, usize), String> {
let (text, total) = read_all(path)?;
let lines = text
.lines()
.enumerate()
.filter(|(index, _)| {
let number = index + 1;
number >= start && number <= end
})
.map(|(index, line)| crate::run::LogLine {
number: index + 1,
stream: stream.to_string(),
text: line.to_string(),
})
.collect();
Ok((lines, total))
}
fn read_all(path: Option<&str>) -> Result<(String, usize), String> {
let Some(path) = path else {
return Ok((String::new(), 0));
};
let bytes = match std::fs::read(path) {
Ok(bytes) => bytes,
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
return Ok((String::new(), 0));
}
Err(error) => return Err(error.to_string()),
};
let text = String::from_utf8_lossy(&bytes).into_owned();
let total = text.lines().count();
Ok((text, total))
}
fn redact(path: &Path, secrets: &[&String]) -> Result<(), String> {
let mut bytes = match std::fs::read(path) {
Ok(bytes) => bytes,
Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(()),
Err(error) => return Err(error.to_string()),
};
for secret in secrets {
if !secret.is_empty() {
bytes = replace_bytes(&bytes, secret.as_bytes(), b"[REDACTED]");
}
}
std::fs::write(path, bytes).map_err(|error| error.to_string())
}
fn replace_bytes(input: &[u8], pattern: &[u8], replacement: &[u8]) -> Vec<u8> {
if pattern.is_empty() {
return input.to_vec();
}
let mut output = Vec::with_capacity(input.len());
let mut remaining = input;
while let Some(index) = remaining
.windows(pattern.len())
.position(|window| window == pattern)
{
output.extend_from_slice(&remaining[..index]);
output.extend_from_slice(replacement);
remaining = &remaining[index + pattern.len()..];
}
output.extend_from_slice(remaining);
output
}
fn rewrite_or_remove(
paths: &[&Path],
mut rewrite: impl FnMut(&Path) -> Result<(), String>,
) -> Result<(), String> {
let errors = paths
.iter()
.filter_map(|path| {
rewrite(path)
.err()
.map(|error| format!("{}: {error}", path.display()))
})
.collect::<Vec<_>>();
if errors.is_empty() {
return Ok(());
}
let cleanup_errors = paths
.iter()
.filter_map(|path| match std::fs::remove_file(path) {
Ok(()) => None,
Err(error) if error.kind() == std::io::ErrorKind::NotFound => None,
Err(error) => Some(format!("{}: {error}", path.display())),
})
.collect::<Vec<_>>();
if cleanup_errors.is_empty() {
Err(errors.join("; "))
} else {
Err(format!(
"{}; cleanup failed: {}",
errors.join("; "),
cleanup_errors.join("; ")
))
}
}
#[cfg(test)]
mod tests {
use tempfile::tempdir;
use super::*;
#[test]
fn sanitizes_every_log() {
let directory = tempdir().unwrap();
let stdout = directory.path().join("stdout.log");
let stderr = directory.path().join("stderr.log");
let secret = "synthetic-secret".to_string();
std::fs::write(&stdout, format!("before {secret} after")).unwrap();
std::fs::write(&stderr, format!("{secret} twice {secret}")).unwrap();
require_redaction(directory.path()).unwrap();
finalize_redaction(&[&stdout, &stderr], &[&secret]).unwrap();
assert_eq!(
std::fs::read_to_string(stdout).unwrap(),
"before [REDACTED] after"
);
assert_eq!(
std::fs::read_to_string(stderr).unwrap(),
"[REDACTED] twice [REDACTED]"
);
assert!(!directory.path().join(REDACTION_MARKER).exists());
}
#[test]
fn redaction_preserves_non_utf8_bytes() {
let directory = tempdir().unwrap();
let stdout = directory.path().join("stdout.log");
let stderr = directory.path().join("stderr.log");
let secret = "synthetic-secret".to_string();
let mut bytes = vec![0xff];
bytes.extend_from_slice(secret.as_bytes());
bytes.push(0xfe);
std::fs::write(&stdout, bytes).unwrap();
std::fs::write(&stderr, []).unwrap();
require_redaction(directory.path()).unwrap();
finalize_redaction(&[&stdout, &stderr], &[&secret]).unwrap();
assert_eq!(
std::fs::read(stdout).unwrap(),
[vec![0xff], b"[REDACTED]".to_vec(), vec![0xfe]].concat()
);
}
#[test]
fn missing_logs_complete_redaction() {
let directory = tempdir().unwrap();
let stdout = directory.path().join("stdout.log");
let stderr = directory.path().join("stderr.log");
require_redaction(directory.path()).unwrap();
finalize_redaction(&[&stdout, &stderr], &[]).unwrap();
assert!(!directory.path().join(REDACTION_MARKER).exists());
}
#[test]
fn rewrite_failure_removes_every_log() {
let directory = tempdir().unwrap();
let stdout = directory.path().join("stdout.log");
let stderr = directory.path().join("stderr.log");
std::fs::write(&stdout, "secret").unwrap();
std::fs::write(&stderr, "secret").unwrap();
let error = rewrite_or_remove(&[&stdout, &stderr], |_| Err("synthetic failure".into()))
.unwrap_err();
assert!(error.contains("synthetic failure"));
assert!(!stdout.exists());
assert!(!stderr.exists());
}
#[test]
fn recovery_without_secret_values_removes_every_log() {
let directory = tempdir().unwrap();
let stdout = directory.path().join("stdout.log");
let stderr = directory.path().join("stderr.log");
std::fs::write(&stdout, "secret").unwrap();
std::fs::write(&stderr, "secret").unwrap();
require_redaction(directory.path()).unwrap();
let error = finalize_redaction(&[&stdout, &stderr], &[]).unwrap_err();
assert!(error.contains("unavailable after recovery"));
assert!(!stdout.exists());
assert!(!stderr.exists());
assert!(!directory.path().join(REDACTION_MARKER).exists());
}
#[test]
fn addressed_reads_cover_empty_missing_bounded_and_error_paths() {
let directory = tempdir().unwrap();
let missing = directory.path().join("missing.log");
let path = directory.path().join("output.log");
std::fs::write(&path, "before\nhit one\nmiddle\nhit two\nafter\n").unwrap();
assert_eq!(read_tail(None, 8).unwrap(), (String::new(), false));
assert_eq!(
read_tail(Some(missing.to_str().unwrap()), 8).unwrap(),
(String::new(), false)
);
let (tail, truncated) = read_tail(Some(path.to_str().unwrap()), 6).unwrap();
assert_eq!(tail, "after\n");
assert!(truncated);
let pattern = regex::Regex::new("hit").unwrap();
let (matches, total_matches, total_lines) =
read_matches(Some(path.to_str().unwrap()), "stdout", &pattern, 1, 1).unwrap();
assert_eq!(total_matches, 2);
assert_eq!(total_lines, 5);
assert_eq!(matches.len(), 3);
assert_eq!(matches[0].number, 1);
assert_eq!(matches[2].number, 3);
let (lines, total) = read_lines(Some(path.to_str().unwrap()), "stderr", 2, 4).unwrap();
assert_eq!(total, 5);
assert_eq!(lines.len(), 3);
assert_eq!(lines[0].stream, "stderr");
assert_eq!(lines[2].number, 4);
assert_eq!(read_lines(None, "stdout", 1, 2).unwrap().1, 0);
assert_eq!(
read_lines(Some(missing.to_str().unwrap()), "stdout", 1, 2)
.unwrap()
.1,
0
);
assert!(read_tail(Some(directory.path().to_str().unwrap()), 8).is_err());
assert!(read_lines(Some(directory.path().to_str().unwrap()), "stdout", 1, 2).is_err());
}
#[test]
fn redaction_and_cleanup_helpers_cover_absence_and_composed_failures() {
let directory = tempdir().unwrap();
let missing = directory.path().join("missing.log");
let output = directory.path().join("output.log");
let empty = String::new();
assert!(!redaction_pending(None));
assert!(finalize_redaction(&[], &[]).is_ok());
assert!(require_redaction(&directory.path().join("missing-directory")).is_err());
assert!(redact(&missing, &[]).is_ok());
assert!(redact(directory.path(), &[]).is_err());
std::fs::write(&output, "unchanged").unwrap();
redact(&output, &[&empty]).unwrap();
assert_eq!(std::fs::read_to_string(&output).unwrap(), "unchanged");
assert!(rewrite_or_remove(&[&output], |_| Ok(())).is_ok());
let missing_error =
rewrite_or_remove(&[&missing], |_| Err("rewrite failed".to_string())).unwrap_err();
assert!(missing_error.contains("rewrite failed"));
assert!(!missing_error.contains("cleanup failed"));
let cleanup_error =
rewrite_or_remove(&[directory.path()], |_| Err("rewrite failed".to_string()))
.unwrap_err();
assert!(cleanup_error.contains("rewrite failed"));
assert!(cleanup_error.contains("cleanup failed"));
let missing_marker_directory = tempdir().unwrap();
std::fs::create_dir(missing_marker_directory.path().join(REDACTION_MARKER)).unwrap();
let absent_log = missing_marker_directory.path().join("absent.log");
assert!(finalize_redaction(&[&absent_log], &[]).is_err());
let existing_marker_directory = tempdir().unwrap();
std::fs::create_dir(existing_marker_directory.path().join(REDACTION_MARKER)).unwrap();
let existing_log = existing_marker_directory.path().join("output.log");
std::fs::write(&existing_log, "safe").unwrap();
assert!(finalize_redaction(&[&existing_log], &[&empty]).is_err());
let loop_path = directory.path().join("loop");
std::os::unix::fs::symlink(&loop_path, &loop_path).unwrap();
assert!(read_tail(Some(loop_path.to_str().unwrap()), 8).is_err());
let retained = tempdir().unwrap();
require_redaction(retained.path()).unwrap();
let directory_log = retained.path().join("output.log");
std::fs::create_dir(&directory_log).unwrap();
let error = finalize_redaction(&[&directory_log], &[]).unwrap_err();
assert!(error.contains("cleanup failed"));
assert!(directory_log.exists());
assert!(retained.path().join(REDACTION_MARKER).exists());
}
}