-
항해 99 5기 WIL_5항해 99 2022. 2. 13. 22:10
▶ Weekly I Learned (회고)
백엔드와 프론트엔드로써의 협업은 처음이라 초반에는 막막했지만 또 잘 진행되어가는 듯 하다.
이번 일요일은 아주 푹 쉬었으며 내일부터 다시 건강한 하루를 살도록 해야지..!
<CS>
[CORS]
Cross Origin Resource Sharing 교차출처 리소스 공유, 교차출처란 다른 출처를 의미
cf) SOP
Same Origin Policy, 같은 출처에서만 리소스를 공유할 수 있다는 규칙을 가진 정책
기본적으로 브라우저는 출처가 다른 사이트와 리소스 공유하는 것을 위 원칙에 의해 막아두었다.
출처가 다른 리소스 요청을 허용하는 예외를 만들어 주어야 하는데, 이를 위해 'CORS 정책을 지킨 리소스 요청'을 하여야 한다.
즉, CORS로 이 출처의 사이트는 괜찮아, 하고 지정해주어야 하는 것이다.
여기서 출처(origin)란 scheme(protocol), host(domain), port로 구성되어 있다.
라는 url이 있다면 https://는 프로토콜, www.google.com은 host :443은 포트이다. (https의 기본포트는 443이라서 생략이 가능하다.)
CORS는 보안 문제 중 CSRF나 XSS를 방지해줄 수 있다.
CSRF (Cross Site Request Forgery): 사이트 간 요청 위조, 일단 사용자가 웹사이트에 로그인 상태에서 사이트간
요청 위조 공격 코드가 삽입된 페이지를 열면,
공격 대상이 되는 웹사이트는 위조된 공격 명령이 믿을 수 있는 사용자로부터 발송된 것으로 판단하게 되어 공격에 노출된다.
공격 과정은 해당링크 참조
XSS (Cross Site Scripting): 웹사이트의 관리자가 아닌 사람이 웹페이지에 악성 스크립트를 삽입하는 것
공격 과정은 해당링크 참조
CORS 동작방식
1) 프리플라이트 요청 (Prefilght Request)
Option 메소드를 통해 다른 도메인의 리소스에 요청이 가능한지 (실제 요청이 전송하기에 안전한지)
확인 작업을 하고 요청이 가능하다면 실제 요청을 보낸다.
(Cross-origin 요청은 유저 데이터에 영향을 줄 수 있기 때문이다.)
2) 단순 요청 (Simple Request)
위와 달리 즉시 cross origin인지 확인하며 다음 조건을 모두 충족해야한다.
메서드는 GET, POST, HEAD 중 하나,
헤더는 Accept, Accept-Language, Content-Language, Content-Type만 허용,
Content-Type 헤더는 다음의 값들만 허용
application/x-www-form-urlencoded
multipart.form-data
text/plain
3) 인증정보 포함 요청 (Credentialed Request)
인증 관련 헤더를 포함할 때 사용하는 요청이다. 브라우저가 제공하는 비동기 리소스 요청 API인 XMLHttpRequest 객체나 fetch API는 별도의 옵션 없이 브라우저의 쿠키 정보나 인증과 관련된 헤더를 기본적으로 요청에 담지 않으므로, credentials 옵션을 변경하지 않고서는 cookie를 주고 받을 수 없다.
옵션종류
- omit : 절대로 cookie 들을 전송하거나 받지 않는다.
- same-origin : 동일 출처(same origin)이라면, user credentials (cookies, basic http auth 등..)을 전송한다. (default 값)
- include : cross-origin 호출이라 할지라도 언제나 user credentials (cookies, basic http auth 등..)을 전송한다.
이 외에도 위의 내용들은 하단의 링크를 참조하면 더욱 자세히 알 수 있다.
출처:
https://www.youtube.com/watch?v=bW31xiNB8Nc
https://evan-moon.github.io/2020/05/21/about-cors/
▶ 공부 시 참고 링크들
https://expressjs.com/en/resources/middleware/cors.html
'항해 99' 카테고리의 다른 글
항해 99 5기 TIL_37 (0) 2022.02.16 항해 99 5기 TIL_36 (0) 2022.02.15 항해 99 5기 TIL_35 (0) 2022.02.13 항해 99 5기 TIL_34 (0) 2022.02.13 항해 99 5기 TIL_33 (0) 2022.02.12