Information Security 10 min read

Understanding Same-Origin Policy and CORS in Web Development

This article explains the same‑origin policy, its security implications for DOM, web data and network communication, and how Cross‑Origin Resource Sharing (CORS) with simple and preflight requests enables controlled cross‑domain interactions while protecting users from attacks such as XSS, CSRF, and others.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Understanding Same-Origin Policy and CORS in Web Development

Recently the author encountered a high‑frequency front‑end interview question about POST requests during a ByteDance interview and decided to explain the underlying concepts, starting with the same‑origin policy.

The same‑origin policy is a core browser security mechanism that restricts how a document or its scripts can interact with resources from another origin; an origin is defined by identical protocol, host, and port. Without such restrictions, attacks like XSS, SQL injection, OS command injection, HTTP header injection, and CSRF could endanger user data.

Same‑origin enforcement manifests in three areas: DOM access, web‑data access (e.g., XMLHttpRequest or Fetch), and network communication. Browsers block cross‑origin script‑initiated HTTP requests unless the server explicitly permits them via CORS headers.

CORS (Cross‑Origin Resource Sharing) allows controlled cross‑origin requests. When a request is not a “simple request,” the browser first sends a preflight OPTIONS request containing Access-Control-Request-Method and Access-Control-Request-Headers . The server responds with headers such as Access-Control-Allow-Origin , Access-Control-Allow-Methods , and Access-Control-Allow-Headers to indicate which origins, methods, and headers are permitted.

Simple requests are those that use only GET, HEAD, or POST methods, include only a limited set of safe headers, and do not involve custom streams or event listeners. If these conditions are met, no preflight is performed.

When credentials (e.g., cookies) are included, the server must not use a wildcard (*) for Access-Control-Allow-Origin or other headers; it must specify the exact allowed origin to prevent credential leakage.

In summary, the preflight request is an automatic OPTIONS request issued by browsers during CORS interactions to ensure security; the server’s response determines whether the actual cross‑origin request can proceed, thereby protecting user data and privacy.

frontendCORSCross-OriginWeb Securitysame-origin
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.