CSP (Content Security Policy) nonce-sourceã«ã¤ãã¦èª¿ã¹ã¦ã¿ã
CSP nonce-sourceã¨ã¯ï¼
Content Security Policy (CSP) ã¯ãã¯ãã¹ãµã¤ãã¹ã¯ãªããã£ã³ã° (XSS) ããã¼ã¿ã¤ã³ã¸ã§ã¯ã·ã§ã³æ»æãå«ããããç¥ããã種é¡ã®æ»æãæ¤åºãã¦è»½æ¸ãããã»ãã¥ãªãã£ã®è¿½å ã¬ã¤ã¤ã¼ã§ãããããã®æ»æææ³ã¯ããã¼ã¿çªçãããµã¤ãæ¹å¤ããã«ã¦ã§ã¢ææã¾ã§ããã¹ã¦ã«ä½¿ç¨ããã¾ãã https://developer.mozilla.org/ja/docs/Security/CSP ããã
XSSã«é¢ãã¦ç°¡åã«è¨ãã¨ãå®è¡ã§ããJavaScriptãå¶éãã¦ãå¤é¨ããã¹ã¯ãªãããæ³¨å ¥ããã¦ããã©ã¦ã¶ãå®è¡ããªããã¨ã§XSSãé²å¾¡ããæ©è½ã¨ãããã¨ã«ãªãã¾ãã
ä¾ãã°ãèªåã®ãµã¼ãä¸ã®jsãã¡ã¤ã«ããã®ã¿ã®scriptãå®è¡ããããã«æå®ããã°ãå¤é¨ããã¹ã¯ãªãããæ¿å ¥ããã¦ãå®è¡ã¯ãããå®å ¨ã«ãªãã¾ãã
ããããã¤ã³ã©ã¤ã³ã®JavaScriptãä¸åç¦æ¢ãã¦ãã¾ãã¨è²ã ã¨é¢åã§ãã
ããã§ãå®è¡ãã¦ããscriptã¿ã°ã«ã¯nonceå±æ§ã§ãªã¯ã¨ã¹ããã¨ã«ã¯ã³ã¿ã¤ã ãªãã©ã³ãã ãªæååããæå®ãããã®å¤ãCSPãããã«æå®ããã¨ããã®ããnonce-sourceã§ããCSP 1.1ã§å°å ¥ããã¾ããã
ä¾ãã°ãHTTPã¬ã¹ãã³ã¹ãããï¼CSPãããï¼ã§
Content-Security-Policy: script-src 'nonce-sXXD/nluT6AqhuVwL0IJqA=='
ã¨æå®ãã¾ããsXXD/nluT6AqhuVwL0IJqA==
ãnonceã®å¤ã§base64å¤ã§ãããã®å ´åãHTMLã®ä¸ã«ä»¥ä¸ã®2ã¤ã®scriptã¿ã°ãããã¨ã
<script type="text/javascript" nonce="sXXD/nluT6AqhuVwL0IJqA==">
// ããã¯nonceãæ£ããã®ã§å®è¡ããã
alert('This works!');
</script>
<script type="text/javascript">
// ããã¯nonceããªãï¼nonceãä¸æ£ï¼ã®ã§å®è¡ãããªã
alert('This does not work!');
</script>
ã¨ãããã¨ã«ãªãã¾ãã
ãã¨ãã¨HTMLã½ã¼ã¹ã«ããæ£å½ãªscriptã¿ã°ã¯nonceãæå®ãããã¨ã§å®è¡ããã¾ãããå¤é¨ããä¸æ£ã«æ³¨å ¥ãããscriptã«ã¯æ£å½ãªnonceããªãã®ã§å®è¡ãããªããã¨ããããã§ãã
ãªããªãããããã§ãããå®éã«å°å ¥ããã«ã¯ãã©ã¦ã¶ã®å¯¾å¿ç¶æ³ãªã©ã確èªããå¿ è¦ãããã¾ããã¨ãããã¨ã§èª¿ã¹ã¦ã¿ã¾ããã
ãã©ã¦ã¶ã®å¯¾å¿ç¶æ³ã®æ¤è¨¼çµæ
script-src nonce
CSPã®script-srcã«nonceã®ã¿ãæå®ããå ´åã§ãã
Content-Security-Policy: script-src 'nonce-$RANDOM'
OS | ãã©ã¦ã¶ | ãã¼ã¸ã§ã³ | nonceä»ãscriptã¿ã° | nonceãªãscriptã¿ã° | å¤å® |
---|---|---|---|---|---|
Windows | Firefox | 33.0.1 | å®è¡ | å®è¡ããã | â |
Linux | Firefox | 33.0 | å®è¡ | å®è¡ããã | â |
Mac | Firefox | 33.0 | å®è¡ | å®è¡ããã | â |
Windows | Firefox | 23.0.1 | å®è¡ | å®è¡ | à |
Windows | Firefox | 22.0 | å®è¡ | å®è¡ | à |
Linux | Chrome | 38.0.2125.104 | å®è¡ | å®è¡ããã | â |
Mac | Chrome | 38.0.2125.104 | å®è¡ | å®è¡ããã | â |
Linux | Chromium | 37.0.2062.120 | å®è¡ | å®è¡ããã | â |
Linux | Opera | 12.16 | å®è¡ | å®è¡ | à |
Mac | Safari | 8.0 (10600.1.25) | å®è¡ããã | å®è¡ããã | à |
Windows | IE | 11.0 | å®è¡ | å®è¡ | à |
Firefox 33ãChromeï¼Chromiumï¼37/38ã¯æ£å¸¸ã«nonce-sourceããµãã¼ããã¦ãã¾ãã
OperaãIEã¯nonce-sourceããµãã¼ããã¦ããããnonceããªãscriptãå®è¡ããã¾ãã
Safariã¯nonce-sourceããµãã¼ããã¦ãªãã ãã§ãªããscriptãå ¨ãå®è¡ãããªããªã£ã¦ãã¾ã£ã¦ãã¾ããSafariã¯CSP 1.0ã«ã¯å¯¾å¿ãã¦ããããããã®ãããªçµæã«ãªãããã§ãã
ãã®CSPãããã§ã¯ãCSP 1.0ã®ã¿å¯¾å¿ã®ãã©ã¦ã¶ã§scriptãå ¨ãå®è¡ãããªããªãä¸å ·åãçãã¦ãã¾ãããã§ãã
script-src unsafe-inline and nonce
ãã©ã¼ã«ããã¯ã®ããã«ãCSPã®script-srcã«unsafe-inlineã¨nonceã®ä¸¡æ¹ãæå®ããå ´åã§ãã
Content-Security-Policy: script-src 'unsafe-inline' 'nonce-$RANDOM'
OS | ãã©ã¦ã¶ | ãã¼ã¸ã§ã³ | nonceä»ãscriptã¿ã° | nonceãªãscriptã¿ã° | å¤å® |
---|---|---|---|---|---|
Windows | Firefox | 33.0.1 | å®è¡ | å®è¡ | à |
Linux | Firefox | 33.0 | å®è¡ | å®è¡ | à |
Mac | Firefox | 33.0 | å®è¡ | å®è¡ | à |
Windows | Firefox | 23.0.1 | å®è¡ | å®è¡ | à |
Windows | Firefox | 22.0 | å®è¡ | å®è¡ | à |
Linux | Chrome | 38.0.2125.104 | å®è¡ | å®è¡ããã | â |
Mac | Chrome | 38.0.2125.104 | å®è¡ | å®è¡ããã | â |
Linux | Chromium | 37.0.2062.120 | å®è¡ | å®è¡ããã | â |
Linux | Opera | 12.16 | å®è¡ | å®è¡ | à |
Mac | Safari | 8.0 (10600.1.25) | å®è¡ | å®è¡ | à |
Windows | IE | 11.0 | å®è¡ | å®è¡ | à |
Chromeï¼Chromiumï¼ã¯nonceã®ã¿ã®å ´åã¨åãã§ãããFirefox 33ã§ã¯nonceããªãscriptãå®è¡ããã¦ãã¾ã£ã¦ãã¾ããããã¯Firefoxã®ãã°ã§ããããï¼
Firefox 22ãOperaãSafariãIEã¯nonce-sourceããµãã¼ããã¦ããªãããããã¹ã¦ã®scriptãå®è¡ããã¦ãã¾ãã
ãã®CSPãããã§ã¯ãCSP 1.0対å¿ã®Safariã§ãscriptãå®è¡ãããªããªã£ã¦ãã¾ãä¸å ·åã¯ãªããªãã¾ããããnounce-sourceã«å¯¾å¿ãã¦ããã¯ãã®Firefoxã§ä¿è·ãç¡å¹ã«ãªã£ã¦ãã¾ã£ã¦ãã¾ãã
調æ»çµæã®ã¾ã¨ã
nonce-sourceã«å¯¾å¿ãã¦ãããã©ã¦ã¶ã§ã¯ä¿è·ãåããæªå¯¾å¿ã®ãã©ã¦ã¶ã§ã¯ä»ã¾ã§ã©ããscriptããã¹ã¦å®è¡ãããã°ããã®ã§ããã
Content-Security-Policy: script-src 'nonce-$RANDOM'
ã®ã¿ã ã¨ãCSP 1.0対å¿ã®Safariã§scriptãå ¨ãå®è¡ãããªããªãä¸å ·åãçããContent-Security-Policy: script-src 'unsafe-inline' 'nonce-$RANDOM'
ã¨ããã¨å¯¾å¿ãã¦ããã¯ãã®Firefoxã§ä¿è·ãç¡å¹ã«ãªã
ã¨ããç¶æ³ã§ãããªããªããã¾ãããã¾ãããã
ã¨ãããã¨ã§ãChromeã¨Firefoxã®å¯¾å¿ãã¼ã¸ã§ã³ã«ã®ã¿ãCSPããããåºåããã®ãç¾å®çããªã¨æãã¾ãã
nonce-sourceã«å¯¾å¿ããChromeã®ãã¼ã¸ã§ã³ã£ã¦ããã¤ãªãã§ãããï¼ãããæ¹ãã¾ãããããæãé¡ããã¨ãããããã§ãã
Firefoxã¯31ã®ãããªã®ã§ããã
ãã©ã¦ã¶ã®å¯¾å¿ç¶æ³ã®æ¤è¨¼æ¹æ³
æ¤è¨¼ã¨å®è£ ã®ããã®ãªãã¸ããªãGitHubã«ç¨æãã¾ããã
ã½ã¼ã¹ã³ã¼ããã¤ã³ã¹ãã¼ã«ãã¾ãã
$ git clone https://github.com/kenjis/php-csp-nonce-source.git
$ cd php-csp-nonce-source
$ composer install
PHPã®ãã«ãã¤ã³Webãµã¼ããèµ·åãã¾ãã
$ php -S localhost:8000
http://localhost:8000/check/ ã«ã¢ã¯ã»ã¹ããé ã«ãªã³ã¯ãã¯ãªãã¯ãã¾ãã
nonceä»ãã®scriptã¿ã°ãå®è¡ãããã¨ãThis works!ãã¨ã¢ã©ã¼ããåºã¾ãããã®ã¢ã©ã¼ããåºãã®ã¯æ£å¸¸ã§ãã
nonceãªãã®scriptã¿ã°ãå®è¡ãããã¨ãThis does not work!ãã¨ã¢ã©ã¼ããåºã¾ãããã®ã¢ã©ã¼ããåºãã¨ãããã¨ã¯ããã©ã¦ã¶ãnonce-sourceã«å¯¾å¿ãã¦ããªãã¨ãããã¨ã«ãªãã¾ãã
åè
Date: 2014/10/28