Skip to content

LearnWeb and API

CORS, CSRF and clickjacking testing

Brief

A browser can attach the session cookie to requests another site starts, so the server must decide which sites may read its answers (CORS), which may change state (CSRF) and which may frame its pages. Response headers and one harmless form post show each gap.

Stage
Access
Reviewed
On this page
  1. Check first
  2. Which cookie flags decide what a cross-site request carries?
  3. Does the API trust an origin it should not, with credentials?
  4. Does a state-changing form post work with the cookie alone?
  5. Can another site frame the account pages?
  6. The defender sees
  7. Done when
  8. Not covered
  9. Tools
  10. Related research
  11. Reviewed

All commands

Situation
A test account on an application named in the scope letter that keeps its session in a cookie, and the routes from the first hour that return account data or change state. Every request goes from our terminal to the application, and the one change we make is a display name we set back.
Goal
A table of the application's cross-site controls: the session cookie's flags, the origins CORS trusts with credentials, which state-changing routes need a token, and whether its pages can be framed.
Outputs
Captured on 5 Nov 2026 against the Atarus Bench Web and API lab fixture, edition web-api-0.3, running on loopback, with one origin playing every host, renamed to the example domains and printed as https (the lab ran plain http). The lookalike origin was run as the fixture's own loopback origin with .attacker.example appended and is printed with the same rename; the fixture accepts a reserved .example origin on these routes only when the request carries no Sec-Fetch-Site header, which browsers send, so no browser page could use it. Nothing here was run in a browser.

Check first

  • Where the application keeps its session. A cookie is what a browser attaches on its own; a bearer token that the page's script adds is not, and CORS and CSRF matter much less for it.
  • The application's own origins, including the other subdomains and ports that call its API legitimately.
  • Which routes change state, from the first hour's list, and which one does the most harm: email, password, payout details.
  • Which browsers the client's users run, since Chromium's default for cookies without SameSite changes what reaches the server.
  • A harmless field we may change and set back, such as the test account's display name.

Steps

curl -si -c jar.txt -H 'Content-Type: application/json' \
  -d "{\"email\":\"tester1@example.com\",\"password\":\"$TESTER_PASS\"}" \
  https://app.example.com/api/login | grep -i '^set-cookie'

Expected output

Set-Cookie: session=sess-1001; Path=/; HttpOnly   # <- no SameSite and no Secure

SameSite decides whether the browser attaches the cookie to a request another site starts: Strict never, Lax only on a top-level navigation with a safe method such as GET, and None always, which browsers accept only with Secure. A cookie with no attribute, as here, is treated as Lax by Chromium-based browsers. Chromium still sends it on a top-level cross-site POST for two minutes after it is set, and browsers without that default send it on a cross-site form post at any time. The missing Secure also lets the cookie travel over plain HTTP. The sign-in saves the session in jar.txt for the steps below.

Negative result. SameSite=Lax or Strict, with Secure and HttpOnly, on every cookie that carries the session.

Fix. Set SameSite=Lax (or Strict where cross-site links into the application are not needed), Secure and HttpOnly on the session cookie, and name it with the __Host- prefix, which requires Secure, Path=/ and no Domain attribute, so a subdomain cannot set or overwrite it.

Does the API trust an origin it should not, with credentials?

for o in https://attacker.example https://app.example.com.attacker.example; do
  echo "Origin: $o"
  curl -si -b jar.txt -H "Origin: $o" https://app.example.com/api/account | grep -iE '^(HTTP/|access-control-)'
done

Expected output

Origin: https://attacker.example
HTTP/1.1 200 OK   # <- no CORS headers: the browser would block the read
Origin: https://app.example.com.attacker.example
HTTP/1.1 200 OK
Access-Control-Allow-Origin: https://app.example.com.attacker.example   # <- a lookalike origin passes a prefix check
Access-Control-Allow-Credentials: true

Two origins against a route that returns the account: a foreign one, and a lookalike that starts with the application's own origin. The foreign one gets no CORS headers, so a browser would not let its page read the answer. The lookalike gets its own origin back with Access-Control-Allow-Credentials: true: the check compares the start of the string, and app.example.com.attacker.example stands in for a name under a domain anyone can register. Other common mistakes give the same result: any origin reflected, a suffix check that attacker-example.com passes, or null trusted.

