Skip to content

Stop escaping # before {, $ and @ in shell values - #1986

Open
Harsh23Kashyap wants to merge 4 commits into
basecamp:mainfrom
Harsh23Kashyap:fix/escape-hash-before-interpolation
Open

Harsh23Kashyap wants to merge 4 commits into
basecamp:mainfrom
Harsh23Kashyap:fix/escape-hash-before-interpolation

Conversation

@Harsh23Kashyap

Copy link
Copy Markdown

escape_ascii_shell_value builds its result with String#dump, which writes # as \# when it comes right before {, $ or @. Inside double quotes the shell keeps that backslash, so a builder arg like MSG='a#{b}' reaches docker as a\#{b}. #1114 fixed the same thing for env files, but this shell path was left alone.

The fix removes that \# escape right after dump, before the backtick and dollar handling, and leaves an escaped backslash followed by a literal # alone. It is a small regex in lib/kamal/utils.rb.

How I checked it:

  • Two new tests in test/utils_test.rb: one on the exact string, one that round trips values through printf. Both fail on main (2 failures) and pass with the change.
  • 3000 random strings sent through sh: 173 came back different on main, 0 with the change.
  • Ran the full suite on main and on this branch. The same 4 failures and 6 errors show up on both (they come from my environment, not this change). Rubocop is clean.

Copilot AI balanced review requested due to automatic review settings November 8, 2026 09:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Note

Copilot was unable to run its full agentic suite in this review.

Copilot review overview

2 open issues
What changed in this PR

This PR adjusts Kamal::Utils.escape_shell_value to avoid leaving an extra backslash before # sequences (#{, #$, #@) introduced by String#dump, and adds regression tests to ensure correct escaping and safe shell round-tripping.

Changes:

  • Add a regex to undo String#dump’s escaping of # before {, $, or @ while preserving preceding escaped backslashes.
  • Extend escape_ascii_shell_value with a gsub pass to apply that correction.
  • Add unit tests for the new escaping behavior plus a shell round-trip test.
File Description
test/​utils_test.rb Adds regression and round-trip tests covering # + interpolation-like sequences.
lib/​kamal/​utils.rb Introduces DUMP_ESCAPED_INTERPOLATION_HASH_REGEX and applies it in escape_ascii_shell_value.

Review effort: Lite

Comment thread test/utils_test.rb Resolved
Comment thread lib/kamal/utils.rb Resolved
Use Open3.capture3 to check shell success as well as the output. Skip only when /bin/sh is absent.
@Harsh23Kashyap

Copy link
Copy Markdown
Author

Addressed both review points in 0dd0b36 and a80af54. The round-trip test now calls /bin/sh explicitly through Open3.capture3, checks the exit status and output, and skips when /bin/sh is absent. The regex comment includes a concrete before/after example. No production behavior changed in this follow-up.

Checked on Ruby 3.4.7:

  • Utils suite: 11 tests, 38 assertions, no failures, errors or skips, in 20 fresh processes.
  • Removing the production regex replacement makes the two regression tests fail in each of two runs; restoring it passes.
  • Rubocop passes for both changed files.
  • The non-integration suite runs 863 tests. It has the same 4 failures and 6 errors on the previous PR head and this update, in exactly the same tests. This is not a clean full-suite result. Docker integration and Windows were not run.

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