Escape all knife wrapper arguments to prevent shell command injection#4203
Open
tas50 wants to merge 1 commit into
Open
Escape all knife wrapper arguments to prevent shell command injection#4203tas50 wants to merge 1 commit into
tas50 wants to merge 1 commit into
Conversation
👷 Deploy Preview for chef-server processing.
|
The org/user management commands in wrap-knife.rb build a knife command
as a single string that is run through a shell via run_command. Each
argument was passed through Shellwords.escape EXCEPT arguments
containing '=', which were concatenated verbatim:
if arg.include?('=')
arg
else
Shellwords.escape(arg)
end
The '=' carve-out was intended to preserve "key=value" config options,
but it lets any argument containing '=' inject arbitrary shell syntax.
For example a field value such as "x=y; <command>" would have its
metacharacters interpreted by the shell instead of being passed to
knife as a literal argument.
Escape every argument unconditionally. Shellwords.escape leaves benign
characters such as '=' untouched, so legitimate "key=value" options
continue to work while shell metacharacters are neutralized.
Signed-off-by: Tim Smith <tsmith84@proton.me>
a73c943 to
ed3fae1
Compare
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.
Problem
The
org-*/user-*management commands insrc/chef-server-ctl/plugins/wrap-knife.rbassemble a knife invocation as a single string and run it through a shell viarun_command:Every argument is escaped with
Shellwords.escapeexcept arguments containing=, which are concatenated verbatim. The carve-out was meant to preservekey=valueconfig options, but it means any argument containing=can inject arbitrary shell syntax. A positional field or option value such as:has its
;and following command interpreted by the shell rather than being passed toknifeas a literal argument.Fix
Escape every argument unconditionally:
Shellwords.escapeleaves benign characters (including=,-,_,.,/, and alphanumerics) untouched, so legitimatekey=valueconfig options continue to work unchanged, while shell metacharacters in any argument are neutralized instead of executed.Note: the chef-server-ctl gem bundle targets Ruby 3.1 and could not be installed in my local environment (Ruby 4.x), so I verified the change with
ruby -cand am relying on CI for the rspec suite.