CSP - 콘텐츠 보안 정책

내용물


  • Intro
  • Directives
  • Values
  • Summary

  • 소개

    Lastly but by no means least, carrying on from my previous blog about website security week , 우리는 CSP 또는 콘텐츠 보안 정책에 대해 이야기할 것입니다.

    CSP is Content Security Policy 이것은 귀하의 웹사이트를 보호하기 위한 귀하의 무기고에서 가장 강력한 도구 중 하나입니다.

    다음은 헤더Content-Security-Policy로 또는 HTML의 메타 태그를 통해 콘텐츠 보안 정책을 설정하는 두 가지 방법입니다. 예를 들면 다음과 같습니다.

    <meta http-equiv="Content-Security-Policy" content="default-src 'none'; img-src https://google.com; child-src 'none';">
    

    지시문

    The content policy is made up of directives (the thing to restrict) and the value(s) on how it can be restricted. We won't cover all all the possible directives in this blog but you can find a list of all the directives here .

    구문은 다음과 같습니다.

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

    설정해야 하는 몇 가지 주요 지시문이 있습니다.

    기본-src



    이름에서 알 수 있듯이 더 구체적인 지시문이 사용되지 않은 경우의 대체 방법입니다. '없음'으로 설정하는 것이 좋습니다.

    Content-Security-Policy: default-src 'none'; 
    

    연결-src



    이것은 가져오기를 통해 '연결'하고 HTTP 요청을 할 수 있는 대상에 영향을 줍니다.

    Content-Security-Policy: default-src 'none'; connect-src https://some.api.com; 
    

    img-src



    이것은 이미지를 로드할 수 있는 위치에 영향을 줍니다.

    Content-Security-Policy: default-src 'none'; img-src https://some.img.host https://another.img.place; 
    

    폼 액션



    이는 HTML 양식 속성을 통해 양식 제출을 보낼 수 있는 위치에 영향을 줍니다.

    Content-Security-Policy: default-src 'none'; form-action https://some.api.host; 
    

    이는 콘텐츠 보안 정책에 설정해야 하는 몇 가지 지침에 불과합니다. 콘텐츠 보안 정책 지침이 구체적일수록 정책이 더 강력해집니다.

    가치

    You can specify many different types of values for each directive and its important to understand the affect of each one.

    '없음'



    이렇게 하면 리소스를 로드할 수 없습니다.

    Content-Security-Policy: default-src 'none'; 
    

    '본인'



    현재 도메인의 리소스만 허용합니다.

    Content-Security-Policy: default-src 'self'; 
    

    호스트



    임의의 수의 호스트에서 로드를 허용합니다. 또한 선택적 프로토콜을 가질 수도 있습니다. http://또는 https://, 선택적 포트(예: some.website:8080 및/또는 선택적 경로(예: https://some.website/path/to/file .

    Content-Security-Policy: img-src https://some.img.host some.other.images.com img.org:8080 img.co.uk/path/to/img.jpg; 
    

    개요



    예를 들어 스키마만 설정할 수 있습니다. https:, http:, data: 하지만 일반적으로 data:xxxx인 인라인 이미지를 제외하고는 이것을 권장하지 않습니다.

    Content-Security-Policy: img-src data:; 
    

    목하



    이것은 script HTML tag nonce attribute 과 함께 작동하며 서버는 고유한 값을 생성해야 합니다.

    Content-Security-Policy: script-src nonce-DhcnhD3khTMePgXwdayK9BsMqXjhguVV; 
    



    이것은 리소스의 SHA 해시입니다. 예를 들어 콘텐츠 보안 정책을 적용하면 다른 값을 사용할 수 없는 경우 브라우저에서 이를 생성하여 사용할 수 있습니다.

    Content-Security-Policy: script-src sha256-jzgBGA4UWFFm; 
    

    이러한 값을 서로 조합하여 사용하여 콘텐츠 보안 정책을 최대한 잠글 수 있습니다.

    다음은 예입니다.

    Content-Security-Policy: default-src 'none'; script-src 'self' https://static.cloudflareinsights.com; img-src 'self'; style-src 'self'; connect-src 'self' https://cloudflareinsights.com https://api.challenge.new; font-src 'self'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'; manifest-src 'self';

    요약

    In summary, setting a content security policy is one of the most powerful tools in your arsenal to secure your website. It can take some time to set up a strict content security policy but that time is payed back tenfold in the benefits it provides.

    Set that content security policy now!

    Happy Building!

    좋은 웹페이지 즐겨찾기