Skip to content
Path traversal Disclosed report Intermediate

Path traversal in NuGet package upload

The NuGet package upload feature failed to validate the package name from the .nuspec file, allowing path traversal. An attacker could overwrite another user's NuGet package file, potentially backdooring it.

GitLab

Originally disclosed on GitLab

  • Fixed in under an hour
  • Disclosed after under an hour

HackerOne report #972492 by taraszelyk on 2020-09-01, assigned to @rchan-gitlab:

Report | Attachments | How To Reproduce

Report

Summary

The vulnerability allows an attacker to overwrite NuGet package of another user. The issue exists in update_package_from_metadata_service.rb which fails to check the filename of the for path traversal.

update_package_from_metadata_service.rb

def execute  
...  
    # Updating file_name updates the path where the file is stored.  
    # We must pass the file again so that CarrierWave can handle the update  
    [@]package_file.update!(  
        file_name: package_filename,  
        file: [@]package_file.file  
    )  
...  
def package_filename  
    "#{package_name.downcase}.#{package_version.downcase}.nupkg"  
end

Here package_name are fetched package_version from *.nuspec file from <id> and <version> values. Then they are concatenated and extension is appended to the resulting filename that is passed to update! function to update a path to the package.

package_name is not properly checked which allows us to overwrite any file with path traversal. The only restriction here is that we are limited to .nupkg files, so the attack surface is lower, but it still can be used to do harmful things.

To overwrite NuGet package file from the victim repository we need to know the full path to the files.
Assuming that packages are stored in var/opt/gitlab/gitlab-rails/shared/packages/ by default, it's pretty easy to build a full path. We need to know the following things:
packages_folder
victim_project_id
package_id
package_file_id

Steps to reproduce

  1. Create two accounts for attacker and for victim.
  2. As a victim, create a new repository and upload a valid NuGet package to the registry.
  3. As an attacker, gather project_id, package_id, and package_file_id.
  • project_id can be found in the repository main page.
  • package_id can be found in URL after navigating to the package page.
  • package_file_id can be found after clicking on the package file and inspecting the URL. For example, URL will look like this: http://gitlab.local/root/nuget/-/package_files/87/download, so 87 is package_file_id
  1. As an attacker, prepare a malicious package with a backdoor(it can be any, doesn't matter here).

Unzip .nupkg file and edit <package_name>.nuspec file and update id value to the following:
../../../../../../../../../../../../../../../../../../../../../../../../ + &lt;default_package_dir&gt; + / + 1,2 characters of sha256(project_id)+ / + 3,4 characters of sha256(project_id) + / + sha256(project_id) + /packages/ + package_id + /files/ + package_file_id + &lt;name of the package without version and extension&gt;
Also, you will need to update version to match the version in package you want to rewrite.

For example,

project_id = 51  
package_id = 74  
package_file_id = 87  
name of the package = asdf.0.20.0.nupkg  
package directory = /var/opt/gitlab/gitlab-rails/shared/packages/  

Example of crafted *.nuspec file:

&lt;?xml version=&#34;1.0&#34; encoding=&#34;utf-8&#34;?&gt;  
&lt;package xmlns=&#34;http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd&#34;&gt;  
  &lt;metadata&gt;  
    &lt;id&gt;../../../../../../../../../../../../../../../../../../../../../../../../var/opt/gitlab/gitlab-rails/shared/packages/03/1b/031b4af5197ec30a926f48cf40e11a7dbc470048a21e4003b7a3c07c5dab1baa/packages/74/files/87/asdf&lt;/id&gt;  
    &lt;version&gt;0.20.0&lt;/version&gt;  
     ...  
  &lt;/metadata&gt;  
&lt;/package&gt;  

This will overwrite file on /var/opt/gitlab/gitlab-rails/shared/packages/03/1b/031b4af5197ec30a926f48cf40e11a7dbc470048a21e4003b7a3c07c5dab1baa/packages/74/files/87/asdf.0.20.0.nupkg

  1. Zip the package back and upload it.
gitlab_nuget_path_traversal.mp4 From the original on GitLab

What is the current bug behavior?

The application allows an attacker to overwrite NuGet packages of other users via path traversal.

What is the expected correct behavior?

Application checks package_filename for path traversal before updating the path to the package.

Output of checks

This was tested on local Gitlab 13.3.1 instance.
I can try to reproduce it on gitlab.com if needed.

Results of GitLab environment info
$ gitlab-rake gitlab:env:info

System information  
System:       
Current User:   git  
Using RVM:  no  
Ruby Version:   2.6.6p146  
Gem Version:    2.7.10  
Bundler Version:1.17.3  
Rake Version:   12.3.3  
Redis Version:  5.0.9  
Git Version:    2.28.0  
Sidekiq Version:5.2.9  
Go Version: unknown

GitLab information  
Version:    13.3.1  
Revision:   d54b96a3079  
Directory:  /opt/gitlab/embedded/service/gitlab-rails  
DB Adapter: PostgreSQL  
DB Version: 11.7  
URL:        http://gitlab.local  
HTTP Clone URL: http://gitlab.local/some-group/some-project.git  
SSH Clone URL:  [email protected]:some-group/some-project.git  
Using LDAP: no  
Using Omniauth: yes  
Omniauth Providers: 

GitLab Shell  
Version:    13.6.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

This vulnerability can be used to overwrite NuGet packages of other users which can be used to backdoor valid packages and infect any user that uses it.

Also, it can be used to store .nupkg files on random places of the system. This potentially can lead to some serious issues.

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

The application concatenated the user-controlled package name and version from the .nuspec file into a file path without sanitising for path traversal sequences.

Impact

An attacker could overwrite another user's NuGet package, potentially backdooring it and infecting users who download it; the attack is limited to .nupkg files.

What the attacker needed

Attacker and victim accounts; victim uploads a NuGet package; attacker knows victim's project_id, package_id…

The fix

The application should check the package filename for path traversal before updating the package path.

How it was found

  1. Create two accounts, one for the attacker and one for the victim.
  2. As the victim, create a repository and upload a valid NuGet package.
  3. As the attacker, gather the project_id, package_id, and package_file_id of the victim's package.
  4. Prepare a malicious package by editing the .nuspec file to set the id to a path traversal string that points to the victim's package file, and set the version to match.
  5. Zip the package back and upload it as the attacker.

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.

When you upload a NuGet package, the system reads the package name from inside the file and uses it to decide where to save the package. Because it didn't check that the name was safe, an attacker could put special characters like '../' in the name to make the system save the file in a different folder, overwriting someone else's package. This is called path traversal.

What is Path traversal?

Input that builds a file path escapes the intended folder, reading or writing files the application should never touch.

More from GitLab

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Path traversal and file inclusion

The lesson behind this report.

Input that builds a file path escapes the intended folder, reading or writing files the application should never touch.