fix(entity): defuse entity-candidate ReDoS on long ASCII runs (#2065) - #2127
Merged
Conversation
The English entity-candidate pattern in i18n/en.json backtracks catastrophically on a long unbroken run of printable ASCII (base64, minified JS, hashes, data URIs), pinning `mempalace mine` on a single ~5000-char window for hours. Collapse such runs to a space before single-word candidate matching, in both consumers that apply the pattern (palace._candidate_entity_words and entity_detector.extract_candidates). Scoped to ASCII ([!-~]) so non-ASCII scripts stay untouched — a CJK paragraph is one unbroken run with no ASCII whitespace, and zh-CN/zh-TW have no multi-word fallback, so a blanket collapse would erase their detection. Threshold 24 sits above the 20-char cap of the simple-name pattern, so no real name is dropped. Fixes #2063. Co-authored-by: Ryan Wei <9876551+RyanWei@users.noreply.github.com> Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
3 tasks
Contributor
There was a problem hiding this comment.
Pull request overview
This PR mitigates a catastrophic backtracking (ReDoS) risk in entity-candidate extraction by collapsing long unbroken printable-ASCII runs (e.g., base64/minified blobs) before applying the single-word candidate regexes, while explicitly preserving non-ASCII scripts (CJK/Cyrillic/etc.) to avoid regressions in those locales.
Changes:
- Add
_collapse_long_ascii_runs()(ASCII-only, length-thresholded) and apply it to single-word candidate matching inextract_candidates(). - Apply the same mitigation to
palace._candidate_entity_words()to protect the mining hot path. - Add regression tests covering the threshold boundary, non-ASCII preservation (zh), and a subprocess timeout guard to prevent CI hangs.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
mempalace/entity_detector.py |
Introduces and applies the ASCII-run collapse prepass before single-word candidate regex matching. |
mempalace/palace.py |
Applies the same collapse prepass before cached candidate regex findall() in _candidate_entity_words(). |
tests/test_entity_detector.py |
Adds scope/boundary/property/subprocess regression tests to ensure the mitigation is effective and doesn’t regress CJK. |
tests/test_palace.py |
Adds a targeted regression test for _candidate_entity_words() dropping overlong ASCII blobs while keeping real names. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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
Conflict-resolved merge of #2065 onto current
develop.tests/test_palace.pyimports with single-writer + extract_mode testsCloses #2065.
Test plan
tests/test_palace.py+tests/test_entity_detector.pypass