CSP3のunsafe-dynamicとunsafe-hash-attributes

unsafe-dynamicは'strict-dynamic'という別ディレクティブとなりました (20160622)



Content Security Policy Level 3のFirst Public Working Draftが1月に公開されました。


以前「まもなく公開される CSP Level3 の変更点」この記事で書いたように、CSP level 2から幾つかの変更点がありました。


また、メーリングリスト上では議論は続いており、Githubリポジトリ( https://w3c.github.io/webappsec-csp/ )では編集が続いています。


その中で、「unsafe-dynamic」と「unsafe-hash-attributes」について軽くメモ書き。

unsafe-dynamic

unsafe-dynamicキーワードは、インラインスクリプトを許可するunsafe-inlineと同じようにscript-srcディレクティブで指定できます。


unsafe-dynamicを指定することで、parser-insertedな<script>エレメントの実行を防ぐことが出来ます。


parser-insertedな<script>エレメントは、document.write()やinnerHTMLを通して生成されたものです。createElementで生成された<script>エレメントはparser-insertedではありません。

以下のようにunsafe-dynamicが指定され

Content-Security-Policy: script-src 'nonce-abcdefg' 'unsafe-dynamic'


以下のscriptエレメントがHTMLに含まれており、実行された場合

<script src="https://cdn.example.com/script.js" nonce="abcdefg" ></script>


script.jsの中身

var s = document.createElement('script');
s.src = 'https://othercdn.not-example.net/dependency.js';
document.head.appendChild('s');

document.write('<scr' + 'ipt src='/sadness.js'></scr' + 'ipt>');
  • dependency.jsはcreateElementで生成され、parser-insertedではないので実行される
  • sadness.jsはdocument.writeで生成され、parser-insertedなので実行されない。


chromeにはすでに実装が行われてるようです
https://codereview.chromium.org/1641533006

unsafe-hash-attributes

unsafe-dynamicと同様script-srcディレクティブで指定される。

やむを得ず、attributeからjavascriptの実行をしなければならない時などに使用する。

<button id="action" onclick="doSubmit()">

unsafe-hash-attributesで許可するattributeのハッシュ値(sha256, sha384, sha512)と一致した場合に実行される。

Content-Security-Policy: 'unsafe-hash-attributes' 'sha256-jzgBGA4UWFFmpOBq0JpdsySukE1FrEN5bUpoK8Z29fY='