Gitlab Pages token theft using service workers
GitLab Pages session cookies were not bound to the subdomain they were issued for, so a cookie obtained on one Pages site could be replayed against another. An attacker could use a service worker on their own Pages site to intercept the OAuth2 callback and steal a victim's Pages session cookie, then use it to read private Pages sites the victim can access.
- Triaged in 17 days
- Fixed in 3 months
- Paid in 17 days
- Disclosed after 5 months
Summary
It is possible to steal Gitlab Pages session tokens by intercepting requests to the /auth endpoint on a Pages site using service workers.
Attack Flow
Setup
- The attacker creates a private Gitlab Pages site at the root of their user page
attacker.gitlab.io, ensuring that the project isprivate to collaboratorsfor Gitlab Pages visibility. - The attacker invites the victim to collaborate to the project, thus giving them access to the private Pages site.
- The victim clicks on a link to
attacker.gitlab.io, and a service worker is registered for the domain, passing through all requests back to the server except the/authendpoint, which it intercepts.
Bypassing the CSRF protection (OAuth2 state param)
4. A script on the website requests a valid session cookie and redirection URL from an attacker controlled website.
5. The attacker controlled website sends its own request to attacker.gitlab.io, and retrieves a redirection URL and a session cookie.
6. A script on the website redirects the victim to the redirection URL retrieved by the attacker, and the user authenticates (since the user is already logged in, this does not require any kind of interaction)
Intercepting the OAuth2 Response
7. The OAuth2 service redirects the user back to attacker.gitlab.io/auth with the code and state, which is intercepted by the attacker's service worker and sent to the attacker controlled website.
8. The attacker controlled website sends a request to the auth endpoint with the original session cookie and the intercepted code and state to obtain an authenticated session cookie.
9. The attacker can use this cookie to access other GitLab Pages sites that the victim has access to.
Steps to reproduce
- Create an attacker account and victim account. The victim account should have access to a private GitLab Pages site that the attacker does not have access to.
- Start the attacker controlled server (ensure that it is on a remote server with HTTPS)
- Import the Proof of Concept repository into the attacker's account, rename it to
{attacker username}.gitlab.ioand change the attacker server to the server deployed in step 2. - Invite the victim to collaborate on the
{attacker username}.gitlab.ioproject. - While logged into the victim account, visit
{attacker username}.gitlab.io. - The attacker controlled server will output a valid
gitlab-pagessession cookie, which can be inserted into a browser or curl command (curl -v --cookie '{cookie}' https://{victim username}.gitlab.io/{private Gitlab Pages project}) to gain access to the private Gitlab Pages project on the victim's account.
Examples
Proof of Concept:
Pages Project export: F1565711
Pages Project on gitlab.com: https://gitlab.com/dotchinavideo/dotchinavideo.gitlab.io
Attacker controlled server: F1565227 (requires flask and flask-cors installed, outputs the stolen token to stdout)
What is the current bug behavior?
The current behavior is that gitlab-pages session cookies are not properly validated to ensure that the requested subdomain is the same as the subdomain to which the session cookie was issued for.
What is the expected correct behavior?
The expected behavior is for each gitlab-pages session cookie be validated to prevent cross-subdomain usage.
Relevant logs and/or screenshots
F1565251
Output of checks
This bug happens on GitLab.com
Results of GitLab environment info
This bug happens on GitLab.com
Impact
Once an attacker has a valid Pages session token, they are able to access every private Gitlab Pages site that the victim has access to.
Attachments
Context
GitLab Pages serves static sites for projects and user or group namespaces. A site published at the root of a user page lives under {username}.gitlab.io. Private Pages sites are protected by a gitlab-pages session cookie: when a visitor without access hits a private site, Pages sends them through an OAuth2 flow against GitLab, and on return to the /auth endpoint it issues a session cookie that lets them view the site.
The report targets the Pages component (https://gitlab.com/gitlab-org/gitlab-pages) on GitLab.com. The weakness is classified as improper authorization, with a CVSS score of 5.3 (medium) and a bounty of 1,680 USD. It was disclosed on 8 June 2022.
Setup
Two accounts are needed: an attacker account and a victim account. The victim must have access to a private GitLab Pages site that the attacker cannot access. The attacker also needs a server they control, reachable over HTTPS on a remote host, running the proof of concept code (it requires flask and flask-cors and prints the stolen token to stdout).
The attacker's Pages project must be published at the root of their user page, attacker.gitlab.io, with Pages visibility set to private to collaborators. The victim is then invited to collaborate on that project, which is what gives them access to the private Pages site and makes the attack reachable.
Finding it
The report states that gitlab-pages session cookies are not validated against the subdomain they were issued for. A cookie minted for one Pages subdomain is therefore accepted on another. Because a Pages site can register a service worker for its own origin, an attacker who controls a Pages site can intercept requests made to that origin, including the OAuth2 callback to /auth, and take the authorization code and state before the site's own logic sees them.
Exploitation
Step 1: publish a private Pages site
The attacker creates a private GitLab Pages site at the root of their user page, attacker.gitlab.io, with Pages visibility set to private to collaborators.
Step 2: invite the victim
The attacker invites the victim to collaborate on the project, giving the victim access to the private Pages site.
Step 3: register the service worker
The victim clicks a link to attacker.gitlab.io. A service worker is registered for the domain. It passes all requests through to the server except the /auth endpoint, which it intercepts.
Step 4: request a session cookie and redirect URL
A script on the attacker's Pages site requests a valid session cookie and redirection URL from the attacker-controlled server.
Step 5: obtain the cookie and redirect URL
The attacker-controlled server sends its own request to attacker.gitlab.io and retrieves a redirection URL and a session cookie. The attacker-controlled server retrieves a redirection URL and a session cookie, which is how the OAuth2 state parameter is bypassed.
Step 6: redirect the victim into the OAuth2 flow
A script on the site redirects the victim to the redirection URL retrieved by the attacker. The victim authenticates. Since the victim is already logged in, this requires no interaction from them.
Step 7: intercept the OAuth2 response
The OAuth2 service redirects the victim back to attacker.gitlab.io/auth with the code and state. The attacker's service worker intercepts this request and sends it to the attacker-controlled server.
Step 8: exchange the code for an authenticated cookie
The attacker-controlled server sends a request to the auth endpoint with the original session cookie and the intercepted code and state, and obtains an authenticated session cookie.
Step 9: use the cookie elsewhere
The attacker can use this cookie to access other GitLab Pages sites that the victim has access to.
Reproduction
- Create an attacker account and a victim account. The victim account should have access to a private GitLab Pages site that the attacker does not have access to.
- Start the attacker-controlled server, ensuring it is on a remote server with HTTPS.
- Import the proof of concept repository into the attacker's account, rename it to
{attacker username}.gitlab.ioand change the attacker server to the server deployed in step 2. - Invite the victim to collaborate on the
{attacker username}.gitlab.ioproject. - While logged into the victim account, visit
{attacker username}.gitlab.io. - The attacker-controlled server outputs a valid
gitlab-pagessession cookie. Insert it into a browser or a curl command to reach the private project on the victim's account:
curl -v --cookie '{cookie}' https://{victim username}.gitlab.io/{private Gitlab Pages project}
The --cookie flag supplies the stolen gitlab-pages session cookie, and the URL points at the victim's private Pages project.
Proof of concept artefacts
The report references a Pages project export (F1565711), the live Pages project at https://gitlab.com/dotchinavideo/dotchinavideo.gitlab.io, and the attacker-controlled server (F1565227). The server requires flask and flask-cors and outputs the stolen token to stdout. A log or screenshot is referenced as F1565251.
Root cause
gitlab-pages session cookies are not properly validated to ensure that the requested subdomain is the same as the subdomain to which the session cookie was issued for. The expected behaviour is for each gitlab-pages session cookie to be validated to prevent cross-subdomain usage.
The report attributes the issue to gitlab-pages session cookies not being validated against the subdomain they were issued for.
Impact
Once an attacker has a valid Pages session token, they can access every private GitLab Pages site that the victim has access to. The attack requires the victim to be invited as a collaborator on the attacker's Pages project and to visit the attacker's Pages site while logged in; because the victim is already logged in, authenticating requires no interaction.
Fix
GitLab should validate each gitlab-pages session cookie so that it cannot be used across subdomains, binding the cookie to the subdomain it was issued for. The report states the expected behaviour but does not describe the change that was made.
Variants to test
- Other Pages-style hosting features that issue per-site session cookies: check whether a cookie issued for one subdomain is accepted on another by replaying it against a second private site.
- OAuth2 flows where the
statevalue and the session cookie can both be obtained by a server-side request: if the callback accepts a state that was never bound to the victim's browser session, the flow is replayable. - Service worker registration on attacker-controlled origins that share a parent domain with the target: look for any endpoint on that origin that carries credentials or authorization codes.
- Private static site hosting with collaborator-based access: test whether inviting a victim to a private site is enough to get their browser to run attacker JavaScript on a trusted-looking origin.
Hunting notes
This class of bug tends to hide in multi-tenant static hosting and in OAuth2 callback endpoints that issue their own session cookies. During recon, look for per-site cookies with names tied to the hosting product, private site visibility modes that rely on collaborator lists, and callback paths such as /auth that receive codes and state. A positive signal is a cookie that works on a subdomain other than the one that set it.
What happened, in plain words
GitLab is a website where people store and share code. One of its features, GitLab Pages, lets a project publish a small website. If your username is alice, a site published at the root of your user page appears at alice.gitlab.io. Pages sites can be public, or they can be private: only people you invite as collaborators can open them.
To keep a private Pages site private, GitLab uses a session cookie. A cookie is a small piece of text that a website asks your browser to store and send back on every later visit, so the site can recognise you. Think of it like a cloakroom ticket: you hand it in, and the attendant knows which coat is yours. The cookie GitLab Pages issues is called a gitlab-pages session cookie.
When someone without access opens a private Pages site, Pages sends them through a login round trip with GitLab itself. This round trip is called OAuth2. At the end of it, the browser comes back to a path called /auth, and Pages hands out the session cookie that lets the visitor see the site.
The researcher found that this cookie is not checked against the address it was issued for. A cookie handed out for one Pages site is accepted on a different Pages site. That is the improper authorization weakness: the system does not verify that the person presenting the ticket is entitled to the coat they are asking for.
The report was filed on GitLab's bug bounty programme on HackerOne, rated medium with a CVSS score of 5.3, paid a bounty of 1,680 USD, and disclosed on 8 June 2022. The affected component is https://gitlab.com/gitlab-org/gitlab-pages.
The pieces you need to know
Before the attack makes sense, a few everyday ideas need explaining.
- A server is a computer somewhere on the internet that answers requests from browsers. The attacker in this report runs their own server, reachable over HTTPS (the padlock version of the web), because the attack needs a place to collect stolen data.
- A service worker is a small program that a website can install into your browser. Once installed, it sits between your browser and that website and can look at, change, or block every request your browser makes to that site. It is like a receptionist who opens all your post before passing it on, and can quietly copy one letter.
- The
stateparameter is a random value that OAuth2 uses to tie a login round trip to the browser that started it. It is like a numbered claim ticket: the number you come back with must match the number the desk wrote down, otherwise the desk refuses to hand over the coat. This is a defence against CSRF (cross-site request forgery), where an attacker tricks your browser into completing an action you did not intend. - An authorization code is a short-lived value that the login service gives back at the end of the round trip. The site exchanges it for a session cookie. Think of it as a voucher you swap for the actual cloakroom ticket.
curlis a command-line tool for making web requests. It is used at the end of the report to prove the stolen cookie works.
Setting up the attack
The attacker needs two accounts: their own, and a victim's. The victim must have access to a private GitLab Pages site that the attacker cannot access, because that private site is the thing the attacker wants to reach.
The attacker also needs a server they control, on a remote host, reachable over HTTPS. The proof of concept code that runs there needs the Python packages flask and flask-cors, and it prints the stolen token to the screen (stdout).
The attacker's own Pages project is published at the root of their user page, attacker.gitlab.io, with Pages visibility set to private to collaborators. Then the attacker invites the victim to collaborate on that project. That invitation is the hook: it gives the victim access to the attacker's private Pages site, so the victim's browser will happily load it and run its code.
Finding the flaw
The researcher noticed that gitlab-pages session cookies are not validated against the subdomain they were issued for. A cookie minted for one Pages subdomain is therefore accepted on another.
Because a Pages site can register a service worker for its own origin (its own address on the web), an attacker who controls a Pages site can intercept requests made to that origin. That includes the OAuth2 callback to /auth. The service worker can take the authorization code and the state before the site's own logic ever sees them.
Walking through the attack
Step 1: publish a private Pages site
The attacker creates a private GitLab Pages site at the root of their user page, attacker.gitlab.io, with Pages visibility set to private to collaborators.
Step 2: invite the victim
The attacker invites the victim to collaborate on the project. This gives the victim access to the private Pages site, which is what makes the next step possible.
Step 3: register the service worker
The victim clicks a link to attacker.gitlab.io. A service worker is registered for the domain. It passes all requests through to the server except the /auth endpoint, which it intercepts. In plain terms: the receptionist forwards all the post as normal, but keeps any letter addressed to /auth and copies it to the attacker.
Step 4: request a session cookie and redirect URL
A script on the attacker's Pages site asks the attacker-controlled server for a valid session cookie and a redirection URL. The attacker's server is the one place the attacker fully controls, so it is where the values are prepared.
Step 5: obtain the cookie and redirect URL
The attacker-controlled server sends its own request to attacker.gitlab.io and retrieves a redirection URL and a session cookie. This is how the OAuth2 state parameter is bypassed: the attacker-controlled server retrieves a redirection URL and a session cookie.
Step 6: redirect the victim into the OAuth2 flow
A script on the site redirects the victim to the redirection URL retrieved by the attacker. The victim authenticates. Since the victim is already logged in to GitLab, this requires no interaction from them: the browser simply follows the redirect and the login round trip completes on its own.
Step 7: intercept the OAuth2 response
The OAuth2 service redirects the victim back to attacker.gitlab.io/auth with the code and state. The attacker's service worker intercepts this request and sends it to the attacker-controlled server.
Step 8: exchange the code for an authenticated cookie
The attacker-controlled server sends a request to the auth endpoint with the original session cookie and the intercepted code and state, and obtains an authenticated session cookie. The voucher is swapped for the real cloakroom ticket, and the ticket is now in the attacker's hands.
Step 9: use the cookie elsewhere
The attacker can use this cookie to access other GitLab Pages sites that the victim has access to. This is the payoff of the missing subdomain check: the ticket works at every cloakroom in the building, not just the one that issued it.
Reproducing it
- Create an attacker account and a victim account. The victim account should have access to a private GitLab Pages site that the attacker does not have access to.
- Start the attacker-controlled server, ensuring it is on a remote server with HTTPS.
- Import the proof of concept repository into the attacker's account, rename it to
{attacker username}.gitlab.ioand change the attacker server to the server deployed in step 2. - Invite the victim to collaborate on the
{attacker username}.gitlab.ioproject. - While logged into the victim account, visit
{attacker username}.gitlab.io. - The attacker-controlled server outputs a valid
gitlab-pagessession cookie. Insert it into a browser or a curl command to reach the private project on the victim's account:
curl -v --cookie '{cookie}' https://{victim username}.gitlab.io/{private Gitlab Pages project}
Line by line: curl is the command-line web client. -v means verbose, so curl prints the details of the request and response, which is useful as evidence. --cookie '{cookie}' supplies the stolen gitlab-pages session cookie in place of a browser. The URL points at the victim's private Pages project.
Proof of concept artefacts
The report references a Pages project export (F1565711), the live Pages project at https://gitlab.com/dotchinavideo/dotchinavideo.gitlab.io, and the attacker-controlled server (F1565227). The server requires flask and flask-cors and outputs the stolen token to stdout. A log or screenshot is referenced as F1565251.
Why it works
gitlab-pages session cookies are not properly validated to ensure that the requested subdomain is the same as the subdomain to which the session cookie was issued for. The expected behaviour is for each gitlab-pages session cookie to be validated to prevent cross-subdomain usage.
The report attributes the issue to gitlab-pages session cookies not being validated against the subdomain they were issued for.
What an attacker gains
Once an attacker has a valid Pages session token, they can access every private GitLab Pages site that the victim has access to. The attack requires the victim to be invited as a collaborator on the attacker's Pages project and to visit the attacker's Pages site while logged in. Because the victim is already logged in, authenticating requires no interaction.
The fix
GitLab should validate each gitlab-pages session cookie so that it cannot be used across subdomains, binding the cookie to the subdomain it was issued for. The report states the expected behaviour but does not describe the change that was made.
Similar things to test
- Other Pages-style hosting features that issue per-site session cookies: check whether a cookie issued for one subdomain is accepted on another by replaying it against a second private site.
- OAuth2 flows where the
statevalue and the session cookie can both be obtained by a server-side request: if the callback accepts a state that was never bound to the victim's browser session, the flow is replayable. - Service worker registration on attacker-controlled origins that share a parent domain with the target: look for any endpoint on that origin that carries credentials or authorization codes.
- Private static site hosting with collaborator-based access: test whether inviting a victim to a private site is enough to get their browser to run attacker JavaScript on a trusted-looking origin.
Where to look when hunting
This class of bug tends to hide in multi-tenant static hosting and in OAuth2 callback endpoints that issue their own session cookies. During recon, look for per-site cookies with names tied to the hosting product, private site visibility modes that rely on collaborator lists, and callback paths such as /auth that receive codes and state. A positive signal is a cookie that works on a subdomain other than the one that set it.
Glossary
- API: a defined way for one piece of software to ask another for data or actions, like a service counter with a fixed list of things you can request.
- Authorization code: a short-lived value returned at the end of an OAuth2 login round trip, exchanged for a session cookie.
- Cookie: a small piece of text a website asks your browser to store and send back on later visits, so the site can recognise you.
- CSRF (cross-site request forgery): tricking a logged-in user's browser into sending a request the user did not intend.
- CSS: the language that controls how web pages look.
- curl: a command-line tool for making web requests.
- CVSS: a standard scoring system for the severity of a security flaw.
- Flask: a small Python web framework used to build the attacker's server.
- flask-cors: a Flask add-on that controls which other websites may call the server from a browser.
- GitLab: a website for hosting and sharing code, which also offers static site hosting through Pages.
- GitLab Pages: a GitLab feature that publishes a project as a website, for example at
{username}.gitlab.io. - HTTPS: the encrypted, padlocked version of the web protocol.
- Improper authorization: a weakness where the system does not correctly check whether someone is allowed to do what they are asking to do.
- OAuth2: a standard login round trip where a site sends you to an identity service and back, receiving a code it can exchange for access.
- Origin: the combination of scheme, host and port that identifies a website to the browser; service workers are tied to one origin.
/authendpoint: the path on a Pages site that receives the OAuth2 callback with the code and state.- Service worker: a small program a website installs in your browser that can inspect, change or block requests to that site.
- Session cookie: a cookie that proves you are logged in for the current visit.
stateparameter: a random value in OAuth2 that ties a callback to the browser session that started the flow, defending against CSRF.- stdout: the standard text output of a program, normally the terminal screen.
- Subdomain: a named part of a larger domain, such as
attacker.gitlab.ioundergitlab.io. - Token: a value that stands in for proof of identity or permission, such as a session cookie.