Start learning free

LESSON 39 OF 56 · SECURITY: SECURE THE WEB APP

CORS with Cookies: Don't Echo the Origin in Python

Practice

When a page on another site calls your API with cookies, the browser sends the visitor's shop cookie along. Your answer decides whether that page's script may read the result: Access-Control-Allow-Origin (Allow-Origin, for short) must name the page's origin, and Access-Control-Allow-Credentials must be true. With cookies, * is refused, so the starting code copies whatever Origin header arrives back into Allow-Origin.

The attack: Ana is signed in to the shop and opens a page on https://evil.example. Its script fetches /orders with her cookie, the API answers with Access-Control-Allow-Origin: https://evil.example, and the attacker reads Ana's order list.

The fix is an allow-list of the shop's own origins, compared as whole strings. Checking only how the origin begins lets a look-alike in:

>>> origin = "https://shop.example.com.evil.example"
>>> origin.startswith("https://shop.example.com")
True
>>> origin in ["https://shop.example.com"]
False

Origins on the list still get their own origin back, with credentials. Any other origin gets the answer with no CORS headers: the API still answers 200, but the browser keeps the result from that site's script. With Ana signed in, this lesson's requests answer:

OPTIONS /login  Origin: https://shop.example.com  →  200  Allow-Origin: https://shop.example.com, Allow-Credentials: true, Allow-Headers: Content-Type
POST /login     Origin: https://shop.example.com  →  200  {"email": "ana@example.com"}, Allow-Origin: https://shop.example.com, Allow-Credentials: true
GET /orders     Origin: https://shop.example.com        →  200  [{"id": 1, …}, {"id": 3, …}], Allow-Origin: https://shop.example.com, Allow-Credentials: true
GET /orders     Origin: https://admin.shop.example.com  →  200  [{"id": 1, …}, {"id": 3, …}], Allow-Origin: https://admin.shop.example.com, Allow-Credentials: true
GET /orders     Origin: https://evil.example            →  200  [{"id": 1, …}, {"id": 3, …}], no CORS headers
GET /orders     Origin: https://shop.example.com.evil.example  →  200  [{"id": 1, …}, {"id": 3, …}], no CORS headers
GET /orders     Origin: null                            →  200  [{"id": 1, …}, {"id": 3, …}], no CORS headers
GET /orders     (no Origin)                             →  200  [{"id": 1, …}, {"id": 3, …}], no CORS headers

Your task: below USERS, add a list SHOP_ORIGINS with https://shop.example.com and https://admin.shop.example.com. In add_cors_headers, below the if origin is None block, return response untouched when origin is not in that list.


Assignment
Follow the Coding Tutorial to add the shop's allow-list and send the CORS headers only to the origins on it.


Hint
Write each origin exactly as the browser sends it: scheme and host, with no / at the end. not in on a list compares whole strings, so look-alikes and null don't match.


STEP 1 OF 3
    Ctrl + Enter to check

    Secx Cors Credentials in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free