Repository navigation
[lexical][lexical-extension][lexical-markdown][lexical-playground][lexical-yjs] Bug Fix: Linear-time regexes, prototype-pollution guards, and faster CSS parsing - #8593
Conversation
…rf/Security: Remove polynomial-time regexes and guard against prototype pollution Audited the library for ReDoS-prone (polynomial-time) regular expressions and confirmed the offenders empirically (clean O(n^2) scaling on adversarial input). Refactored each one to run in linear time; behavior is preserved and verified by differential testing (2.5M+ inputs, incl. an exhaustive sweep for the table-divider). Constrained to Safari 15+ syntax (no atomic groups, possessive quantifiers, or lookbehind). - lexical (setDOMStyle): `/\s*!important\s*$/i` was O(n^2) on whitespace-heavy style values (it runs on every applied style). Replaced with trimEnd + slice. - lexical-markdown / lexical-playground (markdown table divider): replaced `/^(\| ?:?-*:? ?)+\|\s?$/` (a `-*` nested inside `(...)+`, which JSC may run super-linearly) with a guaranteed-O(n) manual scan, `isTableRowDivider`. - lexical-playground (AutoLink email matcher, url validator): the unbounded `+`/`*` quantifiers backtracked in O(n^2) under RegExp.exec; bounded them to RFC-generous limits so every realistic address/URL still matches. Also fixed a prototype-pollution sink in deepThemeMergeInPlace: a malicious `__proto__` key (e.g. from JSON.parse) leaked into Object.prototype. The merge now skips `__proto__`/`constructor`/`prototype` and only copies own keys, with regression tests. https://claude.ai/code/session_019a6JqhMNpfJ1o2UgtidAXg
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
…oto__ in serialized node state
Editor-state deserialization wrote unknown-state keys straight from
serialized (potentially untrusted) JSON into a plain object:
`this.unknownState[k] = v`. A crafted `{"$":{"__proto__":{...}}}` —
`__proto__` is an own enumerable key once it comes through JSON.parse —
re-parented the record. Because NodeState.getValue resolves keys with the
`in` operator (which walks the prototype chain), an attacker could inject
arbitrary state values: importing `{"__proto__":{"someKey":"evil"}}` made
`$getState(node, someKeyState)` return "evil" instead of the default.
NodeState now skips `__proto__`/`constructor`/`prototype` (never valid
createState keys) at both write sites (updateFromUnknown and
parseAndPruneNextUnknownState). The collab-v2 Yjs importer (SyncV2) applies
the same guard to remote property attributes; its state attributes already
route through the guarded NodeState path. Added a regression test that
fails without the guard.
https://claude.ai/code/session_019a6JqhMNpfJ1o2UgtidAXg
…bleRowDivider parser The Markdown table-delimiter scan was duplicated in lexical-markdown and the playground's custom transformer set. Export `isTableRowDivider` from `@lexical/markdown` and reuse it in the playground instead of keeping a second copy. https://claude.ai/code/session_019a6JqhMNpfJ1o2UgtidAXg
The CSS parser built the current property/value by appending one character at a time (`currentValue += char`), which is slow for long values. Accumulate contiguous runs with `slice` instead, flushing only at dropped characters (comments) and delimiters (`:`/`;`). Behavior is unchanged (verified identical across 200k+ random and edge-case inputs); throughput improves ~33% on typical inline styles and ~3.3x on very long values. Added regression tests covering comments mid-value, escaped quotes, and ':'/';' inside parentheses. https://claude.ai/code/session_019a6JqhMNpfJ1o2UgtidAXg
Review: Linear-Time Regexes, Prototype-Pollution Guards, and Faster CSS ParsingReviewed by: Navi (AI review assistant for @potatowagon) SummaryA security and performance hardening PR that addresses three categories of issues:
What I Verified
RecommendationApprove ✅ — This is a well-crafted security/performance hardening PR from a maintainer. All changes are correctness-preserving, well-tested, and CI is fully green. The prototype pollution guards are particularly important for collaborative editing scenarios where remote peers could inject malicious payloads. — via Navi on behalf of @potatowagon |
Picks up meta#8593 (linear-time regexes / prototype-pollution guards / faster CSS parsing) and meta#8598 (TabNode.setTextContent Safari IME fix) — both touch lexical/extension/markdown/playground/yjs and don't intersect with the release tooling on this branch, so the merge is clean. https://claude.ai/code/session_01UEAeW4ZkfApZnrJTGc6Xm9
Description
This PR audits the library for two classes of issue — polynomial-time (ReDoS) regular expressions and prototype pollution — fixes them, de-duplicates a parser, and speeds up CSS parsing. All regex work stays within Safari 15+ syntax (no atomic groups, possessive quantifiers, or lookbehind), and every change preserves existing behavior, verified by differential testing against the originals.
Polynomial-time regexes → linear
Each offender was confirmed empirically (clean O(n²) scaling on adversarial input) and rewritten to run in linear time.
IMPORTANT_REG_EXP/\s*!important\s*$/ilexical(setDOMStyle, runs on every applied style value)trimEnd+slice, no regexEMAIL_REGEXlexical-playgroundAutoLinkurlRegExp(Stack Overflow URL regex)lexical-playgroundvalidateUrlTABLE_ROW_DIVIDER_REG_EXP/^(| ?:?-*:? ?)+|\s?$/lexical-markdown+ playground-*nested in(...)+can backtrack under Safari/JavaScriptCoreisTableRowDividerThe
LINKimport/export regexes,CODE_SINGLE_LINE_REGEX, and the AutoLinkURL_REGEXwere also benchmarked and confirmed linear, so they are left unchanged.Prototype pollution
deepThemeMergeInPlace(@lexical/extension): a__proto__key fromJSON.parseleaked intoObject.prototypeglobally; the recursive merge now skips__proto__/constructor/prototypeand only copies own keys.LexicalNodeState(core) andSyncV2(collab-v2): unknown-state deserialization wrote keys straight from serialized JSON, so a crafted{"$":{"__proto__":{…}}}re-parented the record. BecausegetValueresolves keys with theinoperator (which walks the prototype chain), this allowed state injection —$getStatereturned the attacker's value instead of the default. Both write sites now skip the dangerous keys, and the Yjs importer applies the same guard to remote property attributes (its state attributes already route through the guardedNodeStatepath).__proto__setter;Map/YMap.set/setAttribute/setData/setPropertytake string arguments; style writes only ever store strings; and the remaining computed keys are dev-controlled (node types, transformer tags,createStatekeys).Refactor
isTableRowDivideris now exported from@lexical/markdownand reused by the playground instead of being duplicated.Performance:
getStyleObjectFromCSSThe parser built the current property/value by appending one character at a time; it now accumulates contiguous runs with
slice, flushing only at dropped characters (comments) and delimiters (:/;). Throughput improves ~33% on typical inline styles and ~3.3× on very long values (~372ms → ~102ms for a 200k-char value), with byte-identical output.Test plan
Before
IMPORTANT_REG_EXPon whitespace-only input scaled quadratically (21ms → 83ms → 329ms → 1303ms as N doubled 4k → 8k → 16k → 32k);EMAIL_REGEXandurlRegExpshowed the same O(n²) curve on adversarial input.deepThemeMergeInPlace(target, JSON.parse('{"__proto__":{"polluted":"YES"}}'))set({}).polluted === 'YES'(global pollution), and importing{"$":{"__proto__":{"injectedKey":"evil"}}}made$getState(node, injectedKeyState)return'evil'instead of the default.getStyleObjectFromCSSbuilt values via per-character concatenation (~372ms for a single 200k-char value).After
IMPORTANT~1303ms → <0.1ms @32k, CSS long-value ~372ms → ~102ms).!importantstrip, email/url regexes, and CSS parser — 0 mismatches; the email/URL corpora include every address/URL the e2e suite asserts.deepThemeMergeInPlaceandNodeState(the latter fails without the fix), and CSS-parser edge cases (comments mid-value, escaped quotes, and:/;inside parentheses).pnpm tscpasses monorepo-wide; ESLint and Prettier are clean; existing unit suites for the touched packages pass (setDOMStyle,MarkdownTransformers,LexicalMarkdown,LexicalNodeState,LinkExtension,HTMLCopyAndPaste,deepThemeMergeInPlace,LexicalBuilder).