fix: resolve link hrefs in a single pass (O(n) instead of O(n^2)) - #118
Open
BrianWillows wants to merge 1 commit into
Open
fix: resolve link hrefs in a single pass (O(n) instead of O(n^2))#118BrianWillows wants to merge 1 commit into
BrianWillows wants to merge 1 commit into
Conversation
parse() rewrote the whole output string via out.replace('<a>', ...) on
every closing ']', rescanning all prior output each time -> O(n^2) on
link-heavy input. Since markdown is untrusted input, this is a CWE-407
DoS: ~16k links (~96KB) blocked the event loop for ~2.4s, growing
quadratically.
Record each '<a>' marker's offset as output is appended and fill hrefs in
one final left-to-right pass. Same 'first unresolved marker wins'
semantics (verified byte-for-byte identical on 3000+ fuzzed inputs incl.
nested/adjacent/reference links, images and literal <a> in source);
existing tests pass. Parsing is now linear.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
Summary
parse()resolves each link'shrefby rewriting the entire output stringon every closing
]:outgrows with the input, andString.replacerescans it from the start eachtime, so a document with N links is O(n²). Because Markdown is untrusted
input by definition, this is a CPU-exhaustion / DoS vector (CWE-407): a single
link-heavy field can block the event loop for seconds.
Measured on the current code, input
'[a](b)'.repeat(n):Fix
Instead of rewriting
outon every], record the offset of each<a>markeras output is appended, and fill in the hrefs in a single left-to-right pass at
the end. Offsets stay valid because the parser only ever appends to
out. Thiskeeps the exact same "first unresolved
<a>wins" semantics while makingparsing linear.
Verification
npm test, 33 passing).links, nested/adjacent/reference links, images, and literal
<a>in thesource — output is byte-for-byte identical before and after.
Notes
Found and fixed with AI assistance (Claude). Happy to adjust naming/comments or
add a regression test if you'd like.