- Cross-Origin Resource Sharing
- ๊ต์ฐจ ์ถ์ฒ ๋ฆฌ์์ค ๊ณต์
- Origin์ด ๋ค๋ฅธ ์์ฒญ์ ๋ณด์์์ ์ด์ ๋ก ์ฐจ๋จ
- ํ์ง๋ง ๋ค๋ฅธ Origin์ผ๋ก๋ถํฐ์ ๋ฆฌ์์ค ๊ณต์ ๊ฐ ํ์ํ ๊ฒฝ์ฐ๊ฐ ๋ง๋ค.
- ๊ทธ๋์ CORS๊ฐ ์กด์ฌ.
- CORS ํ์ค์ ์งํจ ์์ฒญ์ผ๋ก ๋ค๋ฅธ Origin์ด๋๋ผ๋ ์์ฒญ/์๋ต์ด ๊ฐ๋ฅํ ๋ก ํจ.
URI = scheme:[//authority]path[?query][#fragment]
Protocol
+Hostname
(+Port
)
The same-origin policy is a critical security mechanism that restricts
how a document or script loaded from one origin can interact with a resource
from another origin. It helps isolate potentially malicious documents,
reducing possible attack vectors.
http://store.company.com/dir/page.html
URL | Outcome | Reason |
---|---|---|
http://store.company.com/dir2/other.html | Same origin | Only the path differs |
http://store.company.com/dir/inner/another.html | Same origin | Only the path differs |
https://store.company.com/page.html | Failure | Different protocol |
http://store.company.com:81/dir/page.html | Failure | Different port (http:// is port 80 by default) |
http://news.company.com/dir/page.html | Failure | Different host |
- ๋ณธ ์์ฒญ์ ๋ณด๋ด๊ธฐ ์ ์์ฒญ ์ ํจ์ฑ ๊ฒ์ฌ๋ฅผ ์ํ ์๋น ์์ฒญ
Key | Description |
---|---|
Origin | Indicates where a fetch originates from. |
Access-Control-Request-Method | ๋ณธ ์์ฒญ์ด ์ด๋ฃจ์ด์ง ๋ ์ด๋ค HTTP method๊ฐ ์ฌ์ฉ๋ ์ง ์๋ฒ์ ์๋ ค์ฃผ๊ธฐ ์ํจ |
Access-Control-Request-Headers | ๋ณธ ์์ฒญ์ด ์ด๋ฃจ์ด์ง ๋ ์ด๋ค HTTP headers๊ฐ ์ฌ์ฉ๋ ์ง ์๋ฒ์ ์๋ ค์ฃผ๊ธฐ ์ํจ |
Access-Control-Allow-Origin | Indicates whether the response can be shared. |
Access-Control-Allow-Methods | resource์ ์์ธ์คํ ๋ ํ์ฉ๋๋ ๋ฉ์๋ |
Access-Control-Allow-Headers | ๋ณธ ์์ฒญ์ ์ฌ์ฉํ ์ ์๋ HTTP headers |
Access-Control-Max-Age | Indicates how long the results of a preflight request can be cached. |
- response 200๊ณผ CORS ์ ์ฑ
์ ํฉ์ฑ ์ฌ๋ถ๋ ๊ด๊ณ๊ฐ ์๋ค.
- response๋ฅผ ๋ฐ์ ํ ์๋ฐ ์ฌ๋ถ๋ฅผ ๊ฒฐ์ ํ๊ธฐ ๋๋ฌธ์ 200์ด๋๋ผ๋ ์ ์ฑ ์๋ฐ์ผ๋ก ์๋ฌ ์ฒ๋ฆฌ๋ ์ ์์.
- ๊ทธ๋ฌ๋ Preflight ์์ฒญ์ ์ค์ํ ๊ฒ์
์ ํจํ
Access-Control-Allow-Origin
๊ฐ response header์ ๋ด๋ ค์ค๋๋ ๋ค.
- Preflight ์์ฒญ์ ๋ณด๋ด์ง ์๊ณ , ๋ณธ ์์ฒญ์ ํตํด CORS ์ ์ฑ ์ ํฉ์ฑ ์ฌ๋ถ๋ฅผ ํ๋จ.
- ๋ค์ ์ค ํ๋์ methods
- GET
- HEAD
- POST
- Headers
- Accept
- Accept-Language
- Content-Language
- Content-Type
- application/x-www-form-urlencoded
- multipart/form-data
- text/plain
- DPR
- Downlink
- Save-Data
- Viewport-Width
- Width
- ๊ธฐ๋ณธ์ ์ผ๋ก ๋ธ๋ผ์ฐ์ ๋ XMLHttpRequest or Fetch ํธ์ถ์ ์ฟ ํค ์ ๋ณด, ์๊ฒฉ ์ฆ๋ช ์ ๋ณด๋ด์ง ์์.
- ํน์ ํ๋๊ทธ๋ฅผ ์ค์ ํ์ฌ ์๊ฒฉ ์ฆ๋ช ์ ๋ณด๋ผ ์ ์๊ฒ ํด์ฃผ๋ ์์ฒญ ๋ฐฉ์.
- ์๋ต ํค๋์ ๋ฐ๋์
Access-Control-Allow-Credentials: true
๊ฐ ์์ด์ผ ํจ. - ๊ทธ๋ฆฌ๊ณ Access-Control-Allow-Origin header ์ "*" ์์ผ๋์นด๋ ๋ง๊ณ ๋ฐ๋์ ๊ฐ ์ง์ ํด์ผ ํจ.
์๊ฒฉ์ฆ๋ช
์์ฒญ ๋ฐ ์์ผ๋์นด๋(Credentialed requests and wildcards)
credentialed request ์ ์๋ตํ ๋ ์๋ฒ๋ Access-Control-Allow-Origin ํค๋
"*" ์์ผ๋์นด๋๋ฅผ ์ฌ์ฉํ๋ ๋์ ์ ๋ฐ๋์ ์ ๊ฐ์ ์ง์ ํด์ผ ํฉ๋๋ค.
์ ์์ ์ ์์ฒญ ํค๋์ Cookie ํค๋๊ฐ ํฌํจ๋์ด ์๊ธฐ ๋๋ฌธ์ Access-Control-Allow-Origin
ํค๋์ ๊ฐ์ด "*"์ธ ๊ฒฝ์ฐ ์์ฒญ์ด ์คํจํฉ๋๋ค. ์ ์์ฒญ์ Access-Control-Allow-Origin
ํค๋๊ฐ "*" ์์ผ๋ ์นด๋๊ฐ ์๋๋ผ "http://foo.example" ๋ณธ๋ ์ฃผ์์ด๊ธฐ ๋๋ฌธ์
์๊ฒฉ์ฆ๋ช
์ธ์ ์ปจํ
์ธ ๋ ์น ํธ์ถ ์ปจํ
์ธ ๋ก ๋ฆฌํด๋ฉ๋๋ค.