3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

DOM Based XSS を防ぐ Trusted Types という仕様

Posted at

Trusted Types

DOM Based XSS を防ぐ「Trusted Types」という仕様が、W3CのWeb Incubator Community Group (WICG)
で議論されています。

Trusted Typesでは、element.innerHTML などにPolicyにあった値(TrustedHTML)のみが代入できるように制限されます。

TrustedHTMLの他にも、同様に指定したポリシーTrustedScript, TrustedScriptURL, TrustedURLなどがあります。

すでに、Polyfillが提供されているので実行してみる。
TrustedTypes.createPolicyで適応するPolicyを宣言し、文字列を引数としてcreateHTMLメソッドでTrustedHTMLを作成する。

このTrustedHTMLのみが、element.innerHTMLに代入できる。
直接文字を代入しようとするとTypeErrorとなる。

<script src="https://wicg.github.io/trusted-types/dist/es5/trustedtypes.build.js" data-csp="trusted-types mypolicy"></script>
<script>
    function customSanitize(s){
        return s.replace("<", "&lt;");
    }

    const p = TrustedTypes.createPolicy('mypolicy',{
        createHTML: (s) => { return customSanitize(s) },
    });

    e = document.getElementsByTagName('div')[0]
    e.innerHTML = p.createHTML('<test>'); // does not throw. e.innerHTML == '&lt;test>'
    e.innerHTML = "<test>" // TypeError.
</script>

tt.png

Trusted-TypesヘッダでPolicy指定することで、そのポリシーのみを適応可能になる。勝手にポリシーを作ったり、上書きでない。

Trusted-Types: "one", "two", "default"

感想

仕様が難しく、ポリシーの適応周りあまりちゃんと理解できなかった...

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?