Skip to content

Replace github.com/google/shlex with github.com/kballard/go-shellquote - #2335

Open
NopeCharon wants to merge 2 commits into
getsops:mainfrom
NopeCharon:shellquote
Open

NopeCharon wants to merge 2 commits into
getsops:mainfrom
NopeCharon:shellquote

Conversation

@NopeCharon

Copy link
Copy Markdown

Fixes #1812.

github.com/google/shlex has been unmaintained since 2019. This replaces it with github.com/kballard/go-shellquote, as suggested in the issue, for both places that split a command line:

  • SOPS_EDITOR / EDITOR in sops edit,
  • SOPS_AGE_KEY_CMD and SOPS_AGE_SSH_PRIVATE_KEY_CMD in the age key source.

go-shellquote follows the /bin/sh word splitting rules. Besides getting rid of the dependency, this fixes shlex dropping backslashes inside double quotes:

Input shlex go-shellquote
"C:\Program Files\x\ed.exe" -w C:Program Filesxed.exe, -w C:\Program Files\x\ed.exe, -w

Inside double quotes, a backslash now only escapes $, `, ", \ and newlines, as in a POSIX shell. The only other difference I found is that go-shellquote does not strip unquoted # comments (vim # foo), which isn't useful in an editor or key command anyway. Unterminated quotes and trailing backslashes are still reported as errors.

github.com/google/shlex stays in go.mod as an indirect dependency, because github.com/ory/dockertest/v3 (used only by the HC Vault tests) needs it. It is no longer compiled into the sops binary.

The second commit fixes a panic I ran into while writing tests. If SOPS_AGE_KEY_CMD or SOPS_AGE_SSH_PRIVATE_KEY_CMD is set but empty (or contains only whitespace), sops crashed with panic: runtime error: slice bounds out of range [1:0]. That happens with both libraries. sops now reports failed to parse command: command is empty along with the other identity loading errors.

Tests:

  • Added TestGetOutputFromCmd. It covers backslashes in double quotes (fails with shlex), environment variables, unterminated quotes, and empty commands. I also added an empty SOPS_AGE_KEY_CMD case to TestMasterKey_loadIdentities, which panics without the fix.
  • go mod tidy produces no diff. go vet ./..., the Linux and Windows builds, and go test -race (all packages except hcvault, which needs Docker) pass.
  • Manually ran sops edit with EDITOR='"/path/my editor/ed.sh" --wait' and decrypted with SOPS_AGE_KEY_CMD='cat "/path/key.txt"'.

github.com/google/shlex has not seen any changes since 2019 and is
unmaintained. It is used to split the SOPS_EDITOR/EDITOR environment
variables for `sops edit`, and the SOPS_AGE_KEY_CMD and
SOPS_AGE_SSH_PRIVATE_KEY_CMD environment variables for the age key
source.

Switch both call sites to github.com/kballard/go-shellquote, which
follows POSIX shell word splitting rules. Besides getting rid of the
unmaintained dependency, this fixes shlex dropping backslashes inside
double-quoted strings: `"C:\Program Files\Editor\editor.exe" --wait`
used to be split into `C:Program FilesEditoreditor.exe` and `--wait`,
while it is now split into `C:\Program Files\Editor\editor.exe` and
`--wait`. Inside double quotes, a backslash is only treated as an
escape character if it is followed by `$`, `` ` ``, `"`, `\` or a
newline, as in a POSIX shell.

The only other difference is that go-shellquote does not strip `#`
comments, which is not something that is useful in an editor or key
command.

github.com/google/shlex remains an indirect dependency, since
github.com/ory/dockertest/v3 (only used by tests) still needs it, but
it is no longer compiled into the sops binary.

Fixes getsops#1812.

Signed-off-by: NopeCharon <115794254+NopeCharon@users.noreply.github.com>
When SOPS_AGE_KEY_CMD or SOPS_AGE_SSH_PRIVATE_KEY_CMD is set, but is
empty or only consists of whitespace, splitting the command results in
no arguments at all, and sops panicked with

    panic: runtime error: slice bounds out of range [1:0]

when trying to run the command. This can easily happen when the
variable is exported without a value, for example by a CI template.

Return an error instead, which is reported together with the other
errors encountered while loading age identities.

Signed-off-by: NopeCharon <115794254+NopeCharon@users.noreply.github.com>
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.

Find replacement for github.com/google/shlex

1 participant