dashboard: format NDJSON / SSE / form-encoded bodies in the Body tab#37
Open
krishnamallam wants to merge 2 commits into
Open
dashboard: format NDJSON / SSE / form-encoded bodies in the Body tab#37krishnamallam wants to merge 2 commits into
krishnamallam wants to merge 2 commits into
Conversation
…y tab The Body tab pretty-printed only single-object JSON; structured-but-not-single bodies (NDJSON, SSE event streams, urlencoded forms) rendered as one flat line. Add _format_body(body, content_type): JSON-first, then per-type formatting with a verbatim fallback that never raises. content_type is plumbed from the detail route; redact_marks + HTML escaping still run after formatting.
… cases) Adversarial review of the formatter found and fixed: - DoS: uncapped ljust in _format_form let one long key pad every line (a 100KB urlencoded body rendered ~650MB). Cap the alignment column at 40. - Fidelity/over-matching: parse_qsl decoded values and accepted non-form text. Now require every &-segment to be a real key=value pair (else verbatim) and show raw, undecoded values for audit accuracy. - Robustness: coerce a non-string content_type to None so a malformed row can't 500 the Body tab. - Wire-accurate NDJSON/SSE line splitting (split on \n, strip \r) so a Unicode line separator inside a JSON string can't fragment a logical line. Adds regression + edge tests: DoS output bound, raw display, content-type coercion, charset param, SSE/NDJSON verbatim fallbacks, CRLF, alignment.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Addresses #36.
Summary
The dashboard Body tab pretty-printed only single-object JSON. Structured-but-not-single bodies rendered as one flat line. This adds type-aware formatting:
application/x-ndjson,application/jsonl) — each line pretty-printed.text/event-stream) — eachdata:JSON payload indented; non-data lines kept.application/x-www-form-urlencoded) — rawkey = valuelines, aligned on=._format_bodynever raises;redact_marks+ HTML-escaping still run after formatting.content_typeis plumbed from the detail route (read from the stored request headers).Hardening (from an adversarial review of the diff)
str.ljust(width)with an uncapped width — one ~50 KB form key padded every other line, so a 100 KB urlencoded body rendered ~650 MB server-side. The alignment column is now capped, keeping output proportional to input.key=valuesegments are formatted — prose / partial bodies fall back to verbatim instead of being reshaped.content_typeis coerced toNonerather than throwing.split("\n")+ strip\r).Test plan
uv run pytest— 191 passed, 1 skipped (20format_bodytests, incl. DoS output bound, raw-display fidelity, content-type coercion, charset param, SSE/NDJSON verbatim fallbacks, CRLF, alignment, route-level NDJSON + form rendering)uv run ruff check . && uv run ruff format --check . && uv run mypy upbox— cleanNote on #36
This handles the non-single-JSON cause from #36's triage. The originally reported symptom (a small valid JSON body showing unindented) was not reproduced — the formatter renders valid JSON correctly (already covered by existing passing tests), so that symptom is most likely a stale running
upbox startprocess (a restart picks up merged code), not a code bug. Leaving #36 open so that can be confirmed after merge rather than auto-closing it.