Repository navigation
Replace github.com/google/shlex with github.com/kballard/go-shellquote - #2335
Open
NopeCharon wants to merge 2 commits into
Open
NopeCharon wants to merge 2 commits into
NopeCharon wants to merge 2 commits into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1812.
github.com/google/shlexhas been unmaintained since 2019. This replaces it withgithub.com/kballard/go-shellquote, as suggested in the issue, for both places that split a command line:SOPS_EDITOR/EDITORinsops edit,SOPS_AGE_KEY_CMDandSOPS_AGE_SSH_PRIVATE_KEY_CMDin the age key source.go-shellquote follows the
/bin/shword splitting rules. Besides getting rid of the dependency, this fixes shlex dropping backslashes inside double quotes:"C:\Program Files\x\ed.exe" -wC:Program Filesxed.exe,-wC:\Program Files\x\ed.exe,-wInside 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/shlexstays ingo.modas an indirect dependency, becausegithub.com/ory/dockertest/v3(used only by the HC Vault tests) needs it. It is no longer compiled into thesopsbinary.The second commit fixes a panic I ran into while writing tests. If
SOPS_AGE_KEY_CMDorSOPS_AGE_SSH_PRIVATE_KEY_CMDis set but empty (or contains only whitespace), sops crashed withpanic: runtime error: slice bounds out of range [1:0]. That happens with both libraries. sops now reportsfailed to parse command: command is emptyalong with the other identity loading errors.Tests:
TestGetOutputFromCmd. It covers backslashes in double quotes (fails with shlex), environment variables, unterminated quotes, and empty commands. I also added an emptySOPS_AGE_KEY_CMDcase toTestMasterKey_loadIdentities, which panics without the fix.go mod tidyproduces no diff.go vet ./..., the Linux and Windows builds, andgo test -race(all packages excepthcvault, which needs Docker) pass.sops editwithEDITOR='"/path/my editor/ed.sh" --wait'and decrypted withSOPS_AGE_KEY_CMD='cat "/path/key.txt"'.