Skip to content

Conversation

@jonathanlab
Copy link
Contributor

wip

Comment on lines +25 to +26
const regexPattern = pattern
.replace(/\./g, "\\.")

Check failure

Code scanning / CodeQL

Incomplete string escaping or encoding High

This does not escape backslash characters in the input.

Copilot Autofix

AI about 8 hours ago

In general, when converting glob patterns to regular expressions, every regex metacharacter (including the backslash itself) must be escaped before you introduce your own special handling for * and **. Doing this manually with chained .replace calls is fragile; using a single replacement that escapes all regex-special characters is much safer.

The best minimal fix here is to first escape all regex metacharacters in the pattern, then translate the escaped glob wildcards to their regex equivalents. That means:

  1. Take the raw pattern.
  2. Escape all regex metacharacters ([\^$.*+?()|{}] and \).
  3. Replace the escaped glob sequences for ** and * with placeholders / appropriate regex:
    • Convert the escaped ** to a placeholder (e.g. {{GLOBSTAR}}) and then to .*.
    • Convert the escaped * to [^/]*.
  4. Build the final RegExp from this fully-escaped, transformed pattern.

Concretely in packages/core/src/commands/assign.ts, inside matchFiles, we should:

  • Introduce a helper that escapes regex metacharacters, or inline the logic in the regexPattern computation.
  • Change the current chain:
    const regexPattern = pattern
      .replace(/\./g, "\\.")
      .replace(/\*\*/g, "{{GLOBSTAR}}")
      .replace(/\*/g, "[^/]*")
      .replace(/{{GLOBSTAR}}/g, ".*");
    to:
    const escapedPattern = pattern.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
    const regexPattern = escapedPattern
      .replace(/\\\*\\\*/g, "{{GLOBSTAR}}")
      .replace(/\\\*/g, "[^/]*")
      .replace(/{{GLOBSTAR}}/g, ".*");
  • Keep the rest of the function unchanged.

No new imports are needed; we rely only on the built-in RegExp and String.prototype.replace.

Suggested changeset 1
packages/core/src/commands/assign.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/packages/core/src/commands/assign.ts b/packages/core/src/commands/assign.ts
--- a/packages/core/src/commands/assign.ts
+++ b/packages/core/src/commands/assign.ts
@@ -22,10 +22,12 @@
 
   for (const pattern of patterns) {
     // Convert glob pattern to regex
-    const regexPattern = pattern
-      .replace(/\./g, "\\.")
-      .replace(/\*\*/g, "{{GLOBSTAR}}")
-      .replace(/\*/g, "[^/]*")
+    // First escape all regex metacharacters, including backslashes,
+    // then translate glob wildcards (** and *) into regex equivalents.
+    const escapedPattern = pattern.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
+    const regexPattern = escapedPattern
+      .replace(/\\\*\\\*/g, "{{GLOBSTAR}}")
+      .replace(/\\\*/g, "[^/]*")
       .replace(/{{GLOBSTAR}}/g, ".*");
 
     const regex = new RegExp(`^${regexPattern}$`);
EOF
@@ -22,10 +22,12 @@

for (const pattern of patterns) {
// Convert glob pattern to regex
const regexPattern = pattern
.replace(/\./g, "\\.")
.replace(/\*\*/g, "{{GLOBSTAR}}")
.replace(/\*/g, "[^/]*")
// First escape all regex metacharacters, including backslashes,
// then translate glob wildcards (** and *) into regex equivalents.
const escapedPattern = pattern.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
const regexPattern = escapedPattern
.replace(/\\\*\\\*/g, "{{GLOBSTAR}}")
.replace(/\\\*/g, "[^/]*")
.replace(/{{GLOBSTAR}}/g, ".*");

const regex = new RegExp(`^${regexPattern}$`);
Copilot is powered by AI and may make mistakes. Always verify output.
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