Path traversal in Nuget Package Registry
The NuGet package registry extracted the package name and version from an uploaded nuspec file and used them to build a filename without sanitising path traversal sequences. An attacker could create a file with a .nupkg extension anywhere on the filesystem, and by chaining this with a race condition in Gitaly, read sensitive files on the GitLab instance.
- Triaged in 17 hours
- Fixed in 49 days
- Paid in 17 hours
- Disclosed after 2.2 years
Summary
There's a path traversal issue in Nuget package registry which was released to GitLab-EE recently. The issue allows an attacker to create any file with an extension “.nupkg” in the filesystem. By combining the bug with a race condition in Gitaly which I used several times before (#762421, #732330). It could finally be used to read sensitive files in a GitLab instance.
For some context, a large part of the exploit were explained in #762421, the npm registry issue. Here I will focus on the simple path traversal part which makes a little bit difference.
The root cause of the path traversal lies at ee/app/services/packages/nuget/metadata_extraction_service.rb
XPATHS = {
package_name: '//xmlns:package/xmlns:metadata/xmlns:id',
package_version: '//xmlns:package/xmlns:metadata/xmlns:version'
}.freeze
...
def extract_metadata(file)
doc = Nokogiri::XML(file)
XPATHS.map do |key, query|
[key, doc.xpath(query).text]
end.to_h
It extracts the uploaded nupkg (which is in zip format) for the contained nuspec file (which is an XML). And then looks for attribute id and version. Then the extracted package_name(id), and package_version(version) will be concatenated into a new filename in ee/app/services/packages/nuget/update_package_from_metadata_service.rb
@package_file.transaction do
@package_file.update!(
file_name: package_filename,
file: @package_file.file
)
...
def package_filename
"#{package_name.downcase}.#{package_version.downcase}.nupkg"
end
So my payload is:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
<metadata>
<id>DummyProject.DummyPackage</id>
<version>../../../../../nyangawa</version>
</metadata>
</package>
name the file above as dummy.nuspec and zip it into dummy.nupkg and upload it through PUT /api/v4/projects/#{id}/packages/nuget/ endpoint will make GitLab to create a nyangawa.nupkg somewhere in the filesystem.
Then I wrote a script (I used in #762421) to combine this issue and the race in Gitaly. I could finally read any file I want in my GitLab instance.
Steps to reproduce
- Download the attached exploit.tar.gz and extract it.
- Install some requirements by gem install faraday and gem install rubyzip
- Edit exp.rb to update some url and credentials
- Execute the exp.rb to watch the result of .gitlab_shell_secret of target GitLab instance.
Examples

Results of GitLab environment info
root@localhost:/# gitlab-rake gitlab:env:info
System information
System:
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://10.26.0.5
HTTP Clone URL: http://10.26.0.5/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
ps. I changed my username because of a lost bet, don't be strange :p
Best regards, SaltyYolk
Impact
Common arbitrary file read issue caused by path traversal similar to my previous reports.
Context
GitLab ships a package registry that lets a project host packages in several formats, including NuGet. A client uploads a .nupkg file, which is a zip archive, and GitLab stores it and indexes its metadata so the package can be listed and downloaded later. The NuGet registry was a recent addition to GitLab-EE at the time of this report.
The bug described here is a path traversal in the NuGet metadata handling. It lets an attacker create a file with a .nupkg extension at an arbitrary location in the filesystem. On its own that is limited, but combined with a race condition in Gitaly, which the researcher had used in earlier reports, it becomes an arbitrary file read against the GitLab instance.
Setup
The researcher tested against their own GitLab instance. The environment information reported was:
GitLab information
Version: 12.8.7-ee
Revision: 2643fd87200
Directory: /opt/gitlab/embedded/service/gitlab-rails
DB Adapter: PostgreSQL
DB Version: 10.12
The reproduction steps are:
- Download the attached
exploit.tar.gzand extract it. - Install the requirements with
gem install faradayandgem install rubyzip. - Edit
exp.rbto update the URL and credentials. - Run
exp.rband watch the result of reading.gitlab_shell_secretfrom the target instance.
Finding it
The researcher looked at how the NuGet registry extracts metadata from an uploaded package. The relevant code is in ee/app/services/packages/nuget/metadata_extraction_service.rb:
XPATHS = {
package_name: '//xmlns:package/xmlns:metadata/xmlns:id',
package_version: '//xmlns:package/xmlns:metadata/xmlns:version'
}.freeze
...
def extract_metadata(file)
doc = Nokogiri::XML(file)
XPATHS.map do |key, query|
[key, doc.xpath(query).text]
end.to_h
The service opens the uploaded .nupkg, which is a zip archive, pulls out the contained .nuspec file, which is XML, and reads two values from it: the id and the version. Those two values are then concatenated into a new filename in ee/app/services/packages/nuget/update_package_from_metadata_service.rb:
@package_file.transaction do
@package_file.update!(
file_name: package_filename,
file: @package_file.file
)
...
def package_filename
"#{package_name.downcase}.#{package_version.downcase}.nupkg"
end
The filename is built by concatenating the two metadata fields. Because the version value is attacker controlled and is placed into a path, a value containing ../ sequences escapes the intended directory. The .nupkg suffix is appended after the traversal, so the attacker can only create files ending in .nupkg, but the directory is otherwise unrestricted.
Exploitation
Step 1: Craft the malicious nuspec
The researcher wrote a .nuspec file whose version element contains a traversal sequence:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
<metadata>
<id>DummyProject.DummyPackage</id>
<version>../../../../../nyangawa</version>
</metadata>
</package>
The id is a normal-looking package name, while the version walks up five directories. When GitLab builds the filename it produces ../../../../../nyangawa.nupkg, creating nyangawa.nupkg somewhere in the filesystem.
Step 2: Package and upload
The file is saved as dummy.nuspec, zipped into dummy.nupkg, and uploaded through the NuGet package endpoint:
PUT /api/v4/projects/#{id}/packages/nuget/
GitLab extracts the metadata, concatenates the fields, and creates nyangawa.nupkg somewhere in the filesystem outside the intended location.
Step 3: Combine with the Gitaly race
On its own, writing a .nupkg file is not enough to read anything. The researcher reused a script from an earlier npm registry report that combines the file write with a race condition in Gitaly. That combination allowed reading any file on the GitLab instance. The reproduction steps above run that script and watch it read .gitlab_shell_secret from the target.

Root cause
The metadata extraction service trusts the id and version values from the uploaded .nuspec and passes them straight into a filename. The update service then builds the path with string interpolation:
"#{package_name.downcase}.#{package_version.downcase}.nupkg"
The resulting name is not kept inside the package directory. The .nupkg suffix limits what can be created but does not prevent directory traversal. The write alone is not a file read; the impact comes from pairing it with the Gitaly race, which the researcher had already demonstrated in earlier reports.
Impact
The bug allows an attacker to create a file with a .nupkg extension in the filesystem. Combined with the Gitaly race condition, it becomes an arbitrary file read, similar to the researcher's previous reports. In the demonstrated case the target was .gitlab_shell_secret. The report describes this as a common arbitrary file read issue caused by path traversal.
Fix
The report does not describe the fix that was applied.
Variants to test
Hunting notes
During recon, look for features that accept archives and then rename or relocate the stored file based on values inside the archive. The signal is a filename built by string interpolation from user-controlled fields. To show impact, pair the write with a known primitive such as the Gitaly race, and demonstrate reading a sensitive file rather than only creating one.
What happened, in plain words
GitLab is a website where teams keep their source code and related work. One of its features is a package registry: a place where a project can store ready-made bundles of software, called packages, so that other people or tools can download them later. GitLab can host packages in several formats, and one of those formats is called NuGet, which is the format used by the .NET world.
When someone uploads a NuGet package, GitLab does not just dump the file on disk. It opens the package, reads a small description file inside it, and uses the values from that description to decide what to call the stored file. The bug in this report is that GitLab trusted those values too much. By putting a special sequence of characters into one of them, an attacker could make GitLab write a file outside the folder where packages are supposed to live. That kind of bug is called a path traversal: you trick a program into walking out of its intended folder and into another one, a bit like writing ../../ in a file dialog to climb up from Documents to your home folder.
On its own, writing a file with a .nupkg extension somewhere else is not very useful. But the researcher combined it with a second trick, a race condition in a GitLab component called Gitaly, which they had already used in earlier reports. Together, the two tricks let them read any file on the GitLab server. In the demonstration they read a secret file called .gitlab_shell_secret.
The pieces you need to know
Before walking through the report, here are the ideas it relies on, explained from zero.
- GitLab: a website and server application for hosting code and collaborating on it. You can run your own copy of GitLab on your own server, which is what the researcher did here. The report is about "Your Own GitLab Instance", meaning a GitLab the researcher controlled.
- GitLab-EE: the Enterprise Edition of GitLab, the paid version with extra features. The NuGet package registry was a recent addition to it at the time.
- Package registry: a storage area inside GitLab where packages are kept. Think of it as a shelf in a library: you put a box on the shelf, and later anyone with the right card can take it down.
- NuGet: a package format from the .NET ecosystem. A NuGet package is a single file ending in
.nupkg. .nupkgfile: a zip archive, the same kind of compressed container you get when you zip a folder on your computer. Inside it there is a description file ending in.nuspec..nuspecfile: an XML file that describes the package. XML is a text format made of tags in angle brackets, like<id>...</id>, used to store structured data. The.nuspecholds things like the package's name and version.- API: an interface that lets one program talk to another over the network, without a human clicking buttons. GitLab's API accepts HTTP requests, the same protocol your browser uses, and returns data. The endpoint in this report is a URL that the API listens on.
- HTTP request: the message a browser or program sends to a server.
PUTis one of the HTTP methods; it means "store or replace this thing on the server". - Endpoint: a specific URL path on a server that performs a specific action, for example
/api/v4/projects/.../packages/nuget/. - Compare it to a form that asks for your name and then uses it as a filename: if you type
../../secret, the program may write outside its folder. - Race condition: a bug that appears when two operations happen at almost the same time and the program's assumptions about their order break. Imagine two people grabbing the last seat on a train at the same moment; the outcome depends on who is faster.
- Gitaly: a GitLab component that handles Git repositories on disk. The race condition here is not explained in detail in this report; the researcher refers to earlier reports where it was described.
- Arbitrary file read: the ability to read any file on the server, not just the ones you are supposed to see. That is the serious impact here.
.gitlab_shell_secret: a secret file used by GitLab Shell, a component that handles SSH access to repositories. Reading it is valuable to an attacker because secrets unlock further access.- Ruby and gems: Ruby is a programming language; a gem is a reusable library for Ruby, installed with the
gem installcommand. The exploit script is written in Ruby and needs two gems. - Nokogiri: a Ruby library for parsing XML. The code in the report uses it to read values out of the
.nuspecfile. - String interpolation: putting a variable's value inside a piece of text. In Ruby,
"#{name}.txt"means "take whatever is innameand stick.txtafter it". This is how the dangerous filename is built.
The setup
The researcher tested against their own GitLab instance. The environment information they reported was:
GitLab information
Version: 12.8.7-ee
Revision: 2643fd87200
Directory: /opt/gitlab/embedded/service/gitlab-rails
DB Adapter: PostgreSQL
DB Version: 10.12
This block is the output of a GitLab command that prints details about the installation. It tells us the GitLab version (12.8.7-ee, the Enterprise Edition), the code revision, where the application lives on disk, and which database it uses (PostgreSQL, version 10.12). Knowing the exact version matters because a bug may exist in one release and be fixed in the next.
The steps were: The steps were:
- Download the attached
exploit.tar.gzand extract it. - Install the requirements with
gem install faradayandgem install rubyzip. - Edit
exp.rbto update the URL and credentials. - Run
exp.rband watch the result of reading.gitlab_shell_secretfrom the target instance.
Step by step: the archive contains the exploit script. You then open exp.rb in a text editor and fill in the address of your GitLab and the credentials the script should use. Finally you run the script and watch the result of reading the target's .gitlab_shell_secret.
Finding the bug
The researcher looked at how the NuGet registry extracts metadata from an uploaded package. The relevant code is in ee/app/services/packages/nuget/metadata_extraction_service.rb:
XPATHS = {
package_name: '//xmlns:package/xmlns:metadata/xmlns:id',
package_version: '//xmlns:package/xmlns:metadata/xmlns:version'
}.freeze
...
def extract_metadata(file)
doc = Nokogiri::XML(file)
XPATHS.map do |key, query|
[key, doc.xpath(query).text]
end.to_h
Line by line:
XPATHS = { ... }.freezedefines a fixed table of two lookups. Each entry has a label (package_name,package_version) and an XPath query. XPath is a small language for pointing at parts of an XML document, much like a postal address points at a house.package_name: '//xmlns:package/xmlns:metadata/xmlns:id'says: find the<id>element inside<metadata>inside<package>. Thexmlns:prefix is there because the XML uses a namespace, a way of saying which vocabulary the tags belong to.package_version: '//xmlns:package/xmlns:metadata/xmlns:version'does the same for the<version>element..freezemakes the table unchangeable after it is defined, a Ruby safety habit.def extract_metadata(file)starts a method that takes the uploaded file.doc = Nokogiri::XML(file)loads the file into Nokogiri, the XML parser, producing a document object you can query.XPATHS.map do |key, query|loops over the two entries in the table.[key, doc.xpath(query).text]runs the query against the document and takes the text inside the matching element. The result is a pair: the label and the value found.end.to_hturns the list of pairs into a hash, Ruby's name for a dictionary of key and value pairs.
So this service opens the uploaded .nupkg, which is a zip archive, pulls out the contained .nuspec file, which is XML, and reads two values from it: the id and the version. Nothing here checks what those values contain.
Those two values are then concatenated into a new filename in ee/app/services/packages/nuget/update_package_from_metadata_service.rb:
@package_file.transaction do
@package_file.update!(
file_name: package_filename,
file: @package_file.file
)
...
def package_filename
"#{package_name.downcase}.#{package_version.downcase}.nupkg"
end
Line by line:
@package_file.transaction doopens a database transaction, a block of changes that either all succeed or all fail together, like a bank transfer that must debit one account and credit another as a single unit.@package_file.update!(starts an update of the stored package record. The exclamation mark in Ruby means the method raises an error if something goes wrong.file_name: package_filename,sets the record's filename to whateverpackage_filenamereturns.file: @package_file.filere-attaches the file itself.)andendclose the call and the transaction.def package_filenamedefines the method that builds the name."#{package_name.downcase}.#{package_version.downcase}.nupkg"is the heart of the bug. It takes the package name, lowercases it, adds a dot, takes the version, lowercases it, and adds.nupkg. The#{...}syntax is string interpolation: whatever is inside is pasted into the text.
The filename is built by concatenating the two metadata fields. Sanitisation means cleaning input so that dangerous characters lose their meaning. Because the version value is attacker controlled and is placed into a path, a value containing ../ sequences escapes the intended directory. The .nupkg suffix is appended after the traversal, so the attacker can only create files ending in .nupkg, but the directory is otherwise unrestricted.
Exploitation
Step 1: Craft the malicious nuspec
The researcher wrote a .nuspec file whose version element contains a traversal sequence:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
<metadata>
<id>DummyProject.DummyPackage</id>
<version>../../../../../nyangawa</version>
</metadata>
</package>
Line by line:
<?xml version="1.0" encoding="utf-8"?>is the standard opening line of an XML document, saying which XML version and character encoding are used.<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">opens the root element and declares the namespace, the vocabulary the tags belong to. This is the normal shape of a NuGet description file.<metadata>opens the section that holds the package's details.<id>DummyProject.DummyPackage</id>gives the package a normal-looking name. This is the value that becomes the first part of the filename.<version>../../../../../nyangawa</version>is the malicious part.../means "go up one folder", so five of them climb five levels up from wherever the program currently is.nyangawais the name the attacker wants the file to have.</metadata>and</package>close the sections opened above.
The id is a normal-looking package name, while the version walks up five directories. When GitLab builds the filename it produces ../../../../../nyangawa.nupkg, creating nyangawa.nupkg somewhere in the filesystem. The reason for choosing a harmless-looking id is that the first part of the name is not the interesting one; the traversal lives in the version.
Step 2: Package and upload
The file is saved as dummy.nuspec, zipped into dummy.nupkg, and uploaded through the NuGet package endpoint:
PUT /api/v4/projects/#{id}/packages/nuget/
This is an HTTP request line. PUT is the method, meaning "store this on the server". /api/v4/projects/#{id}/packages/nuget/ is the path: it targets the API (version 4), a specific project (the #{id} placeholder stands for the project's numeric identifier), and the NuGet package area of that project.
GitLab extracts the metadata, concatenates the fields, and creates nyangawa.nupkg somewhere in the filesystem outside the intended location.
Step 3: Combine with the Gitaly race
On its own, writing a .nupkg file is not enough to read anything. The researcher reused a script from an earlier npm registry report that combines the file write with a race condition in Gitaly. That combination allowed reading any file on the GitLab instance. The reproduction steps above run that script and watch it read .gitlab_shell_secret from the target.

Root cause
The metadata extraction service trusts the id and version values from the uploaded .nuspec and passes them straight into a filename. The update service then builds the path with string interpolation:
"#{package_name.downcase}.#{package_version.downcase}.nupkg"
This single line is the whole problem in miniature: two pieces of text that came from the attacker are glued together and used as a file path. The resulting name is not kept inside the package directory. The .nupkg suffix limits what can be created but does not prevent directory traversal. The write alone is not a file read; the impact comes from pairing it with the Gitaly race, which the researcher had already demonstrated in earlier reports.
Impact
The bug allows an attacker to create a file with a .nupkg extension in the filesystem. Combined with the Gitaly race condition, it becomes an arbitrary file read, similar to the researcher's previous reports. In the demonstrated case the target was .gitlab_shell_secret. The report describes this as a common arbitrary file read issue caused by path traversal.
Fix
The report does not describe the fix that was applied.
Variants to test
Hunting notes
During recon, look for features that accept archives and then rename or relocate the stored file based on values inside the archive. The signal is a filename built by string interpolation from user-controlled fields. To show impact, pair the write with a known primitive such as the Gitaly race, and demonstrate reading a sensitive file rather than only creating one.
Glossary
- API: an interface that lets one program talk to another over the network, usually by sending HTTP requests and receiving data back.
- Arbitrary file read: the ability to read any file on a server, not just the ones the user is meant to see.
- Endpoint: a specific URL path on a server that performs a specific action.
- Faraday: a Ruby library for making HTTP requests.
- Gem: a reusable library for the Ruby programming language, installed with
gem install. - Hash: Ruby's name for a dictionary, a collection of key and value pairs.
- HTTP: the protocol browsers and programs use to talk to web servers.
- HTTP method: the verb of a request, such as
GETto fetch orPUTto store. - Interpolation: pasting a variable's value into a piece of text, written
#{...}in Ruby. - Namespace: in XML, a way of saying which vocabulary a set of tags belongs to.
- Nokogiri: a Ruby library for parsing XML.
- NuGet: a package format from the .NET ecosystem; a package is a
.nupkgfile. .nupkg: the file extension of a NuGet package, which is a zip archive..nuspec: the XML description file inside a NuGet package, holding its name and version.- Package registry: a storage area inside GitLab where packages are kept for later download.
- PostgreSQL: a popular open-source database system.
- Recon: the information-gathering phase of security testing, where you look for features worth probing.
- Ruby: a programming language, often used for web applications and scripts.
- Rubyzip: a Ruby library for creating and reading zip files.
- Sanitisation: cleaning user input so that dangerous characters lose their meaning.
- String interpolation: see interpolation.
- Transaction: a group of database changes that either all succeed or all fail together.
- XML: a text format made of tags in angle brackets, used to store structured data.
- XPath: a small language for pointing at parts of an XML document.