@aegisjsproject/sanitizer

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

A polyfill for the Sanitizer API with various sanitizer configs

原始使用文档
文档目录5 节
  1. AegisJSProject Sanitizer
  2. Example
  3. Restricting Allowed Content (eg for comments)
  4. Adding to allowed elements / attributes
  5. Enforce Sanitization by default (on eg innerHTML , where su…

@aegisjsproject/sanitizer

Sanitizer API polyfill & config

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


AegisJSProject Sanitizer

This is a library & polyfill for the Sanitizer API.

It provides a minimal polyfill for Element.prototype.setHTML() & Document.parseHTML(), as well as config files for HTML, SVG, & MathML. Please note, however, that the default sanitizer config in these sanitizer methods only support HTML by default.

The "base" config (not what is used by default) DOES add support for <svg>, and the "complete" config supports <svg> & <math>.

This helps prevent XSS via:

  • Stripping event attributes such as onclick
  • Removing unsafe URL attributes such a <a href="javascript:...">
  • Prevents adding <script>s and <style>s
  • Removes other potentially dangerous elements & attributes

Example

import '@aegijsproject/sanitizer/polyfill.js';
import { sanitizer } from '@aegisjsproject/sanitizer/config/base.js';

document.body.setHTML(`
  <header id="header">
    <h1 onclick="alert(location.href)" data-foo="bar">Hello, World!</h1>
  </header>
  <nav id="nav" class="flex row">
    <button type="button" popovertarget="bacon" popovertargetaction="show" accesskey="b">Show Bacon Ipsum</button>
    <a href="#foo">Normal Link</a>
    <a href="javascript:alert('javascript:')"><code>javascript:</code> Link</a>
    <a href="data:text/plain,Not%20Allowed" target="_blank"><code>data:</code> Link</a>
    <a href="${URL.createObjectURL(file)}" target="_blank"><code>blob:</code> Link</a>
  </nav>
  <main id="main"></main>
  <div popover="auto" id="bacon">
    <div>
      <b>Bacon Ipsum</b>
      <button type="button" popovertarget="bacon" popovertargetaction="hide">
        <svg xmlns="http://www.w3.org/2000/svg" width="12" height="16" viewBox="0 0 12 16" fill="currentColor" role="presentation" aria-label="Close Popover">
          <path fill-rule="evenodd" d="M7.48 8l3.75 3.75-1.48 1.48L6 9.48l-3.75 3.75-1.48-1.48L4.52 8 .77 4.25l1.48-1.48L6 6.52l3.75-3.75 1.48 1.48L7.48 8z"/>
        </svg>
      </button>
    </div>
    <p>Bacon ipsum dolor amet pork belly frankfurter drumstick jowl brisket capicola
      short ribs.Cow chislic ham hock t-bone shoulder salami rump corned beef spare
      ribs prosciutto bresaola picanha drumstick. Swine tail pork belly ribeye beef
      kielbasa. Beef cupim ball tip pastrami spare ribs strip steak tongue salam
      venison. Venison cupim meatball strip steak meatloaf prosciutto buffalo
      frankfurter hamburger flank boudin.</p>
  </div>
`, sanitizer);

Restricting Allowed Content (eg for comments)

const sanitizer = {
  elements: ['span', 'div', 'p', 'code', 'pre', 'blockquote', 'img', 'a'],
  attributes: ['href', 'src', 'loading', 'height', 'width', 'class', 'alt', 'target'],
};

fetch('https://api.example.com/comments')
  .then(resp => resp.json())
  .then(comments => {
    document.querySelector('.comments').append(...comments.map(comment => {
      const el = document.createElement('div');
      el.setHTML(comment.body, sanitizer);
      return el;
    }));
  });

Adding to allowed elements / attributes

import { elements, attributes } from '@aegisjsproject/sanitizer/config/html.js';

const sanitizer = {
  elements: ['hello-world', ...elements],
  attributes: ['foo', ...attributes],
};

document.querySelector('.container').setHTML(`
  <hello-world foo="bar"></hello-world>
`, sanitizer);

Enforce Sanitization by default (on eg innerHTML, where supported)

if ('trustedTypes' in globalThis) {
  trustedTypes.createPolicy('default', {
    createHTML(input) {
      const el = document.createElement('div');
      el.setHTML(input);
      return el.innerHTML;
    }
  });
}

版本与发布记录

@aegisjsproject/sanitizer 共发布 21 个版本,首次发布于 2024/03/27;过去 12 个月发布了 5 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.3.0latest2026/06/144 个月前
0.2.42025/12/2410 个月前
0.2.32025/12/1710 个月前
0.2.22025/12/1710 个月前
0.2.12025/11/1911 个月前
0.2.02025/10/241 年前

关于 @aegisjsproject/sanitizer 的常见问题

@aegisjsproject/sanitizer 是什么?

A polyfill for the Sanitizer API with various sanitizer configs

如何安装 @aegisjsproject/sanitizer?

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

@aegisjsproject/sanitizer 支持 TypeScript 吗?

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

@aegisjsproject/sanitizer 支持 ESM 还是 CommonJS?

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

@aegisjsproject/sanitizer 需要什么版本的 Node.js?

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

@aegisjsproject/sanitizer 可以商用吗?

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

@aegisjsproject/sanitizer 的下载量有多少?

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

@aegisjsproject/sanitizer 还在维护吗?

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