Skip to content

Conversation

@arvid-e
Copy link
Contributor

@arvid-e arvid-e commented Jan 13, 2026

R1

if (excludedPaths.length > 0) {
const escapedPaths = excludedPaths.map((p) => {
const cleanPath = p.startsWith('/') ? p.substring(1) : p;
return cleanPath.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 6 days ago

General fix approach: when interpolating arbitrary strings into a regular expression pattern, escape them with a proper regex-escaping routine instead of ad-hoc replace calls. Here, leverage the already-imported escapeStringRegexp helper so that all regex metacharacters, including backslashes, are properly escaped. If we still need to treat / specially, we should do that on top of the generic escaping in a consistent way.

Best concrete fix: change the excludedPaths.map callback so that it uses escapeStringRegexp(cleanPath) rather than manually replacing / with \/. Since escapeStringRegexp escapes all regex metacharacters but does not require us to escape / itself (because the pattern is provided as a string literal to new RegExp rather than /.../ syntax), we do not need the manual replace at all. The resulting code will safely handle backslashes and any other special characters in excludedPaths. This change is entirely local to the escapedPaths computation and preserves the logic of the RegExp construction and the query.and condition.

Specifically, in packages/remark-lsx/src/server/routes/list-pages/index.ts, in the if (excludedPaths.length > 0) block around lines 95–101, replace the body of the map so that:

  • cleanPath is still computed as before to strip a leading slash.
  • The return statement calls escapeStringRegexp(cleanPath) instead of cleanPath.replace(/\//g, '\\/').

No new imports or additional helpers are needed; escapeStringRegexp is already imported at the top of the file.


Suggested changeset 1
packages/remark-lsx/src/server/routes/list-pages/index.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/remark-lsx/src/server/routes/list-pages/index.ts b/packages/remark-lsx/src/server/routes/list-pages/index.ts
--- a/packages/remark-lsx/src/server/routes/list-pages/index.ts
+++ b/packages/remark-lsx/src/server/routes/list-pages/index.ts
@@ -95,7 +95,7 @@
       if (excludedPaths.length > 0) {
         const escapedPaths = excludedPaths.map((p) => {
           const cleanPath = p.startsWith('/') ? p.substring(1) : p;
-          return cleanPath.replace(/\//g, '\\/');
+          return escapeStringRegexp(cleanPath);
         });
 
         const regex = new RegExp(`^\\/(${escapedPaths.join('|')})(\\/|$)`);
EOF
@@ -95,7 +95,7 @@
if (excludedPaths.length > 0) {
const escapedPaths = excludedPaths.map((p) => {
const cleanPath = p.startsWith('/') ? p.substring(1) : p;
return cleanPath.replace(/\//g, '\\/');
return escapeStringRegexp(cleanPath);
});

const regex = new RegExp(`^\\/(${escapedPaths.join('|')})(\\/|$)`);
Copilot is powered by AI and may make mistakes. Always verify output.
@yuki-takei
Copy link
Contributor

import escapeStringRegexp from 'escape-string-regexp';

https://github.com/copilot/c/aeec533e-bebd-445e-a393-3dae2b5237f6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants