Skip to content
XSS Write-up Advanced

Breaking GitHub Private Pages for $35k

A CRLF injection in the page_id parameter of GitHub's private pages authentication endpoint allowed an attacker to inject a script into the response body, achieving XSS. Combined with a cookie prefix bypass and cache poisoning, an attacker could execute JavaScript on a private page domain and potentially pivot to other private pages.

Robert Chen Follow

Published on robertchen.cc

Bounty$35,000 View original

I found and reported this vulnerability with @ginkoid.

Robert Chen (@NotDeGhost), Philip, on robertchen.cc

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

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. 11 min read.

Context

GitHub Pages can serve private pages, and those pages are hosted on the separate github.io domain. Because of that separation, the github.com authentication cookies are not sent to the private pages server. The report states plainly that private page authentication therefore has no way to determine a user's identity without additional integration with github.com. To bridge the gap, GitHub built a custom authentication flow, which the authors note introduces the possibility of bugs.

The report was submitted to GitHub's private pages private bug bounty. That programme carried two CTF bonuses on top of the base payout:

  • $10k for reading the flag at flag.private-org.github.io without user interaction, with a further $5k if the flag was read from an account outside the private-org organisation.
  • $5k for reading the flag at flag.private-org.github.io with user interaction.

The work was done jointly by Robert Chen (@NotDeGhost) and @ginkoid. The report says it was the first report that paid out for the author on HackerOne, that $35,000 was the highest bounty received so far, and that the author believed it to be the highest GitHub had paid to date. The vulnerability was rated at the high end of the "High" severity, with a $20,000 base payout, and the CTF bonus brought the total to $35,000.

Setup

The report does not describe any special accounts, roles, tools or software versions. The only precondition it makes explicit is knowledge of the authentication flow, which the authors reconstructed by observation and by reading GitHub Enterprise source code, to which they say they had access. That access led them to suspect the private page server was implemented in openresty nginx.

Finding it

The authentication flow

At the time of the report, the flow looked like this:

From the original on robertchen.cc

In more detail, as the report describes it:

  1. On visiting a private page, the server checks for the __Host-gh_pages_token cookie. If the cookie is absent or incorrectly set, the private page server redirects to https://github.com/login. That initial redirect also sets a nonce stored in the __Host-gh_pages_session cookie.
  2. The report notes that this cookie uses the __Host- cookie prefix, which in theory prevents it from being set from JavaScript against a non-host (parent) domain, as an additional defence in depth measure.
  3. /login then redirects to /pages/auth?nonce=&page_id=&path=. This endpoint generates a temporary authentication cookie and passes it to https://pages-auth.github.com/redirect in the token parameter. The nonce, page_id and path are forwarded in the same way.
  4. /redirect forwards to https://repo.org.github.io/__/auth. This final endpoint sets the authentication cookies on the repo.org.github.io domain, namely __Host-gh_pages_token and __Host-gh_pages_id. It also validates the nonce against the previously set __Host-gh_pages_session.

Throughout the flow, the original request path and page id travel in the path and page_id query parameters, and the nonce travels in the nonce parameter. The report adds that the flow may have changed slightly since, in part because of this report, but that the overall idea is the same.

The first observation: whitespace and the Set-Cookie header

The authors describe their approach as playing around with the flow. While investigating, they noticed that page_id parsing seemed to ignore whitespace, and, more interestingly, that the parameter was rendered directly into the Set-Cookie header.

For example, page_id=12345%20 produced:

HTTP
Set-Cookie: __Host-gh_pages_id=12345 ; Secure; HttpOnly; path=/

This suggested pseudocode along these lines:

Text
page_id = query.page_id

do_page_lookup(to_int(page_id))
set_page_id_cookie(page_id)

In other words, page_id is converted to an integer for the lookup but is also rendered verbatim into the Set-Cookie header. That is a classic CRLF injection shape, but with a constraint: any non-whitespace character broke the integer parsing. Sending page_id=12345%0d%0a%0d%0a broke the authentication flow and produced an interesting response, but the report says there was no immediate impact beyond that.

HTTP
; Secure; HttpOnly; path=/
Cache-Control: private
Location: https://83e02b43.near-dimension.github.io/
X-GLB-L

As a side note, the report explains that because the Location: header was appended after the Set-Cookie header, the response pushes Location out of the sent HTTP headers. Even though the response is a 302 redirect, the Location header is ignored and the body content is rendered.

Exploitation

Step 1: a null byte ends integer parsing

Having looked through GitHub Enterprise source, the authors suspected the private page server ran on openresty nginx. Being relatively low level, perhaps it had issues with null bytes. The report's framing is that it could not hurt to try.

Appending a null byte causes the integer parsing to end, so the following payload could be used:

JavaScript
"?page_id=" + encodeURIComponent("\r\n\r\n\x00<script>alert(origin)</script>")

