@aegisjsproject/trusted-types

中文文档
1.1.0发布于 2026/06/15MITESM + CJS零依赖构建来源已验证近 30 天下载 2224

A polyfill for the Trusted Types API

原始使用文档
文档目录3 节
  1. Concepts and Usage
  2. Injection Sinks
  3. Included Scripts/Modules

@aegisjsproject/trusted-types

A polyfill for the Trusted Types API

CodeQL Node CI Lint Code Base

GitHub license GitHub last commit GitHub release GitHub Sponsors

npm node-current npm bundle size gzipped npm

GitHub followers GitHub forks GitHub stars Twitter Follow

Donate using Liberapay


Concepts and Usage

Client-side, or DOM-based, XSS attacks happen when data controlled by a user (such as that input into a form field) reaches a function that can execute that data. These functions are known as injection sinks. DOM-based XSS attacks happen when a user is able to write arbitrary JavaScript code and have it executed by one of these functions.

The Trusted Types API locks down risky injection sinks, requiring you to process the data before passing it to one of these functions. If you use a string, then the browser will throw a TypeError and prevent the use of the function.

Trusted Types works alongside Content-Security Policy with the trusted-types and require-trusted-types-for directives.

Injection Sinks

The Trusted Types API locks down injection sinks that can act as a vector for DOM-XSS attacks. An injection sink is any Web API function that should only be called with trusted, validated or sanitized input. Examples of injection sinks include:

  • Functions that insert HTML into the document such as Element.innerHTML, Element.outerHTML, or Document.write.
  • Functions that create a new same-origin Document with caller-controlled markup such as DOMParser.parseFromString.
  • Functions that execute code such as Global_Objects/eval.
  • Setters for Element attributes that accept a URL of code to load or execute.

Trusted Types will force you to process the data before passing it to any injection sink rather than use a string. This ensures that the data is trustworthy.

[!IMPORTANT] Since the document cannot directly access HTTP headers, this polyfill requires either <meta http-equiv="Content-Security-Policy"> or a data-trusted-policies attribute to be set on <html>.

The Trusted Types API gives web developers a way to lock down the insecure parts of the DOM API to prevent client-side Cross-site scripting (XSS) attacks.

Included Scripts/Modules

  • trusted-types: Polyfills the globalThis.trustedTypes object
  • harden: Overwrites "injection sink" property & attribute setters
  • bundle: Loads polyfill and conditionally "harden"s

版本与发布记录

@aegisjsproject/trusted-types 共发布 4 个版本,首次发布于 2024/04/02;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.1.0latest2026/06/154 个月前
1.0.22024/09/202 年前
1.0.12024/04/022 年前
1.0.02024/04/022 年前

关于 @aegisjsproject/trusted-types 的常见问题

@aegisjsproject/trusted-types 是什么?

A polyfill for the Trusted Types API

如何安装 @aegisjsproject/trusted-types?

在项目目录运行 npm install @aegisjsproject/trusted-types,或使用 pnpm add / yarn add / bun add。

@aegisjsproject/trusted-types 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/aegisjsproject__trusted-types 是否存在。

@aegisjsproject/trusted-types 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@aegisjsproject/trusted-types 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@aegisjsproject/trusted-types 可以商用吗?

@aegisjsproject/trusted-types 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@aegisjsproject/trusted-types 的下载量有多少?

根据 npm 官方下载统计,@aegisjsproject/trusted-types 在 2026/10/05 – 2026/11/03 这 30 天内被下载 2,224 次,日均约 74 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@aegisjsproject/trusted-types 还在维护吗?

最新版本 1.1.0 发布于 2026/06/15(4 个月前);至今共发布 4 个版本,其中过去 12 个月发布了 1 个;首次发布于 2024/04/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。