Skip to content

Conversation

siddharthsambharia-portkey
Copy link
Contributor

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

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional changes)

How Has This Been Tested?

  • Unit Tests
  • Integration Tests
  • Manual Testing

Screenshots (if applicable)

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Related Issues

#1328

@matter-code-review
Copy link
Contributor

matter-code-review bot commented Sep 10, 2025

Code Quality bug fix Maintainability

Description

Summary By MatterAI MatterAI logo

🔄 What Changed

A new utility function parseRegex has been introduced in plugins/default/regexReplace.ts. This function is responsible for correctly parsing a string input into a RegExp object, specifically handling cases where the input includes regex delimiters and flags (e.g., /pattern/flags). It also validates the provided flags against a set of valid JavaScript regex flags. The regexReplace plugin's handler now utilizes parseRegex instead of directly instantiating RegExp.

🔍 Impact of the Change

This change significantly improves the robustness and correctness of regex handling within the regexReplace plugin. Previously, explicit regex flags might have been misinterpreted as part of the pattern. Now, the plugin can correctly interpret and apply flags like 'g' (global), 'i' (case-insensitive), etc., as intended by the user. It also prevents the use of invalid regex flags, leading to more predictable behavior and fewer runtime errors.

📁 Total Files Changed

  • plugins/default/regexReplace.ts: Introduced parseRegex function (19 additions) and updated RegExp instantiation to use it (1 deletion).

🧪 Test Added

N/A

🔒Security Vulnerabilities

N/A

Motivation

This change is motivated by a bug fix to correct the regex replacement behavior within the plugin by ensuring proper parsing and validation of regex patterns and flags.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional changes)

How Has This Been Tested?

  • Unit Tests
  • Integration Tests
  • Manual Testing

Screenshots (if applicable)

N/A

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Related Issues

.

Tip

Quality Recommendations

  1. Add comprehensive unit tests for the parseRegex function to cover various valid and invalid regex patterns and flag combinations, ensuring its robustness.

  2. Include comments for the parseRegex function explaining its purpose, how it handles different input formats (e.g., /pattern/flags vs. pattern), and the list of valid flags it supports.

Tanka Poem ♫

Regex now parses,
Flags are checked, patterns clear.
No more 'g' default,
Precision in every match,
Code's logic, a clearer path. ✨

Sequence Diagram

sequenceDiagram
    participant PluginHandler as Regex Replace Plugin Handler
    participant ParseRegex as parseRegex(input: string)

    PluginHandler->>+ParseRegex: parseRegex(regexPattern: string)
    Note over ParseRegex: Validate flags (e.g., g, i, m, s, u, y, d)
    alt Valid Regex Pattern and Flags
        ParseRegex-->>-PluginHandler: new RegExp(pattern, flags)
    else Invalid Flags
        ParseRegex-->>-PluginHandler: throw Error('Invalid regex flags')
    end
Loading

Copy link
Contributor

@matter-code-review matter-code-review bot left a 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.

}

const regex = new RegExp(regexPattern, 'g');
const regex = new RegExp(regexPattern);
Copy link
Contributor

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

Suggested change
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.

Copy link
Contributor

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

@siddharthsambharia-portkey siddharthsambharia-portkey changed the title fix: remove the "g" flag from regex replace plugin fix: remove the regex replace plugin with the correct regex function parsing Sep 11, 2025
Copy link
Contributor

@matter-code-review matter-code-review bot left a 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]*$/;
Copy link
Contributor

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

Suggested change
const validFlags = /^[gimsuyd]*$/;
const validFlags = /^[gimsu]*$/;

// Valid JavaScript regex flags
const validFlags = /^[gimsuyd]*$/;

const match = input.match(/^\/(.+?)\/([gimsuyd]*)$/);
Copy link
Contributor

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

Suggested change
const match = input.match(/^\/(.+?)\/([gimsuyd]*)$/);
const match = input.match(/^\/(.+?)\/([gimsu]*)$/);

if (match) {
const [, pattern, flags] = match;

if (flags && !validFlags.test(flags)) {
Copy link
Contributor

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

Suggested change
if (flags && !validFlags.test(flags)) {
if (flags && flags.length > 0 && !validFlags.test(flags)) {

@narengogi narengogi linked an issue Sep 15, 2025 that may be closed by this pull request
Copy link
Collaborator

@narengogi narengogi left a 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

@siddharthsambharia-portkey
Copy link
Contributor Author

nice optimization removing the global search from the regex matches

🙏

@VisargD VisargD merged commit f400d72 into Portkey-AI:main Sep 16, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Regex Replace Guardrails

3 participants