Skip to content

FIX: Validate regex replacement templates in SearchReplaceConverter (#4076) - #4077

Open
Vardhman Gupta (Kaap10) wants to merge 1 commit into
microsoft:mainfrom
Kaap10:fix/search-replace-converter-template-validation
Open

Vardhman Gupta (Kaap10) wants to merge 1 commit into
microsoft:mainfrom
Kaap10:fix/search-replace-converter-template-validation

Conversation

@Kaap10

Copy link
Copy Markdown
Contributor

Description

Fixes SearchReplaceConverter to validate each replacement template in replace against the compiled regex pattern during initialization, preventing unhandled runtime crashes (re.error: bad escape, re.error: invalid group reference, IndexError: unknown group name).

  • In SearchReplaceConverter.__init__, iterates through self._replace_list and verifies each template compiles cleanly against self._compiled_regex by testing substitution on an empty string (self._compiled_regex.sub(item, "")).
  • Catches (re.error, IndexError) and raises a descriptive ValueError(f"Invalid regex replacement template in replace: {e}") from e.
  • Updates constructor docstring Raises section to document the validation error.
  • Preserves full backward compatibility for valid capture group backreferences (\1, \g<name>) as used in 1_text_to_text_converters.ipynb and test_prompt_template_converter.py.
  • Added unit tests in test_search_replace_converter.py covering invalid escape sequences (e.g. Windows file paths with \U or \u), invalid positional capture groups, unknown named capture groups, and invalid templates in replacement lists, alongside positive regression tests for valid group backreferences.

Demonstration

# Before:
converter = SearchReplaceConverter(pattern="PLACEHOLDER", replace=r"C:\Users\admin")
# Succeeded silently in __init__, but crashed at runtime in convert_async:
# re.error: bad escape \U at position 2

# After:
converter = SearchReplaceConverter(pattern="PLACEHOLDER", replace=r"C:\Users\admin")
# Raises ValueError immediately during initialization:
# ValueError: Invalid regex replacement template in replace: bad escape \U at position 2

Closes #4076

Copilot AI balanced review requested due to automatic review settings November 9, 2026 03:55

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

This branch has not been deployed

No deployments
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.

BUG: SearchReplaceConverter constructor does not validate replacement templates, causing unhandled runtime crash

2 participants