sanitize()

Создаёт плагин санитизации, который удаляет опасные HTML-теги, атрибуты и URL-протоколы из скомпилированного вывода.

import { sanitize } from "@unifast/node";

Сигнатура

function sanitize(options?: SanitizePluginOptions): UnifastPlugin

Параметры

options?

Конфигурация санитизации

СвойствоТипПо умолчаниюОписание
enabledbooleantrueВключить или отключить санитизацию
schemaSanitizeSchemaПользовательская схема санитизации

SanitizeSchema

СвойствоТипОписание
allowedTagsstring[]Имена разрешённых HTML-тегов (все остальные удаляются)
allowedAttributesRecord<string, string[]>Отображение имён тегов в списки разрешённых атрибутов
allowedProtocolsRecord<string, string[]>Отображение имён атрибутов в списки разрешённых URL-протоколов

Использование

import { compile, sanitize } from "@unifast/node";

const result = compile(md, {
  plugins: [
    sanitize({
      enabled: true,
      schema: {
        allowedTags: ["h1", "h2", "h3", "p", "a", "strong", "em", "code", "pre", "img", "ul", "ol", "li", "blockquote", "table", "thead", "tbody", "tr", "th", "td"],
        allowedAttributes: {
          a: ["href", "title", "target"],
          img: ["src", "alt", "width", "height"],
          code: ["class"],
          pre: ["class"],
        },
        allowedProtocols: {
          href: ["https", "http", "mailto"],
          src: ["https", "http"],
        },
      },
    }),
  ],
});

Примеры

Удаление опасного HTML

import { compile, sanitize } from "@unifast/node";

const untrustedMd = `
# Hello

<script>alert("xss")</script>

<img src="x" onerror="alert('xss')">

[Click me](javascript:alert('xss'))
`;

const result = compile(untrustedMd, {
  plugins: [sanitize()],
});

console.log(result.output);
// Теги <script>, атрибуты onerror и URL javascript: удаляются

Пользовательский список разрешённых тегов

import { compile, sanitize } from "@unifast/node";

const result = compile(md, {
  plugins: [
    sanitize({
      schema: {
        allowedTags: ["p", "a", "strong", "em", "code", "pre"],
      },
    }),
  ],
});

console.log(result.output);
// Сохраняются только указанные теги; все остальные удаляются

Ограничение URL-протоколов

import { compile, sanitize } from "@unifast/node";

const result = compile(md, {
  plugins: [
    sanitize({
      schema: {
        allowedProtocols: {
          href: ["https", "mailto"],
          src: ["https"],
        },
      },
    }),
  ],
});

console.log(result.output);
// Разрешены только ссылки https: и mailto:

Отключение санитизации

import { compile, sanitize } from "@unifast/node";

const result = compile(md, {
  plugins: [sanitize({ enabled: false })],
});

console.log(result.output);
// Санитизация не применяется — используйте только с доверенным входом