How far this reaches depends on the cookie. A page on the lookalike origin can read the account only if the browser sends the session cookie on its cross-site request: the cookie must be SameSite=None, and the browser must still allow third-party cookies, which Chrome and Edge do by default and Firefox and Safari partition or block. With this lab's cookie, Chromium would not send it, so we report the CORS policy at the severity the current cookie allows and raise it only if a test page in a browser reads the account.

Negative result. Both origins get no Access-Control-Allow-Origin, or only the application's exact own origin does.

Fix. Compare the whole origin, scheme, host and port, against an exact list of the application's own origins; never a prefix, a suffix or an unanchored pattern. Send Vary: Origin on every response that varies by it, and allow credentials only for those exact origins.

Does a state-changing form post work with the cookie alone?

curl -s -w '\n' -b jar.txt -H 'Origin: https://attacker.example' \
  --data-urlencode 'display_name=atarus-csrf-test' https://app.example.com/api/account/profile

Expected output

{"display_name":"atarus-csrf-test"}   # <- changed with the cookie and a foreign Origin, no token

Any site can make a browser send a form post with application/x-www-form-urlencoded and no preflight (the OPTIONS request a browser sends first before a cross-site request with a custom header or a JSON body), and the browser attaches the cookie wherever SameSite allows. The profile route accepted our post with nothing but the cookie and a foreign Origin header. A forged post from a current browser also carries Sec-Fetch-Site: cross-site, which curl does not send, so before reporting we repeat the post with -H 'Sec-Fetch-Site: cross-site': a server that checks that header refuses it. The lab cannot show that request, because its own browser rail refuses it before the route runs. The display name is the least-impact proof; the report names every route that behaves this way and is rated by what the most harmful one changes. The password route shows the clean case. In the lab it never changes a password; on an engagement we learn whether a route like this checks a token from the application's own request in the proxy history, and we post with the cookie alone only to routes whose change is harmless and reversible:

curl -s -w '\n%{http_code}\n' -b jar.txt -H 'Origin: https://attacker.example' \
  -d 'new_password=unused' https://app.example.com/api/account/password

Expected output

{"error":"csrf token missing or wrong"}
403   # <- this route wants a per-session token in a header

It refuses the same post because it needs a token tied to the session in a header that a cross-site form cannot set. We set the display name back straight away:

curl -s -w '\n' -b jar.txt --data-urlencode 'display_name=tester 1' https://app.example.com/api/account/profile

Expected output

{"display_name":"tester 1"}

Negative result. Every state-changing route answers 403 without the token, or refuses a foreign Origin.

Fix. A per-session token on every state-changing route, checked on the server (the framework's built-in protection, switched on everywhere), and a check on every state change that Origin exactly matches one of the application's own origins, or that Sec-Fetch-Site is same-origin, refusing the request otherwise. SameSite=Lax is the second layer, not the only one.

Can another site frame the account pages?

curl -sI https://app.example.com/account/settings | grep -iE '^(x-frame-options|content-security-policy):' || echo 'no framing header'

Expected output

no framing header   # <- any site may load this page in a frame

Without X-Frame-Options or a frame-ancestors directive, another site can load the account page in a transparent frame over its own decoy button, so a click the user means for the decoy lands on the application's real button. It matters on pages where one click changes something. As with CORS, the frame is signed in only if the browser sends the cookie into a cross-site frame, which needs SameSite=None; with this lab's cookie, Chromium would show the signed-out page. The proof is the missing header; a test page in a browser shows the frame loading if the client asks to see it.

Negative result. X-Frame-Options: DENY or SAMEORIGIN, or Content-Security-Policy with frame-ancestors 'none' or 'self'.

Fix. Content-Security-Policy: frame-ancestors 'self' on every page, plus X-Frame-Options: SAMEORIGIN so older browsers apply the same rule. Use 'none' with DENY where the application never frames itself.

The defender sees

Each request is an ordinary line in the access log. The one field that tells a forged request from the user's own is the Origin header, or Sec-Fetch-Site from a browser, on a state change; logging both lets the defender search for state changes started by another site. If the application keeps an audit log, the display name change and its reversal appear there as two edits by the test account's own session; the lab fixture keeps none. A CORS read from another site leaves nothing on the server that differs from the user's own request except Origin.

Done when

The session cookie's flags are recorded, every credentialed CORS route has had a foreign and a lookalike origin, each state-changing route is listed with whether it requires a token, one harmless route has had a post with the cookie alone and been set back, and the framing headers are read.

Not covered

Confirming each gap in a browser with a test page, login CSRF, WebSocket origin checks, postMessage handlers, JSONP, and caches keyed without the Origin header.