[High] audit_export streaming swallows serde_json::to_writer errors → malformed JSON possible
Severity: High · Domain: Error handling · Source: audit-04-error-handling.md
Location
crates/librefang-api/src/routes/audit.rs:380-388
Problem
The shape is:
let mut buf = Vec::new();
buf.push(b','); // separator first
let _ = serde_json::to_writer(&mut buf, &value); // ignored
If to_writer fails, the chunk contains only , — producing output like [ , {...}, , {...} ], which is invalid JSON. Even if to_writer into a Vec<u8> is practically infallible (no I/O), the let _ = masks the invariant; any future refactor that swaps the writer for a real tokio::io::AsyncWrite instantly silently corrupts exports.
Fix
Either:
serde_json::to_writer(&mut buf, &value)
.expect("serializing serde_json::Value into Vec<u8> is infallible");
or build via to_vec and concatenate:
let payload = serde_json::to_vec(&value).expect("infallible");
if !first { buf.push(b','); }
buf.extend_from_slice(&payload);
Tests
- Property test: round-trip arbitrary audit rows through export →
serde_json::from_slice::<Vec<Value>> succeeds for every output.
[High]
audit_exportstreaming swallowsserde_json::to_writererrors → malformed JSON possibleSeverity: High · Domain: Error handling · Source:
audit-04-error-handling.mdLocation
crates/librefang-api/src/routes/audit.rs:380-388Problem
The shape is:
If
to_writerfails, the chunk contains only,— producing output like[ , {...}, , {...} ], which is invalid JSON. Even ifto_writerinto aVec<u8>is practically infallible (no I/O), thelet _ =masks the invariant; any future refactor that swaps the writer for a realtokio::io::AsyncWriteinstantly silently corrupts exports.Fix
Either:
or build via
to_vecand concatenate:Tests
serde_json::from_slice::<Vec<Value>>succeeds for every output.