SSRF in CI after first run
A GitLab CI job could reach the cloud instance metadata endpoint on a re-run of a build, even though the first run blocked it. An attacker with the ability to define a CI job could read metadata such as hostname, user-data, public keys and network details.
Originally disclosed on HackerOne
- Triaged in 14 days
- Fixed in 8 months
- Paid in 8 months
- Disclosed after 10 months
Summary by the researcher
GitLab CI runners did not properly restrict access to the google cloud instance metadata APIs, allowing a malicious CI job to create a service token and access internal buckets containing private keys and logfiles. I've written a brief blog post on this issue here: https://dylankatz.com/attacking-cloud-containers-using-ssrf/
The report
NOTE! Thanks for submitting a report! Please replace all the [square] sections below with the pertinent details. Remember, the more detail you provide, the easier it is for us to triage and respond quickly, so be sure to take your time filling out the report!
Summary: During the first run, the CI pipeline seems to defend against SSRF properly, however when a build is re-run a second time, I am able to access internal metadata endpoints for digitalocean
Description:
The following resources are accessible on the second run of a CI build. For instance,
http://169.254.169.254/metadata/v1.json
and http://169.254.169.254/metadata/v1/
are both visible.
Steps To Reproduce:
(Add details for how we can reproduce the issue)
- Create a
.gitlab-ci.yml. This was my PoC:
# This file is a template, and might need editing before it works on your project.
# Official framework image. Look for the different tagged releases at:
# https://hub.docker.com/r/library/node/tags/
image: node:latest
# This folder is cached between builds
# http://docs.gitlab.com/ce/ci/yaml/README.html#cache
cache:
paths:
- node_modules/
test:
stage: test
script:
- npm install
- npm test
pack:
stage: deploy
script:
- chmod +x run.sh
- ./run.sh
- npm install
- npm pack
artifacts:
paths:
- ./*.tgz
- Create a bash file containing this line:
curl -L http://169.254.169.254/metadata/v1/
- Run the build pipeline. It will work as intended with no leaks. Now re-run the build. You should see this output:
id
hostname
user-data
vendor-data
public-keys
region
interfaces/
dns/
floating_ip/
tags/
features/
This indicates access to internal resources, and thus successful SSRF.
Impact
Any internal resources visible to the node. For gitlab cloud, this looks to be digitalocean metadata, but this will also allow access to any resources the gitlab server can see.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the SSRF protection was only applied on the first pipeline run and was not enforced when the same build was re-run.
Impact
A malicious CI job could read cloud metadata, which may expose hostname, user-data, public keys and network details, and could reach any internal resource visible to the runner.
What the attacker needed
Ability to define a CI job; pipeline must be re-run
How it was found
- Create a pipeline configuration file that runs a shell script during the deploy stage.
- Put a command in the script that requests the cloud metadata address.
- Run the pipeline once and observe that the request is blocked.
- Re-run the same build and observe the metadata listing returned.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
CI pipelines run code on a shared build machine. That machine can reach a special internal address that cloud providers use to hand out information about the server itself. The report says the first run of a pipeline blocked access to that address, but a second run did not, so a job could read private server details.
What is SSRF?
The server can be made to fetch an address the attacker chooses, reaching internal services, cloud metadata or other customers' data.