Repository navigation
fix(report): replace translate with str.replace in _json_for_script - #549
Conversation
- a multi-character str.translate table leaves the CPython ASCII fast path, so escaping the embedded JSON was ~10x slower than json.dumps on large reports (regression since 1.0.1) - on a 5.2 MB payload: before ~210 ms, after ~22 ms, json.dumps ~14 ms - str.replace on <, >, &, U+2028 and U+2029 keeps the fast path - output is byte-identical, checked by a new test against the 1.0.1 translate table - add a CHANGELOG entry Closes awslabs#457
Security Scan ResultsASH Security Scan Report
Scan Metadata
SummaryScanner ResultsThe table below shows findings by scanner, with status based on severity thresholds and dependencies:
Top 4 HotspotsFiles with the highest number of security findings:
Detailed FindingsShow 7 actionable findingsFinding 1: SECRET-HEX-HIGH-ENTROPY-STRING
Description: Code Snippet: Finding 2: B310
Description: Code Snippet: Finding 3: B108
Description: Code Snippet: Finding 4: B307
Description: Code Snippet: Finding 5: B307
Description: Code Snippet: Finding 6: B102
Description: Code Snippet: Finding 7: B310
Description: Code Snippet: Report generated by Automated Security Helper (ASH) at 2026-11-08T23:11:45+00:00 |
harrycav
left a comment
There was a problem hiding this comment.
Thanks for this. I re-ran it on d7f1930 against dev (ce6c100).
- Byte-identical to the 1.0.1 translate table over about 6,000 generated values: nested containers, astral and lone-surrogate strings,
</script>,<!--, literal<, NaN/inf. No differences, and no raw<,>or&in any output. - Each replacement only emits a backslash,
uand hex digits, so no later pair can rewrite an earlier one; order doesn't matter. - Mutation check: dropping the
<,>or&entry fails the new tests. Dropping U+2028/2029 doesn't, sincejson.dumpsalready escapes both under the defaultensure_ascii=True, as your comment says. Fine as a backstop. - 4.8 MB payload:
json.dumps23 ms, dev 352 ms, this branch 31 ms. - Reporting tests pass (162) and
ruff checkis clean.test_scalability_characteristicsfailed once under-n autoon its wall-clock ratio; it passes alone and doesn't touch this code, so I believe it's the known flake (#393).
LGTM.
The HTML report embeds each document's results in a <script> block via _json_for_script. json.dumps writes NaN and infinities as bare NaN and Infinity tokens: valid JavaScript, so the page loaded, but not JSON, so anything that extracted the payload and parsed it strictly failed. A blank numeric cell from pandas is NaN, so this was the ordinary case. _json_for_script now dumps with allow_nan=False. Finite data takes that first dump unchanged, so its bytes and cost are as before (#549's str.replace escaping is untouched). Only when the dump raises does a recursive pass replace NaN/inf/-inf (including numpy float64, nested in dicts, lists and tuples) with None before dumping again, still with allow_nan=False so a future miss fails loudly. A non-finite float dict key keeps the string json.dumps already wrote for it. Both embedded blobs, the document data and the field thresholds, go through this helper. On the page, null renders as an empty cell in the per-document views (main.js writes these values through textContent), the same as None already did, where NaN used to show as "NaN". Fixes #450
tl;dr: Since 1.0.1, writing an HTML report with
individual_results_jsonl_pathescaped its embedded JSON with a multi-characterstr.translatetable, which is about ten times slower thanjson.dumps._json_for_scriptnow applies the same five escapes withstr.replace. The page bytes are unchanged, so the #431 script-block protection is unchanged.Closes #457.
for agents
intent
#457 "Done when": use the
.replacechain; a test asserting byte-identical output against thetranslateversion for<,>,&, U+2028, U+2029 and non-ASCII text; the existingtests/reporting/html/test_script_embedding.pyguards pass; timing re-measured with the tree recorded.behavior
docs_json,thresholds_jsoninEvaluationHTMLReporter).<,>,&, U+2028, U+2029 are still written as\uXXXX.json.dumpsoptions, every other report section.code
src/stickler/reporting/html/html_reporter.py:_SCRIPT_SAFE(str.maketranstable) replaced by_SCRIPT_SAFE_ESCAPES(pairs);_json_for_scriptapplies them withstr.replace. The comment states why.tests/reporting/html/test_script_embedding.py:test_json_for_script_is_byte_identical_to_the_translate_escape, 9 cases, with the 1.0.1 table kept in the test as the oracle.CHANGELOG.md:[Unreleased]/### Fixedentry.commits
fix(report): replace translate with str.replace in _json_for_script: patch, fixes a 1.0.1 performance regression; no public contract changes.verification
devce6c100plus this change (the commit SHA is on this PR), CPython 3.12.13, a 5.2 MB list of dicts whosereasonstrings contain>=and<:json.dumps14 ms,_json_for_scriptbefore 210 ms, after 22 ms.</script>, backslashes, long strings), and over 200,000 strings on CPython 3.10 and 3.14.<or&escape fails the new test. Dropping U+2028/U+2029 changes nothing today, becausejson.dumpsalready escapes both underensure_ascii=True, as the code comment says.pytest tests/ -q -n auto:4110 passed, 13 skipped, 1 xfailed.ruff checkclean on the changed files.not covered
json.dumps; the issue measured 11x and 1.2x).