Skip to content
Chapter 12Lesson 4

Quiz - URLs, origins, and security boundaries

Quiz progress

0 / 0

You build a search URL by hand: `?q=${encodeURIComponent(searchTerm)}`. Later, on the server, you parse it with new URLSearchParams(req.url.search). A user searches for the literal string a+b. What goes wrong?

Nothing — encodeURIComponent and URLSearchParams agree on every character, so the round trip is safe.
encodeURIComponent leaves the + untouched, and URLSearchParams decodes that + as a space — so the search term comes back as a b.
URLSearchParams throws because the query string was not built by a URLSearchParams instance.

Your base URL comes from a validated environment variable at boot, while a next redirect target comes from a query string a user controls. Which construction pattern fits each?

Use new URL() for the env-derived base and let it throw; guard the user-supplied next with URL.canParse() before parsing.
Wrap both in try/catch so neither can ever crash the request.
Use URL.canParse() for the base and new URL() for next, since user input is the one you want to fail fast on.

Classify the pair https://a.github.io and https://b.github.io on both axes.

Same site (they share github.io), different origin.
Different origin and different site — github.io is on the Public Suffix List as an eTLD, so the registrable domain is the whole a.github.io.
Same origin and same site — they only differ by a subdomain label.

A developer wires account deletion to GET /account/delete?id=42 and assumes the same-origin policy protects it from evil.com. Why is the account still deletable from an attacker’s page?

The same-origin policy gates the response read, not the request. The GET fires (e.g. from an <img>), the session cookie attaches, the delete runs — and no CORS error ever appears because the attacker never needed to read the response.
The same-origin policy only applies to fetch, so embedding the URL in an <img> bypasses it entirely as a loophole.
It is protected — the browser would block the cross-origin GET and show a CORS error, so the account is safe.

Which of these cross-origin fetch calls trigger a preflight OPTIONS request? Select all that apply.

A POST sending Content-Type: application/json.
A GET carrying an Authorization: Bearer … header.
A plain GET with no custom headers and no body.

Your client sends fetch(url, { credentials: 'include' }) and the server replies with Access-Control-Allow-Origin: * plus Access-Control-Allow-Credentials: true. The browser blocks the read. What is the fix?

Validate the incoming Origin against an allow-list, echo that exact origin back in Access-Control-Allow-Origin, and add Vary: Origin.
Change the client to credentials: 'same-origin' so the wildcard becomes legal again.
Add Access-Control-Max-Age so the browser caches the preflight and stops re-checking the wildcard.

A cross-origin call fails with the console error “Response to preflight request doesn’t pass access control check: It does not have HTTP ok status.” Where is the bug?

The OPTIONS preflight handler returned a non-2xx status — it should return 204 with the CORS headers.
The client fetch call set the wrong mode; switch it to 'no-cors'.
Access-Control-Allow-Origin is missing from the real request’s response — add it to the GET/POST handler.

Quiz complete

Score by topic