๐Ÿ“Œ CORS


๋ฐฐ๊ฒฝ ์ง€์‹

SOP(Same Origin Policy)

  • ๋‹ค๋ฅธ ์ถœ์ฒ˜์˜ ๋ฆฌ์†Œ์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์— ์ œํ•œํ•˜๋Š” ๋ณด์•ˆ ๋ฐฉ์‹

์™œ SOP?

  • CSRF Attack ๋•Œ๋ฌธ!
  • ์‚ฌ์šฉ์ž๋Š” ์ธ์ฆ ํ† ํฐ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ƒํƒœ
  • ํ•ด์ปค๊ฐ€ ๋งŒ๋“  ์ฃผ์†Œ๋กœ ์ด๋™ํ•˜๋Š”๋ฐ, ์—ฌ๊ธฐ๋‹ค ํ•ด์ปค๋Š” ์•…์˜์ ์ธ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹ฌ์Œ
  • ํ•ด์ปค๋Š” ์ธ์ฆ ํ† ํฐ์„ ๊ฐ€์ง€๊ณ  ์•…์˜์ ์ธ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Œ
  • ๋งŒ์•ฝ ์†ก๊ธˆ์ด๋‚˜ ๋ฏผ๊ฐํ•œ ์ •๋ณด๋ฅผ ๊ฑด๋“œ๋ฆด ์‹œ ๊ต‰์žฅํžˆ ์œ„ํ—˜

์—ฌ๊ธฐ์„œ SOP๊ฐ€ ์œ„๋ ฅ์„ ๋ฐœํœ˜

  • ์›น์‚ฌ์ดํŠธ ์ž…์žฅ์—์„œ๋Š” ์ž์‹ ๊ณผ ๋‹ค๋ฅธ ์ถœ์ฒ˜๋ฅผ ๊ฐ€์ง€๊ธฐ ๋•Œ๋ฌธ์— ์œ„ ์š”์ฒญ์€ ๋ฐ›์„ ์ˆ˜ ์—†๋‹ค๊ณ  ํŒ๋‹จ

์ถœ์ฒ˜(Origin)๋ž€?

  • Protocol, Host, Port ์„ธ๊ฐ€์ง€๊ฐ€ ๊ฐ™์•„์•ผ ๊ฐ™์€ ์ถœ์ฒ˜๋ผ๊ณ  ํŒ๋‹จ
  • ์ฐธ๊ณ ๋กœ ์ธํ„ฐ๋„ท ์ต์Šคํ”Œ๋กœ์–ด๋Š” ์ถœ์ฒ˜ํŒ๋‹จ์— Port๊ฐ€ ํฌํ•จ๋˜์ง€ ์•Š์Œ

๐Ÿค” ๊ทธ๋Ÿฌ๋ฉด ๋‹ค๋ฅธ ์ถœ์ฒ˜ ๋ฆฌ์†Œ์Šค๊ฐ€ ํ•„์š”ํ•˜๋ฉด ์–ด๋–ป๊ฒŒํ•ด??

CORS!!


CORS๋ž€?

๊ต์ฐจ ์ถœ์ฒ˜ ๋ฆฌ์†Œ์Šค ๊ณต์œ (Cross-Origin Resource Sharing, CORS)๋Š” ์ถ”๊ฐ€ HTTP ํ—ค๋”๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ, ํ•œ ์ถœ์ฒ˜์—์„œ ์‹คํ–‰์ค‘์ธ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ๋‹ค๋ฅธ ์ถœ์ฒ˜์˜ ์„ ํƒํ•œ ์ž์›์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•˜๋„๋ก ๋ธŒ๋ผ์šฐ์ €์— ์•Œ๋ ค์ฃผ๋Š” ์ฒด์ œ์ž…๋‹ˆ๋‹ค. - MOZILLA

CORS ์ ‘๊ทผ์ œ์–ด ์‹œ๋‚˜๋ฆฌ์˜ค

