Skip to content

Conversation

@soderlind
Copy link
Owner

Potential fix for https://github.com/soderlind/wordpress-readme-preview/security/code-scanning/3

To address this problem, we need to ensure that any metacharacters in token, including the backslash \, are correctly escaped before using it in a dynamically constructed regular expression. The preferred and safest way is to use a general-purpose function to escape all regex metacharacters, not just a specific subset. The npm library escape-string-regexp is the canonical solution, but if only standard library usage is allowed, we can write a function to escape all regex metacharacters, including backslash.

In file src/parser/validator.ts, at line 464, replace the inline .replace(/([*~])/g,'\$1')with a function call such asescapeRegExp(token), where escapeRegExp` escapes all regex metacharacters safely. Add the following function definition somewhere in the file (near the top or above where it's used):

function escapeRegExp(s: string): string {
  // Escapes all special characters for safe insertion into regex
  return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}

Then update the invocation inside countMatches to call this helper function. No other lines need adjustment.

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

soderlind and others added 4 commits October 21, 2025 12:50
…ng or encoding

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
…ng or encoding

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
@soderlind soderlind marked this pull request as ready for review October 21, 2025 11:14
@soderlind soderlind merged commit b5f8a77 into main Oct 21, 2025
2 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