|
| 1 | +/* |
| 2 | + * @since 2020-09-09 22:53:14 |
| 3 | + * @author acrazing <joking.young@gmail.com> |
| 4 | + */ |
| 5 | + |
| 6 | +import { selfCloseTags } from './config'; |
| 7 | +import { parse } from './parse'; |
| 8 | +import { INode, SyntaxKind } from './types'; |
| 9 | + |
| 10 | +export interface SafeHtmlOptions { |
| 11 | + allowedTags: string[]; |
| 12 | + allowedAttrs: string[]; |
| 13 | + tagAllowedAttrs: Record<string, string[]>; |
| 14 | + allowedUrl: RegExp; |
| 15 | +} |
| 16 | + |
| 17 | +export const safeHtmlDefaultOptions: SafeHtmlOptions = { |
| 18 | + allowedTags: [ |
| 19 | + 'div', |
| 20 | + 'p', |
| 21 | + 'h1', |
| 22 | + 'h2', |
| 23 | + 'h3', |
| 24 | + 'h4', |
| 25 | + 'h5', |
| 26 | + 'h6', |
| 27 | + 'ol', |
| 28 | + 'ul', |
| 29 | + 'li', |
| 30 | + 'table', |
| 31 | + 'thead', |
| 32 | + 'tbody', |
| 33 | + 'tr', |
| 34 | + 'th', |
| 35 | + 'td', |
| 36 | + 'span', |
| 37 | + 'a', |
| 38 | + 'img', |
| 39 | + ], |
| 40 | + allowedAttrs: ['style'], |
| 41 | + tagAllowedAttrs: { |
| 42 | + a: ['href', 'target'], |
| 43 | + img: ['src'], |
| 44 | + }, |
| 45 | + allowedUrl: /^(?:mailto|tel|https?|ftp|[^:]*[^a-z0-9.+-][^:]*):|^[^:]*$/i, |
| 46 | +}; |
| 47 | + |
| 48 | +export function safeHtml( |
| 49 | + input: string, |
| 50 | + options: Partial<SafeHtmlOptions> = {}, |
| 51 | +): string { |
| 52 | + const config: SafeHtmlOptions = { |
| 53 | + ...safeHtmlDefaultOptions, |
| 54 | + ...options, |
| 55 | + tagAllowedAttrs: { |
| 56 | + ...safeHtmlDefaultOptions.tagAllowedAttrs, |
| 57 | + ...options.tagAllowedAttrs, |
| 58 | + }, |
| 59 | + }; |
| 60 | + const ast = parse(input); |
| 61 | + return stringify(ast, config, input); |
| 62 | +} |
| 63 | + |
| 64 | +function stringify( |
| 65 | + ast: INode[], |
| 66 | + config: SafeHtmlOptions, |
| 67 | + input: string, |
| 68 | +): string { |
| 69 | + return ast |
| 70 | + .map((node) => { |
| 71 | + if (node.type === SyntaxKind.Text) { |
| 72 | + return node.value; |
| 73 | + } |
| 74 | + if (config.allowedTags.indexOf(node.name) === -1) { |
| 75 | + return ''; |
| 76 | + } |
| 77 | + if (selfCloseTags[node.name]) { |
| 78 | + if (node.body !== void 0) { |
| 79 | + throw new Error( |
| 80 | + `self closed tag "${node.name}" should not have body`, |
| 81 | + ); |
| 82 | + } |
| 83 | + } else { |
| 84 | + if (!node.body || !node.close) { |
| 85 | + throw new Error(`tag "${node.name}" should have body and close`); |
| 86 | + } |
| 87 | + } |
| 88 | + const attrs = node.attributes |
| 89 | + .filter((a) => { |
| 90 | + if ( |
| 91 | + config.allowedAttrs.indexOf(a.name.value) > -1 || |
| 92 | + config.tagAllowedAttrs[node.name]?.indexOf(a.name.value) > -1 |
| 93 | + ) { |
| 94 | + if (!a.value) { |
| 95 | + return true; |
| 96 | + } |
| 97 | + if (a.name.value !== 'src' && a.name.value !== 'href') { |
| 98 | + return true; |
| 99 | + } |
| 100 | + if (config.allowedUrl.test(a.value.value)) { |
| 101 | + return true; |
| 102 | + } |
| 103 | + return false; |
| 104 | + } |
| 105 | + return false; |
| 106 | + }) |
| 107 | + .map((a) => input.substring(a.start, a.end)) |
| 108 | + .join(' '); |
| 109 | + const head = '<' + node.rawName + (attrs ? ' ' + attrs : '') + '>'; |
| 110 | + if (!node.body) { |
| 111 | + return head; |
| 112 | + } |
| 113 | + return head + stringify(node.body, config, input) + `</${node.rawName}>`; |
| 114 | + }) |
| 115 | + .join(''); |
| 116 | +} |
0 commit comments