CORS, XSS

TIL 2020.12.24

Joo Hee Paige Kim
3 min readDec 24, 2020

Cross-Origin Resource Sharing (CORS)

  • an HTTP-header based mechanism that allows a server to indicate any other origins (domain, scheme, or port) than its own from which a browser should permit loading of resources. CORS also relies on a mechanism by which browsers make a “preflight” request to the server hosting the cross-origin resource, in order to check that the server will permit the actual request. In that preflight, the browser sends headers that indicate the HTTP method and headers that will be used in the actual request.
  • 예전에는 server에 client file을 가지고 있고, user의 요청에 의해 server에 있던 client를 user가 받아가서 그 client에서 server와 통신 또는 그 client의 담겨져있는 data를 user가 일반적으로 보는 방식으로 진행
  • 서버에서 내려준 client가 server에 위해가 되는 행동을 하지 않을 것이며, origin이 server에서 나와서 server의 것으로 다시 server에요청을 하였기에, same origin으로 굳이 요청을 server가 막을 필요가 없었음
  • 하지만, 현재는 single page application 등장과 고도화된 웹 application 발달로, 다른 도메인의 정보공유가 빈번해졌기 때문에 same origin이 아닌 cross origin 요청을 해야외에 CORS의 (cross origin애서 리소스 즉 서버자원을 요청하여 사용)필요성이 생김. 예를 들어, 우리가 만든 application에서 youtube 또는 github같은 다른 서버 resource 요청 필요
  • 보안상의 이유로 원래 도메인이 다르면 요청 주고 받을 수 없게 하는 것이 웹 브라우저의 기본 정책이다. 서버에서 내려준 클라이언트가 아니면 요청을 열어놓으면 어떤 요청을 할 지 모르기 때문에 막혀있다.
  • 개발자들이 회사들에게 웹 애플리케이션 고도화를 위해 개선 요청을 하였고 그래서, 이제는 server가 allow한 범위내에서 cross origin 요청 허용
  • 조건에 만족한 request는 cross domain에서 resource 요청 가능

Simple requests

Some requests don’t trigger a CORS preflight. Those are called “simple requests” in this article, though the Fetch spec (which defines CORS) doesn’t use that term. A “simple request” is one that meets all the following conditions:

One of the allowed methods:

and other requirements : https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

Preflighted requests

Unlike “simple requests” (discussed above), for “preflighted” requests the browser first sends an HTTP request using the OPTIONS method to the resource on the other origin, in order to determine if the actual request is safe to send. Cross-site requests are preflighted like this since they may have implications to user data.

어떠한 client가 어떠한 요청을 어떠한 다른 origin에 있는 server에 요청을 하고자 request할 때, browser가 자동으로 OPTIONS을 보내고 , cross origin 허가 받고 진짜 request를 보냄

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

Cross-site scripting (XSS)

a security exploit which allows an attacker to inject into a website malicious client-side code. This code is executed by the victims and lets the attackers bypass access controls and impersonate users. According to the Open Web Application Security Project, XSS was the seventh most common Web app vulnerability in 2017.

현대 브라우저의 경우, 기본적인 방어를 마련해두고 있음

input 태그 등 클라이언트에 대한 입력을 통해 발생하는 경우가 많으므로, 스크립트를 일반 문자열로 변환하는 필터 장치를 마련하여 예방할 수 있다.

  • code injection 브라우저(클라이언트)에 악성 코드 삽입
  • 데이터베이스에 악성코드 삽입=> SQL injection

CSRF (Cross-Site Request Forgery)

  • 해커가 피싱 페이지에 접속한 피해자의 인증 정보를 가로채 악용하는 공격

https://developer.mozilla.org/en-US/docs/Glossary/CSRF

--

--