-
Notifications
You must be signed in to change notification settings - Fork 16
fix: create the logfile without a shell (CWE-78 command injection) #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| require 'browserstack/localbinary' | ||
| require 'browserstack/localexception' | ||
| require 'json' | ||
| require 'fileutils' | ||
|
|
||
| module BrowserStack | ||
|
|
||
|
|
@@ -73,10 +74,16 @@ def start(options = {}) | |
| @binary_path | ||
| end | ||
|
|
||
| if @is_windows | ||
| system("echo > #{@logfile}") | ||
| else | ||
| system("echo '' > '#{@logfile}'") | ||
| # Create/truncate the logfile without a shell. The previous | ||
| # `system("echo ... > #{@logfile}")` passed @logfile to /bin/sh (or cmd.exe), | ||
| # so shell metacharacters in a caller-supplied logfile path executed as commands | ||
| # (CWE-78). File.write treats the path purely as a filename. | ||
| logfile_dir = File.dirname(@logfile) | ||
| FileUtils.mkdir_p(logfile_dir) unless File.directory?(logfile_dir) | ||
| begin | ||
| File.write(@logfile, "") | ||
| rescue SystemCallError => e | ||
| raise BrowserStack::LocalException.new("Unable to open logfile: #{e.message}") | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nit] |
||
| end | ||
|
|
||
| if defined? spawn | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nit] This fix correctly closes the logfile-creation shell sink. For completeness, note that the same
@logfilevalue is still interpolated into a shell string elsewhere —start_command(further down this file):That string is only executed via
IO.popen(start_command)in theelseofif defined? spawn— i.e. on Ruby < 1.9, wherespawnis not defined. On every supported Ruby (1.9+),start_command_args(the array form, no shell) is used instead, so this path is effectively dead and this PR does not regress it.This is pre-existing and out of scope for this ticket (the finding here is the
echo-into-systemlogfile sink, which is fully removed). Flagging only so a human can decide whether a follow-up ticket to harden/remove the legacystart_commandstring form (and its unquoted@key/@folder_path/proxy interpolations) is worthwhile. Not blocking.