The \r\n\r\n terminates the headers and starts the body, the \x00 ends the integer parsing, and the <script> element becomes the rendered body. The result was an XSS.

From the original on robertchen.cc

The report notes a limit here: the response is rejected if there is a null byte in the header, so the null byte has to come at the beginning of the body. That means this cannot be turned into a header injection attack.

At this point the authors had arbitrary JavaScript execution on a private page domain. The remaining problem was the nonce. The page_id and path parameters are known, but the nonce prevents sending victims down the authentication flow with a poisoned page_id. The nonce would have to be fixated or predicted.

Step 2: sibling pages share cookies

The first observation is that sibling private pages in the same organisation can set cookies on each other, because *.github.io is not on the Public Suffix List. Cookies set on private-org.github.io are therefore passed down to private-page.private-org.github.io.

That gives an easy nonce bypass if the __Host- prefix protections can be bypassed: simply set a fake nonce in a sibling page and let it be passed down. The report notes that this prefix is not enforced on all browsers.

From the original on robertchen.cc

However, the report concludes that only IE would be vulnerable to such a bypass, so this route was not good enough.

Step 3: attacking the nonce itself

The authors considered attacking the nonce directly. It seemed securely generated, and the report says cryptography is not the author's strong suit; in any case, finding a bypass for the entropy used in nonce generation seemed unlikely. So the question became how to fixate the nonce.

They went back to the source, and to the RFCs. In particular, they came across the question of how cookies are normalised, and specifically how cookies should be treated with regard to capitalisation: is __HOST- the same as __Host-?

On browsers it is easy to confirm that they are handled differently:

JavaScript
document.cookie = "__HOST-Test=1"; // works
document.cookie = "__Host-Test=1"; // fails

It turns out that the GitHub private pages server ignores capitalisation when parsing cookies. That is the prefix bypass. With it, the authors put together a simple proof of concept for a full XSS:

HTML
<script>
const id = location.search.substring("?id=".length)

document.cookie = "__HOST-gh_pages_session=dea8c624-468f-4c5b-a4e6-9a32fe6b9b15; domain=.private-org.github.io";
location = "https://github.com/pages/auth?nonce=dea8c624-468f-4c5b-a4e6-9a32fe6b9b15&page_id=" + id + "%0d%0a%0d%0a%00<script>alert(origin)%3c%2fscript>&path=Lw";
</script>

The script reads the page id from the id query parameter, sets a __HOST-gh_pages_session cookie scoped to .private-org.github.io, and sends the browser to /pages/auth with a matching nonce, the poisoned page_id, and path=Lw. The report says this by itself would be enough for the $5k bonus, but the authors wanted to push further.

Step 4: cache poisoning

The report describes an additional design flaw: the response on the /__/auth? endpoint was cached solely on the parsed integer value of page_id. In itself this is technically harmless, because the token set by the endpoint is scoped to the private page and has no other privileges. The report nevertheless calls it a somewhat questionable design practice, noting that if additional privileges were granted to tokens later, it could become a source of security problems.

Regardless, the caching behaviour gave an easy way to escalate severity. Because the cache key is the parsed integer value, a successful cache poison with an XSS payload could affect other users who had not interacted with a malicious payload at all.

From the original on robertchen.cc

The scenario in the report:

  1. The adversary controls unprivileged.org.github.io and wants to access privileged.org.github.io.
  2. They poison the authentication flow for unprivileged.org.github.io, and the XSS payload gets cached.
  3. When a privileged user visits unprivileged.org.github.io, they experience an XSS attack on the unprivileged.org.github.io domain.
  4. Because cookies can be set on the shared parent domain org.github.io, the adversary can now attack privileged.org.github.io.

The report says this would allow any attacker with read permissions against a private page to permanently poison the authentication flow for that page.

Step 5: public-private pages

To get the $15,000 bonus, the attack had to be performed from a user account that is not in the organisation. The authors abused another seemingly irrelevant misconfiguration, which they call "public-private pages".

A possible misconfiguration in private pages allows public repositories to have their own "private" pages. These pages go through the normal authentication cycle but are public to everyone. If an organisation has one of these public-private pages, any user with a GitHub account has "read access".

From the original on robertchen.cc

The report explains that this happens when a private page repository is changed to public. The scenario is plausible: an organisation might initially create a private repository with a corresponding private page, and later decide to open source the project, changing the repository status to public.

Combining this with the earlier steps, an unprivileged outside user could pivot from the public-private page to compromise the authentication flows of internal private pages. The report presents a proof of concept showing how an external attacker could use an internal employee to pivot to otherwise private pages.

From the original on robertchen.cc

With that, the authors secured the maximum CTF bonus. The report adds that persistence could possibly be achieved through AppCache or other techniques, but leaves that as an exercise to the reader.

Root cause

