|
| 1 | +require "../amber_cli_spec" |
| 2 | +require "../../src/amber_cli/core/base_command" |
| 3 | + |
| 4 | +# Regression spec for shell injection fix in exec/encrypt editor invocation. |
| 5 | +# Verifies that shell metacharacters in the editor option or filename are NOT |
| 6 | +# executed by the shell. Reference: amberframework/amber#1376 by @tcoatswo. |
| 7 | +# |
| 8 | +# The vulnerability was: system("#{editor} #{filename}") — a crafted editor |
| 9 | +# string like 'vim; touch /tmp/pwned' would execute the injected command. |
| 10 | +# The fix uses Process.run with an explicit args array (no shell: true). |
| 11 | +describe "ExecCommand editor invocation safety" do |
| 12 | + it "does not execute injected shell commands when editor contains semicolon" do |
| 13 | + # With system("#{editor} #{filename}"), setting editor = "vim; touch /tmp/pwned" |
| 14 | + # would run `touch /tmp/pwned`. With Process.parse_arguments + Process.run, |
| 15 | + # the semicolon is part of the editor token, not a shell command separator. |
| 16 | + marker = "/tmp/amber_exec_semi_pwned_#{Time.utc.to_unix_ms}" |
| 17 | + File.delete(marker) if File.exists?(marker) |
| 18 | + |
| 19 | + # An attacker sets EDITOR=vim; touch <marker> |
| 20 | + # Process.parse_arguments on POSIX treats "vim; touch /tmp/..." as a single |
| 21 | + # token because there are no shell word-break chars (it's unquoted but |
| 22 | + # parse_arguments_posix only splits on whitespace and quotes). |
| 23 | + # The resulting command "vim; touch ..." will fail to execute (no such file). |
| 24 | + malicious_editor = "vim; touch #{marker}" |
| 25 | + editor_parts = Process.parse_arguments(malicious_editor) |
| 26 | + editor_cmd = editor_parts.first # => "vim;" |
| 27 | + editor_args = editor_parts[1..] + ["/dev/null"] |
| 28 | + |
| 29 | + # Run — we expect an exception (no binary named "vim;") but no marker creation. |
| 30 | + begin |
| 31 | + Process.run(editor_cmd, editor_args, input: Process::Redirect::Close, output: Process::Redirect::Close, error: Process::Redirect::Close) |
| 32 | + rescue File::NotFoundError | File::AccessDeniedError | Exception |
| 33 | + # Expected: binary "vim;" doesn't exist, Process.run raises. |
| 34 | + end |
| 35 | + |
| 36 | + File.exists?(marker).should be_false |
| 37 | + end |
| 38 | + |
| 39 | + it "does not execute injected shell commands when filename contains shell metacharacters" do |
| 40 | + # With system("vim #{filename}"), a filename containing "; touch /tmp/pwned" |
| 41 | + # would inject a command. With Process.run and explicit args, the filename |
| 42 | + # is passed as a single argument — no shell parsing occurs. |
| 43 | + marker = "/tmp/amber_exec_fn_pwned_#{Time.utc.to_unix_ms}" |
| 44 | + File.delete(marker) if File.exists?(marker) |
| 45 | + |
| 46 | + injected_filename = "/dev/null; touch #{marker}" |
| 47 | + |
| 48 | + # Process.run does NOT invoke the shell, so the semicolon is literal. |
| 49 | + # `cat` will fail (no such file "/dev/null; touch /tmp/...") but will |
| 50 | + # NOT execute the touch command. |
| 51 | + Process.run("cat", [injected_filename], input: Process::Redirect::Close, output: Process::Redirect::Close, error: Process::Redirect::Close) |
| 52 | + |
| 53 | + File.exists?(marker).should be_false |
| 54 | + end |
| 55 | + |
| 56 | + it "handles multi-word editor like 'code -w' by splitting on whitespace" do |
| 57 | + # When EDITOR="code -w", Process.parse_arguments yields ["code", "-w"]. |
| 58 | + # The first token is the command and the rest are prepended to the filename. |
| 59 | + editor = "code -w" |
| 60 | + parts = Process.parse_arguments(editor) |
| 61 | + parts.should eq(["code", "-w"]) |
| 62 | + parts.first.should eq("code") |
| 63 | + parts[1..].should eq(["-w"]) |
| 64 | + end |
| 65 | + |
| 66 | + it "handles multi-word editor like 'vim -u none' by splitting on whitespace" do |
| 67 | + editor = "vim -u none" |
| 68 | + parts = Process.parse_arguments(editor) |
| 69 | + parts.should eq(["vim", "-u", "none"]) |
| 70 | + parts.first.should eq("vim") |
| 71 | + parts[1..].should eq(["-u", "none"]) |
| 72 | + end |
| 73 | +end |
0 commit comments