Quiz · Hard
Nineteen harder scenario questions on JWT pitfalls, CORS and preflights, OAuth 2.0 flows, BOLA, mass assignment, webhooks, rate limiting, GraphQL N+1 and real-time options.
Your API issues JWTs containing the user's email and role. A teammate says the token is safe because it is 'encrypted'. What is the correct response?
Correct: JWTs are always encrypted
Correct, as long as the signature uses HS256
Wrong: a standard signed JWT is only Base64URL-encoded, so anyone holding it can read the payload; the signature only prevents tampering
Wrong: JWTs are plain text with no signature
A normal JWT (JWS) is signed, not encrypted. Do not put secrets in the payload; use JWE or an opaque token if the claims must be hidden.
An API verifies JWTs using the algorithm named in each token's own header. An attacker sends a token with "alg": "none" and no signature. What is the flaw?
The server must enforce the expected algorithm and key; trusting the header's algorithm lets attackers skip or change verification
Tokens must be shorter
The server should use HTTP instead of HTTPS
There is no flaw because 'none' is invalid JSON
Never let the token choose how it is verified. Pin the allowed algorithms and key (and reject none), then check exp, iss and aud.
A front-end at https://app.example.com calls https://api.example.com/data. The browser console shows a CORS error but curl works. Why?
The API is down for browsers
curl bypasses TLS
The browser blocks all JSON
CORS is a browser-enforced policy; the API must send the right Access-Control-Allow-Origin header for the page's origin
Different subdomains are different origins. Browsers only expose a cross-origin response to scripts if the server's CORS headers allow it. curl and servers do not apply the policy.
A fetch call sends Content-Type: application/json and an Authorization header to another origin. What does the browser do first?
Nothing, it sends the request directly
It sends an OPTIONS preflight request to check permission
It converts the request to GET
It blocks it permanently
A JSON content type and a custom/Authorization header make the request non-simple, so the browser sends an OPTIONS preflight with Access-Control-Request-Method and Access-Control-Request-Headers first.
The server replies with Access-Control-Allow-Origin: * but the front-end uses credentials: 'include' and the browser still blocks the response. Why?
Cookies are not allowed over HTTPS
Browsers refuse a wildcard origin when credentials are included; the server must echo the specific origin and send Access-Control-Allow-Credentials: true
The wildcard must be **
Credentials only work with GET
With credentials, * is not accepted. Respond with the exact origin, set Vary: Origin, and add Access-Control-Allow-Credentials: true.
Which OAuth 2.0 flow should a single-page app or native mobile app use to sign users in and call an API?
Implicit flow
Resource owner password credentials
Client credentials
Authorization code flow with PKCE
Public clients cannot keep a secret, so they use the authorization code flow with PKCE. Implicit leaks tokens in URLs and the password grant exposes the user's password to the app. Client credentials has no user.
A nightly backend job must call another company's API with its own identity and no user present. Which OAuth 2.0 grant fits?
Client credentials
Authorization code with PKCE
Implicit
Device code
Client credentials is for machine-to-machine access: the app authenticates with its own id and secret to get a token for its own resources.
GET /api/invoices/1042 returns another customer's invoice when you change the number from 1041. Which vulnerability is this?
SQL injection
CSRF
Broken object level authorization (BOLA / IDOR)
Clickjacking
The API checks that you are logged in but not that the invoice belongs to you. Authorize every object access on the server (ownership or permission checks); hiding or randomizing ids is not enough.
You verify a webhook by computing an HMAC of the request body, but verification randomly fails. Parsing the JSON and re-serializing it before hashing changes whitespace. What should you hash?
The re-serialized JSON
Only the URL
Only the headers
The exact raw request body bytes
The signature was computed over the raw bytes the provider sent. Any re-serialization changes them. Compute the HMAC on the raw body and compare in constant time.
An attacker captures a valid signed webhook and replays it a day later. Which measure helps most?
Make the webhook URL longer
Include and check a signed timestamp (reject old events) and store processed event ids
Use HTTP instead of HTTPS
Respond with 500
A valid signature on an old message is still valid unless freshness is checked. Reject events outside a short window and deduplicate by event id.
A client receives 429 Too Many Requests with Retry-After: 30. What should a well-behaved client do?
Retry immediately in a tight loop
Switch to POST
Wait at least 30 seconds, then retry (with backoff), instead of hammering the API
Treat it as success
429 means you are being rate limited; Retry-After states when to try again. Immediate retries make the problem worse and may get the client blocked.
A GraphQL API always returns HTTP 200, even when a query fails, and puts problems in an errors array. Your monitoring only alerts on 5xx status codes. What is the risk?
Failures go unnoticed; you must inspect the errors field (and track it) in monitoring
None, 200 means success
GraphQL does not support errors
The server is using the wrong port
GraphQL reports field-level and partial errors in the response body, usually with status 200. Monitoring has to parse the body or use GraphQL-aware tooling.
A GraphQL query for 50 posts, each with its author, triggers 51 database queries. What is the standard solution?
Batching and caching per request with a DataLoader-style utility
Switching the API to SOAP
Turning on HTTP/3
Removing the author field from the schema
This is the N+1 problem. A DataLoader collects the author ids requested during one tick and fetches them in a single query.
You need a live notification feed where the server pushes events to browsers over plain HTTP, with automatic reconnection. Which technology fits best?
WebSockets
Long polling only
Server-Sent Events (SSE)
SOAP
SSE is purpose-built for one-way server-to-client streams, reuses HTTP, and EventSource reconnects automatically. WebSockets suit two-way interaction like chat or games.
A browser app stores its JWT in localStorage. What is the main security trade-off?
Tokens expire faster
Any injected script (XSS) can read and exfiltrate it
The token cannot be sent in headers
HTTPS no longer applies
localStorage is accessible to all scripts on the page. HttpOnly cookies are not readable by JavaScript but then need CSRF protection.
A client updates PATCH /users/42 with {"name":"Ada","isAdmin":true} and becomes an administrator, because the API binds the whole body to the model. What is this flaw called?
Cache poisoning
Replay attack
Clickjacking
Mass assignment
The server let the client set a field it should control. Use an allowlist of writable fields or a dedicated input object.
The OAuth 2.0 state parameter in the authorization request is mainly used to...
encrypt the access token
choose the scopes
prevent CSRF by tying the response to the request that started it
set the token lifetime
The client generates a random state, stores it, and checks that the same value comes back on the redirect. Otherwise an attacker could trick the app into completing a login the user never started.
In this token, which part would an attacker change to try to make themselves an admin, and what stops it from working?
Payload; the signature no longer matches, so verification fails
Header; the issuer's URL
Signature; the payload is encrypted
Payload; the header hides it
Claims live in the payload. Changing them invalidates the signature, which only the holder of the secret or private key can recompute, so the API rejects the token.
What is the purpose of step 1 (OPTIONS) in the diagram?
To download the data
To log the user in
To compress the body
To ask the API whether the real PUT request from this origin is allowed
The preflight asks permission using Origin, Access-Control-Request-Method and Access-Control-Request-Headers. Only if the response allows it does the browser send the actual request.