The report identifies several distinct mistakes:

  1. Direct rendering of page_id into Set-Cookie. The value is converted to an integer for the page lookup but is also written verbatim into the header. Whitespace was tolerated, which made the injection visible, and a null byte ended the integer parsing so that arbitrary content could follow the CRLF sequence. The null byte cannot appear in the header, so the injected content lands in the body rather than the headers.
  2. Case-insensitive cookie parsing on the server. The __Host- prefix is meant to prevent a cookie from being set from JavaScript against a parent domain. Browsers enforce the prefix case-sensitively, but the private pages server ignored capitalisation when parsing cookies, so a __HOST- cookie set by JavaScript on the shared parent domain was accepted as the session cookie. That allowed nonce fixation.
  3. Shared parent domain. *.github.io is not on the Public Suffix List, so sibling pages can set cookies on each other and on the shared parent domain. This is what makes both the nonce fixation and the cross-page pivot possible.
  4. Cache keyed on the parsed integer. The /__/auth? response was cached solely on the parsed integer value of page_id, so a poisoned response could be served to other users.
  5. Public-private pages. A private page repository changed to public leaves a page that goes through the authentication cycle but is readable by any GitHub account, giving an outside attacker a foothold inside the organisation's domain.

The report says the authentication flow may have changed slightly, in part due to this report, while the overall idea remains the same.

Impact

Under the conditions described, an attacker could achieve arbitrary JavaScript execution on a private page domain. With the nonce bypass, the XSS could be delivered to a victim through the authentication flow. With cache poisoning, the payload could affect users who never interacted with a malicious payload, and any attacker with read permissions against a private page could permanently poison that page's authentication flow. With a public-private page, an outside user with only a GitHub account could pivot through an internal employee to otherwise private pages. The report hedges persistence through AppCache as possible but not demonstrated.

Fix

The report does not describe the fix in detail. It says only that the authentication flow may have changed slightly, in part because of the report, and that the overall idea is the same.

Variants to test

  1. Reflected parameters in Set-Cookie. Send a parameter that is used both for a numeric lookup and written into a cookie header, with trailing whitespace, then with %0d%0a. A positive result is a response whose Set-Cookie line is split and whose following lines are attacker-controlled.
  2. Null byte after CRLF. Where a numeric parse rejects non-whitespace, try %00 before the payload. A positive result is a response body containing the injected markup while the lookup still succeeds.
  3. Cookie prefix case sensitivity. Set __HOST-name=value from JavaScript on a parent domain and see whether the server treats it as __Host-name. A positive result is the server accepting the capitalised cookie as the protected one.
  4. Shared parent domain cookie scope. Check whether *.example.com or another multi-tenant parent is on the Public Suffix List. If not, test whether a cookie set on one tenant is sent to a sibling tenant.
  5. Cache key mismatch. Compare the value used for the cache key with the value rendered into the response. A positive result is a poisoned response served to a request with a different raw parameter but the same parsed value.
  6. Repository visibility changes. Take a repository with private pages, change it to public, and check whether the pages remain reachable through the authentication cycle by an account outside the organisation. A positive result is a page that authenticates but is readable by anyone.

Timeline

  • 05/21/2020: reported to the GitHub Private Program on HackerOne.
  • 06/20/2020: resolved and paid out by GitHub.
  • 04/03/2021: blog post published.

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. 25 min read.

What happened, in plain words

GitHub Pages is a service that lets people publish websites straight from a GitHub repository. Some of those sites can be made private, so that only certain people can see them. Those private sites live on a separate domain, github.io, which is not the same as github.com. Because they are on a different domain, the login cookies that your browser holds for github.com are never sent to the private pages server. That server therefore has no direct way of knowing who you are, so GitHub built a custom login flow to work around the gap. The report notes that building a custom flow like this introduces the possibility of bugs.

Robert Chen (@NotDeGhost) and @ginkoid found a chain of bugs in that flow. The chain let them run their own JavaScript inside a private page domain, and the report describes how cache poisoning and public-private pages could deliver it to other users. The report says this was the first report that paid out for the author on HackerOne, that $35,000 was the highest bounty received so far, and that the author believed it to be the highest GitHub had paid to date. The vulnerability was rated at the high end of the "High" severity, with a $20,000 base payout, and a CTF bonus brought the total to $35,000.

The report was submitted to GitHub's private pages private bug bounty. That programme carried two CTF bonuses on top of the base payout:

  • $10k for reading the flag at flag.private-org.github.io without user interaction, with a further $5k if the flag was read from an account outside the private-org organisation.
  • $5k for reading the flag at flag.private-org.github.io with user interaction.

A "CTF" here means a capture the flag exercise: a hidden value (the flag) that you have to read to prove you got in. "Without user interaction" means the victim does not have to click anything; "with user interaction" means they do.

Setup

