perf: linear nested [quote] and nested brackets (was O(n^2) DoS) - #261
Merged
Conversation
Two converter/parser quadratic blowups found by a security audit: - BbcodeToDjot::parseQuotesWithDepth recursed on each closed quote's inner content and used unanchored preg_match per position, so deeply nested [quote] was O(n^2) (~19s for 90KB). Now a single stack-based pass, O(n). - The inline parser ran the char-by-char bracket-depth scan for every even when the text has no link/span trigger (, , ), so a balanced nested run like [[[[x]]]] was O(n^2) (~33s at 20k). parseLink now short-circuits on a memoized trigger-presence check (matching carve-php), O(n). Output unchanged; regression tests added.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #261 +/- ##
============================================
- Coverage 92.37% 92.36% -0.02%
Complexity 3637 3637
============================================
Files 109 109
Lines 10287 10292 +5
============================================
+ Hits 9503 9506 +3
- Misses 784 786 +2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Two quadratic blowups found by a security audit:
BbcodeToDjot::parseQuotesWithDepthrecursed on each closed quote's inner content and used an unanchoredpreg_matchper position, so deeply nested[quote]was O(n²) (~19s for 90KB). Now a single stack-based pass, O(n) (~0.05s).[even when the text has no link/span trigger, so a balanced nested run like[[[[x]]]]was O(n²) (~33s at 20k).parseLinknow short-circuits on a memoized trigger-presence check (matching carve-php), O(n) (~0.1s).Output unchanged for well-formed input; regression tests added. Full suite (2699), phpstan, phpcs green.