Web Development Pro | Lesson 048 | Security: Origins, Cookies, and Requests
Web Development Pro · Lesson 48 of 60
Module 5: Quality, accessibility, and security
In this lesson
Prerequisites and learning outcomes
Complete lesson 47 first. This lesson builds on https, same-origin rules, cors, and csrf. By the end, you can explain browser security boundaries without confusing CORS with authentication. Plan 10–15 minutes for the core explanation and additional time for the worked practice.
Begin with the origin tuple
An origin normally consists of scheme, host, and effective port. A page at https://learning.example and one at https://learning.example:443 have the same origin because 443 is the default HTTPS port. Changing to HTTP, another host, or a different port changes the origin. A path change alone does not.
This distinction matters even during local development. A page at http://127.0.0.1:8000 and a data service at http://127.0.0.1:8001 have different origins. localhost and 127.0.0.1 are also different host strings. Do not diagnose every local fetch failure as a JavaScript syntax problem without inspecting the actual URLs.
Separate origin, site, and identity
“Same-site” is a different concept used in cookie policies and other browser features. It broadly considers the scheme and registrable domain rather than the full origin tuple. Two subdomains may be same-site while still being cross-origin. Do not substitute one term for the other when configuring a security boundary.
Neither origin nor site establishes a user's identity. A response from the expected origin can still contain unauthorized data if the server makes a bad decision. Authentication and authorization remain server responsibilities for protected operations. CORS does not replace them, and hiding a button does not enforce them.
The example classifies fixed reserved-domain URLs with the platform URL parser. It makes no network requests and sets no cookies. Running its tests teaches origin comparison without contacting an external service or creating an account.
Understand what CORS permits
The same-origin policy limits certain cross-origin interactions, including script access to responses. CORS is a response-header mechanism through which a server can allow appropriate cross-origin reading. Some requests trigger a preflight before the actual request; others can be sent without that preflight.
A CORS error therefore does not always mean no request reached the server. A simple form-like request may be sent while the browser withholds the response from the calling script. That distinction is crucial for understanding why a server must protect state-changing operations independently.
export function sameOrigin(first, second) {
return new URL(first).origin === new URL(second).origin;
}
This small helper compares parsed origins for ordinary HTTP(S) examples. It is not a security framework for every URL scheme. In particular, some non-HTTP contexts have opaque origins. The lesson's complete test fixtures stay within its documented HTTP(S) contract.
Avoid misleading client-side workarounds
Setting mode:no-cors is not a general solution when your application needs to read JSON from another origin. The resulting opaque response is not a readable data response for the calling script. Adding a response header name such as Access-Control-Allow-Origin to the client request also does not grant the server's permission.
When a service is intended for your page, the service owner must configure the correct response policy and authorization. For a beginner static project, same-origin local JSON is often a simpler choice. Keep the page and its data under the same local server rather than inventing an unnecessary cross-origin dependency.
Do not disable browser security, install an unknown extension, or use an untrusted public proxy to bypass the failure. Those approaches can expose data and hide the actual configuration problem. Record the request URL, safe error details, and intended origin relationship for whoever controls the service.
Explain cookie attributes accurately
Cookies are attached to eligible requests according to their scope and browser rules. Secure restricts transmission to secure contexts, with browser-specific local exceptions; deploy real services over HTTPS. HttpOnly prevents script access through document.cookie, but it does not stop the browser from sending that cookie with an eligible request.
SameSite controls some cross-site cookie sending behavior. Strict, Lax, and None have different effects, and SameSite=None requires Secure in modern browsers. These settings help reduce certain risks but are not a universal replacement for server-side CSRF protection. Domain, Path, expiry, browser policy, and the request context also matter.
Cookie attributes are configured by the server for a real authenticated application. This static demo does not create a pretend login or suggest storing session secrets in localStorage. A token readable by page script can be exposed if that script is compromised; storage choice belongs in a reviewed authentication design.
Recognize cross-site request forgery
CSRF involves causing a user's browser to send an unwanted request using credentials the browser supplies automatically. A protected server needs to ensure that a state-changing request reflects authorized intent, not merely that a valid session cookie is present. Common defenses include framework-provided anti-CSRF mechanisms and appropriate origin checks.
Do not mutate server state through a simple navigation GET. Treat changes such as updating an address or registering for a paid event as explicit protected operations. Method choice alone is not complete protection, but clear semantics are an important part of a coherent design.
For a credentialed CORS response, a wildcard allowed origin is not a substitute for an explicitly permitted origin. Configuration must match the intended callers and credential behavior. Details belong with the server owner and framework documentation; this lesson's browser-only course stops before implementing an authenticated backend.
Keep transport guarantees in perspective
HTTPS protects communication in transit under the web's certificate trust model. It does not validate your event schema, make third-party JavaScript trustworthy, or ensure that the endpoint has good authorization. Mixed-content restrictions can also affect pages that try to load insecure resources from a secure page.
A release checklist should therefore distinguish HTTPS delivery, allowed origins, cookie policy, authorization, and CSRF defenses. Our local tests verify only URL-origin calculations and explicit classification examples. Browser enforcement, live headers, credential behavior, and production security remain untested and outside this static demo's verified scope.
Worked practice
Worked exercise 1: Compare local addresses
Task: The page uses port 8000 and JSON uses port 8001 on the same host. Worked answer: these are different origins because the port differs. Prefer serving this course's local JSON beside the page, or ask the service owner to configure the intended cross-origin access correctly.
Worked exercise 2: Interpret a CORS failure
Task: A server log shows a request even though the browser reports CORS failure. Worked answer: this is possible because sending a request and exposing its response to script are different decisions. Investigate the request type and response policy; do not assume CORS prevented every side effect.
Worked exercise 3: Correct an HttpOnly claim
Task: Someone says HttpOnly prevents all session misuse. Worked answer: it prevents direct cookie reading through document.cookie, but eligible requests may still carry the cookie. XSS can still cause harmful actions, and CSRF defenses and server authorization remain necessary.
Knowledge check
Question 1
Which change keeps the same origin? A) HTTPS to HTTP. B) Port 443 to 8443. C) /events.html to /access.html on the same HTTPS host and port. D) One subdomain to another.
Check question 1: answer and explanation
Answer: C. Path is not part of the origin tuple.
Question 2
What does CORS principally govern here? A) Whether permitted cross-origin responses are exposed to script. B) Whether every state-changing request reflects the user’s intent. C) Whether a session grants permission to modify a record. D) Whether every sent request is prevented from affecting server state.
Check question 2: answer and explanation
Answer: A. Authentication and data validation are separate responsibilities.
Question 3
Why is no-cors not a JSON-reading fix? A) It changes the browser request into a same-origin one. B) Its opaque response is not readable as the needed response data. C) It adds the required permission header to the server response. D) It exposes an otherwise blocked response when credentials are omitted.
Check question 3: answer and explanation
Answer: B. It changes request handling without granting ordinary response access.
Question 4
What does HttpOnly restrict? A) Every request carrying the cookie. B) All cross-site navigation. C) Server-side use of the cookie. D) Script access through document.cookie.
Check question 4: answer and explanation
Answer: D. It does not remove the cookie from all eligible requests.
Question 5
What is a suitable conclusion from the supplied tests? A) Live cookies are configured securely. B) All CSRF attacks are prevented. C) The fixed HTTP(S) origin comparisons behave as expected. D) The server's CORS policy passed.
Check question 5: answer and explanation
Answer: C. No server requests or browser enforcement were tested.