Fix ReDoS in chessboard FEN sanitize (/ .+$/) used by examples/wchess #3374
+1,313
−1,259
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.
What does this PR do?
Reports and fixes a potential Regular Expression Denial of Service (ReDoS) in the FEN–sanitize step of the embedded chessboard script. With a crafted input the regex can cause extremely high CPU usage and freeze the process.
File:
examples/wchess/wchess.wasm/chessboardjs-1.0.0/js/chessboard-1.0.0.js
Before
After (fix)
Why is this needed?
The pattern
/ .+$/
is vulnerable to catastrophic backtracking.When the input looks like:
.
does not cross the newline, while$
forces a match at end-of-string; the leading space overlaps with the first set of.+
, so the engine backtracks quadratically from each starting space. This leads to long stalls (tens of seconds on common hardware).The new pattern adds a negative lookahead
(?! )
to ensure the.+
part does not start with another space, removing the overlap and making the match linear-time. For valid FEN (which uses a single space after the position), behavior is unchanged. Inputs with multiple consecutive spaces are intentionally not trimmed (safer semantics).How I reproduced and verified
before:

after:

step:
git clone https://github.com/wukunyu264/whisper.cpp.git
cd whisper.cpp
npm i --prefix js-tests
cd js-tests
npm test
Type of change
Backwards compatibility
Security / performance impact