The report does not describe any special accounts, roles, tools or software versions. The only precondition it makes explicit is knowledge of the authentication flow, which the authors reconstructed by observation and by reading GitHub Enterprise source code, to which they say they had access. That access led them to suspect the private page server was implemented in openresty nginx.

Two terms worth unpacking before we go on. "Source code" is the human-written text that a program is built from; being able to read it is like being handed the recipe instead of only tasting the cake. "openresty nginx" is a web server: the program that sits on a machine and answers requests from browsers. Knowing which server software is running matters because different servers have different quirks, and quirks are where bugs hide.

Finding it

The authentication flow

At the time of the report, the flow looked like this:

From the original on robertchen.cc

The image shows the sequence of redirects and cookies that make up the private page login flow, as described in the text below.

In more detail, as the report describes it:

  1. On visiting a private page, the server checks for the __Host-gh_pages_token cookie. If the cookie is absent or incorrectly set, the private page server redirects to https://github.com/login. That initial redirect also sets a nonce stored in the __Host-gh_pages_session cookie.
  2. The report notes that this cookie uses the __Host- cookie prefix, which in theory prevents it from being set from JavaScript against a non-host (parent) domain, as an additional defence in depth measure.
  3. /login then redirects to /pages/auth?nonce=&page_id=&path=. This endpoint generates a temporary authentication cookie and passes it to https://pages-auth.github.com/redirect in the token parameter. The nonce, page_id and path are forwarded in the same way.
  4. /redirect forwards to https://repo.org.github.io/__/auth. This final endpoint sets the authentication cookies on the repo.org.github.io domain, namely __Host-gh_pages_token and __Host-gh_pages_id. It also validates the nonce against the previously set __Host-gh_pages_session.

Throughout the flow, the original request path and page id travel in the path and page_id query parameters, and the nonce travels in the nonce parameter. The report adds that the flow may have changed slightly since, in part because of this report, but that the overall idea is the same.

Let us slow down, because almost every word above is jargon.

A cookie is a small piece of text that a website asks your browser to store and to send back on every later request to that site. Think of it as a cloakroom ticket: the site gives it to you once, and you show it on every visit so the site knows you already checked in. A token is a cookie whose job is specifically to prove identity, like a wristband at an event.

A redirect is the server telling your browser "not here, go to this other address instead". A query parameter is the part of a web address after the ?, made of name=value pairs joined by &. In https://example.com/page?colour=red&size=large, colour and size are parameters. An endpoint is a specific address on a server that does one particular job, like a specific counter in a large office.

A nonce is a value that is used once and then should never be reused. Here it acts like a numbered claim ticket: the server hands you one at the start of the login flow, and later checks that the ticket you present matches the one it issued. If an attacker could choose the nonce in advance, they could prepare a matching ticket and the check would pass. That is called nonce fixation, and it is the goal of a later step.

A cookie prefix is a naming rule. If a cookie's name starts with __Host-, browsers apply extra restrictions, in particular the cookie must be set without a Domain attribute and only for the exact host that set it. The idea is that JavaScript running on a parent domain cannot forge such a cookie for a child domain. The report calls this "defence in depth", meaning an extra layer of protection on top of the main one.

A domain is the part of an address that identifies a site, such as github.io. A parent domain is the shared part above a subdomain: in private-page.private-org.github.io, the parent is private-org.github.io, and above that github.io. Cookies can be scoped to a parent domain so that they are sent to all its subdomains, which is exactly the behaviour that becomes important later.

The first observation: whitespace and the Set-Cookie header

The authors describe their approach as playing around with the flow. While investigating, they noticed that page_id parsing seemed to ignore whitespace, and, more interestingly, that the parameter was rendered directly into the Set-Cookie header.

A header is a line of metadata that travels with every web request and response, separate from the page content. Set-Cookie is the response header a server uses to hand a cookie to the browser. If a server writes user-supplied text into a header without cleaning it, an attacker may be able to inject extra characters that the server treats as part of its own message. That class of bug is called header injection, and its most common form is CRLF injection: CR (carriage return) and LF (line feed) are the two characters that mark the end of a line in HTTP, so injecting them lets an attacker start a new line, and therefore a new header, of their own.

For example, page_id=12345%20 produced:

HTTP
Set-Cookie: __Host-gh_pages_id=12345 ; Secure; HttpOnly; path=/

Line by line: Set-Cookie: is the header name. __Host-gh_pages_id=12345 is the cookie being set, with the value 12345. The %20 in the request was a space, and you can see it survived as a space after 12345. Secure means the cookie is only sent over HTTPS. HttpOnly means JavaScript in the page cannot read it. path=/ means it applies to the whole site. The important detail is that the space the attacker sent was echoed back inside the header, which shows the value is being copied in without cleaning.

This suggested pseudocode along these lines:

Text
page_id = query.page_id

