Skip to content

Convert cmd escaping to support linear regexp engine#2383

Merged
ericcornelissen merged 15 commits intomainfrom
lregexp-cmd
Mar 6, 2026
Merged

Convert cmd escaping to support linear regexp engine#2383
ericcornelissen merged 15 commits intomainfrom
lregexp-cmd

Conversation

@ericcornelissen
Copy link
Owner

@ericcornelissen ericcornelissen commented Mar 1, 2026

Relates to #2122

Update `cmd.js` to use the lregexp package to transparently create
regular expressions that use the linear engine in order to avoid any
ReDoS vulnerabilities.This commit target the logic behind Shescape's
`escape(All)` API only. The refactoring is mostly straightforward with
the exception of the logic for `"` (double quotes).

In particular, the regular expression for `"` previously used a negative
lookbehind. This has been replaced with a capturing group matching the
start of the string or any character not matching the characters found
previously in the negative lookbehind. This capturing group is put back
in to the output string verbatim, thus having the same effect as a
negative lookbehind.
@ericcornelissen ericcornelissen added the refactor Changes existing code without changing functionality label Mar 1, 2026
@deepsource-io
Copy link

deepsource-io bot commented Mar 1, 2026

DeepSource Code Review

We reviewed changes in 8de3ff2...54d5099 on this pull request. Below is the summary for the review, and you can see the individual issues we found as inline review comments.

See full review on DeepSource ↗

PR Report Card

Overall Grade   Security  

Reliability  

Complexity  

Hygiene  

Code Review Summary

Analyzer Status Updated (UTC) Details
JavaScript Mar 5, 2026 11:38a.m. Review ↗

Update `cmd.js` to use the lregexp package to transparently create
regular expressions that use the linear engine in order to avoid any
ReDoS vulnerabilities. This commit target the logic behind Shescape's
`quote(All)` API only. The refactoring is mostly straightforward with
the exception of the logic for `\` (backslashes).

Like f79a6b3, the negative lookbehind, and in this case positive
lookahead too, is replaced by a capturing group that the replacement
puts back in the string. This doesn't change the behavior of the
replacing because what is in the capturing group never needs to be
captured again in the same pass (it can't be another `\`).
2 kill 2 mutants uncovered by the changes in the previous commit, in
particular the start of string anchor in the backslashes regexp and
the global flag in the backslashes regexp by testing backslashes at
the start of the string and multiple backslashes in a string resp.

Additionally, add various test cases that ensure the order of the
replacements for quoting is correct. In particular, backslashes
must be escaped **after** special characters because the latter
introduces characters that require escaping the former.
@github-actions github-actions bot added the test Relates to testing label Mar 3, 2026
@github-actions github-actions bot added the fuzz Relates to fuzzing label Mar 4, 2026
@ericcornelissen

This comment was marked as resolved.

@ericcornelissen ericcornelissen marked this pull request as ready for review March 4, 2026 21:23
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1dc7703557

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Rework escaping for CMD so that escaping a `"` works as follows

1. Escape the `"` itself (as `\\^"`) and inject a marker as a unique
   anchor for the required additional escaping of backslashes. The
   marker is guaranteed to be unique because we previously removed it
   indiscriminately from the argument.
2. Use the marker for further replacements. If it's preceded by `\\` it
   means the `\\` proceeds a `"` in the original string. If it's not it
   should still be removed.
@ericcornelissen

This comment was marked as resolved.

@coderabbitai

This comment was marked as spam.

@coderabbitai

This comment was marked as spam.

@ericcornelissen ericcornelissen removed the fuzz Relates to fuzzing label Mar 4, 2026
@ericcornelissen

This comment was marked as resolved.

@ericcornelissen ericcornelissen merged commit 0f38a42 into main Mar 6, 2026
43 checks passed
@ericcornelissen ericcornelissen deleted the lregexp-cmd branch March 6, 2026 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactor Changes existing code without changing functionality test Relates to testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant