Skip to content

fix(no-ticket): escape the args in the terraformrc block - #543

Open
cloudsmith-iduffy wants to merge 5 commits into
masterfrom
iduffy/fix-terraformrc-escaping
Open

cloudsmith-iduffy wants to merge 5 commits into
masterfrom
iduffy/fix-terraformrc-escaping

Conversation

@cloudsmith-iduffy

Copy link
Copy Markdown
Collaborator

Description

cloudsmith credential-helper install terraform puts every helper arg into the credentials_helper "cloudsmith" block as "<arg>", with no escaping. The args include the resolved --config-file and --credentials-file paths. On Windows those paths look like C:\Users\jo\config.ini, and \U is not a valid escape in HCL. Terraform then rejects the whole CLI config file, so every Terraform command fails, and the user's other settings in that file stop working too.

Before:

credentials_helper "cloudsmith" {
  args = ["-w", "acme", "--config-file", "C:\Users\jo\config.ini"]
}
Error: Error parsing <CLI config file>: At <line>:<column>: illegal char escape

After:

credentials_helper "cloudsmith" {
  args = ["-w", "acme", "--config-file", "C:\\Users\\jo\\config.ini"]
}

What changed in terraformrc.py:

  • render_block escapes \ and ". It writes ${ and control characters as \uXXXX escapes.
  • render_block raises TypeError for a bare string or a non-string item. A bare string would otherwise render one arg per character.
  • _BLOCK_RE ignores a } inside a quoted string. Before this change, an arg with a } cut the match short, so the next install or uninstall left part of the old block behind. Run the install again to repair a block that an earlier version wrote.

I checked the escaping against Terraform 1.16. Terraform reads the CLI config with HCL 1, not HCL 2, so the usual $${ and %%{ escapes do not apply:

  • ${x} and %{x} are already literal. $${x} is not an escape: the helper gets $${x} as written.
  • After a raw ${, HCL 1 stops decoding escapes. "${C:\\x}" reaches the helper with two backslashes.
  • \u0024{ decodes to ${ and does not start that mode, so the CLI writes ${ that way.

To check this, I put a small helper into ~/.terraform.d/plugins that writes its argv to a file. Then I ran terraform init against a module on an unknown registry host. For a Windows path, ", ${, }, {, %{, a newline, a control character, and non-ASCII text, the argv was the same as the Python list I passed to render_block.

To reproduce the bug on macOS or Linux, use a directory whose name has a backslash in it:

H=$(mktemp -d); mkdir -p "$H/Users\\jo"; printf '[default]\n' > "$H/Users\\jo/config.ini"
HOME=$H TF_CLI_CONFIG_FILE=$H/terraformrc CLOUDSMITH_NO_KEYRING=1 \
  cloudsmith credential-helper install terraform -w acme -r repo -C "$H/Users\\jo/config.ini"
TF_CLI_CONFIG_FILE=$H/terraformrc terraform version

On master, terraform version prints illegal char escape. On this branch, it does not print an error.

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update
  • Refactoring
  • Other (please describe)

Additional Notes

🤖 Generated with Claude Code

The Terraform install wrote each helper arg into the HCL block without
escapes. A Windows path such as C:\Users\jo\config.ini is not valid
HCL, so Terraform rejected the full CLI config file. Escape backslashes
and quotes. Write `${` and control characters as Unicode escapes,
because HCL 1 does not decode escapes after `${`. Ignore braces in
quoted strings when the installer finds the block again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 24, 2026 17:25

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.

🟡 Changes recommended

Quotes in valid HCL comments prevent block detection, causing reinstall to append duplicate helper blocks and uninstall to leave them untouched.

1 open issue
What changed in this PR

This PR fixes Terraform credential-helper argument escaping, particularly for Windows configuration paths, and improves matching of helper blocks containing quoted braces.

Changes:

  • Escape HCL-sensitive characters and reject invalid argument types.
  • Update block matching and add regression tests for escaping and replacement.
  • Register the shared windows test marker and document the fix.
File Description
pyproject.toml Register the windows pytest marker.
cloudsmith_cli/​credential_helpers/​terraform/​terraformrc.py Escape arguments, validate types, and recognize quoted braces.
cloudsmith_cli/​cli/​tests/​test_credential_helper_terraform_installer.py Test escaping, invalid arguments, and block replacement.
CHANGELOG.md Describe the Terraform fix and recovery instructions.

Review effort: Balanced

Read HCL comments (#, // and /* */) as whole tokens when the CLI looks
for the credentials_helper block. Before, a quote in a comment started
a string, the match failed, and install added a second block while
uninstall left the old one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@cloudsmith-iduffy
cloudsmith-iduffy marked this pull request as ready for review October 24, 2026 17:53
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@cloudsmith-iduffy

Copy link
Copy Markdown
Collaborator Author

@copilot resolve the merge conflicts on this branch.

…mrc-escaping

# Conflicts:
#	CHANGELOG.md

Co-authored-by: cloudsmith-iduffy <178375997+cloudsmith-iduffy@users.noreply.github.com>
auto-merge was automatically disabled October 29, 2026 11:04

Head branch was pushed to by a user without write access

Copilot AI commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

@copilot resolve the merge conflicts on this branch.

Merged the latest master and resolved the CHANGELOG.md conflict in db1d476.

This branch has not been deployed

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants