1、限制資源獲取 2、報告資源獲取越權
限制方式:javascript
1、default-src限制全局 跟連接請求有關的東西,限制他的做用範圍 2、制定資源類型
content-src img-src style-src script-src frame-src font-src media-src manifest-src
好比限制掉外來的js,好比xss攻擊html
<!--test.html--> <body> <script> console.log('inline js') </script> </body>
// server.js const http = require('http'); http.createServer(function(req,res){ console.log('req come', req.url); const html = fs.readFileSync('test.html'); res.writeHead(200,{ 'Content-Type': 'text/html', 'Content-Security-Policy': 'default-src http: https:' }) res.end(html); }).listen(8888); console.log('server listening on 8888'); console.log('http://localhost:8888/');
啓動server,運行8888端口,發現控制檯報錯了,這就是Content-Security-Policy限制的做用,那麼若是我經過外鏈的方式創建js呢java
<!--test.html--> <body> <script> console.log('inline js') </script> <script src="/test.js"></script> </body>
// server.js const http = require('http'); const fs = require('fs'); http.createServer(function(req,res){ console.log('req come', req.url); if (req.url === '/') { const html = fs.readFileSync('test.html'); res.writeHead(200,{ 'Content-Type': 'text/html', 'Content-Security-Policy': 'default-src http: https:' }) res.end(html); } else { res.writeHead(200,{ 'Content-Type': 'application/javascript' }) res.end('console.log("loaded script")'); } }).listen(8888); console.log('server listening on 8888'); console.log('http://localhost:8888/');
'Content-Security-Policy': 'default-src \'self\' http://baidu.js'
'Content-Security-Policy': 'form-action \'self\''
'Content-Security-Policy': 'script-src http: https:'
<meta http-equiv='Content-Security-Policy' content='script-src "self"; form-action "self"'>