fix: harden against confirmed review findings - #2
Merged
Conversation
Three verified hardening fixes to the liberal XML/feed parser, each with a regression test that fails on the pre-fix source. 1. Missing </item> no longer drops the whole feed (feed.mojo). A dropped end tag between two items, or before the closing </channel>, silently parsed to zero items. Now flush the open item both when a new item/entry starts while one is open and whenever the item element leaves the stack via an ancestor close. feedparser recovers both; so do we. 2. Numeric character references to forbidden XML scalars (NUL, C0 controls, surrogates, out-of-range) are mapped to U+FFFD instead of emitted verbatim (xml_parser.mojo). A raw NUL/control byte can truncate or corrupt a downstream consumer. Strict mode raises. 3. Bounded the element-name stack (_MAX_DEPTH = 1024) (feed.mojo). An adversarial document with thousands of unclosed start tags grew the stack without bound and made every liberal end-tag reverse scan O(depth) -> O(n^2). Bounding the stack bounds both; parse time is now linear. Co-Authored-By: Claude <noreply@anthropic.com>
Owner
Author
|
🤖 Independent Claude review: Ready to mark for review — all three fixes are correct and I reproduced the pre-fix/post-fix behavior locally (76 tests green on this branch; pixi pin solves + builds). Verified independently (cloned fresh, reverted src to
Non-blocking (fixes are sound; these are test/scope gaps, not broken behavior):
|
conorbronsdon
marked this pull request as ready for review
July 6, 2026 07:58
Owner
Author
|
Code review (opus, static — verify CI): SHIP All three fixes address real defects and resolve cleanly (hand-traced against the
Idioms clean ( |
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.
From an automated multi-agent review (personal-context#62); implemented + verified by Claude Code.
Three confirmed hardening findings in the liberal XML/feed parser. Each was reproduced on the current source, fixed, re-verified, and locked in with a regression test that fails pre-fix. Full suite (76 tests) green after changes; sources are
mojo formatclean.1. Missing
</item>silently drops the whole feed (HIGH, correctness) —src/feed/feed.mojoA single dropped
</item>desynced item tracking and discarded every item.Before
After
Fix: flush the open item on two paths — (a) a new
<item>/<entry>opening while one is still open, and (b) whenever the item element leaves the name-stack via an ancestor close (stack.shrinkat/belowitem_depth). Matches feedparser recovery. Regression tests:test_missing_mid_item_close_recovers_both,test_missing_last_item_close_recovers_via_ancestor,test_missing_entry_close_recovers_atom.2. Numeric char refs to NUL / C0 controls emitted verbatim (security) —
src/feed/xml_parser.mojo�,,decoded straight to raw control bytes that can truncate/corrupt a downstream consumer.Before:
<t>a�bcd</t>→ output contains a NUL byte and C0 controls (7 bytes).After: each forbidden scalar → U+FFFD (13 bytes, no NUL/C0). Legal control refs (
	/ / ) still pass through. Strict mode raises. Same validation class as mojo-xml (XML 1.0 Char production,_is_valid_xml_char). Regression tests:test_forbidden_control_char_ref_becomes_replacement,test_strict_forbidden_char_ref.3. O(n²) end-tag reverse scan + unbounded name-stack (security / DoS) —
src/feed/feed.mojoThousands of unclosed start tags grew the stack without bound; each liberal end-tag reverse scan was O(depth), so stray-end-tag-heavy input was O(n²).
Before (timing, n = start tags):
1000→1.2ms 2000→4.5ms 4000→18ms 8000→67ms(doubling n ≈ 4× time — quadratic).After:
1000→1.1ms 2000→2.1ms 4000→4.3ms 8000→8.4ms(linear). Fix: cap the name-stack at_MAX_DEPTH = 1024(far above any legitimate feed); bounding depth bounds both memory and per-tag scan cost. Regression test:test_deep_unclosed_nesting_is_bounded.Notes
pixi.tomlalready pinsmojo = ">=1.0.0b3.dev2026070506,<2"(the correct dev-nightly-aware form; solves and builds clean). Not the buggy>=1.0.0b3.src/feed/*.mojoandtest/*.mojo). If a SECURITY note or conformance count needs updating for findings 2/3, that belongs in the docs PR.Verification
pixi run test: 76 tests, all passing (32 xml_parser + 23 feed + 11 date + 10 real_feed). No live-server tests skipped.pixi run mojo formatclean; bench builds.