Fix polynomial regular expressions used on user controlled data#5857
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the application's security posture by mitigating ReDoS vulnerabilities. It refines several regular expressions that previously processed untrusted input, ensuring they operate efficiently and safely. The changes are thoroughly validated with new unit tests, providing robust coverage for the updated patterns and utility functions. Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new test file (packages/components/src/utils.test.ts) that adds comprehensive unit tests for the removeInvalidImageMarkdown and convertRequireToImport utility functions, as well as for two regular expressions used for CommonJS detection and import extraction within executeJavaScriptCode in packages/components/src/utils.ts. The code changes in utils.ts include refining the regex patterns for removeInvalidImageMarkdown, convertRequireToImport's destructured match, and the CommonJS detection and import extraction regexes. Additionally, the convertRequireToImport function is now exported. A review comment suggests improving maintainability by exporting the CommonJS detection and import extraction regular expressions from utils.ts and importing them into the test file, rather than duplicating them, to ensure tests always validate the exact regexes used in the application.
Fixes for CodeQL findings 76-79.
Uncontrolled data is passed into regex's that are un-optimal which could result in a DoS due to poor performance.
This PR creates unit tests that worked for the original regex's and updates the regex's to be safer.
Line 1239:
Why regex can't be made safe here (without possessive quantifiers):
The problem isn't the backtracking within the regex — it's the g flag forcing the engine to retry at every ! character. With n occurrences of ![ and each requiring a scan of up to O(n) chars to find ], the total is O(n²) regardless of whether .? or [^\]] is used. JavaScript has no possessive quantifiers ([^\]]*+) or atomic groups ((?>...)) to prevent this.
What the new implementation does differently:
Each indexOf call scans forward and never rescans already-visited characters. The total work across the whole loop is O(n) — linear regardless of how many ![ sequences appear in the input.
Testing
packages/components/src/utils.test.ts