Web安全

  • 跨域
    • 常见于前后端分离的项目
    • 主要用于保护后端资源,尤其是接口,避免谁都能访问
    • 是浏览器进行的拦截
    • 跨域的手段
      • 前端使用jsonp的方式绕过
        • 不推荐,虽然是自己的合法脚本绕过了后端域名限制,但不是正常的方式,还是应该后端去允许合理域名的访问
        • 本质上市通过<script>标签把接口等资源当成了文件引进来,绕过了浏览器的检查
      • 应该在后端服务器上进行配置,在HTTP响应的头部中告诉浏览器哪些域名可以访问后端接口。比如.NET的web.xml中。
  • CSP
    • 不管是否前后端分离,都有可能涉及
    • 主要用于保护前端被攻击后胡乱访问其他非法站点的资源
    • 是浏览器进行的拦截,需要浏览器支持,服务端在HTML响应的头部中会指定策略,然后由浏览器执行,部分浏览器可以关掉该检查,比如Firefox可以在about:config中关掉csp检查。
      • 不同的浏览器有可能行为不同
    • 无论哪种配置手段,最终都体现在响应的HTML文件的header的Content-Security-Policy部分中。
    • 配置CSP的手段
      • Web server中统一配置响应头中的csp内容(有可能会覆盖掉后面的配置方式)。
      • 修改HTML。和跨域一样以<meta>的方式放到HTTP响应的头部中,但是在不同的字段。
      • 服务端代码中进行配置,修改响应头。
    • 常见场景/用途
      • Prevent direct dynamic code evaluation by disabling eval. Under certain circumstances eval can be also useful, but we always recommend to use Function objects to create dynamically executed code (see also OWASP Article).
      • Prevent certain image sources that can leak sensitive information like CRSF tokens (see GitHub's post-CSP journey for more details and examples).
      • Restrict browsers to only load resources from trusted origins and prevent, for example the web page of being embedded into iframes or completely preventing iframes.
    • 语法
      • (CSP官方文档)[https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy]
      • (CSP腾讯云中文手册)[https://cloud.tencent.com/developer/chapter/13541]
      • 范围
        • default-src
          • 所有类别元素的默认值。如果后面单独元素的指令没有设置,就会使用default-src中的配置。
          • serves as a fallback for the other CSP fetch directives. For each of the following directives that are absent, the user agent will look for the default-src directive and will use this value for it:
          • 'unsafe-eval':Allows the use of eval() and similar methods for creating code from strings. You must include the single quotes.
          • 'unsafe-inline':Allows the use of inline resources, such as inline <script> elements, javascript: URLs, inline event handlers, and inline <style> elements. You must include the single quotes.
        • frame-src
          • frame子页面单独的csp配置
          • specifies valid sources for nested browsing contexts loading using elements such as <frame> and <iframe>.
          • 虽然Firefox的默认行为和Chrome好像不太一样,frame中默认不允许打开data:内容的a超链接,但也一般不要打开data:配置,这样会有安全风险,木马等有可能修改frame中的链接地址,而用户并不会发现(浏览器刷新按钮不转圈,因为不是整个页面刷新)。
          • Content-Security-Policy: frame-src <source> <source>;
          • <source的取值可以是<host-source>、<scheme-source>、'self'、'unsafe-eval'、'unsafe-hashes'、'unsafe-inline'、'none'、'nonce-<base64-value>'、'<hash-algorithm>-<base64-value>'中的某一个。
            • <host-source>包括data:、mediastream、blob、filesystem,其中data:不建议打开,不安全,或者只限定image-src可以打开,script-src如果打开的话,太不安全了,因为那就可以在iframe中加载脚本了。
        • script-src
          • JavaScript脚本单独的csp配置
          • 'unsafe-eval':Allows the use of eval() and similar methods for creating code from strings. You must include the single quotes.
          • 'unsafe-inline':表示允许使用内联的JavaScript代码
        • style-src
        • img-src
          • 图片单独的csp配置
          • data:,表示允许使用data:编码的图片数据
      • 通用规则
        • 可作用于所有类型的元素。即可以用于default-src,也可以用于frame-src、script-src、style-src、img-src等。
        • blob
          • Allows blob: URIs to be used as a content source.
          • blob: ;
          • blob: ;
原文地址:https://www.cnblogs.com/wyp1988/p/11396481.html