Skip to content

fix(api/audit): expect() on serde_json::to_writer in stream_json (closes #5360)#5361

Merged
houko merged 7 commits into
mainfrom
fix/audit-export-json
May 22, 2026
Merged

fix(api/audit): expect() on serde_json::to_writer in stream_json (closes #5360)#5361
houko merged 7 commits into
mainfrom
fix/audit-export-json

Conversation

@houko

@houko houko commented May 20, 2026

Copy link
Copy Markdown
Contributor

Closes #5360


Summary

Audit: docs/issues/audit-export-malformed-json.md (Severity: High).

routes/audit.rs:385 had:

let _ = serde_json::to_writer(&mut buf, &value);

If to_writer failed, the chunk contained only , — producing
output like [ , {...}, , {...} ] which is invalid JSON.

serde_json::to_writer into a Vec<u8> is practically infallible
today (no I/O involved), but the let _ = masked the invariant:
any future refactor that swaps the writer for a real
tokio::io::AsyncWrite (S3 streaming, gzip pipe, network tee, …)
would silently corrupt every export.

Fix

-let _ = serde_json::to_writer(&mut buf, &value);
+serde_json::to_writer(&mut buf, &value)
+    .expect("serializing serde_json::Value into Vec<u8> is infallible");

Makes the infallibility load-bearing — any future writer-swap
that CAN fail is forced to handle the error rather than inherit
the silent-corruption behaviour.

Regression guard

test_stream_json_multi_entry_is_well_formed builds a 5-entry
export and round-trips it through serde_json::from_str::<Vec< serde_json::Value>>. The existing
test_stream_json_includes_prev_hash exercised the 1-entry
case; the new test pins the multi-entry separator handling that
the original let _ = could corrupt under failure.

Verification

cargo test -p librefang-api --lib audit::tests::test_stream_json
cargo clippy -p librefang-api --all-targets -- -D warnings

Both green.

Test plan

  • CI green
  • GET /api/audit/export?format=json with 10+ audit rows →
    body parses as Vec<Value> (browser dev tools / jq .)

@github-actions github-actions Bot added area/docs Documentation and guides size/M 50-249 lines changed labels May 20, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

 #5360)

audit: audit-export-malformed-json (Severity: High).

routes/audit.rs:385 used:
  let _ = serde_json::to_writer(&mut buf, &value);

If to_writer fails, the chunk contains only ',' — producing
output like [ , {...}, , {...} ] which is invalid JSON.

to_writer into Vec<u8> is practically infallible today (no I/O),
but the let _ masks the invariant: any future refactor that swaps
the writer for a real tokio::io::AsyncWrite (S3 streaming, gzip
pipe, etc.) silently corrupts every export. .expect() makes the
infallibility load-bearing — any future writer-swap that CAN fail
is forced to handle the error.

Regression test
test_stream_json_multi_entry_is_well_formed pins the EXTERNAL
contract: every 5-entry export round-trips through
serde_json::from_str::<Vec<Value>>. cargo test + cargo clippy
clean.
@houko
houko force-pushed the fix/audit-export-json branch from db74cc0 to ebc2804 Compare May 21, 2026 03:38
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@github-actions github-actions Bot added the has-conflicts PR has merge conflicts that need resolution label May 21, 2026
houko added 2 commits May 21, 2026 20:53
# Conflicts:
#	openapi.json
#	sdk/go/librefang.go
#	sdk/javascript/index.js
#	sdk/python/librefang/librefang_client.py
#	sdk/rust/src/lib.rs
# Conflicts:
#	xtask/baselines/openapi.sha256
@github-actions github-actions Bot added ready-for-review PR is ready for maintainer review and removed has-conflicts PR has merge conflicts that need resolution labels May 21, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@houko
houko merged commit 7ac9d46 into main May 22, 2026
7 checks passed
@houko
houko deleted the fix/audit-export-json branch May 22, 2026 01:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/docs Documentation and guides ready-for-review PR is ready for maintainer review size/M 50-249 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(api/audit): audit_export streaming swallows serde_json error → malformed JSON possible

1 participant