-
Notifications
You must be signed in to change notification settings - Fork 763
fix: remove the regex replace plugin with the correct regex function parsing #1329
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
fix: remove the regex replace plugin with the correct regex function parsing #1329
Conversation
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.
Removing the 'g' flag from regex will change behavior from replacing all matches to only the first match. This appears intentional but needs verification.
plugins/default/regexReplace.ts
Outdated
} | ||
|
||
const regex = new RegExp(regexPattern, 'g'); | ||
const regex = new RegExp(regexPattern); |
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.
🐛 Bug Fix
Issue: Removing the 'g' (global) flag changes regex behavior from replacing all matches to only replacing the first match in each text item
Fix: Verify this behavioral change is intentional, as it fundamentally alters the plugin's functionality
Impact: Users expecting global replacement will now only get first-match replacement
const regex = new RegExp(regexPattern); | |
const regex = new RegExp(regexPattern, 'g'); |
Alternative: If the intention is to replace only the first match globally, consider documenting this change or providing a configuration option for users who need global replacement.
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.
✅ Resolved: Fixed by implementing parseRegex function that properly handles user-provided flags including global 'g' flag when specified
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.
Good implementation of regex parsing with flag validation. The new parseRegex function properly handles user-provided flags while maintaining security.
|
||
function parseRegex(input: string): RegExp { | ||
// Valid JavaScript regex flags | ||
const validFlags = /^[gimsuyd]*$/; |
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.
🔒 Security Issue Fix
Issue: The regex flag validation includes 'y' and 'd' flags which are not supported in all JavaScript environments and could cause runtime errors
Fix: Remove unsupported flags to ensure cross-environment compatibility
Impact: Prevents potential runtime errors in older JavaScript environments
const validFlags = /^[gimsuyd]*$/; | |
const validFlags = /^[gimsu]*$/; |
// Valid JavaScript regex flags | ||
const validFlags = /^[gimsuyd]*$/; | ||
|
||
const match = input.match(/^\/(.+?)\/([gimsuyd]*)$/); |
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.
🔒 Security Issue Fix
Issue: The regex pattern matching includes 'y' and 'd' flags in the capture group which could cause inconsistent behavior
Fix: Remove unsupported flags from the pattern matching to align with validation
Impact: Ensures consistent flag handling across the function
const match = input.match(/^\/(.+?)\/([gimsuyd]*)$/); | |
const match = input.match(/^\/(.+?)\/([gimsu]*)$/); |
if (match) { | ||
const [, pattern, flags] = match; | ||
|
||
if (flags && !validFlags.test(flags)) { |
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.
🐛 Bug Fix
Issue: Empty flags string will pass validation but could cause confusion in error messages
Fix: Add explicit check for empty flags to provide clearer logic flow
Impact: Improves code clarity and prevents unnecessary validation of empty strings
if (flags && !validFlags.test(flags)) { | |
if (flags && flags.length > 0 && !validFlags.test(flags)) { |
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.
nice optimization removing the global search from the regex matches
🙏 |
Description
This PR fixed the regex replcae guardrail
Motivation
the regex replace guardrail was adding a 'g' flag by itself that was causing the regex to fail OR not work in the desired way
Type of Change
How Has This Been Tested?
Screenshots (if applicable)
Checklist
Related Issues
#1328