sanitize()

एक sanitization plugin बनाएँ जो compiled output से खतरनाक HTML tags, attributes, और URL protocols को strip करता है।

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

Signature

function sanitize(options?: SanitizePluginOptions): UnifastPlugin

Parameters

options?

Sanitization configuration

PropertyTypeDefaultविवरण
enabledbooleantruesanitization enable या disable करें
schemaSanitizeSchemaCustom sanitization schema

SanitizeSchema

PropertyTypeविवरण
allowedTagsstring[]अनुमत HTML tag names (अन्य सभी strip हो जाते हैं)
allowedAttributesRecord<string, string[]>tag name का अनुमत attribute names पर map
allowedProtocolsRecord<string, string[]>attribute name का अनुमत URL protocols पर map

उपयोग

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 strip करें

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> tags, onerror attributes, और javascript: URLs हटा दिए जाते हैं

Custom allowed tags

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

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

console.log(result.output);
// केवल निर्दिष्ट tags रखे जाते हैं; अन्य सभी strip हो जाते हैं

URL protocols restrict करें

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: links अनुमत हैं

Sanitization disable करें

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

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

console.log(result.output);
// कोई sanitization लागू नहीं — केवल विश्वसनीय input के साथ उपयोग करें