CSP: script-src

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since August 2016.

* Some parts of this feature may have varying levels of support.

HTTP Content-Security-Policy (CSP) script-src 는 JavaScript에 대한 검증된 출처를 지정합니다. 여기에는 <script> 요소에서 직접 호출한 URL뿐만 아니라, 인라인 스크립트 이벤트 핸들러(onclick) 및 스크립트를 실행할 수 있는 XSLT stylesheets 가 포함됩니다.

CSP version 1
Directive type Fetch directive
default-src fallback Yes. If this directive is absent, the user agent will look for the default-src directive.

Syntax

하나 이상의 출처가 script-src 정책에 의해서 허용될 수 있습니다:

Content-Security-Policy: script-src <source>;
Content-Security-Policy: script-src <source> <source>;

Sources

<source> can be any one of the values listed in CSP Source Values.

Note that this same set of values can be used in all fetch directives (and a number of other directives).

예제

Violation case

주어진 CSP 헤더:

bash
Content-Security-Policy: script-src https://example.com/

아래 스크립트가 차단되어서 로드 또는 실행되지 않습니다:

html
<script src="https://not-example.com/js/library.js"></script>

인라인 스크립트도 실행되지 않습니다:

html
<button id="btn" onclick="doSomething()"></button>

addEventListener를 호출하는 것으로 대체해야 합니다.:

js
document.getElementById("btn").addEventListener("click", doSomething);

안전하지 않은 인라인 스크립트

참고 : 인라인 스타일 및 인라인 스크립트를 허용하지 않는 것이 CSP가 제공하는 가장 큰 보안 이점 중 하나 입니다. 그러나, 인라인 스크립트 및 스타일을 사용해야만 한다면 몇가지 방법을 제공합니다.

인라인 스크립트 및 인라인 이벤트 핸들러를 허용하려면, 'unsafe-inline', 인라인 태그에 정의한 값과 동일한 nonce-source 또는 hash-source를 지정할 수 있습니다.

bash
Content-Security-Policy: script-src 'unsafe-inline';

위의 CSP는 <script> 태그를 허용합니다

html
<script>
  var inline = 1;
</script>

nonce-source를 사용하면 특정 인라인 스크립트 태그만 허용 할 수 있습니다:

bash
Content-Security-Policy: script-src 'nonce-2726c7f26c'

<script> 태그에 동일한 nonce를 설정해야 합니다 :

html
<script nonce="2726c7f26c">
  var inline = 1;
</script>

또는, 인라인 스크립트에서 해시를 설정할 수 도 있습니다. CSP는 sha256, sha384 and sha512를 지원합니다.

bash
Content-Security-Policy: script-src 'sha256-B2yPHKaXnvFWtRChIbabYmUBFZdVfKKXHbWtWidDVF8='

해시를 생성할 때에는 <script> 태그를 포함하지 말고, 대소문자, 태그의 앞뒤 공백이 포함되어야 하는 것을 유의해주십시요.

html
<script>
  var inline = 1;
</script>

안전하지 않은 eval 표현식

'unsafe-eval' 출처 표현식은 문자열에서 코드를 생성하는 여러 스크립트 실행 메소드를 제어합니다. 만약'unsafe-eval'script-src 에 정의되어 있지 않으면, 아래믜 명령어는 차단되며 아무런 효과가 일어나지 않습니다.

strict-dynamic

The 'strict-dynamic' source expression specifies that the trust explicitly given to a script present in the markup, by accompanying it with a nonce or a hash, shall be propagated to all the scripts loaded by that root script. At the same time, any whitelist or source expressions such as 'self' or 'unsafe-inline' will be ignored. For example, a policy such as script-src 'strict-dynamic' 'nonce-R4nd0m' https://whitelisted.com/ would allow loading of a root script with <script nonce="R4nd0m" src="https://example.com/loader.js"> and propogate that trust to any script loaded by loader.js, but disallow loading scripts from https://whitelisted.com/ unless accompanied by a nonce or loaded from a trusted script.

bash
script-src 'strict-dynamic' 'nonce-someNonce'

Or:

bash
script-src 'strict-dynamic' 'sha256-base64EncodedHash'

It is possible to deploy strict-dynamic in a backwards compatible way, without requiring user-agent sniffing. The policy:

bash
script-src 'unsafe-inline' https: 'nonce-abcdefg' 'strict-dynamic'

will act like'unsafe-inline' https: in browsers that support CSP1, https: 'nonce-abcdefg' in browsers that support CSP2, and 'nonce-abcdefg' 'strict-dynamic' in browsers that support CSP3.

명세서

Specification
Content Security Policy Level 3
# directive-script-src

브라우저 호환성

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
script-src
External scripts with hash
inline-speculation-rules source expression
Experimental
Source expression allowing WebAssembly execution

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
Experimental. Expect behavior to change in the future.

See also