응답 헤더 필드로 content injetion 방지를 위한거다. (ex. XSS).
개발자가 보안 취약점으로부터 앱을 보호하기 위한 헤더다.
여러 정책 directives 는 한 라인 안에 세미콜론으로 구분하여 사용한다.
Content Security Policy: directive directive-value; directive directive-value
directive-value 가 여러개 올 수도 있다.
Content-Security-Policy:
default-src 'self' *.example.com
특정 리소스가 접근되고 웹 페이지에 로드될 수 있도록 하는 정책.
Content-Security-Policy: child-src https://example.com/
Content-Security-Plicy: default-src 'self'
Context-Security-Plicy: frame-src https://example.com/
Content-Security-Policy: manifest-src https://example.com
Content-Security-Policy: object-src https://example.com
Content-Security-Policy: connect-src https://example.com
Content-Security-Policy: font-src https://example.com
Content-Security-Policy: img-src https://example.com/
Content-Security-Policy: media-src https://example.com
style-src
앱에 적용할 수 있는 Stylesheet 소스를 컨트롤한다.
script-src
앱에 구현할 수 있는 javascript 소스를 컨트롤한다.
모든 DOM 의 properties 를 컨트롤하는 구현 정책을 명세한다.
Content-Security-Policy: plugin-types application/pdf
=> DOM에 오로지 application/pdf 만 로드될 수 있다.
base element 에 로드될 수 있는 URL을 컨트롤한다.이제 어떤 종류의 Directives 정책이 있는지 알아봤으니 기본 동작 원리를 파악하고 써먹어보자.
실전에서는 default-src 하나만 잘 설정해두면 아주 좋다. 따로 명시하지 않은 모든 리소스 정책에 적용되기 때문이다.
Content-Security-Policy:
script-src 'self';
default-src 'self' *.example.com
모든 스크립트는 현재 도메인으로부터 다른 리소스들은 현재 도메인에만 로드 될 수 있게 제한한다.
script-src 'self' => 스크립트를 현재 도메인으로 부터!
default-src 'self' *.example.com => 현재 또는 서브도메인으로부터!
CSP rule 을 위반하는 request 가 있을 때는 해당 리소스는 모두 blank 처리 된다.
아루먼 블록킹 없이 요청을 허용하고, 위반하는 요청은 Report만 되고자 할 때
Content-Security-Policy-Report-Only 를 사용하면 된다.
Content-Security-Policy-Report-Only:
default-src 'self'
report-uri https://example.com/csp_violations/
=> 모든 요청을 허용하되, 현재 도메인이 아닌 외부로부터 로드되는 리소스는 [https://example.com/csp_violations/] 로 보고된다.