CORS and CSRF ============= *CORS* and *CSRF* are the two browser-side protections an open-internet cam needs alongside HTTPS and login. Each takes a few lines to set up. The sections below define the term and show the microdot integration. What CORS does -------------- *Cross-Origin Resource Sharing* (CORS) is the browser mechanism that lets a server opt in to letting specific other origins read its responses. The browser's default *same-origin policy* blocks that read: JavaScript on ``https://example.com`` cannot read responses from ``https://yard-cam.example.com``, because different host counts as different origin. CORS is the server-side way to grant exceptions for chosen peers. If the dashboard is served from the cam itself, every request is same-origin and CORS isn't doing anything. The setup matters when the dashboard lives somewhere else -- a public URL like ``https://app.example.com`` that talks to a cam at ``https://yard-cam.example.com``: .. code-block:: python from microdot.cors import CORS cors = CORS( app, allowed_origins=['https://app.example.com'], allow_credentials=True, max_age=86400, ) ``allowed_origins`` is the list of origins that are permitted to read the cam's responses. The dashboard's origin and *only* the dashboard's origin -- not ``*`` -- so a third-party site can't read the cam's responses by accident. ``allow_credentials=True`` lets cross-origin requests include the session cookie, which is what the dashboard needs to stay logged in across an origin boundary. ``max_age=86400`` tells the browser it can cache the preflight result for a day. Browsers fire an extra ``OPTIONS`` request before any cross-origin call that uses methods other than GET/HEAD/POST or sends custom headers; ``max_age`` cuts that overhead to one preflight per day per route. What CSRF does -------------- *Cross-Site Request Forgery* (CSRF) is the attack where a malicious page makes the user's browser fire an authenticated request at a trusted server. Even with CORS in place, a hidden ``