{"author":{"name":"Titus Wormer","email":"tituswormer@gmail.com","url":"https://wooorm.com"},"bugs":{"url":"https://github.com/syntax-tree/hast-util-sanitize/issues"},"contributors":[{"name":"Jason Switzer","email":"jswitzer@gmail.com"},{"name":"Titus Wormer","email":"tituswormer@gmail.com","url":"https://wooorm.com"}],"dependencies":{"@types/hast":"^3.0.0","@ungap/structured-clone":"^1.0.0","unist-util-position":"^5.0.0"},"description":"hast utility to sanitize nodes","devDependencies":{"@types/node":"^22.0.0","@types/ungap__structured-clone":"^1.0.0","aria-attributes":"^2.0.0","c8":"^10.0.0","deepmerge":"^4.0.0","hast-util-from-html":"^2.0.0","hast-util-to-html":"^9.0.0","hastscript":"^9.0.0","html-element-attributes":"^3.0.0","html-tag-names":"^2.0.0","prettier":"^3.0.0","remark-cli":"^12.0.0","remark-preset-wooorm":"^10.0.0","type-coverage":"^2.0.0","typescript":"^5.0.0","unist-builder":"^4.0.0","unist-util-visit":"^5.0.0","xo":"^0.59.0"},"exports":"./index.js","funding":{"type":"opencollective","url":"https://opencollective.com/unified"},"keywords":["clean","hast-util","hast","html","safe","sanitize","utility","util","unist","xss"],"license":"MIT","name":"hast-util-sanitize","prettier":{"bracketSpacing":false,"semi":false,"singleQuote":true,"tabWidth":2,"trailingComma":"none","useTabs":false},"remarkConfig":{"plugins":["remark-preset-wooorm"]},"repository":{"type":"git","url":"git+https://github.com/syntax-tree/hast-util-sanitize.git"},"scripts":{"build":"tsc --build --clean && tsc --build && type-coverage","format":"remark . -qfo && prettier . -w --log-level warn && xo --fix","test-api":"node --conditions development test/index.js","test-coverage":"c8 --100 --reporter lcov npm run test-api","test":"npm run build && npm run format && npm run test-coverage"},"sideEffects":false,"typeCoverage":{"atLeast":100,"#":"Couple of needed `any`s","ignoreFiles":["lib/index.d.ts"],"strict":true},"type":"module","version":"5.0.2","xo":{"overrides":[{"files":["test/**/*.js"],"rules":{"max-nested-callbacks":"off","no-await-in-loop":"off","no-script-url":"off"}}],"prettier":true,"rules":{"complexity":"off","logical-assignment-operators":"off","unicorn/prefer-at":"off"}},"_id":"hast-util-sanitize@5.0.2","gitHead":"7f30d9e6261583efc544ff6a93ba54ca6e53e1b5","types":"./index.d.ts","homepage":"https://github.com/syntax-tree/hast-util-sanitize#readme","_nodeVersion":"22.7.0","_npmVersion":"10.9.0","dist":{"integrity":"sha512-3yTWghByc50aGS7JlGhk61SPenfE/p1oaFeNwkOOyrscaOkMGrcW9+Cy/QAIOBpZxP1yqDIzFMR0+Np0i0+usg==","shasum":"edb260d94e5bba2030eb9375790a8753e5bf391f","tarball":"http://registry-npm.ywwl.com/hast-util-sanitize/download/hast-util-sanitize-5.0.2.tgz","fileCount":12,"unpackedSize":47656,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCICayDW9Eeu2ztSQ9vkKUbSwayLAtcTRQeI4oUV2lEj9NAiAtcdyUIrBUtubCyseqjIkU3jeSm4lC0/BPgsLqpHheOQ=="}],"size":13292,"noattachment":false,"key":"/hast-util-sanitize/-/hast-util-sanitize-5.0.2.tgz"},"_npmUser":{"name":"wooorm","email":"tituswormer@gmail.com"},"directories":{},"maintainers":[{"name":"wooorm","email":"tituswormer@gmail.com"},{"name":"kmck","email":"keith@mcknig.ht"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/hast-util-sanitize_5.0.2_1729863386937_0.37527453475674455"},"_hasShrinkwrap":false,"readme":"# hast-util-sanitize\n\n[![Build][build-badge]][build]\n[![Coverage][coverage-badge]][coverage]\n[![Downloads][downloads-badge]][downloads]\n[![Size][size-badge]][size]\n[![Sponsors][sponsors-badge]][collective]\n[![Backers][backers-badge]][collective]\n[![Chat][chat-badge]][chat]\n\n[hast][] utility to make trees safe.\n\n## Contents\n\n* [What is this?](#what-is-this)\n* [When should I use this?](#when-should-i-use-this)\n* [Install](#install)\n* [Use](#use)\n* [API](#api)\n  * [`defaultSchema`](#defaultschema)\n  * [`sanitize(tree[, options])`](#sanitizetree-options)\n  * [`Schema`](#schema)\n* [Types](#types)\n* [Compatibility](#compatibility)\n* [Security](#security)\n* [Related](#related)\n* [Contribute](#contribute)\n* [License](#license)\n\n## What is this?\n\nThis package is a utility that can make a tree that potentially contains\ndangerous user content safe for use.\nIt defaults to what GitHub does to clean unsafe markup, but you can change that.\n\n## When should I use this?\n\nThis package is needed whenever you deal with potentially dangerous user\ncontent.\n\nThe plugin [`rehype-sanitize`][rehype-sanitize] wraps this utility to also\nsanitize HTML at a higher-level (easier) abstraction.\n\n## Install\n\nThis package is [ESM only][esm].\nIn Node.js (version 16+), install with [npm][]:\n\n```sh\nnpm install hast-util-sanitize\n```\n\nIn Deno with [`esm.sh`][esmsh]:\n\n```js\nimport {sanitize} from 'https://esm.sh/hast-util-sanitize@5'\n```\n\nIn browsers with [`esm.sh`][esmsh]:\n\n```html\n<script type=\"module\">\n  import {sanitize} from 'https://esm.sh/hast-util-sanitize@5?bundle'\n</script>\n```\n\n## Use\n\n```js\nimport {h} from 'hastscript'\nimport {sanitize} from 'hast-util-sanitize'\nimport {toHtml} from 'hast-util-to-html'\nimport {u} from 'unist-builder'\n\nconst unsafe = h('div', {onmouseover: 'alert(\"alpha\")'}, [\n  h(\n    'a',\n    {href: 'jAva script:alert(\"bravo\")', onclick: 'alert(\"charlie\")'},\n    'delta'\n  ),\n  u('text', '\\n'),\n  h('script', 'alert(\"charlie\")'),\n  u('text', '\\n'),\n  h('img', {src: 'x', onerror: 'alert(\"delta\")'}),\n  u('text', '\\n'),\n  h('iframe', {src: 'javascript:alert(\"echo\")'}),\n  u('text', '\\n'),\n  h('math', h('mi', {'xlink:href': 'data:x,<script>alert(\"foxtrot\")</script>'}))\n])\n\nconst safe = sanitize(unsafe)\n\nconsole.log(toHtml(unsafe))\nconsole.log(toHtml(safe))\n```\n\nUnsafe:\n\n```html\n<div onmouseover=\"alert(&#x22;alpha&#x22;)\"><a href=\"jAva script:alert(&#x22;bravo&#x22;)\" onclick=\"alert(&#x22;charlie&#x22;)\">delta</a>\n<script>alert(\"charlie\")</script>\n<img src=\"x\" onerror=\"alert(&#x22;delta&#x22;)\">\n<iframe src=\"javascript:alert(&#x22;echo&#x22;)\"></iframe>\n<math><mi xlink:href=\"data:x,<script>alert(&#x22;foxtrot&#x22;)</script>\"></mi></math></div>\n```\n\nSafe:\n\n```html\n<div><a>delta</a>\n\n<img src=\"x\">\n\n</div>\n```\n\n## API\n\nThis package exports the identifiers [`defaultSchema`][api-default-schema] and\n[`sanitize`][api-sanitize].\nThere is no default export.\n\n### `defaultSchema`\n\nDefault schema ([`Schema`][api-schema]).\n\nFollows [GitHub][] style sanitation.\n\n### `sanitize(tree[, options])`\n\nSanitize a tree.\n\n###### Parameters\n\n* `tree` ([`Node`][node])\n  — unsafe tree\n* `options` ([`Schema`][api-schema], default:\n  [`defaultSchema`][api-default-schema])\n  — configuration\n\n###### Returns\n\nNew, safe tree ([`Node`][node]).\n\n### `Schema`\n\nSchema that defines what nodes and properties are allowed.\n\nThe default schema is [`defaultSchema`][api-default-schema], which follows how\nGitHub cleans.\nIf any top-level key is missing in the given schema, the corresponding\nvalue of the default schema is used.\n\nTo extend the standard schema with a few changes, clone `defaultSchema`\nlike so:\n\n```js\nimport deepmerge from 'deepmerge'\nimport {h} from 'hastscript'\nimport {defaultSchema, sanitize} from 'hast-util-sanitize'\n\n// This allows `className` on all elements.\nconst schema = deepmerge(defaultSchema, {attributes: {'*': ['className']}})\n\nconst tree = sanitize(h('div', {className: ['foo']}), schema)\n\n// `tree` still has `className`.\nconsole.log(tree)\n// {\n//   type: 'element',\n//   tagName: 'div',\n//   properties: {className: ['foo']},\n//   children: []\n// }\n```\n\n##### Fields\n\n###### `allowComments`\n\nWhether to allow comment nodes (`boolean`, default: `false`).\n\nFor example:\n\n```js\nallowComments: true\n```\n\n###### `allowDoctypes`\n\nWhether to allow doctype nodes (`boolean`, default: `false`).\n\nFor example:\n\n```js\nallowDoctypes: true\n```\n\n###### `ancestors`\n\nMap of tag names to a list of tag names which are required ancestors\n(`Record<string, Array<string>>`, default: `defaultSchema.ancestors`).\n\nElements with these tag names will be ignored if they occur outside of one\nof their allowed parents.\n\nFor example:\n\n```js\nancestors: {\n  tbody: ['table'],\n  // …\n  tr: ['table']\n}\n```\n\n###### `attributes`\n\nMap of tag names to allowed [property names][name]\n(`Record<string, Array<[string, ...Array<RegExp | boolean | number | string>] | string>`,\ndefault: `defaultSchema.attributes`).\n\nThe special key `'*'` as a tag name defines property names allowed on all\nelements.\n\nThe special value `'data*'` as a property name can be used to allow all `data`\nproperties.\n\nFor example:\n\n```js\nattributes: {\n  a: [\n    'ariaDescribedBy', 'ariaLabel', 'ariaLabelledBy', /* … */, 'href'\n  ],\n  // …\n  '*': [\n    'abbr',\n    'accept',\n    'acceptCharset',\n    // …\n    'vAlign',\n    'value',\n    'width'\n  ]\n}\n```\n\nInstead of a single string in the array, which allows any property value for\nthe field, you can use an array to allow several values.\nFor example, `input: ['type']` allows `type` set to any value on `input`s.\nBut `input: [['type', 'checkbox', 'radio']]` allows `type` when set to\n`'checkbox'` or `'radio'`.\n\nYou can use regexes, so for example `span: [['className', /^hljs-/]]` allows\nany class that starts with `hljs-` on `span`s.\n\nWhen comma- or space-separated values are used (such as `className`), each\nvalue in is checked individually.\nFor example, to allow certain classes on `span`s for syntax highlighting, use\n`span: [['className', 'number', 'operator', 'token']]`.\nThis will allow `'number'`, `'operator'`, and `'token'` classes, but drop\nothers.\n\n###### `clobber`\n\nList of [*property names*][name] that clobber (`Array<string>`, default:\n`defaultSchema.clobber`).\n\nFor example:\n\n```js\nclobber: ['ariaDescribedBy', 'ariaLabelledBy', 'id', 'name']\n```\n\n###### `clobberPrefix`\n\nPrefix to use before clobbering properties (`string`, default:\n`defaultSchema.clobberPrefix`).\n\nFor example:\n\n```js\nclobberPrefix: 'user-content-'\n```\n\n###### `protocols`\n\nMap of [*property names*][name] to allowed protocols\n(`Record<string, Array<string>>`, default: `defaultSchema.protocols`).\n\nThis defines URLs that are always allowed to have local URLs (relative to\nthe current website, such as `this`, `#this`, `/this`, or `?this`), and\nonly allowed to have remote URLs (such as `https://example.com`) if they\nuse a known protocol.\n\nFor example:\n\n```js\nprotocols: {\n  cite: ['http', 'https'],\n  // …\n  src: ['http', 'https']\n}\n```\n\n###### `required`\n\nMap of tag names to required [*property names*][name] with a default value\n(`Record<string, Record<string, unknown>>`, default: `defaultSchema.required`).\n\nThis defines properties that must be set.\nIf a field does not exist (after the element was made safe), these will be\nadded with the given value.\n\nFor example:\n\n```js\nrequired: {\n  input: {disabled: true, type: 'checkbox'}\n}\n```\n\n> 👉 **Note**: properties are first checked based on `schema.attributes`,\n> then on `schema.required`.\n> That means properties could be removed by `attributes` and then added\n> again with `required`.\n\n###### `strip`\n\nList of tag names to strip from the tree (`Array<string>`, default:\n`defaultSchema.strip`).\n\nBy default, unsafe elements (those not in `schema.tagNames`) are replaced by\nwhat they contain.\nThis option can drop their contents.\n\nFor example:\n\n```js\nstrip: ['script']\n```\n\n###### `tagNames`\n\nList of allowed tag names (`Array<string>`, default: `defaultSchema.tagNames`).\n\nFor example:\n\n```js\ntagNames: [\n  'a',\n  'b',\n  // …\n  'ul',\n  'var'\n]\n```\n\n## Types\n\nThis package is fully typed with [TypeScript][].\nIt exports the additional type [`Schema`][api-schema].\n\n## Compatibility\n\nProjects maintained by the unified collective are compatible with maintained\nversions of Node.js.\n\nWhen we cut a new major release, we drop support for unmaintained versions of\nNode.\nThis means we try to keep the current release line, `hast-util-sanitize@^5`,\ncompatible with Node.js 16.\n\n## Security\n\nBy default, `hast-util-sanitize` will make everything safe to use.\nAssuming you understand that certain attributes (including a limited set of\nclasses) can be generated by users, and you write your CSS (and JS)\naccordingly.\nWhen used incorrectly, deviating from the defaults can open you up to a\n[cross-site scripting (XSS)][xss] attack.\n\nUse `hast-util-sanitize` after the last unsafe thing: everything after it could\nbe unsafe (but is fine if you do trust it).\n\n## Related\n\n* [`rehype-sanitize`](https://github.com/rehypejs/rehype-sanitize)\n  — rehype plugin\n\n## Contribute\n\nSee [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for\nways to get started.\nSee [`support.md`][support] for ways to get help.\n\nThis project has a [code of conduct][coc].\nBy interacting with this repository, organization, or community you agree to\nabide by its terms.\n\n## License\n\n[MIT][license] © [Titus Wormer][author]\n\n<!-- Definitions -->\n\n[build-badge]: https://github.com/syntax-tree/hast-util-sanitize/workflows/main/badge.svg\n\n[build]: https://github.com/syntax-tree/hast-util-sanitize/actions\n\n[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/hast-util-sanitize.svg\n\n[coverage]: https://codecov.io/github/syntax-tree/hast-util-sanitize\n\n[downloads-badge]: https://img.shields.io/npm/dm/hast-util-sanitize.svg\n\n[downloads]: https://www.npmjs.com/package/hast-util-sanitize\n\n[size-badge]: https://img.shields.io/badge/dynamic/json?label=minzipped%20size&query=$.size.compressedSize&url=https://deno.bundlejs.com/?q=hast-util-sanitize\n\n[size]: https://bundlejs.com/?q=hast-util-sanitize\n\n[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg\n\n[backers-badge]: https://opencollective.com/unified/backers/badge.svg\n\n[collective]: https://opencollective.com/unified\n\n[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg\n\n[chat]: https://github.com/syntax-tree/unist/discussions\n\n[npm]: https://docs.npmjs.com/cli/install\n\n[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c\n\n[esmsh]: https://esm.sh\n\n[typescript]: https://www.typescriptlang.org\n\n[license]: license\n\n[author]: https://wooorm.com\n\n[health]: https://github.com/syntax-tree/.github\n\n[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md\n\n[support]: https://github.com/syntax-tree/.github/blob/main/support.md\n\n[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md\n\n[hast]: https://github.com/syntax-tree/hast\n\n[node]: https://github.com/syntax-tree/hast#nodes\n\n[name]: https://github.com/syntax-tree/hast#propertyname\n\n[github]: https://github.com/gjtorikian/html-pipeline/blob/a2e02ac/lib/html_pipeline/sanitization_filter.rb\n\n[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting\n\n[rehype-sanitize]: https://github.com/rehypejs/rehype-sanitize\n\n[api-default-schema]: #defaultschema\n\n[api-sanitize]: #sanitizetree-options\n\n[api-schema]: #schema\n","publish_time":1729863387148,"_cnpm_publish_time":1729863387148,"dist-tags":{"latest":"5.0.2"}}