SOP, CORS
1. SOP (Same-Origin Policy)
Same-origin policy that only allows sharing resources from the same origin
This origin consists of a combination of protocol, host, and port, and if any one of these is different, it's not considered the same origin
Separates potentially harmful documents and reduces attack paths
2. CORS (Cross-Origin Resource Sharing)
Cross-origin resource sharing, uses additional HTTP headers and tells the browser to grant permission for web applications running in one origin to access selected resources from other origins
In other words, browsers block sharing of resources from other origins by default due to SOP, but using CORS allows access to resources
3. How CORS Works
(1) Preflight Request
Before sending actual request, sends OPTIONS method pre-request to check if there's permission to access resources from that origin
Can check permissions in advance before sending actual request, making it efficient
Can protect servers not prepared for CORS, considering that servers made before CORS only receive SOP requests
But even servers not prepared for CORS will show CORS error when sending preflight request (β CORS basics)
(2) Simple Request
Sends request after omitting preflight request if certain conditions are met
Conditions:
Must be one of
GET,HEAD,POSTrequestsCan only manually set values of
Accept,Accept-Language,Content-Language,Content-Typeheaders other than automatically set headersContent-Typeheader only allowsapplication/x-www-form-urlencoded,multipart/form-data,text/plainvalues
(3) Credentialed Request
Request that includes authentication information in request headers
If origins are different, cookies cannot be sent without separate settings
In this case, CORS settings are needed on both frontend and server sides:
Frontend side must put
withCredentials: truein request headersServer side must put
Access-Control-Allow-Credentials: truein response headersWhen setting
Access-Control-Allow-Originon server side, error occurs if set with wildcard (*) meaning allow all origins. Since dealing with authentication information, origins must be set accurately.
4. CORS Configuration Methods
(1) Node.js Server
(2) Express Server
Last updated