Skip to content

Resolved: Perfect CORS, Zero Protection

A correctly configured CORS allowlist protected the JSON API and nothing at all protected the WebSocket. How one missing Origin check on a handshake leaked a whole document library.

Published
Categories
Bug BountyPenetration Testing
Resolved: Perfect CORS, Zero Protection
Resolved: Perfect CORS, Zero Protection Watch on YouTube

A website you have never heard of can read your private documents. You do not click anything on it. You do not sign into it. You land on a page, and it reads them.

The part that stayed with me is that the app it came from was not broken. Every permission check ran. The CORS allowlist was configured properly, with exactly one origin on it. I went after a document the obvious way and the server shut me out.

Then I asked for the same document a second way, and it handed over the whole thing.

That bug was high severity, and it paid a four digit bounty. What fixed it was one check, on one endpoint, that nobody had ever written.

This is the first article in Resolved, a series where I go back through my own resolved reports, pick one, and walk you through the whole thing from the first request to the patch.

How The App Is Built

The app was a documents workspace. Sidebar full of your files, editor in the middle, everything saving as you type and waiting for you on whatever device you sign in from next.

It was built the way most of them are built. Two parts.

  • The front end. The interface you look at. It stores nothing at all.
  • The backend. A separate service on its own subdomain, holding the accounts, the sessions and every document.

That split is where all of this starts. An origin is a scheme, a host and a port, and all three have to match. Here the scheme matched and the port matched. The host did not.

So every single request the front end made for your own data crossed an origin boundary. That is the only reason the backend carried CORS rules at all. Strip them out and the browser stops letting the app read its own API.

One login had to work across both names, so the cookie came back scoped to the parent domain:

http
Set-Cookie: ntl_session=...; Path=/; Domain=.example.lab; Max-Age=31536000; Secure; SameSite=None; HttpOnly
Set-Cookie: csrf_token=...; Path=/; Domain=.example.lab; Max-Age=31536000; Secure; SameSite=None

Three attributes decide the rest of this article.

AttributeWhat it does here
Domain=.example.labScopes the cookie to the parent domain, so the front end and the backend both receive it
SecureRestricts it to TLS
SameSite=NoneAttaches it to requests started by any other site

SameSite controls whether a cookie rides along on a request another site caused. Strict never sends it. Lax sends it only on a top-level navigation. None always sends it.

None was the correct choice. The front end sits on a different origin to the backend, so anything stricter breaks the product on day one. Hold onto it anyway.

Two Ways Into One Document

Open one document with a proxy running and two requests land in your history.

http
GET /documents/2998236137      HTTP/1.1   -> 200 OK
GET /documents/2998236137/ws   HTTP/1.1   -> 101 Switching Protocols

Same backend, same document, same cookie. One returns JSON. The other returns 101 Switching Protocols, which is the server agreeing to stop speaking HTTP on that connection and speak something else instead.

Two doors into the same room. Work through one completely before you touch the other, because jumping between them is how you end up confusing yourself.

What CORS Actually Blocks

Send the JSON request to your repeater untouched and the response carries two headers worth reading:

http
HTTP/1.1 200 OK
Access-Control-Allow-Origin: https://app.example.lab
Access-Control-Allow-Credentials: true

The browser asks a question on your behalf here. Some page is about to receive this response. Is the origin it sits on cleared to read it?

Access-Control-Allow-Origin is the answer, and it names exactly one origin. Nothing else gets in.

Access-Control-Allow-Credentials answers the second half, which is whether the request was allowed to carry cookies at all. A browser needs both of them before it will release a credentialed response to a cross-origin page.

Now change Origin to a domain you control and send it again.

http
HTTP/1.1 200 OK

{"docId":2998236137,"ownerId":3401776219,"title":"Q4 account review","body":"..."}

Status 200. Full document. Both CORS headers gone.

This is the part people have backwards. The server did not block you. It never does. It processed the request, returned the data, and withheld only its permission for your page to look at what it sent.

Discarding the response is the browser's job, not the server's.

Which means CORS will never look like a block inside your proxy. If you only test through a repeater you will not see it, and you will not miss it either.

So the JSON side is fine. Configured properly, enforced properly, nothing there to write up.

The WebSocket Handshake, Line By Line

A WebSocket does not start life as a WebSocket. It starts as an ordinary GET.

http
GET /documents/2998236137/ws HTTP/1.1
Host: api.example.lab
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: 9fyqhjFGlA/IVnIRwP2Umw==
Sec-WebSocket-Version: 13
Origin: https://app.example.lab
Cookie: ntl_session=...

