File tree Expand file tree Collapse file tree
lib/octocatalog-diff/catalog-util
spec/octocatalog-diff/tests/catalog-util Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -115,13 +115,24 @@ def override_and_append_commandline_with_user_supplied_arguments(cmdline)
115115 unless opt =~ /\A --([^=\s ]+)(=.+)?\z /
116116 raise ArgumentError , "Command line option '#{ opt } ' does not match format '--SOME_OPTION=SOME_VALUE'"
117117 end
118- key = Shellwords . escape ( Regexp . last_match ( 1 ) )
118+ key = Regexp . last_match ( 1 )
119119 val = Regexp . last_match ( 2 )
120- val . sub! ( /\A =/ , '' ) if val . is_a? ( String )
120+
121+ # The key should not contain any shell metacharacters. Ensure that this is the case.
122+ unless key == Shellwords . escape ( key )
123+ raise ArgumentError , "Command line option '#{ key } ' is invalid."
124+ end
125+
126+ # If val is nil, then it's a '--key' argument. Else, it's a '--key=value' argument. Escape
127+ # the value to ensure it do not break the shell interpretation.
128+ new_setting = if val . nil?
129+ "--#{ key } "
130+ else
131+ "--#{ key } =#{ Shellwords . escape ( val . sub ( /\A =/ , '' ) ) } "
132+ end
121133
122134 # Determine if command line already contains this setting. If yes, the setting provided
123135 # here should override. If no, then append to the commandline.
124- new_setting = val . nil? ? "--#{ key } " : "--#{ key } =#{ Shellwords . escape ( val ) } "
125136 ind = key_position ( cmdline , key )
126137 if ind . nil?
127138 cmdline << new_setting
Original file line number Diff line number Diff line change 153153 end
154154
155155 describe '#override_and_append_commandline_with_user_supplied_arguments' do
156+ context 'with invalid key' do
157+ it 'should raise ArgumentError' do
158+ described_object = described_class . allocate
159+ cmdline = [ '--foo' , '--bar=baz' ]
160+ test_cmdline = [ '--foo$bar' ]
161+ described_object . instance_variable_set ( '@options' , command_line : test_cmdline )
162+ expect do
163+ described_object . send ( :override_and_append_commandline_with_user_supplied_arguments , cmdline )
164+ end . to raise_error ( ArgumentError , /Command line option 'foo\$ bar' is invalid/ )
165+ end
166+ end
167+
156168 context 'with standalone key' do
157169 context 'when not existing' do
158170 it 'should append standalone key' do
You can’t perform that action at this time.
0 commit comments