【问题标题】:This document requires 'TrustedScriptURL' assignment此文档需要“TrustedScriptURL”分配
【发布时间】:2020-05-29 07:43:25
【问题描述】:

在我的 Content-Security-Policy 标头中添加require-trusted-types-for 'script'; 后,introduced from Chrome 83 Beta 有助于锁定 DOM XSS 注入接收器,

当我打开我的网站时,它变成了一个空白页面。我的控制台中有很多这三种错误。 (Chrome 版本 83.0.4103.61)

此文档需要分配“TrustedScript”。

此文档需要分配“TrustedScriptURL”。

TypeError:无法在“HTMLScriptElement”上设置“src”属性:此文档需要“TrustedScriptURL”分配。

我已阅读文章Prevent DOM-based cross-site scripting vulnerabilities with Trusted Types。但是,文章只说如何处理 TrustedHTML,而不是 TrustedScriptTrustedScriptURL

任何指南都会有所帮助。谢谢!

【问题讨论】:

  • 我面临同样的问题。
  • @AashutoshRathi 如果这仍然与您相关,请检查我的答案。

标签: javascript html security content-security-policy


【解决方案1】:

我们也遇到了同样的问题。

以下是您解决问题的方法:

  1. 安装DOMPurify 库。 npm install --save DOMPurify

  2. 创建一个文件trusted-security-policies.js

  3. 在您的打包程序的入口点(例如 webpack)中,首先(在可能违反内容安全政策的任何代码之前)导入此文件:

    import './path/to/trusted-security-policies';
    
import DOMPurify from 'dompurify';

if (window.trustedTypes && window.trustedTypes.createPolicy) { // Feature testing
    window.trustedTypes.createPolicy('default', {
        createHTML: (string) => DOMPurify.sanitize(string, {RETURN_TRUSTED_TYPE: true}),
        createScriptURL: string => string, // warning: this is unsafe!
        createScript: string => string, // warning: this is unsafe!
    });
}

这是做什么的:每当一个字符串被分配以被解析为 HTML,或者作为一个 URL,或者作为一个脚本,浏览器自动 strong> 通过定义的处理函数传递这个字符串。

对于 HTML,DOMPurify 库正在从潜在的 XSS 代码中清除 HTML。

对于scriptURLscript,字符串只是通过。 请注意,这实际上会禁用这两个部分的安全性,并且只应在您尚未确定如何使这些字符串自己安全的情况下使用。一旦你有了它,相应地替换处理函数。


编辑,2021 年 12 月:我可以contribute to DOMPurify,所以如果您需要使用自定义元素,现在该库也可以configured 工作在您的 HTML 字符串中,以及 自定义属性release 2.3.4 之前的属性在清理过程中被简单地删除):

/**
 * Control behavior relating to Custom Elements
 */
 
// DOMPurify allows to define rules for Custom Elements. When using the CUSTOM_ELEMENT_HANDLING 
// literal, it is possible to define exactly what elements you wish to allow (by default, none are allowed).
//
// The same goes for their attributes. By default, the built-in or configured allow.list is used.
//
// You can use a RegExp literal to specify what is allowed or a predicate, examples for both can be seen below.
// The default values are very restrictive to prevent accidental XSS bypasses. Handle with great care!


var clean = DOMPurify.sanitize(
    '<foo-bar baz="foobar" forbidden="true"></foo-bar><div is="foo-baz"></div>',
    {
        CUSTOM_ELEMENT_HANDLING: {
            tagNameCheck: null, // no custom elements are allowed
            attributeNameCheck: null, // default / standard attribute allow-list is used
            allowCustomizedBuiltInElements: false, // no customized built-ins allowed
        },
    }
); // <div is=""></div>
 
var clean = DOMPurify.sanitize(
    '<foo-bar baz="foobar" forbidden="true"></foo-bar><div is="foo-baz"></div>',
    {
        CUSTOM_ELEMENT_HANDLING: {
            tagNameCheck: /^foo-/, // allow all tags starting with "foo-"
            attributeNameCheck: /baz/, // allow all attributes containing "baz"
            allowCustomizedBuiltInElements: false, // customized built-ins are allowed
        },
    }
); // <foo-bar baz="foobar"></foo-bar><div is=""></div>
  
var clean = DOMPurify.sanitize(
    '<foo-bar baz="foobar" forbidden="true"></foo-bar><div is="foo-baz"></div>',
    {
        CUSTOM_ELEMENT_HANDLING: {
            tagNameCheck: (tagName) => tagName.match(/^foo-/), // allow all tags starting with "foo-"
            attributeNameCheck: (attr) => attr.match(/baz/), // allow all containing "baz"
            allowCustomizedBuiltInElements: true, // allow customized built-ins
        },
    }
); // <foo-bar baz="foobar"></foo-bar><div is="foo-baz"></div>

【讨论】:

  • 嘿!谢谢你会试试这个。
【解决方案2】:

检查这个。可能会帮到你。

https://zeronights.ru/wp-content/themes/zeronights-2019/public/materials/3_ZN2019_Jakub_Vrana_Krzysztof_Kotowicz_Trusted_Types_and_the_end_of_DOM_XSS.pdf

潜在修复参考:

Trusted Types 和 Chrome 浏览器实现的背景:

短期修复选项:

  • 添加仅报告 CSP 标头。 [不是很好,如果您正在运行敏感的产品应用程序,您必须了解各种风险]

长期修复选项:

  • 您可以进行调查,将外部第三方的东西带到您的基地并避免整体痛苦。

我不是专家,我也只是想从中学习,我会说解决方法几乎是因情况而异,而不是灵丹妙药。

一切顺利!

【讨论】:

  • 谢谢,克里斯。这些信息非常有用!
猜你喜欢
  • 2022-08-20
  • 2020-09-09
  • 2020-11-13
  • 2012-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-13
  • 2021-04-27
相关资源
最近更新 更多