Skip to content
XSS Disclosed report Beginner

Stored XSS on PyPi simple API endpoint

The PyPI simple API endpoint built an HTML link using the user-supplied requires_python field without escaping it, so arbitrary HTML and script could be injected into the page. An attacker could run JavaScript in a victim's browser if they visited the crafted endpoint, though the site's CSP blocked it on gitlab.com.

vakzz Follow

Originally disclosed on HackerOne

Bounty$3,000 Medium View original
  • Triaged in 31 hours
  • Fixed in 2 months
  • Paid in 31 hours
  • Disclosed after 5 months

Summary

The recently released PyPi package feature has a new endpoint at /api/:version/projects/:id/packages/pypi/simple/*package_name which exposes an HTML page listing the package versions. The package_link's are generated using the following code:

package_presenter.rb#L50

Ruby
      def package_link(url, required_python, filename)
        "<a href=\"#{url}\" data-requires-python=\"#{required_python}\">#{filename}</a><br>"
      end

The only sanitation on required_python is that it is less than 50 characters (db constraint), otherwise arbitrary html can be injected.

Steps to reproduce

  1. Create project

  2. Create a pypi package with requires_python='"><script>alert(1)</script>'

    Bash

curl -v "https://token:[email protected]/api/v4/projects/18315917/packages/pypi" -F content=@/tmp/lala.txt -F requires_python=2.7 -F version=1 -F name='package_test_1' -F requires_python='"><script>alert(1)</script>' ````

  1. Visit the simple api endpoint and see the injected code: https://gitlab.com/api/v4/projects/18315917/packages/pypi/simple/package_test_1

    HTML
        <!DOCTYPE html>
        <html>
          <head>
            <title>Links for package_test_1</title>
          </head>
          <body>
            <h1>Links for package_test_1</h1>
            <a href="https://gitlab.com/api/v4/projects/18315917/packages/pypi/files/lala.txt#sha256=" data-requires-python=""><script>alert(1)</script>">lala.txt</a><br>
          </body>
        </html>
    

Currently will be blocked by the csp on gitlab.com

Impact

  • An attacker could execute arbitrary javascript by sending a user or getting them to click on a url to the simple api endpoint

Examples

What is the current bug behavior?

The user supplied fields used by the package_presenter are not all sanitized

What is the expected correct behavior?

All of the user supplied fields in package_presenter should be sanitized before being turned into html

Output of checks

This bug happens on GitLab.com

Impact

  • An attacker could execute arbitrary javascript by sending a user or getting them to click on a url to the simple api endpoint

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

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 5 min read.

Context

GitLab's package registry gained support for PyPi packages, and with it a new endpoint that mirrors the PyPi "simple" index. The endpoint lives at /api/:version/projects/:id/packages/pypi/simple/*package_name and returns an HTML page listing the versions of a package, so that pip and similar tools can discover download links.

Each entry in that page is an anchor element built by the package_presenter. The presenter method that assembles the link is at ee/app/presenters/packages/pypi/package_presenter.rb#L50 and looks like this:

Ruby
      def package_link(url, required_python, filename)
        "<a href=\"#{url}\" data-requires-python=\"#{required_python}\">#{filename}</a><br>"
      end

The required_python value is interpolated straight into a double-quoted HTML attribute. The only restriction on it is a database constraint that it be shorter than 50 characters. Nothing escapes or validates its content, so arbitrary HTML can be injected.

Setup

To reproduce, the researcher needed a GitLab project and a personal access token able to publish packages to it. The token is used in the upload URL in the form __token__:$TOKEN. The project used in the report is 18315917, and the package is named package_test_1.

No special role beyond the ability to create a project and push a package is described.

Finding it

The package_link helper interpolates required_python into the markup without escaping. Because the only guard is the 50-character database limit, the field is effectively a raw HTML sink. The report's expected behaviour states that all user-supplied fields used by the presenter should be sanitised before being turned into HTML.

Exploitation

Step 1: Create a project

A project is created to hold the package. The report gives no further detail on this step.

Step 2: Publish a package with a malicious requires_python

The package is uploaded through the PyPi package API, with the requires_python form field set to an attribute-breaking payload:

Bash
curl -v "https://__token__:$[redacted]/api/v4/projects/18315917/packages/pypi" -F content=@/tmp/lala.txt -F requires_python=2.7 -F version=1 -F name='package_test_1' -F requires_python='"><script>alert(1)</script>'

The upload posts a file (content=@/tmp/lala.txt), a version, a package name and a requires_python value. The payload starts with "> to close the data-requires-python attribute and the opening tag, then adds a <script> element. The field appears twice in the command, first as 2.7 and then as the payload.

Step 3: Request the simple index page

The researcher then visits the simple endpoint for the package:

Text
https://gitlab.com/api/v4/projects/18315917/packages/pypi/simple/package_test_1

The response contains the injected markup:

HTML
    <!DOCTYPE html>
    <html>
      <head>
        <title>Links for package_test_1</title>
      </head>
      <body>
        <h1>Links for package_test_1</h1>
        <a href="https://gitlab.com/api/v4/projects/18315917/packages/pypi/files/lala.txt#sha256=" data-requires-python=""><script>alert(1)</script>">lala.txt</a><br>
      </body>
    </html>

The data-requires-python attribute is closed early by the "> in the payload, the <script>alert(1)</script> sits in the document body, and the trailing "> from the original template is left as stray text after the script element. The response is an HTML document containing the injected script element.

At the time of the report, the payload was blocked by the Content Security Policy on gitlab.com. The stored injection was still present in the response, but the CSP prevented the script from executing there.

Root cause

The presenter builds HTML by string interpolation and does not escape the required_python value. The database length constraint is a storage limit, not an output-encoding control, so it does nothing to stop attribute or tag injection. The report frames the bug as one of incomplete sanitisation: not all user-supplied fields used by the package_presenter are sanitised before being turned into HTML.

The report notes that the payload was currently blocked by the CSP on gitlab.com. That is a defence in depth layer, not a fix for the sink, and it does not protect any deployment or context where the policy differs.

Impact

The report states that an attacker could execute arbitrary JavaScript by sending a user, or getting them to click, a URL to the simple API endpoint. The payload is stored with the package and appears in the index page for that package. The report notes the CSP block on gitlab.com as a current limitation on that execution.

Fix

The report's expected behaviour is that all user-supplied fields in package_presenter should be sanitised before being turned into HTML. The report does not describe a specific patch.

Variants to test

  • Other fields interpolated by the same helper. The report's expected behaviour covers all user-supplied fields used by the presenter; try attribute-breaking payloads in other package metadata fields. A positive result is the same early attribute close and a script element in the returned HTML.
  • Other package registry formats that render an index page. Any registry endpoint that emits HTML links from package metadata is a candidate for the same interpolation pattern. A positive result is unescaped metadata appearing inside an attribute or element in the response.
  • Length boundary. The constraint is under 50 characters, so payloads must fit; a short attribute-breaking payload that still produces a script element confirms the sink.
  • CSP behaviour. Where a CSP blocks inline script, check whether the injected markup can still be used for other effects, such as breaking the page structure or injecting elements that the policy permits.

Hunting notes

This kind of bug hides in server-rendered index and listing pages that are assembled by string concatenation rather than a templating engine with auto-escaping. Package registries, feed readers, sitemap and index endpoints, and any API that returns HTML for tooling are good places to look. During recon, watch for endpoints that return text/html from an API path and whose output reflects metadata you control, such as package names, versions and requirement fields.

The signal here was a presenter method that interpolates values into an attribute string with no escaping call, combined with a database constraint that limits length but not content. The researcher demonstrated impact by publishing a package with a payload in requires_python and showing the resulting HTML from the simple endpoint, which made the stored nature of the issue and the exact sink clear even though the CSP blocked execution on gitlab.com.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 12 min read.

What happened, in plain words

GitLab is a platform where teams keep their source code and also store the software packages they build. One of the package formats it supports is PyPi, the packaging system used by the Python programming language. When you install a Python library with a tool called pip, pip asks a server for a simple web page that lists the available versions of that library and where to download them. GitLab added such a page so that pip could fetch packages straight from GitLab.

That page is built by gluing pieces of text together, including a value the package author supplies called requires_python (it says which Python versions the package needs). The code that builds the page dropped that value into the HTML without cleaning it first. So a package author could put HTML of their own into that field, and the page would hand it back to every visitor as if GitLab had written it. That is called stored cross-site scripting, or stored XSS: malicious markup is saved on the server and served to other people later.

On gitlab.com at the time, a browser security feature called Content Security Policy (CSP) stopped the injected script from actually running. The dangerous markup was still in the page, though, and the report treats the missing cleaning as the real bug.

Background you need

What an API is

An API is a way for one program to talk to another over the web. Where a normal web page is made for a human to look at, an API endpoint is a URL that returns data or a document meant for software. Think of a restaurant: the dining room is the website, the kitchen door where waiters hand in orders is the API. You do not walk into the kitchen yourself; you send a request and get something back.

GitLab's API lives under paths that start with /api/, followed by a version number such as v4. The endpoint in this report is:

Text
/api/:version/projects/:id/packages/pypi/simple/*package_name

Read it piece by piece: /api/ marks it as API traffic, :version is the API version (here v4), :id is the numeric identifier of a GitLab project, packages/pypi/simple/ says this is the PyPi simple index, and *package_name is the name of the package you are asking about. The colons and asterisk are placeholders, not literal characters.

What the PyPi simple index is

When pip wants to install something, it does not guess download addresses. It asks a known address, the "simple" index, for a page listing the files available for that package. That page is plain HTML with one link per file. GitLab mirrors this so that pip can install packages hosted on GitLab.

What HTML attributes and escaping are

HTML is the language web pages are written in. A link looks like this: <a href="...">text</a>. The part inside the angle brackets is a tag, href is an attribute (the address the link points to), and the text between the tags is what the visitor sees. Attributes can carry extra information, such as data-requires-python="2.7".

When a program inserts a value into HTML, it must "escape" it: turn characters that have special meaning in HTML, such as <, > and ", into harmless codes. If it does not, a value containing a double quote can end the attribute early, and a value containing <script> can start a script. That is the whole trick behind this bug.

What a token is

A token (here a personal access token) is a long secret string that acts like a password for a program. Instead of typing a username and password, a tool sends the token and the server recognises it. Because it grants access to your account, it must be kept secret. In the commands below it appears as $TOKEN, a placeholder for the real value, which is not reproduced here.

What stored XSS means

Cross-site scripting means getting your own code (usually JavaScript, the language browsers run) into a page that other people view, so that it runs in their browser as if it came from the site. "Stored" means the malicious content is saved on the server first, for example inside a package's details, and then served to every visitor. The everyday comparison: writing graffiti on a sign in a shop rather than shouting it in the street. The shop keeps showing it to everyone who walks in.

The vulnerable code

The page is assembled by a piece of Ruby code called a presenter. Ruby is a programming language, and a presenter is a small class whose job is to prepare data for display. The method that builds each link sits at ee/app/presenters/packages/pypi/package_presenter.rb#L50 and reads:

Ruby
      def package_link(url, required_python, filename)
        "<a href=\"#{url}\" data-requires-python=\"#{required_python}\">#{filename}</a><br>"
      end

Line by line:

  1. def package_link(url, required_python, filename) declares a method named package_link that takes three inputs: the download address, the Python requirement text, and the file name.
  2. The next line is one long piece of text (a string) that is returned. Inside it, #{url}, #{required_python} and #{filename} are placeholders: Ruby swaps each one for the matching input value at the moment the string is built.
  3. The surrounding text is the HTML for a link: <a href=" opens the link tag and its address attribute, " data-requires-python=" closes the address and opens the requirement attribute, "> closes that attribute and the opening tag, #{filename} becomes the clickable text, and </a><br> closes the link and adds a line break.

The problem is that the inputs are pasted in as-is. The only limit on required_python is a rule in the database that it must be shorter than 50 characters. A length limit controls how much you can store, not what the characters mean, so it does nothing to stop someone putting HTML in there. The report's expected behaviour is that every user-supplied field used by the presenter should be cleaned before it becomes HTML.

Reproducing it

Step 1: Create a project

A GitLab project is a container for code and packages, like a folder with its own settings and members. The researcher created one to hold the test package. The report gives no further detail on this step.

Step 2: Publish a package with a malicious requires_python

To publish, the researcher used curl, a command-line tool that sends web requests. The upload goes to the PyPi package API of the project, with the token used as the username in the address, in the form __token__:$TOKEN. The token is used as the username in the address, in the form __token__:$TOKEN. The project used in the report is 18315917 and the package is named package_test_1.

Bash
curl -v "https://__token__:$[redacted]/api/v4/projects/18315917/packages/pypi" -F content=@/tmp/lala.txt -F requires_python=2.7 -F version=1 -F name='package_test_1' -F requires_python='"><script>alert(1)</script>'`

Line by line:

  1. curl -v runs curl with verbose output, so the researcher can see the request and response details.
  2. "https://__token__:$[redacted]/api/v4/projects/18315917/packages/pypi" is the address being called. The __token__:$TOKEN@ part before the host name is how curl sends a username and password; here the username is the literal word __token__ and the password is the token. The path points at the package upload endpoint of project 18315917.
  3. -F content=@/tmp/lala.txt attaches a file from the local disk as the package content. -F means "send this as a form field".
  4. -F requires_python=2.7 sets the Python requirement field to 2.7.
  5. -F version=1 sets the package version to 1.
  6. -F name='package_test_1' sets the package name.
  7. -F requires_python='"><script>alert(1)</script>' sets the same field a second time, now to the payload. The payload starts with ">, which is a double quote followed by a greater-than sign. In HTML, that pair closes the current attribute and the opening tag. Then comes <script>alert(1)</script>, a script element that would pop up an alert box if it ran. The field appears twice in the command, first as 2.7 and then as the payload.

Why this step matters: the researcher is not attacking the upload itself. They are planting text in a field that will later be pasted into a web page, to see whether the page cleans it.

Step 3: Request the simple index page

The researcher then opened the simple endpoint for the package in a browser:

Text
https://gitlab.com/api/v4/projects/18315917/packages/pypi/simple/package_test_1

This is the page pip would fetch. The response contained the injected markup:

HTML
    <!DOCTYPE html>
    <html>
      <head>
        <title>Links for package_test_1</title>
      </head>
      <body>
        <h1>Links for package_test_1</h1>
        <a href="https://gitlab.com/api/v4/projects/18315917/packages/pypi/files/lala.txt#sha256=" data-requires-python=""><script>alert(1)</script>">lala.txt</a><br>
      </body>
    </html>

Line by line:

  1. <!DOCTYPE html> tells the browser this is an HTML document.
  2. <html>, <head>, <title>Links for package_test_1</title> and </head> set up the document and its tab title.
  3. <body> starts the visible part, and <h1>Links for package_test_1</h1> is the page heading.
  4. The long <a ...> line is the interesting one. href="https://gitlab.com/api/v4/projects/18315917/packages/pypi/files/lala.txt#sha256=" is the download address. Then data-requires-python=" opens the requirement attribute, and the payload's "> closes it immediately, so the attribute ends up empty. Next, <script>alert(1)</script> sits directly in the page body as a real script element. Finally the template's own closing "> is left over as stray text, followed by lala.txt</a><br>.
  5. </body> and </html> close the document.

The response is an HTML document containing the injected <script> element. At the time of the report, the CSP on gitlab.com blocked it from running. CSP is a set of rules a site sends with a page telling the browser which sources of script are allowed; it is a safety net, not a repair. The stored injection was still in the response.

Why it happened

The presenter builds HTML by pasting strings together and never escapes required_python. The database rule that the field be under 50 characters is a storage limit, not an output-encoding control, so it cannot stop attribute or tag injection. The report frames the bug as incomplete sanitisation: not all user-supplied fields used by the package_presenter are cleaned before being turned into HTML.

The report notes that the payload was currently blocked by the CSP on gitlab.com. That is a defence in depth layer, not a fix for the sink, and it does not protect any deployment or context where the policy differs.

Impact

The report states that an attacker could execute arbitrary JavaScript by sending a user, or getting them to click, a URL to the simple API endpoint. The payload is stored with the package and appears in the index page for that package. The report notes the CSP block on gitlab.com as a current limitation on that execution.

Fix

The report's expected behaviour is that all user-supplied fields in package_presenter should be sanitised before being turned into HTML. The report does not describe a specific patch.

Variants to test

  • Other fields interpolated by the same helper. The report's expected behaviour covers all user-supplied fields used by the presenter; try attribute-breaking payloads in other package metadata fields. A positive result is the same early attribute close and a script element in the returned HTML.
  • Other package registry formats that render an index page. Any registry endpoint that emits HTML links from package metadata is a candidate for the same interpolation pattern. A positive result is unescaped metadata appearing inside an attribute or element in the response.
  • Length boundary. The constraint is under 50 characters, so payloads must fit; a short attribute-breaking payload that still produces a script element confirms the sink.
  • CSP behaviour. Where a CSP blocks inline script, check whether the injected markup can still be used for other effects, such as breaking the page structure or injecting elements that the policy permits.

Hunting notes

This kind of bug hides in server-rendered index and listing pages that are assembled by string concatenation rather than a templating engine with auto-escaping. Package registries, feed readers, sitemap and index endpoints, and any API that returns HTML for tooling are good places to look. During recon, watch for endpoints that return text/html from an API path and whose output reflects metadata you control, such as package names, versions and requirement fields.

The signal here was a presenter method that interpolates values into an attribute string with no escaping call, combined with a database constraint that limits length but not content. The researcher demonstrated impact by publishing a package with a payload in requires_python and showing the resulting HTML from the simple endpoint, which made the stored nature of the issue and the exact sink clear even though the CSP blocked execution on gitlab.com.

Glossary

  • API: a way for one program to talk to another over the web, usually at a URL under /api/ that returns data rather than a page for humans.
  • Attribute (HTML): extra information inside a tag, such as href or data-requires-python, written as name="value".
  • Content Security Policy (CSP): rules a site sends with a page telling the browser which scripts are allowed to run; a safety net against injected code.
  • Cross-site scripting (XSS): getting your own code into a page other people view, so it runs in their browser as if the site had written it.
  • curl: a command-line tool for sending web requests.
  • Database constraint: a rule the database enforces on stored data, such as a maximum length; it controls storage, not meaning.
  • Endpoint: a specific URL that a program can send a request to.
  • Escaping: converting characters that have special meaning in HTML (like <, > and ") into harmless codes before inserting a value into a page.
  • Form field: one named value sent with a request, here supplied with curl's -F option.
  • HTML: the language web pages are written in, made of tags, attributes and text.
  • JavaScript: the programming language browsers run; injected scripts are usually JavaScript.
  • pip: the standard tool for installing Python packages.
  • Presenter: a small class in a program whose job is to prepare data for display.
  • PyPi: the packaging system for Python, including the "simple" index that pip reads to find downloads.
  • Ruby: the programming language GitLab is written in.
  • Sanitisation: cleaning user-supplied data so it cannot be interpreted as code or markup.
  • Simple index: the HTML page a package server offers so pip can discover download links for a package.
  • Sink: the place in code where untrusted data ends up being interpreted, here the HTML string built by the presenter.
  • Stored XSS: XSS where the malicious content is saved on the server and served to every visitor.
  • Token (personal access token): a long secret string that acts like a password for a program.
  • requires_python: a package field stating which Python versions the package needs; here the field that carried the payload.

More by vakzz

View all

24 more disclosed reports, best first.

More from GitLab

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Cross-site scripting (XSS)

The lesson behind this report.

Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.