Skip to content

fix(client): run SSH init without a shell and quote the key path - #119

Open
Macbet wants to merge 1 commit into
fix/client-api-defectsfrom
fix/ssh-init-no-shell
Open

Macbet wants to merge 1 commit into
fix/client-api-defectsfrom
fix/ssh-init-no-shell

Conversation

@Macbet

@Macbet Macbet commented Nov 18, 2026

Copy link
Copy Markdown

Stacked on #116 (fix/client-api-defects). Review only the last commit. Merge #116 first.

Problem

When sshFile was set, init ran through bash -c:

GIT_SSH_COMMAND='ssh -i %s -o StrictHostKeyChecking=no' %s init [-no-color] [-backend-config=%s] -input=false

The key path, the binary path and the backend config file name were interpolated unquoted. That breaks on spaces, and it is command injection if any of them is influenced by job input. This path also needed bash, ignored redirectErrorStream, duplicated the init flag logic, and logged the whole command line at WARN.

Change

  • SSH init now goes through the same launcher path as a normal init, so the command line is identical: same flags, same order. Tofu takes the same path.
  • GIT_SSH_COMMAND is set as an environment variable. Git runs it through sh -c, so the key path inside it is POSIX single-quoted, with each ' written as '\''.
  • sshFile still wins over a GIT_SSH_COMMAND in terraformEnvironmentVariables, as the old inline prefix did. Callers such as Terrakube pass user-controlled variables in that map, and a stray value, even "", would silently drop the key. Other commands still get the caller's value.
  • The WARN log of the full command is gone. INFO has no command or env values; the key path is logged at DEBUG.
  • -o StrictHostKeyChecking=no is unchanged on purpose. Changing the host key policy needs a separate decision.

Behaviour changes

  • SSH init no longer needs bash.
  • redirectErrorStream now applies to SSH init. With it on, stderr goes to the output listener, as plain init already did. Terrakube sets redirectErrorStream(true) everywhere.
  • Key paths and backend config names with spaces, quotes, $, backticks or ; now arrive literally.

Tests

New tests in TerraformClientApiTest, using a fake terraform binary:

  • the quoting helper round-trips 14 hostile values through sh;
  • SSH init runs the binary directly with exactly the same args as a plain init, for both terraform and tofu;
  • a key path with spaces, ', $HOME, backticks and ; survives git-style splitting of GIT_SSH_COMMAND;
  • a backend config name containing ; arrives as one argument;
  • sshFile beats a caller's GIT_SSH_COMMAND (both non-empty and "");
  • redirectErrorStream is honoured.

A reviewer also ran real git with a fake ssh and saw the key path arrive intact. Docker verify (temurin 17, --init): 45/45 in terraform-client.

init with an sshFile used to run 'GIT_SSH_COMMAND=... terraform init ...'
through bash -c, with the key path, binary path and backend config file
name interpolated unquoted: spaces broke it and any of them could inject
commands. It now runs terraform directly through the normal init path and
passes GIT_SSH_COMMAND as an environment variable, with the key path
POSIX single-quoted because git hands that value to sh.

The sshFile still wins over a GIT_SSH_COMMAND in the caller's environment
variables, as the bash prefix did.

Behaviour changes:
- SSH init no longer needs bash and runs the same command line as init.
- redirectErrorStream now applies to SSH init (it was ignored before), so
  with it set, SSH init's stderr reaches the output listener as plain
  init's already did.
- The full command line is no longer logged at WARN; an INFO line without
  the command or env values replaces it, the key path is logged at DEBUG.
- StrictHostKeyChecking=no is unchanged.
@sonarqubecloud

Copy link
Copy Markdown

@Macbet
Macbet added this pull request to stack #123 November 18, 2026 21:25
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.

1 participant