Arbitrary file read via the UploadsRewriter when moving and issue
The UploadsRewriter did not validate the file name when copying uploads referenced in an issue, so a crafted markdown link with directory traversal could copy any file on the server into a project. An attacker could then read arbitrary files such as tokens, private data and configs.
- Triaged in 2 hours
- Fixed in 4 days
- Paid in 2 hours
- Disclosed after 35 days
Summary
The UploadsRewriter does not validate the file name, allowing arbitrary files to be copied via directory traversal when moving an issue to a new project.
The pattern used to look for references is :
MARKDOWN_PATTERN = %r{\!?\[.*?\]\(/uploads/(?<secret>[0-9a-f]{32})/(?<file>.*?)\)}.freeze
This is used by the UploadsRewriter when copying an issue to also copy across the files:
@text.gsub(@pattern) do |markdown|
file = find_file(@source_project, $~[:secret], $~[:file])
break markdown unless file.try(:exists?)
klass = target_parent.is_a?(Namespace) ? NamespaceFileUploader : FileUploader
moved = klass.copy_to(file, target_parent)
...
def find_file(project, secret, file)
uploader = FileUploader.new(project, secret: secret)
uploader.retrieve_from_store!(file)
uploader
end
As there is no restriction on what file can be, path traversal can be used to copy any file.
Demo
Steps to reproduce
- Create two projects
- Add an issue with the following description:
{{media:1}}
- Move the issue to the second project
- The file will have been copied to the project
Impact
Allows an attacker to read arbitrary files on the server, including tokens, private data, configs, etc
What is the current bug behavior?
The file name and path are not checked when copying an issue between projects
What is the expected correct behavior?
The file or path should be validated before copying files.
Output of checks
Results of GitLab environment info
System information
System: Ubuntu 18.04
Proxy: no
Current User: git
Using RVM: no
Ruby Version: 2.6.5p114
Gem Version: 2.7.10
Bundler Version:1.17.3
Rake Version: 12.3.3
Redis Version: 5.0.7
Git Version: 2.24.1
Sidekiq Version:5.2.7
Go Version: unknown
GitLab information
Version: 12.8.7-ee
Revision: 2643fd87200
Directory: /opt/gitlab/embedded/service/gitlab-rails
DB Adapter: PostgreSQL
DB Version: 10.12
URL: http://gitlab-vm.local
HTTP Clone URL: http://gitlab-vm.local/some-group/some-project.git
SSH Clone URL: [email protected]:some-group/some-project.git
Elasticsearch: no
Geo: no
Using LDAP: no
Using Omniauth: yes
Omniauth Providers:
GitLab Shell
Version: 11.0.0
Repository storage paths:
- default: /var/opt/gitlab/git-data/repositories
GitLab Shell path: /opt/gitlab/embedded/service/gitlab-shell
Git: /opt/gitlab/embedded/bin/git
Impact
Allows an attacker to read arbitrary files on the server, including tokens, private data, configs, etc
Context
GitLab issues can contain attachments. When you paste an image into an issue description, GitLab stores it as an upload and rewrites the markdown to reference it through a path of the form /uploads/<secret>/<file>, where <secret> is a 32-character hexadecimal value and <file> is the file name.
When an issue is moved from one project to another, GitLab has to carry those uploads across as well, because the uploads belong to the source project. The component responsible for this is UploadsRewriter. It scans the issue text for upload references and copies each referenced file from the source project into the target project.
The report is about that copy step: the file name taken from the markdown is passed to the uploader without validation, so a crafted reference can point outside the uploads directory.
Setup
The researcher tested against their own GitLab instance. The environment information reported was:
System information
System: Ubuntu 18.04
Proxy: no
Current User: git
Using RVM: no
Ruby Version: 2.6.5p114
Gem Version: 2.7.10
Bundler Version:1.17.3
Rake Version: 12.3.3
Redis Version: 5.0.7
Git Version: 2.24.1
Sidekiq Version:5.2.7
Go Version: unknown
GitLab information
Version: 12.8.7-ee
Revision: 2643fd87200
Directory: /opt/gitlab/embedded/service/gitlab-rails
DB Adapter: PostgreSQL
DB Version: 10.12
URL: http://[redacted]
HTTP Clone URL: http://[redacted]/some-group/some-project.git
SSH Clone URL: [redacted]:some-group/some-project.git
Elasticsearch: no
Geo: no
Using LDAP: no
Using Omniauth: yes
Omniauth Providers:
GitLab Shell
Version: 11.0.0
Repository storage paths:
- default: /var/opt/gitlab/git-data/repositories
GitLab Shell path: /opt/gitlab/embedded/service/gitlab-shell
Git: /opt/gitlab/embedded/bin/git
The preconditions are modest: two projects the attacker can work with, and the ability to create an issue in one of them and move it to the other. No special role beyond that is described.
Finding it
The starting point is the regular expression UploadsRewriter uses to find upload references in issue text:
MARKDOWN_PATTERN = %r{\!?\[.*?\]\(/uploads/(?<secret>[0-9a-f]{32})/(?<file>.*?)\)}.freeze
The secret group is tightly constrained to 32 hexadecimal characters, but the file group is .*?, which matches anything up to the closing parenthesis. That captured value is then handed to the uploader to locate the file to copy:
@text.gsub(@pattern) do |markdown|
file = find_file(@source_project, $~[:secret], $~[:file])
break markdown unless file.try(:exists?)
klass = target_parent.is_a?(Namespace) ? NamespaceFileUploader : FileUploader
moved = klass.copy_to(file, target_parent)
...
def find_file(project, secret, file)
uploader = FileUploader.new(project, secret: secret)
uploader.retrieve_from_store!(file)
uploader
end
find_file builds a FileUploader for the source project with the supplied secret and calls retrieve_from_store! with the file name straight from the markdown. Nothing checks that the name is a plain file name or that the resolved path stays inside the project's upload directory. Since the value is attacker-controlled, path traversal sequences in the file portion should let the rewriter resolve and copy a file from anywhere the GitLab process can read.
Exploitation
Step 1: Create two projects
Create two projects on the instance. The first holds the malicious issue; the second is the move destination.
Step 2: Add an issue with a crafted description
The issue description contains a markdown image reference whose file component is a traversal path rather than a real upload name:
{{media:1}}
Because the pattern only requires /uploads/ followed by 32 hex characters and then any characters, this reference matches. The secret group is satisfied by the 32-character hex value, and the file group captures the traversal path. When the issue is moved, find_file passes that captured value to retrieve_from_store!, which resolves it relative to the upload store and walks out of it.
Step 3: Move the issue to the second project
Move the issue from the first project to the second. This triggers UploadsRewriter, which processes the description, matches the crafted reference, finds the file at the traversed path, and copies it into the target project.
Step 4: Retrieve the copied file
The file will have been copied to the second project.
The report includes a demonstration video of the file read:
Root cause
The bug is a path traversal in the upload copy logic. The markdown pattern deliberately allows an arbitrary file component, and find_file uses that component directly with retrieve_from_store! without validating it. The uploader resolves the name against the store, so ../ sequences escape the intended uploads directory and reach other files on disk. The only check in the flow is file.try(:exists?), which confirms the traversed file exists rather than that it is a legitimate upload. The secret is validated by the regex, but the secret is not what determines the final path once traversal is in play.
Impact
An attacker who can create an issue and move it between projects can read arbitrary files on the server that the GitLab process is able to access, including tokens, private data and configuration files. The report states this impact twice, in the same words: arbitrary file read on the server, including tokens, private data, configs.
Fix
The report's expected behaviour is that the file or path should be validated before copying files. The current behaviour it identifies as the bug is that the file name and path are not checked when copying an issue between projects. The report does not describe the specific patch that was applied.
Variants to test
- Other markdown-driven copy or move features that resolve upload references, such as snippets or comments being transferred between projects. A positive result is a file outside the uploads directory appearing in the destination.
- Different traversal depths and encodings in the
filecomponent, for example encoded slashes or dot segments, to see whether any normalisation happens beforeretrieve_from_store!. A positive result is the same out-of-store file being copied. - Absolute paths in the
filecomponent, where the uploader accepts them. A positive result is a file from an unrelated directory being copied into the project. - Target files that are readable but not obviously sensitive, to confirm the primitive before aiming at configuration or token files. A positive result is any file content that could not have been uploaded legitimately.
Hunting notes
This class of bug hides in features that take a user-supplied reference from text and turn it into a filesystem lookup: attachment rewriters, importers and exporters, and anything that copies content between containers. During recon, look for markdown patterns that embed identifiers and file names in URLs, and check whether the file-name portion is constrained. The signal is a regex or parser that validates one part of a path (here the secret) while leaving another part open. To show impact, the researcher included a demonstration video of the file read.
What happened, in plain words
GitLab is a website where teams keep their code and track work. One of its features is the issue: a page where people write a description of a task, a bug or an idea, and discuss it. You can paste a picture into an issue description, and GitLab stores that picture as a file attached to the project, called an upload.
When you move an issue from one project to another, GitLab tries to be helpful: it copies the issue's attachments across too, because the attachments belong to the old project. A piece of code called UploadsRewriter does this copying. It reads the issue text, finds every reference to an uploaded file, and copies each one into the new project.
The problem is that UploadsRewriter trusts the file name written in the issue text. If an attacker writes a file name that means "go up a few folders and read this other file instead", GitLab follows it. That lets the attacker copy any file on the server that GitLab can read, such as configuration files or files containing secret tokens, into a project they can open in their browser. This kind of trick is called path traversal: using .. (which means "parent folder") to step outside the folder you are supposed to be limited to.
The researcher demonstrated it on their own GitLab instance.
Background you need first
A few ideas come up again and again below, so here they are from zero.
Project. In GitLab, a project is a container for a code repository plus its issues, merge requests and uploaded files. Think of it as a folder on the website that belongs to a team.
Issue. An issue is a page with a title and a description where you write down work to be done. The description is written in Markdown, a simple way of formatting text: **bold** makes bold text, and  inserts an image from a URL.
Upload. When you paste an image into an issue, GitLab saves the image file on its server and puts a link to it in the description. That saved file is an upload. Uploads belong to the project they were pasted into.
Markdown reference to an upload. GitLab writes the link in a fixed shape: /uploads/<secret>/<file>. The <secret> is a 32-character hexadecimal value (hexadecimal means the characters 0 to 9 and a to f). The <file> is the file name. The secret makes the link hard to guess.
Regular expression (regex). A pattern used to find pieces of text. For example, [0-9a-f]{32} means "exactly 32 characters, each one a digit or a letter from a to f". A regex can also capture parts of what it matches, giving them names so the program can use them later.
Path traversal. On a computer, files live in folders, and .. means "the folder above this one". If a program is supposed to read only from /uploads/ but accepts a name like ../../etc/passwd, it can be tricked into reading a file far outside /uploads/. That is path traversal.
Arbitrary file read. A vulnerability that lets an attacker read any file on the server, not just the ones they are meant to see. "Arbitrary" here means "whatever the attacker chooses".
Token. A short secret string that proves who you are or grants access to something, much like a password. If an attacker can read a file containing tokens, they may be able to act as the server or as other users.
Context: how moving an issue copies uploads
When an issue moves from project A to project B, the attachments still live in project A. So GitLab runs UploadsRewriter, which scans the issue description for upload references and copies each referenced file from the source project into the target project.
The report is about that copy step. The file name taken from the Markdown is passed to the uploader without validation, so a crafted reference can point outside the uploads folder.
Setup: the test environment
The researcher tested against their own GitLab instance, not somebody else's. The environment information reported was:
System information
System: Ubuntu 18.04
Proxy: no
Current User: git
Using RVM: no
Ruby Version: 2.6.5p114
Gem Version: 2.7.10
Bundler Version:1.17.3
Rake Version: 12.3.3
Redis Version: 5.0.7
Git Version: 2.24.1
Sidekiq Version:5.2.7
Go Version: unknown
GitLab information
Version: 12.8.7-ee
Revision: 2643fd87200
Directory: /opt/gitlab/embedded/service/gitlab-rails
DB Adapter: PostgreSQL
DB Version: 10.12
URL: http://[redacted]
HTTP Clone URL: http://[redacted]/some-group/some-project.git
SSH Clone URL: [redacted]:some-group/some-project.git
Elasticsearch: no
Geo: no
Using LDAP: no
Using Omniauth: yes
Omniauth Providers:
GitLab Shell
Version: 11.0.0
Repository storage paths:
- default: /var/opt/gitlab/git-data/repositories
GitLab Shell path: /opt/gitlab/embedded/service/gitlab-shell
Git: /opt/gitlab/embedded/bin/git
This block is the output of a GitLab diagnostic command. It lists the operating system, the versions of the software components GitLab depends on (Ruby, Redis, Git, PostgreSQL and so on), the GitLab version itself (12.8.7-ee, the enterprise edition), and where files are stored on disk. Researchers include it so that anyone reproducing the bug knows exactly which version to try.
The preconditions are modest: two projects the attacker can work with, and the ability to create an issue in one and move it to the other. No special role beyond that is described.
Finding it: the pattern that trusts the file name
The starting point is the regular expression UploadsRewriter uses to find upload references in issue text:
MARKDOWN_PATTERN = %r{\!?\[.*?\]\(/uploads/(?<secret>[0-9a-f]{32})/(?<file>.*?)\)}.freeze
Line by line, in plain words:
MARKDOWN_PATTERN =gives a name to the pattern so the rest of the code can use it.%r{ ... }is Ruby's way of writing a regular expression with curly braces as delimiters, so slashes inside do not need escaping.\!?means an optional exclamation mark, the!that Markdown puts before an image link.\[.*?\]means a[, then any characters (lazily, i.e. as few as possible), then a]. That is the image's alt text.\(and\)are literal opening and closing parentheses around the URL./uploads/is the fixed part of the path where uploads live.(?<secret>[0-9a-f]{32})captures exactly 32 hexadecimal characters and names the capturesecret.(?<file>.*?)captures any characters, as few as possible, and names the capturefile.
The secret group is tightly constrained, but the file group is .*?, which matches anything up to the closing parenthesis. That captured value is then handed to the uploader to locate the file to copy:
@text.gsub(@pattern) do |markdown|
file = find_file(@source_project, $~[:secret], $~[:file])
break markdown unless file.try(:exists?)
klass = target_parent.is_a?(Namespace) ? NamespaceFileUploader : FileUploader
moved = klass.copy_to(file, target_parent)
...
def find_file(project, secret, file)
uploader = FileUploader.new(project, secret: secret)
uploader.retrieve_from_store!(file)
uploader
end
What each part does:
@text.gsub(@pattern) do |markdown|walks through the issue text, finds every match of the pattern, and runs the block of code for each match.gsubmeans "global substitute": it is normally used to replace text, and here the block decides what to do with each match.file = find_file(@source_project, $~[:secret], $~[:file])calls a helper, passing the source project, the captured secret and the captured file name.$~refers to the most recent regex match, so$~[:secret]and$~[:file]are the two captured pieces.break markdown unless file.try(:exists?)stops processing this match if the file does not exist.tryavoids an error if the value is nil.klass = target_parent.is_a?(Namespace) ? NamespaceFileUploader : FileUploaderpicks which uploader class to use depending on whether the destination is a namespace (a group) or a project.moved = klass.copy_to(file, target_parent)performs the actual copy into the destination.def find_file(project, secret, file)defines the helper. It creates aFileUploaderfor the source project with the given secret, then callsuploader.retrieve_from_store!(file)with the file name straight from the Markdown, and returns the uploader.
Nothing checks that the name is a plain file name, or that the resolved path stays inside the project's upload folder. Since the value is attacker-controlled, path traversal sequences in the file portion should let the rewriter resolve and copy a file from anywhere the GitLab process can read.
Exploitation, step by step
Step 1: Create two projects
Create two projects on the instance. The first holds the malicious issue; the second is the move destination. You need two because the bug only triggers during a move between projects: that is the moment the rewriter runs.
Step 2: Add an issue with a crafted description
The issue description contains a Markdown image reference whose file component is a traversal path rather than a real upload name:
{{media:1}}
This is the payload. Because the pattern only requires /uploads/ followed by 32 hex characters and then any characters, this reference matches. The secret group is satisfied by the 32-character hex value, and the file group captures the traversal path. When the issue is moved, find_file passes that captured value to retrieve_from_store!, which resolves it relative to the upload store and walks out of it.
Step 3: Move the issue to the second project
Move the issue from the first project to the second. This triggers UploadsRewriter, which processes the description, matches the crafted reference, finds the file at the traversed path, and copies it into the target project.
Step 4: Retrieve the copied file
The file will have been copied to the second project. The report includes a demonstration video of the file read:
Root cause
The bug is a path traversal in the upload copy logic. The Markdown pattern deliberately allows an arbitrary file component, and find_file uses that component directly with retrieve_from_store! without validating it. The uploader resolves the name against the store, so ../ sequences escape the intended uploads folder and reach other files on disk. The only check in the flow is file.try(:exists?), which confirms the traversed file exists rather than that it is a legitimate upload. The secret is validated by the regex, but the secret is not what determines the final path once traversal is in play.
Impact
An attacker who can create an issue and move it between projects can read arbitrary files on the server that the GitLab process is able to access, including tokens, private data and configuration files. The report states this impact twice, in the same words: arbitrary file read on the server, including tokens, private data, configs.
Fix
The report's expected behaviour is that the file or path should be validated before copying files. The current behaviour it identifies as the bug is that the file name and path are not checked when copying an issue between projects. The report does not describe the specific patch that was applied.
Variants to test
- Other Markdown-driven copy or move features that resolve upload references, such as snippets or comments being transferred between projects. A positive result is a file outside the uploads folder appearing in the destination.
- Different traversal depths and encodings in the
filecomponent, for example encoded slashes or dot segments, to see whether any normalisation happens beforeretrieve_from_store!. A positive result is the same out-of-store file being copied. - Absolute paths in the
filecomponent, where the uploader accepts them. A positive result is a file from an unrelated folder being copied into the project. - Target files that are readable but not obviously sensitive, to confirm the primitive before aiming at configuration or token files. A positive result is any file content that could not have been uploaded legitimately.
Hunting notes
This class of bug hides in features that take a user-supplied reference from text and turn it into a filesystem lookup: attachment rewriters, importers and exporters, and anything that copies content between containers. During recon, look for Markdown patterns that embed identifiers and file names in URLs, and check whether the file-name portion is constrained. The signal is a regex or parser that validates one part of a path (here the secret) while leaving another part open. To show impact, the researcher included a demonstration video of the file read.
Glossary
- Arbitrary file read: a vulnerability that lets an attacker read any file on a server, not just the ones they are meant to see.
- Bounty: a reward paid by a company to a researcher who reports a valid security bug.
- Capture (in a regex): a named piece of a pattern whose matched text the program can use later.
- Critical: the highest severity rating, meaning the bug can cause serious damage and should be fixed urgently.
- CVSS: a standard scoring system for the severity of security bugs. The report does not give a score.
- Directory traversal: see path traversal.
- GitLab: a website for hosting code and tracking work such as issues and merge requests.
- Hexadecimal: a way of writing numbers using the characters 0 to 9 and a to f.
- Issue: a page in a project where work is described and discussed.
- Markdown: a simple text format for styling text and inserting links and images.
- Namespace: a group of projects in GitLab, which can contain other groups and projects.
- Path traversal: tricking a program into leaving the folder it is supposed to stay in by using
.., which means "parent folder". - Payload: the crafted input an attacker sends to trigger a bug.
- Project: a container in GitLab for a code repository plus its issues and uploaded files.
- Regular expression (regex): a pattern for finding and capturing pieces of text.
- Ruby: the programming language GitLab's main application is written in.
- Secret: in this report, the 32-character hexadecimal value in an upload URL that makes the link hard to guess.
- Severity: a rating of how serious a bug is, such as critical.
- Token: a secret string that proves identity or grants access, similar to a password.
- Upload: a file, such as an image, saved on the server and attached to a project.
- UploadsRewriter: the GitLab component that copies uploads when an issue moves between projects.
- Vulnerability: a weakness in software that an attacker can abuse.