This commit is contained in:
Mateusz Gruszczyński committed 2026-10-17 11:57:16 +02:00
1 parent 6b937aaf85
commit 87c8393d86
79 files changed
+6727 -297

No files matched your search

+131
View File
@@ -209,6 +209,41 @@ mod tests {
assert!(!text.contains("pem-data"));
}
#[test]
fn error_redaction_preserves_json_and_debug_escaping_without_leaking_secret_suffixes() {
for secret in ["pem-data", "left\"right", "left\\right", "ending\\", "left\\\"right", "first\nsecond"] {
let plain = serde_json::to_string(&serde_json::json!({
"private_key": secret, "visible": "keep"
})).unwrap();
let redacted = redact_text(&plain);
let value: Value = serde_json::from_str(&redacted).unwrap();
assert_eq!(value["private_key"], "[REDACTED]");
assert_eq!(value["visible"], "keep");
// EventVisitor::record_debug uses this exact formatting operation.
let debug = format!("{plain:?}");
let debug_redacted = redact_text(&debug);
let decoded: String = serde_json::from_str(&debug_redacted).unwrap();
let value: Value = serde_json::from_str(&decoded).unwrap();
assert_eq!(value["private_key"], "[REDACTED]");
assert_eq!(value["visible"], "keep");
assert_eq!(
redact_text(&format!("before {debug} after")),
format!("before {debug_redacted} after")
);
}
}
#[test]
fn error_redaction_removes_private_pem_and_preserves_public_diagnostics() {
for label in ["PRIVATE KEY", "RSA PRIVATE KEY", "EC PRIVATE KEY", "ENCRYPTED PRIVATE KEY"] {
let input = format!("before -----BEGIN {label}-----\nsynthetic-secret\n-----END {label}----- after");
assert_eq!(redact_text(&input), "before [REDACTED PRIVATE KEY] after");
}
let public = "-----BEGIN CERTIFICATE-----\npublic-test-data\n-----END CERTIFICATE-----";
assert_eq!(redact_text(public), public);
}
#[test]
fn error_json_is_one_record_and_reserved_fields_cannot_be_overwritten() {
let event = ErrorEvent {
@@ -291,4 +326,100 @@ mod tests {
LIVE_WINDOW + 80
);
}
#[test]
fn full_history_evicts_oldest_keeps_exact_fields_and_does_not_count_eviction_as_drop() {
fn history_entry(id: usize) -> LogEntry {
let mut item = entry();
item.id = id as u64;
item.time_ms += id as u64;
item.path = format!("/history/{id}");
item.authority = Some("app.example:443".into());
item.matched_by = Some("host");
item.upstream_error = Some("diagnostic with \"quotes\" and UTF-8: €".into());
item
}
let logs = Logs::default();
for id in 0..HISTORY_CAPACITY + 3 {
logs.push(history_entry(id));
}
let retained = logs.recent_limit(HISTORY_CAPACITY + 1);
assert_eq!(retained.len(), HISTORY_CAPACITY);
assert_eq!(
retained.iter().map(|item| item.id).collect::<Vec<_>>(),
(3..HISTORY_CAPACITY + 3).rev().map(|id| id as u64).collect::<Vec<_>>()
);
for item in [&retained[0], &retained[HISTORY_CAPACITY / 2], retained.last().unwrap()] {
assert_eq!(
serde_json::to_value(item).unwrap(),
serde_json::to_value(history_entry(item.id as usize)).unwrap()
);
}
assert_eq!(logs.recent_limit(0).len(), 0);
assert_eq!(logs.recent_limit(2)[0].id, (HISTORY_CAPACITY + 2) as u64);
assert_eq!(logs.dropped.load(Ordering::Relaxed), 0);
let stats = logs.stats();
assert_eq!(stats.queue_capacity, SINK_QUEUE);
assert_eq!(stats.queue_dropped, 0, "disabled sinks must not enqueue");
assert_eq!(stats.sent, 0);
assert_eq!(stats.errors, 0);
}
#[tokio::test]
async fn history_contention_counts_one_drop_but_preserves_live_and_file_delivery() {
let directory = tempfile::tempdir().unwrap();
let path = directory.path().join("access.jsonl");
let mut settings = Settings::default();
settings.file.enabled = true;
settings.file.path = path.to_string_lossy().into_owned();
let shutdown = CancellationToken::new();
let logs = Logs::start(settings, shutdown.clone());
let mut live = logs.subscribe();
let mut sentinel = entry();
sentinel.id = 6;
let mut history = logs.entries.lock().unwrap();
history.push_back(sentinel.clone());
let mut item = entry();
item.path = "/contention/€\"quoted\"".into();
item.authority = Some("app.example:443".into());
item.matched_by = Some("host");
let expected = serde_json::to_value(&item).unwrap();
let producer = logs.clone();
let (finished, completion) = std::sync::mpsc::channel();
let worker = std::thread::spawn(move || {
producer.push(item);
finished.send(()).unwrap();
});
// Release the guard before joining even on failure, so an accidental
// blocking-lock regression fails this test instead of hanging the suite.
let completed = completion.recv_timeout(Duration::from_secs(2));
drop(history);
worker.join().unwrap();
completed.expect("history contention must not block live/sink delivery");
assert_eq!(logs.dropped.load(Ordering::Relaxed), 1);
assert_eq!(serde_json::to_value(logs.recent()).unwrap(), json!([sentinel]));
assert_eq!(serde_json::to_value(live.try_recv().unwrap()).unwrap(), expected);
assert!(matches!(live.try_recv(), Err(broadcast::error::TryRecvError::Empty)));
let persisted = tokio::time::timeout(Duration::from_secs(5), async {
loop {
if let Ok(content) = tokio::fs::read_to_string(&path).await {
if content.ends_with('\n') && logs.stats().sent == 1 {
break content;
}
}
tokio::time::sleep(Duration::from_millis(20)).await;
}
}).await.expect("contended history entry must still reach the configured file sink");
shutdown.cancel();
assert_eq!(persisted.lines().count(), 1);
assert_eq!(serde_json::from_str::<Value>(persisted.trim_end()).unwrap(), expected);
let stats = logs.stats();
assert_eq!(stats.queue_capacity, SINK_QUEUE);
assert_eq!(stats.queue_dropped, 0);
assert_eq!(stats.sent, 1);
assert_eq!(stats.errors, 0);
}
}