Skip to content
Access control Disclosed report Intermediate

Backend: Fallback cache key allows accessing the cache of protected branches from non protected ones

GitLab Runner's fallback cache key validation could be bypassed by appending a slash to a key ending in '-protected', allowing access to the cache of protected branches from non-protected ones. An attacker could read or poison the protected branch cache, potentially executing arbitrary code in that context.

GitLab

Originally disclosed on GitLab

  • Fixed in 54 days
  • Disclosed after 54 days

⚠️ Please read the process on how to fix security issues before starting to work on the issue. Vulnerabilities must be fixed in a security mirror.

HackerOne report #1954886 by 0xn3va on 2023-04-19, assigned to @kmorrison1:

Report | Attachments | How To Reproduce

Report

Summary

GitLab allows setting a fallback cache key that will be used in case the cache with a predefined key is not found, see https://docs.gitlab.com/ee/ci/caching/#use-a-fallback-cache-key For the fallback cache keys the suffixes -protected and -non_protected are not added and the keys are used as is. However, GitLan Runner validates that a fallback cache key does not contain -protected prefix to avoid abusing it for reading/writing data from the cache of a protected branch:

Go
// https://gitlab.com/gitlab-org/gitlab-runner/-/blob/146e799742a9b5999ade14e9e84331018e074fac/shells/abstract.go#L144

	if strings.HasSuffix(cacheFallbackKey, "-protected") {  
		// The `-protected` suffix is reserved for protected refs, so we disallow it from user-specified values.  
		cacheFallbackKey = ""  
	}  

In other words, if a fallback cache key contains the -protected suffix, it will not be used for cache checking. Let's consider how cache keys are used to access the cache.

Go
// https://gitlab.com/gitlab-org/gitlab-runner/-/blob/146e799742a9b5999ade14e9e84331018e074fac/shells/abstract.go#L137

func (b *AbstractShell) extractCacheOrFallbackCacheWrapper(  
	w ShellWriter,  
	info common.ShellScriptInfo,  
	cacheFile string,  
	cacheKey string,  
) {  
	cacheFallbackKey := info.Build.GetAllVariables().Value("CACHE_FALLBACK_KEY")  
	if strings.HasSuffix(cacheFallbackKey, "-protected") {  
		// The `-protected` suffix is reserved for protected refs, so we disallow it from user-specified values.  
		cacheFallbackKey = ""  
	}

	// Execute cache-extractor command. Failure is not fatal.  
	b.guardRunnerCommand(w, info.RunnerCommand, "Extracting cache", func() {  
		b.addExtractCacheCommand(w, info, cacheFile, cacheKey, cacheFallbackKey)  
	})  
}

func (b *AbstractShell) addExtractCacheCommand(  
	w ShellWriter,  
	info common.ShellScriptInfo,  
	cacheFile string,  
	cacheKey string,  
	cacheFallbackKey string,  
) {  
	args := []string{  
		"cache-extractor",  
		"--file", cacheFile,  
		"--timeout", strconv.Itoa(info.Build.GetCacheRequestTimeout()),  
	}

	if url := cache.GetCacheDownloadURL(info.Build, cacheKey); url != nil {  
		args = append(args, "--url", url.String())  
	}

	w.Noticef("Checking cache for %s...", cacheKey)  
	w.IfCmdWithOutput(info.RunnerCommand, args...)  
	w.Noticef("Successfully extracted cache")  
	w.Else()  
	w.Warningf("Failed to extract cache")  
	if cacheFallbackKey != "" {  
		b.addExtractCacheCommand(w, info, cacheFile, cacheFallbackKey, "")  
	}  
	w.EndIf()  
}

As can be seen, cache keys are used by cache.GetCacheDownloadURL that crafts a cache download URL and the crafted URL is passed as an argument to the cache-extractor command. cache.GetCacheDownloadURL does not perform any additional checks and simply concatenates several parameters in the URL depending on which adapter is used (in case of GitLab.com is S3).

