2020-10-30 18:08:56 +00:00
---
stage: none
group: unassigned
2020-11-26 06:09:20 +00:00
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2020-10-30 18:08:56 +00:00
---
2014-02-25 10:53:01 +00:00
# Guidelines for shell commands in the GitLab codebase
2014-12-09 13:51:15 +00:00
This document contains guidelines for working with processes and files in the GitLab codebase.
These guidelines are meant to make your code more reliable _and_ secure.
2014-03-24 11:04:43 +00:00
## References
2019-09-27 12:06:07 +00:00
- [Google Ruby Security Reviewer's Guide ](https://code.google.com/archive/p/ruby-security/wikis/Guide.wiki )
2020-03-23 03:09:21 +00:00
- [OWASP Command Injection ](https://wiki.owasp.org/index.php/Command_Injection )
2019-09-27 12:06:07 +00:00
- [Ruby on Rails Security Guide Command Line Injection ](https://guides.rubyonrails.org/security.html#command-line-injection )
2014-03-24 11:04:43 +00:00
2014-02-25 10:53:01 +00:00
## Use File and FileUtils instead of shell commands
2022-05-26 03:09:21 +00:00
Sometimes we invoke basic Unix commands via the shell when there is also a Ruby API for doing it. Use the Ruby API if it exists. < https: / / www . ruby-doc . org / stdlib-2 . 0 . 0 / libdoc / fileutils / rdoc / FileUtils . html # module-FileUtils-label-Module + Functions >
2014-02-25 10:53:01 +00:00
```ruby
# Wrong
system "mkdir -p tmp/special/directory"
# Better (separate tokens)
system *%W(mkdir -p tmp/special/directory)
# Best (do not use a shell command)
FileUtils.mkdir_p "tmp/special/directory"
# Wrong
contents = `cat #{filename}`
# Correct
contents = File.read(filename)
2014-10-02 16:27:18 +00:00
# Sometimes a shell command is just the best solution. The example below has no
# user input, and is hard to implement correctly in Ruby: delete all files and
# directories older than 120 minutes under /some/path, but not /some/path
# itself.
Gitlab::Popen.popen(%W(find /some/path -not -path /some/path -mmin +120 -delete))
2014-02-25 10:53:01 +00:00
```
This coding style could have prevented CVE-2013-4490.
2019-08-27 08:44:07 +00:00
## Always use the configurable Git binary path for Git commands
2015-11-03 22:10:17 +00:00
```ruby
# Wrong
system(*%W(git branch -d -- #{branch_name}))
# Correct
system(*%W(#{Gitlab.config.git.bin_path} branch -d -- #{branch_name}))
```
2014-02-25 10:53:01 +00:00
## Bypass the shell by splitting commands into separate tokens
2020-11-26 03:09:17 +00:00
When we pass shell commands as a single string to Ruby, Ruby lets `/bin/sh` evaluate the entire string. Essentially, we are asking the shell to evaluate a one-line script. This creates a risk for shell injection attacks. It is better to split the shell command into tokens ourselves. Sometimes we use the scripting capabilities of the shell to change the working directory or set environment variables. All of this can also be achieved securely straight from Ruby
2014-02-25 10:53:01 +00:00
```ruby
# Wrong
system "cd /home/git/gitlab & & bundle exec rake db:#{something} RAILS_ENV=production"
# Correct
system({'RAILS_ENV' => 'production'}, *%W(bundle exec rake db:#{something}), chdir: '/home/git/gitlab')
# Wrong
system "touch #{myfile}"
# Better
system "touch", myfile
# Best (do not run a shell command at all)
FileUtils.touch myfile
```
This coding style could have prevented CVE-2013-4546.
## Separate options from arguments with --
2014-04-24 22:48:22 +00:00
Make the difference between options and arguments clear to the argument parsers of system commands with `--` . This is supported by many but not all Unix commands.
2014-02-25 10:53:01 +00:00
To understand what `--` does, consider the problem below.
2020-03-25 06:07:58 +00:00
```shell
2014-02-25 10:53:01 +00:00
# Example
$ echo hello > -l
$ cat -l
2020-03-25 06:07:58 +00:00
2014-02-25 10:53:01 +00:00
cat: illegal option -- l
usage: cat [-benstuv] [file ...]
```
2014-04-24 22:48:22 +00:00
In the example above, the argument parser of `cat` assumes that `-l` is an option. The solution in the example above is to make it clear to `cat` that `-l` is really an argument, not an option. Many Unix command line tools follow the convention of separating options from arguments with `--` .
2014-02-25 10:53:01 +00:00
2020-03-25 06:07:58 +00:00
```shell
2014-02-25 10:53:01 +00:00
# Example (continued)
$ cat -- -l
2020-03-25 06:07:58 +00:00
2014-02-25 10:53:01 +00:00
hello
```
2019-09-23 18:06:14 +00:00
In the GitLab codebase, we avoid the option/argument ambiguity by _always_ using `--` for commands that support it.
2014-02-25 10:53:01 +00:00
```ruby
# Wrong
2015-11-03 22:10:17 +00:00
system(*%W(#{Gitlab.config.git.bin_path} branch -d #{branch_name}))
2014-02-25 10:53:01 +00:00
# Correct
2015-11-03 22:10:17 +00:00
system(*%W(#{Gitlab.config.git.bin_path} branch -d -- #{branch_name}))
2014-02-25 10:53:01 +00:00
```
This coding style could have prevented CVE-2013-4582.
## Do not use the backticks
2014-04-24 22:48:22 +00:00
Capturing the output of shell commands with backticks reads nicely, but you are forced to pass the command as one string to the shell. We explained above that this is unsafe. In the main GitLab codebase, the solution is to use `Gitlab::Popen.popen` instead.
2014-02-25 10:53:01 +00:00
```ruby
# Wrong
2015-11-03 22:10:17 +00:00
logs = `cd #{repo_dir} && #{Gitlab.config.git.bin_path} log`
2014-02-25 10:53:01 +00:00
# Correct
2015-11-03 22:10:17 +00:00
logs, exit_status = Gitlab::Popen.popen(%W(#{Gitlab.config.git.bin_path} log), repo_dir)
2014-02-25 10:53:01 +00:00
# Wrong
user = `whoami`
# Correct
user, exit_status = Gitlab::Popen.popen(%W(whoami))
```
2019-08-27 08:44:07 +00:00
In other repositories, such as GitLab Shell you can also use `IO.popen` .
2014-02-25 10:53:01 +00:00
```ruby
# Safe IO.popen example
2015-11-03 22:10:17 +00:00
logs = IO.popen(%W(#{Gitlab.config.git.bin_path} log), chdir: repo_dir) { |p| p.read }
2014-02-25 10:53:01 +00:00
```
Note that unlike `Gitlab::Popen.popen` , `IO.popen` does not capture standard error.
2014-12-09 13:51:15 +00:00
## Avoid user input at the start of path strings
Various methods for opening and reading files in Ruby can be used to read the
2020-02-11 21:08:44 +00:00
standard output of a process instead of a file. The following two commands do
2014-12-09 13:51:15 +00:00
roughly the same:
2016-11-09 11:06:49 +00:00
```ruby
2014-12-09 13:51:15 +00:00
`touch /tmp/pawned-by-backticks`
File.read('|touch /tmp/pawned-by-file-read')
```
The key is to open a 'file' whose name starts with a `|` .
Affected methods include Kernel#open, File::read, File::open, IO::open and IO::read.
You can protect against this behavior of 'open' and 'read' by ensuring that an
2020-02-11 21:08:44 +00:00
attacker cannot control the start of the filename string you are opening. For
2014-12-09 13:51:15 +00:00
instance, the following is sufficient to protect against accidentally starting
a shell command with `|` :
2016-11-09 11:06:49 +00:00
```ruby
2014-12-09 13:51:15 +00:00
# we assume repo_path is not controlled by the attacker (user)
path = File.join(repo_path, user_input)
# path cannot start with '|' now.
File.read(path)
```
2015-02-26 17:25:59 +00:00
If you have to use user input a relative path, prefix `./` to the path.
Prefixing user-supplied paths also offers extra protection against paths
starting with `-` (see the discussion about using `--` above).
2014-12-09 13:51:15 +00:00
## Guard against path traversal
Path traversal is a security where the program (GitLab) tries to restrict user
access to a certain directory on disk, but the user manages to open a file
outside that directory by taking advantage of the `../` path notation.
2016-11-09 11:06:49 +00:00
```ruby
2014-12-09 13:51:15 +00:00
# Suppose the user gave us a path and they are trying to trick us
user_input = '../other-repo.git/other-file'
# We look up the repo path somewhere
repo_path = 'repositories/user-repo.git'
# The intention of the code below is to open a file under repo_path, but
2020-02-07 15:09:52 +00:00
# because the user used '..' they can 'break out' into
2014-12-09 13:51:15 +00:00
# 'repositories/other-repo.git'
full_path = File.join(repo_path, user_input)
File.open(full_path) do # Oops!
```
A good way to protect against this is to compare the full path with its
'absolute path' according to Ruby's `File.absolute_path` .
2016-11-09 11:06:49 +00:00
```ruby
2014-12-09 13:51:15 +00:00
full_path = File.join(repo_path, user_input)
if full_path != File.absolute_path(full_path)
raise "Invalid path: #{full_path.inspect}"
end
File.open(full_path) do # Etc.
```
A check like this could have avoided CVE-2013-4583.
2015-06-12 14:17:04 +00:00
## Properly anchor regular expressions to the start and end of strings
2019-02-18 09:36:13 +00:00
When using regular expressions to validate user input that is passed as an argument to a shell command, make sure to use the `\A` and `\z` anchors that designate the start and end of the string, rather than `^` and `$` , or no anchors at all.
2015-06-12 14:17:04 +00:00
If you don't, an attacker could use this to execute commands with potentially harmful effect.
2021-02-04 09:09:30 +00:00
For example, when a project's `import_url` is validated like below, the user could trick GitLab into cloning from a Git repository on the local file system.
2015-06-12 14:17:04 +00:00
```ruby
validates :import_url, format: { with: URI.regexp(%w(ssh git http https)) }
2019-02-18 09:36:13 +00:00
# URI.regexp(%w(ssh git http https)) roughly evaluates to /(ssh|git|http|https):(something_that_looks_like_a_url)/
2015-06-12 14:17:04 +00:00
```
Suppose the user submits the following as their import URL:
2020-03-25 06:07:58 +00:00
```plaintext
2015-06-12 14:17:04 +00:00
file://git:/tmp/lol
```
Since there are no anchors in the used regular expression, the `git:/tmp/lol` in the value would match, and the validation would pass.
When importing, GitLab would execute the following command, passing the `import_url` as an argument:
2020-01-30 15:09:15 +00:00
```shell
2015-06-12 14:17:04 +00:00
git clone file://git:/tmp/lol
```
2021-04-01 06:09:29 +00:00
Git ignores the `git:` part, interpret the path as `file:///tmp/lol` , and imports the repository into the new project. This action could potentially give the attacker access to any repository in the system, whether private or not.