UserCSP

  • 目を醒ませ僕らのなんちゃら〜


  • CSP 導入準備のため、実際に Content-Security-Policy ヘッダが出力されたときに、ウェブブラウザがどのような挙動になるかを調べていた
    • ウェブサーバの設定を変えながら試行錯誤するのは、いかにも面倒くさい
  • ブラウザ側で HTTP レスポンスに含まれるヘッダを変更できないか
    • できるが、標準機能ではない
  • レスポンスに任意のヘッダを追加したり変更できる、ブラウザのアドオンや拡張機能が存在する
    • 単純な機能なので、安心のために自作する




  • 拡張機能の上記設定画面で、Content-Security-Policy ヘッダと Content-Security-Policy-Report-Only ヘッダを設定すると、出力されるようになる
    • Content-Security-Policy-Report-Only: default-src 'self' で はてな にアクセスすると、ウェブコンソールに、設定されたポリシーに従ってリソースを拒否することがレポートされる
The Content Security Policy 'default-src 'self'' was delivered in report-only mode, but does not specify a 'report-uri'; the policy will have no effect. Please either add a 'report-uri' directive, or deliver the policy via the 'Content-Security-Policy' header.
Navigated to http://www.hatena.ne.jp/
(index):1 [Report Only] Refused to load the script 'https://cdn.ad-hatena.com/js/river.js' because it violates the following Content Security Policy directive: "default-src 'self'". Note that 'script-src-elem' was not explicitly set, so 'default-src' is used as a fallback.
(index):27 [Report Only] Refused to execute inline script because it violates the following Content Security Policy directive: "default-src 'self'". Either the 'unsafe-inline' keyword, a hash ('sha256-xYOawB5vZJsporgKCEyYks2LJ5r/svEP1UgyihxaEAg='), or a nonce ('nonce-...') is required to enable inline execution. Note also that 'script-src' was not explicitly set, so 'default-src' is used as a fallback.
(index):30 [Report Only] Refused to load the script 'http://www.googletagmanager.com/gtm.js?id=GTM-WPVF7X' because it violates the following Content Security Policy directive: "default-src 'self'". Note that 'script-src-elem' was not explicitly set, so 'default-src' is used as a fallback.
(index):39 [Report Only] Refused to load the image 'http://n.hatena.ne.jp/my/profile/image?size=16&type=icon' because it violates the following Content Security Policy directive: "default-src 'self'". Note that 'img-src' was not explicitly set, so 'default-src' is used as a fallback.
...
  • Content-Security-Policy-Report-Only ではなく Content-Security-Policy: default-src 'self' とすると、レポートだけではなく、本当にいくつかのリソースが拒否され、ページが機能不全になった
  • ページが完全に機能し、かつ、ある程度セキュアな Content-Security-Policy は下記のようなものだろうか(見やすさのため適宜改行)
script-src 'unsafe-inline' 'unsafe-eval'
http://*.hatena.ne.jp/
http://img.ak.impact-ad.jp/
http://pagead2.googlesyndication.com/
http://platform.twitter.com/widgets.js
http://static.criteo.net/js/ld/publishertag.prebid.js
http://www.googletagmanager.com/gtm.js
http://www.googletagservices.com/activeview/js/current/osd.js
http://www.hatena.com/
http://y.one.impact-ad.jp/imp
https://adservice.google.co.jp/adsid/integrator.js
https://adservice.google.com/adsid/integrator.js
https://c.amazon-adsystem.com/aax2/apstag.js
https://cdn.ad-hatena.com/
https://*.st-hatena.com/
https://platform.twitter.com/js/
https://securepubads.g.doubleclick.net/gpt/
https://static.xx.fbcdn.net/rsrc.php/
https://tpc.googlesyndication.com/
https://www.googletagservices.com/activeview/js/current/osd_listener.js
https://www.googletagservices.com/tag/js/gpt.js
stats.g.doubleclick.net/dc.js
www.google-analytics.com/analytics.js
  • だが、ユーザとしては、これらすべてのリソースを信頼できるか判断するのは難しい
    • ページ機能の一部が失われるが、信頼できそうなリソースのみを許可するポリシーを考える
script-src 'unsafe-inline' 'unsafe-eval'
http://*.hatena.ne.jp/
http://www.hatena.com/
https://cdn.ad-hatena.com/
https://*.st-hatena.com/
http://www.googletagmanager.com/gtm.js
  • こうしておけば、マルバタイジングを始めとした何者かの侵略を、食い止めることができる
    • あらゆるページのために、このようなポリシーを作成するのは現実的ではないが
  • ウェブブラウザには、ページに埋め込まれたスタイルシートスクリプトを、ユーザが任意に上書きできる手段がある
    • それらはユーザスタイルシートやユーザスクリプトと呼ばれ、平成中期によく利用されていた
    • 同様に、ユーザの立場でページに独自のポリシーを定義できても、CSP の目的からそれほど離れないと思う