Repository navigation
fix(security): polynomial ReDoS in comboAgentMiddleware regex → main (CodeQL #612/#613) #3983
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ import assert from "node:assert/strict"; | |
| import { | ||
| stripModelTags, | ||
| injectModelTag, | ||
| extractPinnedModel, | ||
| } from "../../open-sse/services/comboAgentMiddleware.ts"; | ||
|
|
||
| // Regression for the non-global CACHE_TAG_PATTERN bug: `.replace()` with a | ||
|
|
@@ -56,3 +57,45 @@ describe("comboAgentMiddleware — <omniModel> tag stripping (non-global regex b | |
| ); | ||
| }); | ||
| }); | ||
|
|
||
| // Regression for CodeQL js/polynomial-redos (#3870): the unbounded `(?:\\n|\n|\r)*` | ||
| // prefix/suffix on the unanchored CACHE_TAG_PATTERN made `.test()` / `.replace()` | ||
| // run in O(n²) on inputs with many newlines. The fix drops the surrounding runs from | ||
| // the detection pattern and bounds them ({0,16}) in the global strip pattern. | ||
| describe("comboAgentMiddleware — ReDoS safety + newline-wrapped tags (#3870)", () => { | ||
| test("stripModelTags stays linear on a long run of newlines (no tag present)", () => { | ||
| const evil = "\n".repeat(50_000); | ||
| const start = process.hrtime.bigint(); | ||
| const stripped = stripModelTags([{ role: "user", content: evil }]); | ||
| const elapsedMs = Number(process.hrtime.bigint() - start) / 1e6; | ||
| assert.equal(stripped[0].content, evil, "no tag → content returned unchanged"); | ||
| assert.ok(elapsedMs < 1000, `regex must stay linear; took ${elapsedMs.toFixed(1)}ms`); | ||
| }); | ||
|
|
||
| test("stripModelTags stays linear on many newlines before a never-closing tag", () => { | ||
| const evil = "\n".repeat(50_000) + "<omniModel" + "x".repeat(2000); | ||
| const start = process.hrtime.bigint(); | ||
| stripModelTags([{ role: "user", content: evil }]); | ||
| const elapsedMs = Number(process.hrtime.bigint() - start) / 1e6; | ||
| assert.ok(elapsedMs < 1000, `regex must stay linear; took ${elapsedMs.toFixed(1)}ms`); | ||
| }); | ||
|
|
||
| test("extractPinnedModel still finds a tag wrapped in newlines", () => { | ||
| const messages = [ | ||
| { | ||
| role: "assistant", | ||
| content: "answer\n\n<omniModel>claude/claude-opus-4-8</omniModel>\n\n", | ||
| }, | ||
| ]; | ||
| assert.equal(extractPinnedModel(messages), "claude/claude-opus-4-8"); | ||
| }); | ||
|
|
||
| test("stripModelTags removes the newline run wrapping a tag (no blank line left)", () => { | ||
| const out = String( | ||
| stripModelTags([{ role: "user", content: "before\n\n<omniModel>a/b</omniModel>\n\nafter" }])[0] | ||
| .content | ||
| ); | ||
| assert.ok(!out.includes("<omniModel>"), "tag removed"); | ||
| assert.ok(!out.includes("\n\n\n"), "no triple newline left from stripping"); | ||
| }); | ||
|
Comment on lines
+93
to
+100
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧪 Update Regression Test to Prevent Word MergingLet's update the regression test to assert that the paragraph break is preserved and words are not merged when stripping the tag. test("stripModelTags removes the newline run wrapping a tag (no blank line left)", () => {
const out = String(
stripModelTags([{ role: "user", content: "before\n\n<omniModel>a/b</omniModel>\n\nafter" }])[0]
.content
);
assert.ok(!out.includes("<omniModel>"), "tag removed");
assert.ok(!out.includes("\n\n\n"), "no triple newline left from stripping");
assert.equal(out, "before\n\nafter", "should preserve the paragraph break and not merge words");
}); |
||
| }); | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By matching and stripping up to 16 newlines both before and after the
<omniModel>tag, any text preceding and succeeding the tag will be merged together.For example, in an inlined chat history (like the Open WebUI follow-up requests mentioned in the comments), you might have:
With the current regex, the
\nbefore and\n\nafter the tag are both consumed and replaced with"", resulting in:This merges the assistant's message with the user's message, corrupting the chat history structure sent to the provider.
Suggested Fix
To avoid merging words/lines while still cleaning up the tag and its surrounding blank lines, we should only consume the newline run on one side (e.g., the trailing side). Any leading newlines before the tag will either act as the single natural separator between the preceding and succeeding text, or (if the tag was at the very end of the message) will be cleanly stripped by the subsequent
.trimEnd()call.