do_page_lookup(to_int(page_id))
set_page_id_cookie(page_id)

Line by line: the first line takes the page_id value from the query string. The third line converts it to an integer (a whole number) and uses that number to look up the page. The fourth line writes the original, unconverted text into the cookie. The bug is the gap between those two lines: one path treats the value as a number, the other treats it as raw text. Pseudocode is not real code; it is a sketch of what the real code probably does, written to reason about it.

In other words, page_id is converted to an integer for the lookup but is also rendered verbatim into the Set-Cookie header. That is a classic CRLF injection shape, but with a constraint: any non-whitespace character broke the integer parsing. Sending page_id=12345%0d%0a%0d%0a broke the authentication flow and produced an interesting response, but the report says there was no immediate impact beyond that.

%0d is CR and %0a is LF, written in the percent-encoding that URLs use for characters that cannot appear literally. So %0d%0a%0d%0a is two blank-line endings in a row, which in HTTP means "the headers end here and the body begins".

HTTP
; Secure; HttpOnly; path=/
Cache-Control: private
Location: https://83e02b43.near-dimension.github.io/
X-GLB-L

Line by line: the first line is the tail of the Set-Cookie header, now split off because the injected CRLFs ended the line early. Cache-Control: private is a header saying the response is for one user only and should not be stored in shared caches. Location: is the header that tells the browser where to redirect to. X-GLB-L is a header whose name has been cut short, because the injected line breaks pushed everything out of place. The point is that the attacker's line breaks rearranged the server's own headers.

As a side note, the report explains that because the Location: header was appended after the Set-Cookie header, the response pushes Location out of the sent HTTP headers. Even though the response is a 302 redirect, the Location header is ignored and the body content is rendered.

A 302 is the HTTP status code for "found, look elsewhere", the normal redirect. Normally the browser follows the Location header and you never see the body. Here, because the header ended up in the wrong place, the browser does not follow it and instead displays whatever body came back. That is useful to an attacker, because it means injected content in the body will actually be shown.

Exploitation

Step 1: a null byte ends integer parsing

Having looked through GitHub Enterprise source, the authors suspected the private page server ran on openresty nginx. Being relatively low level, perhaps it had issues with null bytes. The report's framing is that it could not hurt to try.

A null byte is a character with the numeric value zero, written \x00 or %00. In many programming languages it marks the end of a piece of text, because text is stored as a run of characters terminated by a zero. That legacy means that some parsers stop reading at a null byte, which is why attackers try it: it can make one part of a program see a short, harmless value while another part sees the full, dangerous one.

Appending a null byte causes the integer parsing to end, so the following payload could be used:

JavaScript
"?page_id=" + encodeURIComponent("\r\n\r\n\x00<script>alert(origin)</script>")

Line by line: "?page_id=" is the start of a query string. encodeURIComponent(...) takes the text inside and converts every character that is not safe in a URL into its percent-encoded form, so \r becomes %0d, \n becomes %0a, \x00 becomes %00, and the angle brackets and slashes of the script tag become %3c, %3e and %2f. The + joins the two strings into one URL fragment. The text inside is the payload: two CRLFs to end the headers, a null byte to stop the integer parsing, and then a <script> element containing alert(origin). alert(origin) is a JavaScript call that pops up a dialog showing the page's origin, which is a harmless way to prove that your code is running on that site.

The \r\n\r\n terminates the headers and starts the body, the \x00 ends the integer parsing so the rest of the value is not rejected, and the <script> element becomes the rendered body. The result was an XSS.

XSS, cross-site scripting, means getting your own JavaScript to run inside someone else's web page, in that page's origin. Because the browser treats all code on a page as belonging to that site, XSS lets an attacker act as that site: read what the page can read, make requests as the logged-in user, and so on. It is the difference between writing a note and being handed the site's keys.

From the original on robertchen.cc

The image accompanies the statement that the XSS worked.

The report notes a limit here: the response is rejected if there is a null byte in the header, so the null byte has to come at the beginning of the body. That means this cannot be turned into a header injection attack.

In other words, the null byte is only tolerated once the headers have already ended. You cannot use it to smuggle extra headers, but you can use it to smuggle body content, which is enough for XSS.

At this point the authors had arbitrary JavaScript execution on a private page domain. The remaining problem was the nonce. The page_id and path parameters are known, but the nonce prevents sending victims down the authentication flow with a poisoned page_id. The nonce would have to be fixated or predicted.

Why does the nonce block them? Because the flow checks that the nonce in the request matches the one the server issued to that browser. An attacker can build a malicious link with a poisoned page_id, but if the nonce in that link does not match the victim's session, the check fails and the payload never reaches the vulnerable step. To get past it, the attacker must either make the victim's browser use a nonce the attacker chose (fixation) or guess the nonce the server will issue (prediction).

Step 2: sibling pages share cookies

