3-10 CSP

Content-Security-Policy:內(nèi)容安全策略

作用

限制方式

資源類型

內(nèi)容安全策略文檔:https://developer.mozilla.org/zh-CN/docs/Web/Security/CSP

總結(jié)

'Content-Security-Policy':'default-src http: https:'  //只加載外鏈資源
'Content-Security-Policy':'default-src \'self\'' //只加載同域下的外鏈資源(包括圖片等所有資源)
'Content-Security-Policy':'script-src \'self\'' //只加載同域下的script資源
'Content-Security-Policy':'default-src \'self\' https://cdn.bootcss.com'  //只加載同域或指定域名下的外鏈資源
'Content-Security-Policy':'default-src \'self\'; form-action \'self\''   //只加載同域下的外鏈資源,form表單只能提交到本地
'Content-Security-Policy':'acript-src \'self\'; form-action \'self\'; report-uri /report' //將不符合條件的資源請求提交報告給服務器/report地址下(資源請求被block掉)
'Content-Security-Policy-Report-Only':'acript-src \'self\'; form-action \'self\'; report-uri /report' //將不符合條件的資源提交報告給服務器/report地址下(資源請求不被block掉)

資源加載被block掉
內(nèi)鏈script和非本域下外鏈的script被block掉

report內(nèi)容

內(nèi)容安全策略也可寫在html的meta標簽里:

<meta http-equiv="Content-Security-Policy" content="connect-src 'self'; form-action 'self'">

report-uri不可寫在meta標簽里



demo

//html
<html>
<head>
  <title>3-10 CSP</title>
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
  <meta http-equiv="Content-Security-Policy" content="connect-src 'self'; form-action 'self';">
</head>
<body>
  <div>this is content</div>
  <form action='https://www.baidu.com/' id="form" method="POST" enctype='multipart/form-data'>
    <input type="submit">
  </form>
  <img src="http://www.baidu.com/img/superlogo_c4d7df0a003d3db9b65e9ef0fe6da1ec.png?qua=high&where=super">
</body>
<script>
  console.log("this is a inline script")
  fetch('http://baidu.com')
</script>
<script src="/test.js"></script>
<script src="https://cdn.bootcss.com/jquery/3.3.1/core.js"></script>
</html>


//server.js
const http=require('http');
const fs=require('fs')
const zlib=require('zlib')

http.createServer(function(request,response){
    const html=fs.readFileSync('test.html')
    if (request.url==='/') {
      response.writeHead(200,{
        'Content-Type':'text/html',
        // 'Content-Security-Policy':'default-src http: https:'
        // 'Content-Security-Policy':'default-src \'self\''
        // 'Content-Security-Policy':'default-src \'self\' https://cdn.bootcss.com'
        // 'Content-Security-Policy':'default-src \'self\'; form-action \'self\''
        // 'Content-Security-Policy':'acript-src \'self\'; form-action \'self\';'
        // 'Content-Security-Policy':'acript-src \'self\'; form-action \'self\'; report-uri /report'
        'Content-Security-Policy-Report-Only':'acript-src \'self\'; form-action \'self\'; report-uri /report'
      })
      response.end(html) 
    }else{
      response.writeHead(200,{
        'Content-Type':'application/javascript'
      })
      response.end('console.log("loaded script")') 
    };
}).listen(8888)
console.log('server listening on 8888')
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,569評論 19 139
  • Spring Web MVC Spring Web MVC 是包含在 Spring 框架中的 Web 框架,建立于...
    Hsinwong閱讀 22,942評論 1 92
  • https://www.html5rocks.com/en/tutorials/security/content-...
    謝大見閱讀 5,901評論 0 3
  • /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home...
    光劍書架上的書閱讀 4,184評論 2 8
  • 本就是想法特別多的孩子,那就好好去表達,不要壓抑了他們。 本就好學,那就創(chuàng)造機會多多學習,成就自己。 你唯一要做的...
    尼非采閱讀 495評論 0 0

友情鏈接更多精彩內(nèi)容