Skip to content

Conversation

@kkartunov
Copy link
Collaborator

Potential fix for https://github.com/topcoder-platform/platform-ui/security/code-scanning/71

To fix this issue robustly:

  • Use a well-tested escape function that escapes all special RegExp characters, including backslashes and whitespace, in a single step.
  • If you must perform additional replacements (e.g., replace spaces in the normalized target string with a specific RegExp subpattern), do so before the call to escapeRegexLiteral, or do all escaping (for both standard RegExp metacharacters and for your application-specific substitutions) in a single regular expression.
  • In this code, instead of first escaping all metacharacters and then replacing spaces, do it all at once: replace all contiguous (one or more) separator characters (e.g., spaces, underscores, hyphens) with a RegExp subpattern (like [-_\s]+), after escaping actual special characters, and ensure that no intermediate string manipulation reduces the escaping quality.

For this file specifically, edit the block constructing the RegExp.

  • Refactor the phase name normalization and escaping to:
    • Replace all groups of spaces (or separators) with a constant placeholder (e.g., __WORDSEP__)
    • Escape the rest of the string (all RegExp metacharacters including backslashes)
    • Replace the placeholder with the target RegExp subpattern (e.g., [-_\s]+)

No new dependencies are required since the standard escaping function is already robust, but you may consider (if allowed) to use a trusted library like escape-string-regexp.


Suggested fixes powered by Copilot Autofix. Review carefully before merging.

…ing or encoding

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
const targetWithPlaceholder = target.replace(sepPattern, WORDSEP_PLACEHOLDER);
// Properly escape ALL regex metacharacters (including backslash), leaving the placeholder intact
const escapedTarget = escapeRegexLiteral(targetWithPlaceholder)
.replace(new RegExp(escapeRegexLiteral(WORDSEP_PLACEHOLDER), 'g'), '[-_\\s]+');

Choose a reason for hiding this comment

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

[⚠️ correctness]
The use of new RegExp(escapeRegexLiteral(WORDSEP_PLACEHOLDER), 'g') to replace the placeholder with the pattern [-_\\s]+ is correct, but consider verifying that WORDSEP_PLACEHOLDER is unique enough to avoid accidental matches in the input string. If WORDSEP_PLACEHOLDER could appear in the input, it might lead to incorrect replacements.

@kkartunov kkartunov marked this pull request as ready for review December 12, 2025 06:49
@kkartunov kkartunov requested a review from jmgasper as a code owner December 12, 2025 06:49
@kkartunov
Copy link
Collaborator Author

@copilot fix the lint as it fails doe to errors.

Copy link
Contributor

Copilot AI commented Dec 12, 2025

@kkartunov I've opened a new pull request, #1378, to work on those changes. Once the pull request is ready, I'll request review from you.

Copilot AI and others added 2 commits December 12, 2025 07:02
Co-authored-by: kkartunov <5585002+kkartunov@users.noreply.github.com>
Fix ESLint semicolon violations in metadataMatching.ts
.replace(/ /g, '\\ ')
const sepInsensitive = new RegExp(`\\b${escapedTarget.replace(/\\ /g, '[-_\\s]+')}\\b`)
// Replace all sequences of space, underscore, or hyphen in the target with a placeholder
const WORDSEP_PLACEHOLDER = '__WORDSEP__'

Choose a reason for hiding this comment

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

[💡 style]
Consider adding semicolons at the end of statements to maintain consistency with the rest of the codebase, which uses semicolons. This can help prevent potential issues with automatic semicolon insertion.

const targetWithPlaceholder = target.replace(sepPattern, WORDSEP_PLACEHOLDER)
// Properly escape ALL regex metacharacters (including backslash), leaving the placeholder intact
const escapedTarget = escapeRegexLiteral(targetWithPlaceholder)
.replace(new RegExp(escapeRegexLiteral(WORDSEP_PLACEHOLDER), 'g'), '[-_\\s]+')

Choose a reason for hiding this comment

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

[⚠️ correctness]
The replacement pattern [-_\s]+ is used to match sequences of separators. Ensure that this pattern aligns with the intended behavior and that no additional separators need to be considered. If the pattern should be more flexible, consider documenting the expected input format.

@kkartunov kkartunov merged commit 92a3962 into dev Dec 12, 2025
8 checks 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.

2 participants