The first observation is that sibling private pages in the same organisation can set cookies on each other, because *.github.io is not on the Public Suffix List. Cookies set on private-org.github.io are therefore passed down to private-page.private-org.github.io.

The Public Suffix List is a maintained list of domain endings under which separate people or organisations register names, such as co.uk or github.io. Browsers use it to decide which domains are truly separate from each other. If a domain is on the list, a cookie set on one part cannot be read by another part. If it is not on the list, the browser treats the whole thing as one site, and cookies set on the parent are sent to every child. *.github.io means "any subdomain of github.io".

That gives an easy nonce bypass if the __Host- prefix protections can be bypassed: simply set a fake nonce in a sibling page and let it be passed down. The report notes that this prefix is not enforced on all browsers.

From the original on robertchen.cc

The image accompanies the note that the __Host- prefix is not enforced on all browsers.

However, the report concludes that only IE would be vulnerable to such a bypass, so this route was not good enough.

IE is Internet Explorer, an old Microsoft browser. A bug that only works in a browser almost nobody uses is not a practical attack, so the authors kept looking.

Step 3: attacking the nonce itself

The authors considered attacking the nonce directly. It seemed securely generated, and the report says cryptography is not the author's strong suit; in any case, finding a bypass for the entropy used in nonce generation seemed unlikely. So the question became how to fixate the nonce.

Cryptography is the mathematics of keeping secrets secret. Entropy here means randomness: a nonce with high entropy is hard to guess because there are so many possible values. If the nonce is generated with enough randomness, guessing it is hopeless, so the authors turned to a different idea: making the server accept a nonce the attacker already knows.

They went back to the source, and to the RFCs. In particular, they came across the question of how cookies are normalised, and specifically how cookies should be treated with regard to capitalisation: is __HOST- the same as __Host-?

An RFC is a document that describes how internet standards work, agreed by the community that builds the internet. Reading the RFC is like reading the official rulebook for a game, to see whether a move is legal. Normalisation means reducing different spellings of the same thing to one standard form, for example treating upper and lower case as equal. The question is whether the server normalises cookie names before checking the prefix.

On browsers it is easy to confirm that they are handled differently:

JavaScript
document.cookie = "__HOST-Test=1"; // works
document.cookie = "__Host-Test=1"; // fails

Line by line: document.cookie is the browser's interface for reading and writing cookies from JavaScript. The first line tries to set a cookie named __HOST-Test with value 1. Because the prefix is written in capitals, the browser does not recognise it as the protected __Host- prefix, so it treats it as an ordinary cookie and accepts it. The second line tries the correctly capitalised __Host-Test, which the browser recognises as protected and refuses, because JavaScript is not allowed to set such a cookie. The comments // works and // fails record the observed result.

It turns out that the GitHub private pages server ignores capitalisation when parsing cookies. That is the prefix bypass. With it, the authors put together a simple proof of concept for a full XSS:

HTML
<script>
const id = location.search.substring("?id=".length)

document.cookie = "__HOST-gh_pages_session=dea8c624-468f-4c5b-a4e6-9a32fe6b9b15; domain=.private-org.github.io";
location = "https://github.com/pages/auth?nonce=dea8c624-468f-4c5b-a4e6-9a32fe6b9b15&page_id=" + id + "%0d%0a%0d%0a%00<script>alert(origin)%3c%2fscript>&path=Lw";
</script>

Line by line: <script> and </script> mark the start and end of JavaScript inside an HTML page. const id = location.search.substring("?id=".length) reads the query string of the current address (location.search), cuts off the leading ?id= (.substring("?id=".length) means "from the position just after those four characters"), and stores the rest in a variable called id. That is the page id the attacker wants to poison. The next line sets a cookie named __HOST-gh_pages_session with a chosen value, scoped with domain=.private-org.github.io so that it is sent to every subdomain of private-org.github.io. The capitalised __HOST- is what lets the browser accept it. The last line sends the browser to GitHub's /pages/auth endpoint with three parameters: nonce set to the same value the attacker just planted, page_id set to the attacker's id followed by the CRLF, null byte and script payload (with the angle brackets and slash percent-encoded), and path set to Lw, which is the percent-encoded form of /. Because the server ignores capitalisation, it reads the planted __HOST-gh_pages_session as if it were the real __Host-gh_pages_session, so the nonce check passes.

The report says this by itself would be enough for the $5k bonus, but the authors wanted to push further.

Step 4: cache poisoning

The report describes an additional design flaw: the response on the /__/auth? endpoint was cached solely on the parsed integer value of page_id. In itself this is technically harmless, because the token set by the endpoint is scoped to the private page and has no other privileges. The report nevertheless calls it a somewhat questionable design practice, noting that if additional privileges were granted to tokens later, it could become a source of security problems.