๋‹จ์ˆœ ์š”์ฒญ (Simple Request)

  • Preflight ์š”์ฒญ ์—†์ด ๋ฐ”๋กœ ์š”์ฒญ

์กฐ๊ฑด์„ ๋ชจ๋‘ ๋งŒ์กฑํ•ด์•ผ ํ•จ

  • ๋ฉ”์„œ๋“œ๋Š” ์•„๋ž˜๋งŒ ํ—ˆ์šฉ
    • GET, POST, HEAD
  • Content-Type์€ ์•„๋ž˜๋งŒ ํ—ˆ์šฉ
    • application/x-www-form-urlencoded
    • multipart/form-data
    • test/plain
  • Header๋Š” ์•„๋ž˜๋งŒ ํ—ˆ์šฉ
    • Accept
    • Accept-Language
    • Content-Language
    • Content-Type

ํ”„๋ฆฌํ”Œ๋ผ์ดํŠธ ์š”์ฒญ (Preflight Request)

  • ์‰ฝ๊ฒŒ ๋งํ•˜์ž๋ฉด ์‚ฌ์ „ ํ™•์ธ ์ž‘์—…
  • OPTIONS ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ๋‹ค๋ฅธ ์ถœ์ฒ˜์˜ ๋ฆฌ์†Œ์Šค์— ์š”์ฒญ์ด ๊ฐ€๋Šฅํ•œ์ง€ ํ™•์ธํ•˜๋Š” ์ž‘์—…
  • ์š”์ฒญ์ด ๊ฐ€๋Šฅํ•˜๋ฉด ์‹ค์ œ ์š”์ฒญ(Actual Request)์„ ๋ณด๋ƒ„

  • ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์‚ฌ์ „ ์š”์ฒญ์„ ๋ณด๋ƒ„
  • ์„œ๋ฒ„๋Š” ์ด์— ์‘๋‹ต
  • ๋งŒ์•ฝ ์ด ์š”์ฒญ์ด ๊ฑฐ๋ถ€๋œ๋‹ค๋ฉด Actual Request๋Š” ๋ณด๋‚ด์ง€ ์•Š์Œ

Preflight Request ํฌ๋งท

  • Origin : ์š”์ฒญ ์ถœ์ฒ˜
  • Access-Control-Request-Method : ์‹ค์ œ ์š”์ฒญ์˜ ๋ฉ”์„œ๋“œ
  • Access-Control-Request-Headers : ์‹ค์ œ ์š”์ฒญ์˜ ์ถ”๊ฐ€ ํ—ค๋”

Preflight Response ํฌ๋งท

  • Access-Control-Allow-Origin : ์„œ๋ฒ„ ์ธก ํ—ˆ๊ฐ€ ์ถœ์ฒ˜
  • Access-Control-Allow-Methods : ์„œ๋ฒ„ ์ธก ํ—ˆ๊ฐ€ ๋ฉ”์„œ๋“œ
  • Access-Control-Allow-Headers : ์„œ๋ฒ„ ์ธก ํ—ˆ๊ฐ€ ํ—ค๋”
  • **Access-Control-Max-Age **: Preflight ์‘๋‹ต ์บ์‹œ ๊ธฐ๊ฐ„
    • ์‚ฌ์ „ ์š”์ฒญ๊ณผ ์‹ค์ œ ์š”์ฒญ์„ ๋งค๋ฒˆ ๋ณด๋‚ด๋ฉด ํšจ์œจ์ ์ด์ง€ ์•Š์œผ๋‹ˆ ๋ธŒ๋ผ์šฐ์ €๋Š” ์‚ฌ์ „ ์š”์ฒญ์„ ์บ์‹ฑํ•ด๋‘ 
Preflight Response ํŠน์ง•
  • ์‘๋‹ต ์ฝ”๋“œ๋Š” 200๋Œ€์—ฌ์•ผ ํ•จ
  • ์‘๋‹ต ๋ฐ”๋””๋Š” ๋น„์–ด์žˆ๋Š” ๊ฒƒ์ด ์ข‹์Œ