Since the crafted URL is passed to the --url parameter, we can use / at the end of the fallback cache key to bypass the validation and craft a valid object name that will be combined with other parameters into a valid URL. In other words, if there is the main-protected cache key, the cache path will be:

namespace/project/1337/main-protected  

and for the fallback cache key main-protected/ the cache path will be:

namespace/project/1337/main-protected/  

As a result, the fallback cache key can be used to access the cache of protected branches from non-protected ones.

Steps to reproduce
  1. Create a project
  2. Add .gitlab-ci.yml file with the following content:
YAML
my-job:  
  script:  
    - echo Currently in $CI_COMMIT_REF_SLUG  
    - test -f cached_file.txt && cat cached_file.txt  
    - test -f cached_file.txt || echo "This is from $CI_COMMIT_REF_SLUG" > cached_file.txt  
  cache:  
    key: "$CI_COMMIT_REF_SLUG"  
    paths:  
      - cached_file.txt  
  1. Wait for the job to compete.
  2. Create a new branch.
  3. Update .gitlab-ci.yml with the following content:
YAML
variables:  
  CACHE_FALLBACK_KEY: "$CI_DEFAULT_BRANCH-protected/"

my-job:  
  script:  
    - echo Currently in $CI_COMMIT_REF_SLUG  
    - test -f cached_file.txt && cat cached_file.txt  
    - test -f cached_file.txt || echo "This is from $CI_COMMIT_REF_SLUG" > cached_file.txt  
  cache:  
    key: "$CI_COMMIT_REF_SLUG-hop"  
    paths:  
      - cached_file.txt  
  1. Wait for the job to complete.
  2. Check out the job logs; the cache for the main branch was downloaded.
Impact

This vulnerability allows an attacker to gain read/write permissions for the cache of protected branches. It can be used to poison the cache and execute arbitrary commands/code in the context of protected branches. Since pipelines can be triggered in parent projects from forks https://docs.gitlab.com/ee/ci/pipelines/merge_request_pipelines.html#run-pipelines-in-the-parent-project an attacker can poison the cache with minimum permissions.

What is the current bug behavior?

Cache keys are placed directly to a cache URL.

What is the expected correct behavior?

Cache keys are encoded before being placed in a cache URL.

Relevant logs and/or screenshots
Screen_Recording_2023-04-19_at_21.04.56.mov From the original on GitLab
Output of checks

This bug happens on GitLab.com

Impact

This vulnerability allows an attacker to gain read/write permissions for the cache of protected branches. It can be used to poison the cache and execute arbitrary commands/code in the context of protected branches. Since pipelines can be triggered in parent projects from forks https://docs.gitlab.com/ee/ci/pipelines/merge_request_pipelines.html#run-pipelines-in-the-parent-project an attacker can poison the cache with minimum permissions.

Attachments

Warning: Attachments received through HackerOne, please exercise caution!

How To Reproduce

Please add reproducibility information to this section:

The original report as published on GitLab. Images and videos are our own copies; raw HTML is shown as text. View it on GitLab.

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 fallback cache key is not properly encoded before being inserted into the cache URL, allowing a trailing slash to bypass the suffix check.

Impact

An attacker could gain read/write access to the cache of protected branches, enabling cache poisoning and potentially arbitrary code execution in the context of protected branches.

What the attacker needed

Access to create a project; ability to run pipelines on a non-protected branch

The fix

Cache keys should be encoded before being placed in a cache URL.

How it was found

  1. Create a project and add a CI job that caches a file using the branch name as key.
  2. Run the job on the default branch to populate its cache.
  3. Create a new branch and modify the CI configuration to set a fallback cache key ending with '-protected/'.
  4. Run the job on the new branch and observe that the cache from the default branch is downloaded.

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 CI/CD caches files between jobs to speed up pipelines. Protected branches (like 'main') have their own cache, and GitLab tries to prevent other branches from accessing it. This report shows a way to bypass that protection by adding a slash to a special fallback cache key, letting a less privileged branch read and write the protected cache.

What is Access control?

A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.

More from GitLab

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Broken access control

The lesson behind this report.

A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.