Web Platform CORS interview questions: preflight, credentials, and origin checks
Reviewed by Mark Dickie · Last updated
4 questions on this page you can answer and see marked on the spot. Go to the first one
Cross-Origin Resource Sharing (CORS) is the browser mechanism that decides whether JavaScript on one origin may read a response from another origin. Interviewers use it to test whether you can tell a browser-enforced read restriction from a server-side access check. Expect questions on simple versus preflighted requests, why credentialed requests reject a wildcard origin, and how a careless origin reflection turns into a data leak.
Which CORS topics do interviewers test?
| Header | What it controls | Where candidates slip |
|---|---|---|
| Access-Control-Allow-Origin | The one origin, or *, that the browser may expose the response to | Echoing the request Origin header back without an allowlist |
| Access-Control-Allow-Credentials | Whether cookies or HTTP auth may travel with the cross-origin request | Pairing it with *, which the browser refuses |
| Access-Control-Allow-Methods and Access-Control-Allow-Headers | Which methods and non-simple headers a preflight approves | Forgetting Authorization or Content-Type: application/json in the preflight response |
| Access-Control-Max-Age | How long the browser may cache a preflight result | Assuming every browser caches for the same length of time |
| Vary: Origin | Tells caches that the response depends on the requesting origin | Omitting it, so a shared cache serves one origin's CORS headers to another |
How do you practise CORS by hand?
- Explain why a GET with no custom headers skips the preflight, and what changes when you add
Content-Type: application/json. - Send a PUT from a page on
localhost:3000to an API onlocalhost:4000, then write the exact OPTIONS response that lets it through. - Set
Access-Control-Allow-Originto*andAccess-Control-Allow-Credentialstotrue, then name the error the browser logs and the fix. - Write a handler that echoes any Origin header, then describe what a malicious page could do to a logged-in user of that API.
Key facts
- Tarmac has 12 Web Platform interview questions on this topic, 10 of them on this page, at difficulty 1–5 of 5.
- Tarmac tracked 748 job postings asking for Web Platform in November 2026.
- Roles asking for Web Platform advertise a median base salary of US$187,116, across 152 job postings as of November 2026.
- Tarmac last reviewed these Web Platform interview questions on 16 November 2026.
At a glance
| Questions | 10 shown · 12 in the bank |
|---|---|
| Difficulty | 1–5 of 5 |
| Formats | Multiple answer, Code output, True / false, Find the bug, Flashcard, Fill in the blank, Multiple choice, Short answer, Ordering, Coding exercise |
| Interactive | 1 run your code against tests, in the app |
What you'll review
- cors
Practice questions
Try one before you open the answer. Pick an option and press Check; it's marked on the spot.
A page at https://example.com/app uses fetch() to call each URL below. Under the browser's same-origin policy, which of these requests are cross-origin, so CORS rules apply? Select all that apply.#
Options
Pick every one that applies.
Show answer
A request is cross-origin when its scheme, host or port differs from the page's origin. So from https://example.com, these are cross-origin: http://example.com (different scheme), api.example.com (different host) and port 8443 (different port). A different path doesn't count, and neither does writing out the default port 443, so both of those stay same-origin.
An origin is made of three parts: scheme, host and port. If any one of them differs, the request is cross-origin. http://example.com/app uses a different scheme (http instead of https). https://api.example.com/app uses a different host, because a subdomain counts as a different host. https://example.com:8443/app uses a different port. https://example.com/other/page only changes the path, and the path is not part of the origin. https://example.com:443/data writes out port 443, which is already the default port for https, so the origin is the same.
A page is served from https://app.example.com/home. The snippet uses the standard URL API to decide which fetch targets count as cross-origin (and so are subject to CORS). What does it print? Give one result per line.#
const page = new URL("https://app.example.com/home");
const targets = [
"https://app.example.com/api/data",
"http://app.example.com/api/data",
"https://api.example.com/data",
"https://app.example.com:8443/data"
];
for (const t of targets) {
console.log(new URL(t).origin === page.origin ? "same-origin" : "cross-origin");
}Show answer
same-origin
cross-origin
cross-origin
cross-origin
An origin is made of three parts: scheme, host and port. The page's origin is https://app.example.com, with the default port 443. The first target only has a different path, so it is same-origin. The second uses the http scheme instead of https, so it is cross-origin. The third uses the host api.example.com, and a different subdomain counts as a different origin. The fourth uses port 8443 instead of the default 443, so it is also cross-origin. The browser applies CORS checks to the last three requests.
A server response that includes both Access-Control-Allow-Origin: * and Access-Control-Allow-Credentials: true lets the browser expose that response to a cross-origin fetch made with credentials: 'include'.#
Options
Show answer
A wildcard Access-Control-Allow-Origin: * does not work for credentialed CORS requests, even when Access-Control-Allow-Credentials: true is also sent. When cookies or HTTP auth are included, the server has to echo the exact requesting origin, or the browser blocks the response. This rule stops authenticated data from leaking to any website.
The Fetch/CORS spec says the wildcard * in Access-Control-Allow-Origin does not count for credentialed requests (ones that send cookies or HTTP auth). If the request uses credentials: 'include', the server has to echo the exact requesting origin, for example Access-Control-Allow-Origin: https://app.example.com, and also send Access-Control-Allow-Credentials: true. If it sends the wildcard instead, the CORS check fails and the browser blocks script access to the response, so the statement is false. This rule stops a server from accidentally exposing authenticated data to every origin on the web.
A frontend at https://app.example.com calls fetch('https://api.example.com/api/profile', { credentials: 'include' }) so the session cookie gets sent. The request reaches the Express server below, but the browser blocks the response with a CORS error. Which line causes the failure?#
const express = require('express');
const app = express();
app.use((req, res, next) => {
res.set('Access-Control-Allow-Origin', '*');
res.set('Access-Control-Allow-Credentials', 'true');
next();
});
app.get('/api/profile', (req, res) => {
res.json({ user: req.cookies?.session ? 'alice' : null });
});
app.listen(3000);Show answer
The bug is on line 5.
When a request is made with credentials (credentials: 'include'), the CORS spec does not allow the wildcard * in Access-Control-Allow-Origin. The response has to name the exact requesting origin, such as https://app.example.com, and also send Access-Control-Allow-Credentials: true. Line 6 is correct and required. The problem is line 5: because it sends *, the browser rejects the response even though the server returned 200. To fix it, check req.headers.origin against an allowlist, echo back that specific origin, and add Vary: Origin so caches don't serve one origin's response to another.
A page at https://app.example.com calls fetch('https://api.example.com/me', { credentials: 'include' }). The server responds with Access-Control-Allow-Origin: *. Why does the browser block the response, and what must the server send instead?#
Show answer
When a request includes credentials (cookies, HTTP auth), the browser refuses the wildcard * in Access-Control-Allow-Origin. The server must echo the exact origin, Access-Control-Allow-Origin: https://app.example.com, and also send Access-Control-Allow-Credentials: true. It should add Vary: Origin so caches don't serve one origin's response to another. The same no-wildcard rule applies to Access-Control-Allow-Headers and Access-Control-Allow-Methods on credentialed requests, where * is treated as a literal string rather than a wildcard.
The Fetch spec forbids wildcard CORS responses for credentialed requests. Without this rule, any site could read a user's authenticated data. So the server has to name the allowed origin exactly and opt in to credentials explicitly. Without both headers, the browser hides the response from JavaScript, even though the request may still reach the server.
A page on https://app.example calls fetch('https://api.example/items'). The API responds with a correct Access-Control-Allow-Origin header and also sends a custom X-Request-Id header. However, response.headers.get('X-Request-Id') returns null in the page's JavaScript. That's because cross-origin scripts can only read CORS-safelisted response headers unless the server lists extra headers in the _____ response header. Separately, the API wants browsers to cache its preflight results for 10 minutes. To do that, it should send _____: 600 on its OPTIONS responses.#
Show answer
A page on https://app.example calls fetch('https://api.example/items'). The API responds with a correct Access-Control-Allow-Origin header and also sends a custom X-Request-Id header. However, response.headers.get('X-Request-Id') returns null in the page's JavaScript. That's because cross-origin scripts can only read CORS-safelisted response headers unless the server lists extra headers in the **Access-Control-Expose-Headers** response header. Separately, the API wants browsers to cache its preflight results for 10 minutes. To do that, it should send **Access-Control-Max-Age**: 600 on its OPTIONS responses.
The browser only gives a cross-origin script the CORS-safelisted response headers: Cache-Control, Content-Language, Content-Length, Content-Type, Expires, Last-Modified and Pragma. Any other header, like X-Request-Id, is hidden unless the server names it in Access-Control-Expose-Headers. Preflight caching is a separate setting. Access-Control-Max-Age gives a number of seconds, so 600 means 10 minutes. During that time the browser can reuse the preflight result instead of sending a new OPTIONS request. Browsers may cap this value, for example Chromium allows at most 2 hours.
Before sending a cross-origin PUT request with a Content-Type: application/json header, the browser first sends which request?#
Options
Show answer
The browser first sends an automatic OPTIONS preflight request. A cross-origin PUT carrying Content-Type: application/json is not a CORS simple request, so the browser checks permission before sending it. The server must answer the preflight with Access-Control-Allow-Origin, -Allow-Methods, and -Allow-Headers permitting the call before the real PUT goes out.
A PUT with Content-Type: application/json is not a CORS "simple request", so the browser sends an automatic OPTIONS preflight first. The server must answer with Access-Control-Allow-Origin, -Allow-Methods, and -Allow-Headers permitting the call before the browser will send the actual PUT.
What problem does CORS solve, and how does a server opt in to allowing a cross-origin request?#
Show answer
By default the same-origin policy stops a page's JavaScript from reading responses from a different origin. CORS lets a server relax that: it returns an Access-Control-Allow-Origin header naming the allowed origin (and, for non-simple requests, answers a preflight OPTIONS with the allowed methods and headers). The browser enforces these headers; CORS is not a server-side authorization mechanism.
CORS is a controlled relaxation of the same-origin policy. The browser is the enforcer — it blocks the script from reading the response unless the server's Access-Control-Allow-* headers permit it. It governs read access from the page, not whether the request reaches the server.
A page on https://app.example calls fetch('https://api.example/items', {method: 'PUT', credentials: 'include', headers: {'Content-Type': 'application/json', 'X-Trace': '1'}}). The browser's CORS-preflight cache has no matching entry, and the server allows the request. Following the WHATWG Fetch Standard, put the browser's steps in order.#
Put these in order
Show answer
A credentialed, non-simple CORS request goes in this order: send the OPTIONS preflight without cookies, CORS-check its response (exact origin plus Allow-Credentials: true), check the allowed methods and headers, cache the result for Max-Age, send the real request with cookies, then CORS-check that response and show JavaScript only safelisted headers and those in Access-Control-Expose-Headers.
The Fetch Standard's CORS-preflight fetch sends an OPTIONS request that never includes credentials. Next it runs the CORS check on that response. Because the request uses credentials, the allowed origin must be the exact origin rather than *, and Allow-Credentials must be true. Only after that check passes does the browser read the allowed methods and headers and confirm they cover PUT, content-type, and x-trace. Cache entries are created only after this validation succeeds, using Max-Age capped by the browser's limit. Then the main PUT is sent with cookies. Its response goes through its own CORS check, and that check also needs a matching origin and Allow-Credentials. Finally, JavaScript can read only the safelisted headers plus those listed in Expose-Headers.
Implement needsPreflight(method, headers). It returns true when a cross-origin fetch() in mode: 'cors' with these author-set request headers would make the browser send a CORS preflight, and false otherwise. Follow the WHATWG Fetch rules for CORS-safelisted methods and request-headers:#
Starter code
function needsPreflight(method, headers) {
// TODO: check method and each header against the CORS safelist
return false;
}Your solution must pass
- plain GET
- PUT method
- JSON content-type
This one is written and run, not read. Solve it in the app and your code is executed against these tests and the hidden ones.
Sources
The official documentation these questions are checked against:
Related interview questions
Job market
See web-platform salaries and hiring demand from live job postings.
The other 2 questions
This page shows 10 and marks what you pick. That's as far as a page can go. A free account opens the other 2 and keeps every answer. What you miss comes back until it's right: after a day, then at longer gaps.
Free · the whole bank · 100 answers per 30 days, each marked with written feedback