Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / Security Fundamentals

    The Same-Origin Policy, CORS and CSRF

    An origin is the combination of scheme + host + port (05networking3web.md Ch 1). https://studybuddy.app and

    Chapter 9 of 136 min readOften asked in interviews

    The Plain Picture#

    Your browser is a building with many tenants — every website you open is a tenant. The building rule: a tenant may send letters to anyone, but may only open mail addressed to itself. A shady website you visit can't read your StudyBuddy data, even though both are open in the same browser.

    But notice the loophole: sending is allowed. A shady site can still send a letter to StudyBuddy in your name, and the browser will helpfully attach your StudyBuddy cookie.

    The Real Names#

    • An origin is the combination of scheme + host + port (Networking 3: DNS, HTTP and HTTPS Ch 1). https://studybuddy.app and https://api.studybuddy.app are different origins; so are http:// and https:// versions of the same site.

    • The same-origin policy (SOP) — the browser's rule that JavaScript from one origin can't read responses from another origin.

    • CORS (Cross-Origin Resource Sharing) — a way for a server to relax the SOP on purpose, by sending headers saying which other origins may read its responses.

    • CSRF (Cross-Site Request Forgery) — the loophole: a malicious site makes your browser send a request to another site where you're logged in, with your cookies attached, to perform an action.

    Under the Hood#

    CORS#

    StudyBuddy's frontend runs at https://studybuddy.app; its API at https://api.studybuddy.app — different origins. For the frontend's JavaScript to read API responses, the API must reply with:

    Output
    Access-Control-Allow-Origin: https://studybuddy.app
    Access-Control-Allow-Credentials: true         (only if cookies are involved)

    For "non-simple" requests (e.g. JSON bodies, PUT/DELETE, custom headers like Authorization), the browser first sends a preflight request — an OPTIONS request (Networking 3: DNS, HTTP and HTTPS Ch 4) asking "may this origin send this method with these headers?". Only if the server's answer allows it does the real request go.

      Browser (on studybuddy.app)                        api.studybuddy.app
        │ ── OPTIONS /notes                                   │
        │    Origin: https://studybuddy.app                   │
        │    Access-Control-Request-Method: PUT  ───────────▶ │
        │ ◀── 204  Access-Control-Allow-Origin: https://studybuddy.app
        │          Access-Control-Allow-Methods: GET, POST, PUT, DELETE
        │ ── PUT /notes/42  (the real request) ─────────────▶ │

    Three truths about CORS that interviews test:

    1. CORS is enforced by the browser, not the server. curl, Postman, mobile apps and other servers ignore it entirely. CORS does not protect your API from attackers — authentication and authorization do.

    2. CORS relaxes security; it doesn't add it. The SOP is the protection; CORS headers are holes you punch in it.

    3. Access-Control-Allow-Origin: * with credentials is refused by browsers — and reflecting back whatever Origin arrives is as bad as *. Allow-list your real frontend origins.

    CSRF#

    Priya is logged into StudyBuddy. She visits evil.example, which contains:

    HTML
    <form action="https://studybuddy.app/account/delete" method="POST" id="f"></form>
    <script>document.getElementById('f').submit()</script>

    Her browser sends the POST with her StudyBuddy session cookie, because it's addressed to StudyBuddy. The server sees a valid session and deletes her account. The SOP didn't help: the attacker never needed to read the response.

    Defences:

    1. SameSite=Lax or Strict cookies (Networking 3: DNS, HTTP and HTTPS Ch 7) — the browser doesn't attach the cookie to cross-site POSTs. Modern browsers treat cookies without a SameSite setting as Lax. This alone blocks most CSRF.

    2. CSRF tokens — a random value embedded in the site's own forms and checked by the server; a foreign site can't read it (SOP), so can't include it.

    3. Never change state on GET (Networking 3: DNS, HTTP and HTTPS Ch 4).

    4. APIs using Authorization: Bearer tokens instead of cookies aren't vulnerable to CSRF — browsers don't attach those headers automatically. (They must protect against XSS stealing the token instead — every choice has a trade-off.)

    Check yourself

    5 questions
    1

    What makes two URLs the same origin?

    Show answer

    Same scheme, host and port.

    2

    What does the same-origin policy block, and what does it still allow?

    Show answer

    It blocks JavaScript from reading cross-origin responses; it still allows sending many cross-origin requests (forms, images, simple requests).

    3

    Does CORS protect your API from a hacker using curl? Why?

    Show answer

    No — CORS is enforced only by browsers; curl ignores it. Authentication/authorization protect the API.

    4

    Explain a CSRF attack in three steps, and name two defences.

    Show answer

    The user is logged into site A → visits malicious site B → B makes the browser send a state-changing request to A, with A's cookies attached. Defences: SameSite cookies, CSRF tokens (and no state changes on GET).

    5

    What is a preflight request?

    Show answer

    An automatic OPTIONS request the browser sends first to ask the server whether the real cross-origin request is allowed.