์™œ Preflight๊ฐ€ ํ•„์š”ํ• ๊นŒ?

  • ์‚ฌ์ „ ์š”์ฒญ์ด ์—†์œผ๋ฉด ์„œ๋ฒ„๋Š” ์š”์ฒญ์— ๋Œ€ํ•ด ๋ชจ๋“  ๋กœ์ง์„ ์ผ๋‹จ ์ˆ˜ํ–‰ํ•จ

  • ๊ทธ๋ฆฌ๊ณ  ๋‚˜์„œ Origin์„ ํŒ๋‹จ

  • ์ผ๋‹จ ๋กœ์ง์ด ์ˆ˜ํ–‰๋œ ํ›„ CORS ์ด์Šˆ๊ฐ€ ๋ฐœ์ƒ

  • ์‚ฌ์ „ ์š”์ฒญ์ด ์žˆ์œผ๋ฉด ์š”์ฒญ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์ „ Origin์„ ํŒ๋‹จ

  • ๋•Œ๋ฌธ์— ์„œ๋ฒ„๋Š” ์•ˆ์ „ํžˆ ์ž˜ ์ง€์ผœ์ง

์ธ์ฆ์ •๋ณด ํฌํ•จ ์š”์ฒญ (Credentialed Request)

  • ์ธ์ฆ ๊ด€๋ จ ํ—ค๋”๋ฅผ ํฌํ•จํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์š”์ฒญ
  • ํด๋ผ์ด์–ธํŠธ ์ธก
    • credentials : include
  • ์„œ๋ฒ„ ์ธก
    • Access-Control-Allow-Credentials : true
    • ๋Œ€์‹  ์ด๋Ÿฌ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์กฐ๊ฑด์ด ์ถฉ์กฑ๋˜์–ด์•ผ ํ•จ
์กฐ๊ฑด
  • Access-Control-Allow-Origin์—๋Š” *๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์œผ๋ฉฐ, ๋ช…์‹œ์ ์ธ URL์ด์–ด์•ผํ•จ
  • ์‘๋‹ต ํ—ค๋”์—๋Š” ๋ฐ˜๋“œ์‹œ Access-Control-Allow-Credentials: true๊ฐ€ ์กด์žฌํ•ด์•ผํ•จ
๐Ÿšจ Access to fetch at โ€™https://localhost:8080โ€™ from origin โ€™http://localhost:8000โ€™ has been blocked by CORS policy: The value of the โ€˜Access-Control-Allow-Originโ€™ header in the response must not be the wildcard โ€™*โ€™ when the requestโ€™s credentials mode is โ€˜includeโ€™.

CORS ํ•ด๊ฒฐํ•˜๊ธฐ!

ํ”„๋ก ํŠธ ํ”„๋ก์‹œ ์„œ๋ฒ„ ์„ค์ •(๊ฐœ๋ฐœ ํ™˜๊ฒฝ)

  • ๋ธŒ๋ผ์šฐ์ € ์ž…์žฅ์—์„œ๋Š” origin๊ณผ target์ด ๊ฐ™๊ธฐ ๋•Œ๋ฌธ์— same origin

์ถ”๊ฐ€

์Šคํ”„๋ง ๋ถ€ํŠธ ์ด์šฉ

@CrossOrigin ์–ด๋…ธํ…Œ์ด์…˜ ์‚ฌ์šฉ

์ฐธ๊ณ 

  • Origin์„ ์„ค์ • ์•ˆํ•ด์ฃผ๋ฉด Default ๊ฐ’์œผ๋กœ ๋ชจ๋“  Origin์„ ํ—ˆ์šฉํ•จ
  • ๋•Œ๋ฌธ์— ์„ค์ •ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ์ข‹์Œ

์ „์—ญ์œผ๋กœ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ• - Config


์ฐธ๊ณ  ์ž๋ฃŒ