A cache is a store of saved responses, kept so that the next person asking for the same thing gets it faster. A cache key is the piece of information used to decide whether a saved response matches a new request. If the key is too coarse, two different requests can be treated as the same, and one person's response gets served to another. That is cache poisoning: planting a malicious response in the cache so that other users receive it.

Regardless, the caching behaviour gave an easy way to escalate severity. Because the cache key is the parsed integer value, a successful cache poison with an XSS payload could affect other users who had not interacted with a malicious payload at all.

From the original on robertchen.cc

The video accompanies the cache poisoning explanation.

The scenario in the report:

  1. The adversary controls unprivileged.org.github.io and wants to access privileged.org.github.io.
  2. They poison the authentication flow for unprivileged.org.github.io, and the XSS payload gets cached.
  3. When a privileged user visits unprivileged.org.github.io, they experience an XSS attack on the unprivileged.org.github.io domain.
  4. Because cookies can be set on the shared parent domain org.github.io, the adversary can now attack privileged.org.github.io.

The report's conclusion is that any attacker with read permissions against a private page could permanently poison the authentication flow for that page.

"Permanently" here means for as long as the poisoned response stays in the cache. The attacker does not need the victim to click a special link; the victim only needs to visit the attacker's page, and the cached payload does the rest.

Step 5: public-private pages

To get the $15,000 bonus, the attack had to be performed from a user account that is not in the organisation. The authors abused another seemingly irrelevant misconfiguration, which they call "public-private pages".

A possible misconfiguration in private pages allows public repositories to have their own "private" pages. These pages go through the normal authentication cycle but are public to everyone. If an organisation has one of these public-private pages, any user with a GitHub account has "read access".

A repository is a project's folder of files plus its history, stored on GitHub. A repository can be private (only invited people can see it) or public (anyone can see it). A misconfiguration is a setting that is not what the designers intended, often because two features interact in a way nobody thought about.

From the original on robertchen.cc

The video accompanies the example of how a public-private page might be made.

The report explains that this happens when a private page repository is changed to public. The scenario is plausible: an organisation might initially create a private repository with a corresponding private page, and later decide to open source the project, changing the repository status to public.

Combining this with the earlier steps, an unprivileged outside user could pivot from the public-private page to compromise the authentication flows of internal private pages. The report presents a proof of concept showing how an external attacker could use an internal employee to pivot to otherwise private pages.

A proof of concept (POC) is a small demonstration that a bug is real and exploitable, without needing to cause actual harm. To pivot means to use one foothold to reach another, like stepping from one stone to the next to cross a river.

From the original on robertchen.cc

The video accompanies the proof of concept showing the pivot.

With that, the authors secured the maximum CTF bonus. The report adds that persistence could possibly be achieved through AppCache or other techniques, but leaves that as an exercise to the reader.

Persistence means keeping access after the initial attack, so that it survives a reboot or a logout. AppCache was an old browser feature for storing web resources offline; the report only mentions it as a possibility and does not demonstrate it.

Root cause

The report identifies several distinct mistakes:

  1. Direct rendering of page_id into Set-Cookie. The value is converted to an integer for the page lookup but is also written verbatim into the header. Whitespace was tolerated, which made the injection visible, and a null byte ended the integer parsing so that arbitrary content could follow the CRLF sequence. The null byte cannot appear in the header, so the injected content lands in the body rather than the headers.
  2. Case-insensitive cookie parsing on the server. The __Host- prefix is meant to prevent a cookie from being set from JavaScript against a parent domain. Browsers enforce the prefix case-sensitively, but the private pages server ignored capitalisation when parsing cookies, so a __HOST- cookie set by JavaScript on the shared parent domain was accepted as the session cookie. That allowed nonce fixation.
  3. Shared parent domain. *.github.io is not on the Public Suffix List, so sibling pages can set cookies on each other and on the shared parent domain. This is what makes both the nonce fixation and the cross-page pivot possible.
  4. Cache keyed on the parsed integer. The /__/auth? response was cached solely on the parsed integer value of page_id, so a poisoned response could be served to other users.
  5. Public-private pages. A private page repository changed to public leaves a page that goes through the authentication cycle but is readable by any GitHub account, giving an outside attacker a foothold inside the organisation's domain.

The report says the authentication flow may have changed slightly, in part due to this report, while the overall idea remains the same.

Impact

Under the conditions described, an attacker could achieve arbitrary JavaScript execution on a private page domain. With the nonce bypass, the XSS could be delivered to a victim through the authentication flow. With cache poisoning, the payload could affect users who never interacted with a malicious payload, and any attacker with read permissions against a private page could permanently poison that page's authentication flow. With a public-private page, an outside user with only a GitHub account could pivot through an internal employee to otherwise private pages. The report hedges persistence through AppCache as possible but not demonstrated.

Fix

