Skip to content

fix(entity): defuse entity-candidate ReDoS on long ASCII runs (#2065) - #2127

Merged
igorls merged 2 commits into
developfrom
fix/2065-onto-develop
Aug 2, 2026
Merged

fix(entity): defuse entity-candidate ReDoS on long ASCII runs (#2065)#2127
igorls merged 2 commits into
developfrom
fix/2065-onto-develop

Conversation

@igorls

@igorls igorls commented Aug 2, 2026

Copy link
Copy Markdown
Member

Summary

Conflict-resolved merge of #2065 onto current develop.

Closes #2065.

Test plan

  • tests/test_palace.py + tests/test_entity_detector.py pass

mvalentsev and others added 2 commits July 23, 2026 12:56
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>
@igorls
igorls requested a review from milla-jovovich as a code owner August 2, 2026 08:26
Copilot AI review requested due to automatic review settings August 2, 2026 08:26
@igorls
igorls merged commit f2e9fef into develop Aug 2, 2026
8 of 9 checks passed

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 in extract_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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants