乐趣区

关于前端:关于-Web-ContentSecurityPolicy-Directive-通过-meta-元素指定的一些测试用例

Content Security Policy 是一种应用题目或 meta 元素来限度或批准加载到指定网站上的内容的策略。这是一个广受反对的平安规范,所有网站运营者都应该对这些规范了然于心。

应用 CSP 通过阐明容许或不容许的规定为 Web 网站减少了一层爱护。这些规定有助于进攻内容注入和跨站点脚本 (XSS) 攻打,这是 OWASP 的十大 Web 应用程序平安危险中的两个。

当攻击者可能通过注入恶意代码来毁坏未受爱护的网站时,就会产生 XSS 攻打。当用户尝试与站点交互时,歹意脚本会在用户的浏览器中执行,从而使攻击者可能拜访受害者与站点的交互,例如登录信息等。

CSP 将阻止大多数脚本注入攻打的产生,因为它能够设置为将 JavaScript 限度为仅从受信赖的地位加载。

本文介绍一些基于 frame-src 这个 Directive 的各种测试用例。
作为容器,定义 iframe 的 web 利用,监听在 3000 端口:wechat 文件夹下

嵌入了另一个网页,监听在 3002 端口,Jerrylist 文件夹上面:

如果 Jerrylist 文件夹下的 csp html 里没有申明任何 csp 相干的 Directive(通过 meta 标签),则 iframe 工作失常:

测试 1:3000 利用 (即嵌入 3002 利用的 web 利用里)减少 frame-src

源代码:

<html>
    <head>
        <meta http-equiv="Content-Security-Policy" content="frame-src'self'">
    </head>
    <h1>Parent</h1>
    <iframe src="http://localhost:3002/csp"></iframe>
</html>

测试后果:

Refused to frame ‘http://localhost:3002/’ because it violates the following Content Security Policy directive: “frame-src ‘self'”.

iframe 加载失败:

测试 2

<html>
    <head>
        <meta http-equiv="Content-Security-Policy" content="frame-src'http://localhost:3002'">
    </head>
    <h1>Parent</h1>
    <iframe src="http://localhost:3002/csp"></iframe>
</html>

谬误音讯:

The source list for the Content Security Policy directive ‘frame-src’ contains an invalid source: ”http://localhost:3002”. It will be ignored.
11:25:37.549 localhost/:6 Refused to frame ‘http://localhost:3002/’ because it violates the following Content Security Policy directive: “frame-src ‘none'”.

iframe 加载失败:

改成 * 的话,又从新工作了:

下列代码也工作:

<html>
    <head>
        <meta http-equiv="Content-Security-Policy" content="frame-src http://localhost:3002/csp">
    </head>
    <h1>Parent</h1>
    <iframe src="http://localhost:3002/csp"></iframe>
</html>

下列代码也工作:

<html>
    <head>
        <meta http-equiv="Content-Security-Policy" content="frame-src http://localhost:*/csp">
    </head>
    <h1>Parent</h1>
    <iframe src="http://localhost:3002/csp"></iframe>
</html>

下列代码也工作:

退出移动版