Skip to content

Latest commit

ย 

History

History
104 lines (85 loc) ยท 5.15 KB

[20210221]_cors.md

File metadata and controls

104 lines (85 loc) ยท 5.15 KB

CORS์— ๋Œ€ํ•œ ์ดํ•ด

  • Cross-Origin Resource Sharing
  • ๊ต์ฐจ ์ถœ์ฒ˜ ๋ฆฌ์†Œ์Šค ๊ณต์œ 

๐Ÿค“ ๊ฒฐ๋ก 

  • Origin์ด ๋‹ค๋ฅธ ์š”์ฒญ์„ ๋ณด์•ˆ์ƒ์˜ ์ด์œ ๋กœ ์ฐจ๋‹จ
  • ํ•˜์ง€๋งŒ ๋‹ค๋ฅธ Origin์œผ๋กœ๋ถ€ํ„ฐ์˜ ๋ฆฌ์†Œ์Šค ๊ณต์œ ๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค.
  • ๊ทธ๋ž˜์„œ CORS๊ฐ€ ์กด์žฌ.
  • CORS ํ‘œ์ค€์„ ์ง€ํ‚จ ์š”์ฒญ์œผ๋กœ ๋‹ค๋ฅธ Origin์ด๋”๋ผ๋„ ์š”์ฒญ/์‘๋‹ต์ด ๊ฐ€๋Šฅํ† ๋ก ํ•จ.

๐Ÿ‘‰ Origin

URI = scheme:[//authority]path[?query][#fragment]

url

  • Protocol + Hostname (+ Port)

๐Ÿ‘‰ Same-origin policy

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

๐Ÿ‘‰ ๋ฐฉ๋ฒ• 3๊ฐ€์ง€

1. Preflight Request

  • ๋ณธ ์š”์ฒญ์„ ๋ณด๋‚ด๊ธฐ ์ „ ์š”์ฒญ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ์œ„ํ•œ ์˜ˆ๋น„ ์š”์ฒญ

Preflight Request

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์— ๋‚ด๋ ค์˜ค๋Š๋ƒ ๋‹ค.

2. Simple Request

  • Preflight ์š”์ฒญ์„ ๋ณด๋‚ด์ง€ ์•Š๊ณ , ๋ณธ ์š”์ฒญ์„ ํ†ตํ•ด CORS ์ •์ฑ… ์ ํ•ฉ์„ฑ ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จ.

Simple Request

๋‹ค์Œ์„ ๋ชจ๋‘ ๋งŒ์กฑํ•˜๋Š” ์š”์ฒญ

  • ๋‹ค์Œ ์ค‘ ํ•˜๋‚˜์˜ 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

3. Credentialed Request

  • ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ธŒ๋ผ์šฐ์ €๋Š” XMLHttpRequest or Fetch ํ˜ธ์ถœ์— ์ฟ ํ‚ค ์ •๋ณด, ์ž๊ฒฉ ์ฆ๋ช…์„ ๋ณด๋‚ด์ง€ ์•Š์Œ.
  • ํŠน์ • ํ”Œ๋ž˜๊ทธ๋ฅผ ์„ค์ •ํ•˜์—ฌ ์ž๊ฒฉ ์ฆ๋ช…์„ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ์š”์ฒญ ๋ฐฉ์‹.

https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#requests_with_credentials

  • ์‘๋‹ต ํ—ค๋”์— ๋ฐ˜๋“œ์‹œ 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" ๋ณธ๋ž˜ ์ฃผ์†Œ์ด๊ธฐ ๋•Œ๋ฌธ์— 
์ž๊ฒฉ์ฆ๋ช… ์ธ์‹ ์ปจํ…์ธ ๋Š” ์›น ํ˜ธ์ถœ ์ปจํ…์ธ ๋กœ ๋ฆฌํ„ด๋ฉ๋‹ˆ๋‹ค.