Content-Security-Policy (CSP)

Falcon·2022년 2월 26일
post-thumbnail

🎯 Goals

  • CSP 가 무엇인지 알고 사용한다.
  • 대표적으로 쓰이는 것들을 암기한다.

개요

응답 헤더 필드로 content injetion 방지를 위한거다. (ex. XSS).
개발자가 보안 취약점으로부터 앱을 보호하기 위한 헤더다.

syntax

여러 정책 directives 는 한 라인 안에 세미콜론으로 구분하여 사용한다.

Content Security Policy: directive directive-value; directive directive-value

directive-value 가 여러개 올 수도 있다.

Content-Security-Policy:
default-src 'self' *.example.com

1. Fetch Directives

특정 리소스가 접근되고 웹 페이지에 로드될 수 있도록 하는 정책.

  • child-src
    브라우저 내에서 nested 된 컨텍스트 생성을 관장함.
Content-Security-Policy: child-src https://example.com/
  • default-src
    정책의 기본 소스 리스트다.
    다른 fetch directives 의 약점을 보호하기위해 만들어졌다.
Content-Security-Plicy: default-src 'self'
  • frame-src
    브라우저 콘텍스트 내에서 로드되는 URL을 제한한다.
Context-Security-Plicy: frame-src https://example.com/
  • manifest-src
    URL을 컨트롤한다. 리소스가 로드될 수 있는 다양한 elements 로부터
Content-Security-Policy: manifest-src https://example.com
  • object-src
    플러그인이 앱에 로드될 수 있는 URL을 제한한다.
Content-Security-Policy: object-src https://example.com
  • connect-src
    앱에 들어갈 수 잇는 script interface URL 을 제한한다.
Content-Security-Policy: connect-src https://example.com
  • font-src
    앱에 로드될 수 있는 폰트 URL 을 제한한다.
Content-Security-Policy: font-src https://example.com
  • img-src
    앱에 로드될 수 있는 이미지 url 을 제한한다.
Content-Security-Policy: img-src https://example.com/
  • media-src
    앱에 로드될 수 있는 음악, 영상 url 을 제한한다.
Content-Security-Policy: media-src https://example.com
  • style-src
    앱에 적용할 수 있는 Stylesheet 소스를 컨트롤한다.

  • script-src
    앱에 구현할 수 있는 javascript 소스를 컨트롤한다.

2. Document Directives

모든 DOM 의 properties 를 컨트롤하는 구현 정책을 명세한다.

  • plugin-types
    DOM 에 로드될 수 있는 플러그인 리소스를 제한한다.
Content-Security-Policy: plugin-types application/pdf

=> DOM에 오로지 application/pdf 만 로드될 수 있다.

  • base-url
    base element 에 로드될 수 있는 URL을 컨트롤한다.
  • sandbox
    HTML sandbox 정책을 user agent 에 의해 적용할 수 있게한다.

3. Navigation Directives

  • form-action
    입력 폼 제출에 사용할 URL을 컨트롤한다.
  • frame-ancestors
    frame, iframe, object, embed, applet 등 임베드 될 수 있는 URL을 제한한다.
  • navigate-to
    어느 URL로 document가 메소드를 통해 추적될 수 있는지를 컨트롤한다.

4. Reporting Directives

  • report-to
    정책 침해 리포트를 송신할 엔드포인트를 명시한다.

실전

이제 어떤 종류의 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/] 로 보고된다.


🔗 References

profile
I'm still hungry

0개의 댓글