Every line is doing a job:

  • Upgrade: websocket asks the server to move this connection off HTTP and onto the WebSocket protocol.
  • Connection: Upgrade tells every proxy and load balancer in between that this specific connection is being switched, so nothing pools it or reuses it afterwards.
  • Sec-WebSocket-Version: 13 is the protocol version from RFC 6455. Thirteen is the only one in use.
  • Sec-WebSocket-Key is sixteen random bytes, base64 encoded.
  • Cookie rides along because of SameSite=None.
  • Origin is the browser naming the site that caused the request.

Sec-WebSocket-Key gets misread constantly. It is not authentication. The server appends a fixed GUID from the RFC, hashes the result with SHA-1, and returns it as Sec-WebSocket-Accept:

python
base64(sha1(key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"))

All that proves is the other end speaks WebSocket and you are not looking at a cached response. It protects nothing and identifies nobody.

Origin is the one that counts. It names the page that triggered the request, not the address being requested, and a page cannot set it. Not from fetch, not from new WebSocket. The browser writes it and locks it. That makes it the only value on the request a server can actually trust.

What The Server Sends Before You Ask

Open the stream before touching anything and there is a single message, server to client:

json
{
  "doc": { "ops": [{ "insert": "Calderwell account review, Q4\n..." }] },
  "doc_len": 419,
  "vsn": 4,
  "title": "Q4 account review",
  "op": "init"
}

Nothing went up. No subscribe frame, no fetch, no ID sent after connecting. The socket opened and the server volunteered the entire document.

That saves a round trip, which is why it was built this way. It also means the connection is the exploit. Open it and you have the data.

Faking Where The Request Came From

Same move as before, this time on the handshake. Replay the upgrade with an Origin you control.

http
HTTP/1.1 101 Switching Protocols

It agreed. If an origin check existed on that endpoint, this is exactly where it would have fired and returned 403 before a single frame moved.

Read what that status line means. The server received a request announcing a foreign origin, saw a session cookie belonging to somebody with no relationship to that origin, and opened the connection anyway.

Then it gets worse, because you can start removing things.

Origin sentResultWhy it matters
https://attacker.test101, document returnedThe plain cross-site case
Header deleted entirely101, document returnedAllowlist code often reads a missing origin as "not cross site", which is backwards
null101, document returnedReal browsers send this from sandboxed iframes, data: URLs and local files
https://app.example.lab.attacker.test101, document returnedA check written with "starts with" passes this. The real origin is only a label on the front of an attacker domain

Test all four on every target. I have had all four pass on a server that looked, from the outside, like it was checking.

Two doors, one document, one cookie, the same authorization code sitting behind both. One of them tells the browser who is allowed to read the answer. The other never asks who is doing the asking.

Nothing failed here. Authorization ran on every request and returned the right answer each time. It simply answered a question nobody should have been able to put to it.

Why CORS Does Not Cover WebSockets

The Same Origin Policy exists to keep one site out of another site's data. CORS is the sanctioned way to loosen that, and the specification attaches it to fetch and XHR.

The JSON request ran straight into that. The browser went looking for permission in the response headers, did not find its own origin named, and threw away data it was already holding.

The WebSocket API sits outside that machinery. No preflight is defined, so no OPTIONS goes out ahead of the handshake. Nothing is asked and nothing is checked. Your browser will send that upgrade to any host from any page.

That is not a browser bug. It is the protocol behaving as specified. Which leaves the server a single way to learn where a handshake originated, and nothing obliges it to look. Where no one wrote the comparison, no comparison runs.

Why SameSite Is Not The Fix

Set that cookie to SameSite=Lax and the browser withholds it from the handshake, since an upgrade request is not a top-level navigation. That is a genuine mitigation, and it is why classic cross-site WebSocket hijacking has become harder to land.

Do not write it down as the fix.

SameSite is evaluated on the registrable domain, not the origin. Every subdomain of the app counts as the same site. One forgotten subdomain serving attacker-controlled content, one subdomain takeover, and Lax hands the cookie over.

SameSite governs who gets to attempt the connection. Only an Origin comparison refuses it.

Finding Other People's Document IDs

Everything so far used a document ID copied out of my own account. The obvious objection is that a random website would never know it.

It does not need to.

The IDs were ten digits and never sequential, so counting upwards gets you nothing. But the allocator was time based, driven by a single counter shared across every account on the system. Create eight documents two seconds apart and the whole run lands inside a band a couple of hundred wide.

That turns an unguessable ID into a bounded search:

  1. Sign up for a normal account on the service.
  2. Save one document of your own.
  3. Read the ID that comes back. That is where the counter is sitting right now.
  4. Step backwards from there, in a window matching when the target was active.

The hostile page did exactly that with an ordinary account of its own. No ID from me at all.

The server then did the sorting. An ID the session could not read closed with code 4004 and the owner's user ID in the message. An ID it could read returned the document. The 4000 to 4999 range is reserved by RFC 6455 for private use, so that code is the application's own decision. Somebody wrote that branch.

A distinguishable error is an oracle. It separated readable documents from unreadable ones, on behalf of a page that should never have been able to ask.

Be honest about scale when you write this up. A busy service burns through that counter, so a document created an hour earlier can sit many thousands of positions behind the present. You target the window the person was active in. Recent records are always the cheapest to reach.

Reading Documents From Another Website

A proxy proves the server's behaviour. It does not prove impact, and a report that stops there gets closed, because anybody can send anything from a repeater.

So run both requests again from an ordinary page on an unrelated domain, with nothing installed and nothing intercepting.

Over HTTP the page receives this, and nothing else:

text
TypeError: Failed to fetch

No status, no headers, no body. The request went out and the server returned 200, but the browser discarded it before any JavaScript could touch it.

Over the socket, the same ID returns the title, the length and the full body, straight into the page.

Identical browser, identical session, identical cookie. One variable moved, and it was the protocol.

one missing origin check on a handshake

Hostile pageunrelated domain, nothing installed
Your browseralready signed in
ntl_sessionDomain=.example.lab, SameSite=None
CORS on the JSON APIone origin on the allowlist
WebSocket handshakeHighno Origin check, no preflight defined
TypeError, Failed to fetch200 returned, response discarded
api.example.lab101 Switching Protocols
Close code 4004separates readable IDs from unreadable
Document librarytitle, length and full body in the first frame

How To Hunt This On Your Own Targets

Three steps, none of which need anything you do not already have.

Find the sockets. Put the app behind a proxy and hunt for a 101 in the history. Work every screen deliberately, because a socket usually belongs to exactly one of them. Collaborative editors, support chat, notification trays, dashboards that refresh themselves. Then grep the bundles for new WebSocket and wss:// to catch endpoints you never reached by clicking.

Read the first frame before sending anything into it. If user data arrives unprompted, you already have something worth chasing.

Replay the handshake. Work through every row of the table above, not only the obvious one.

Then the step that decides whether any of this pays.

Proving It Before You Report

Run the controls in one batch, so every row shares the same session state:

ControlWhat it rules out
Connect from the real originThe session is live and the endpoint works
Connect with no cookieThe endpoint is not simply open to everybody
Connect to another account's documentPer-document authorization is real and enforced
Connect from a foreign originThe only variable left is the Origin header

The first three are the ones that matter. They close off every explanation except the one you are claiming. Without them a triager can reasonably argue the endpoint was public all along.

I ran every one of them before writing a line of that report, and not for the report's benefit. I ran them because I did not yet believe my own finding. The side effect was that the triager had nothing left to push back on.

The One Check That Fixes It

Apply the allowlist you already have at upgrade time, compare the whole origin, and reject a missing or null value explicitly.

js
server.on("upgrade", (req, socket, head) => {
  // Session and per-document authorization run below.
  // Nothing looks at req.headers.origin.
  wss.handleUpgrade(req, socket, head, (ws) => {
    wss.emit("connection", ws, req);
  });
});
js
const ALLOWED = new Set(["https://app.example.lab"]);

server.on("upgrade", (req, socket, head) => {
  const origin = req.headers.origin;
  if (!origin || !ALLOWED.has(origin)) {
    socket.write("HTTP/1.1 403 Forbidden\r\n\r\n");
    socket.destroy();
    return;
  }
  wss.handleUpgrade(req, socket, head, (ws) => {
    wss.emit("connection", ws, req);
  });
});

ALLOWED.has(origin) is an exact match, which is what kills the starts with bypass. The explicit !origin test is what kills the absent and null cases.

With that in place the forged handshake returns 403 with no frame, every legitimate row still passes, and the application behaves exactly as it did before. The check costs the product nothing.

Why This Keeps Happening

Nobody was careless on this target. The allowlist on the JSON API was correct and strict. Per-document authorization was real and enforced in both directions. The session cookie was Secure and HttpOnly.

All of it was applied to one protocol.

That is most of what you are going to find out there. Not applications with no security on them. Applications with good security that only ever got wired into one of the ways in. Somebody locked the door they could see, and the second one was never on the list.

Pick an app you have already tested. Find the 101. Read what arrives in that first frame before you put anything into it.

Further Reading