The report does not describe the fix in detail. It says only that the authentication flow may have changed slightly, in part because of the report, and that the overall idea is the same.

Variants to test

These are the kinds of checks a security tester would run on a similar system, written as the report frames them.

  1. Reflected parameters in Set-Cookie. Send a parameter that is used both for a numeric lookup and written into a cookie header, with trailing whitespace, then with %0d%0a. A positive result is a response whose Set-Cookie line is split and whose following lines are attacker-controlled.
  2. Null byte after CRLF. Where a numeric parse rejects non-whitespace, try %00 before the payload. A positive result is a response body containing the injected markup while the lookup still succeeds.
  3. Cookie prefix case sensitivity. Set __HOST-name=value from JavaScript on a parent domain and see whether the server treats it as __Host-name. A positive result is the server accepting the capitalised cookie as the protected one.
  4. Shared parent domain cookie scope. Check whether *.example.com or another multi-tenant parent is on the Public Suffix List. If not, test whether a cookie set on one tenant is sent to a sibling tenant.
  5. Cache key mismatch. Compare the value used for the cache key with the value rendered into the response. A positive result is a poisoned response served to a request with a different raw parameter but the same parsed value.
  6. Repository visibility changes. Take a repository with private pages, change it to public, and check whether the pages remain reachable through the authentication cycle by an account outside the organisation. A positive result is a page that authenticates but is readable by anyone.

Timeline

  • 05/21/2020: reported to the GitHub Private Program on HackerOne.
  • 06/20/2020: resolved and paid out by GitHub.
  • 04/03/2021: blog post published.

Glossary

  • API: a defined set of addresses and rules that one program uses to talk to another, like a menu of things you can ask for.
  • AppCache: an old browser feature for storing web resources so a site can work offline; mentioned in the report only as a possible route to persistence.
  • Authentication: the process of proving who you are, for example by logging in.
  • Bug bounty: a programme that pays people for finding and reporting security bugs.
  • Cache: a store of saved responses kept so that repeated requests are answered faster.
  • Cache key: the piece of information used to decide whether a saved response matches a new request.
  • Cache poisoning: planting a malicious response in a cache so that other users receive it.
  • Cookie: a small piece of text a site asks your browser to store and send back on later requests, used to remember who you are.
  • Cookie prefix: a naming rule such as __Host- that makes browsers apply extra restrictions to a cookie.
  • CRLF injection: inserting carriage-return and line-feed characters into a value so that the server treats part of it as a new line, and therefore a new header or body.
  • Cross-site scripting (XSS): getting your own JavaScript to run inside someone else's page, in that page's origin.
  • CTF: capture the flag, an exercise where you must read a hidden value to prove you got in.
  • Domain: the part of an address that identifies a site, such as github.io.
  • Endpoint: a specific address on a server that performs one particular job.
  • Entropy: the amount of randomness in a value; high entropy makes it hard to guess.
  • Fixation: making a victim use a value the attacker chose in advance, such as a nonce.
  • Header: a line of metadata sent with a web request or response, separate from the page content.
  • Header injection: writing attacker-controlled text into a header so that it is treated as part of the server's own message.
  • HttpOnly: a cookie attribute meaning JavaScript in the page cannot read the cookie.
  • Integer: a whole number, such as 12345.
  • Misconfiguration: a setting that is not what the designers intended.
  • Nonce: a value meant to be used once, used here to tie a login flow to one browser session.
  • Normalisation: reducing different spellings of the same thing to one standard form.
  • Null byte: a character with value zero, often used to mark the end of text, written \x00 or %00.
  • openresty nginx: a web server built on nginx, the program that answers browser requests.
  • Parent domain: the shared part above a subdomain, such as private-org.github.io for private-page.private-org.github.io.
  • Percent-encoding: the way URLs write characters that are not safe to appear literally, such as %20 for a space.
  • Persistence: keeping access after the initial attack, so it survives a reboot or logout.
  • Pivot: using one foothold to reach another.
  • Proof of concept (POC): a small demonstration that a bug is real and exploitable.
  • Pseudocode: a sketch of what code probably does, written to reason about it rather than to run.
  • Public Suffix List: a maintained list of domain endings under which separate people register names, used by browsers to decide which domains are separate.
  • Query parameter: a name=value pair after the ? in a URL, joined by &.
  • Redirect: the server telling the browser to go to a different address instead.
  • Repository: a project's folder of files plus its history, stored on GitHub.
  • RFC: a document describing how an internet standard works.
  • Secure: a cookie attribute meaning the cookie is only sent over HTTPS.
  • Set-Cookie: the response header a server uses to hand a cookie to the browser.
  • Source code: the human-written text a program is built from.
  • Token: a cookie whose job is to prove identity.
  • 302: the HTTP status code for a redirect.

More by Robert Chen

View all

1 more disclosed report, best first.

More from GitHub

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.