-
Notifications
You must be signed in to change notification settings - Fork 412
fix: Refactor isURL() to use Built-in URL Constructor
#3061
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
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @lahirumaramba, 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 implements a crucial security enhancement by addressing a potential ReDoS vulnerability in the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request correctly addresses a potential Regular Expression Denial of Service (ReDoS) vulnerability in the URL validator. The fix involves making the hostname validation regex stricter by removing an optional character within a repeated group, which prevents catastrophic backtracking. The accompanying tests are well-designed, verifying the fix and ensuring there are no performance regressions with long or invalid URLs. I have one suggestion to further simplify the regex for improved readability and performance.
4cdf34e to
8a967ae
Compare
isURL() to use Built-in URL Constructor
|
/gemini review |
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.
Code Review
This pull request successfully refactors the isURL() function to utilize the built-in URL constructor, which is a significant improvement for security by mitigating ReDoS vulnerabilities and enhancing code simplicity. The addition of new tests for long URLs and ReDoS checks is also highly commendable. However, there are a couple of points regarding validation behavior that need clarification or adjustment to ensure consistency and maintain the desired level of strictness.
| } | ||
| } | ||
| // Allow any query string and hash as long as no invalid character is used. | ||
| return true; |
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.
The original isURL function included a specific regex (pathnameRe) to validate the pathname component, ensuring it only contained a defined set of safe characters. This new implementation removes that validation, relying solely on the URL constructor. The URL constructor is very permissive with pathnames (e.g., it will encode spaces), which might allow URLs that were previously considered invalid by this utility. This is a significant change in validation behavior and could potentially lead to unexpected behavior or security vulnerabilities if the stricter pathname validation was a requirement. Please consider re-introducing a similar level of pathname validation if the original strictness is still desired.
| // Must be alphanumeric with optional dashes/underscores, separated by dots. | ||
| // Cannot start/end with dot or dash (mostly). | ||
| // This regex is safe (no nested quantifiers with overlap). | ||
| if (!/^[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?)*$/.test(hostname)) { |
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.
The new hostname regex ^[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?)*$ explicitly disallows underscores (_) in hostnames. While this aligns better with RFC standards for hostnames and makes an existing test pass, the PR description states the goal is to "match previous behavior". The old regex technically allowed _ via \w. It would be clearer to acknowledge this as an intentional improvement for RFC compliance rather than matching previous behavior, or to clarify if the previous url.parse implicitly rejected underscores despite the regex.
Replace the RegEx-based validation in
isURL()(which was prone to ReDoS) with the standard built-inURLconstructor. This improves security, code simplicity, and correctness by leveraging the platform's native URL parsing.