HtmlPolicy.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace App\Services\Markup\Html;
  3. /**
  4. * 正文里原始 HTML 的白名单。
  5. *
  6. * v2 把原始 HTML 原样交给前端(XmlToReact 逐个 createElement),任何标签、任何属性都能进 DOM。
  7. * 这里收紧:
  8. * - 认识的标签保留;脚本、iframe、svg、表单等连同内容一起丢掉
  9. * - 不认识的标签(库里有 `<nt>`、`<r>`、`<commentary>` 等自定义标签)改成 `span`,原名放 `data-tag`
  10. * - 属性只留白名单,`style` 与 `on*` 一律去掉;链接与图片地址只允许 http(s)、mailto、站内相对路径
  11. */
  12. final class HtmlPolicy
  13. {
  14. /** 认识的标签:保留原名 */
  15. private const ALLOWED = [
  16. 'a', 'abbr', 'b', 'bdi', 'bdo', 'blockquote', 'br', 'caption', 'center', 'cite', 'code', 'col', 'colgroup',
  17. 'dd', 'del', 'details', 'dfn', 'div', 'dl', 'dt', 'em', 'figcaption', 'figure', 'font', 'h1', 'h2', 'h3',
  18. 'h4', 'h5', 'h6', 'hr', 'i', 'img', 'ins', 'kbd', 'li', 'mark', 'ol', 'p', 'pre', 'q', 'rp', 'rt', 'ruby',
  19. 's', 'samp', 'small', 'span', 'strong', 'sub', 'summary', 'sup', 'table', 'tbody', 'td', 'tfoot', 'th',
  20. 'thead', 'tr', 'u', 'ul', 'var', 'wbr', 'video', 'audio', 'source',
  21. ];
  22. /** 已废弃的展示类标签:换成等价的容器 */
  23. private const RENAMED = ['center' => 'div', 'font' => 'span'];
  24. /** 连同内容一起丢掉 */
  25. private const DROPPED = [
  26. 'script', 'style', 'iframe', 'frame', 'frameset', 'object', 'embed', 'applet', 'svg', 'math', 'form',
  27. 'input', 'button', 'select', 'textarea', 'option', 'template', 'noscript', 'link', 'meta', 'base',
  28. 'title', 'head', 'html', 'body', 'canvas', 'dialog', 'portal',
  29. ];
  30. /** 自闭合标签 */
  31. public const VOID = ['br', 'hr', 'img', 'wbr', 'col', 'source'];
  32. /** role / tabindex / aria-*:阅读页注入的可点击图标(评估对勾、issue 叹号)要靠它们支持键盘与读屏 */
  33. private const GLOBAL_ATTRS = ['class', 'title', 'lang', 'dir', 'role', 'tabindex'];
  34. private const TAG_ATTRS = [
  35. 'a' => ['href'],
  36. 'img' => ['src', 'alt', 'width', 'height'],
  37. 'video' => ['src', 'controls', 'width', 'height', 'poster'],
  38. 'audio' => ['src', 'controls'],
  39. 'source' => ['src', 'type'],
  40. 'td' => ['colspan', 'rowspan', 'align'],
  41. 'th' => ['colspan', 'rowspan', 'align'],
  42. 'ol' => ['start'],
  43. 'details' => ['open'],
  44. ];
  45. private const URL_ATTRS = ['href', 'src', 'poster'];
  46. /**
  47. * 标签的去向。
  48. *
  49. * @return string|false|null string:保留并用这个名字;null:改成 span(原名放 data-tag);false:连同内容丢掉
  50. */
  51. public static function tag(string $tag): string|false|null
  52. {
  53. $tag = strtolower($tag);
  54. if (in_array($tag, self::DROPPED, true)) {
  55. return false;
  56. }
  57. if (in_array($tag, self::ALLOWED, true)) {
  58. return self::RENAMED[$tag] ?? $tag;
  59. }
  60. return null;
  61. }
  62. /**
  63. * 过滤属性。
  64. *
  65. * @param array<string, string> $attrs 原始属性(键已小写)
  66. * @return array<string, string>
  67. */
  68. public static function attrs(string $tag, array $attrs): array
  69. {
  70. $allowed = array_merge(self::GLOBAL_ATTRS, self::TAG_ATTRS[$tag] ?? []);
  71. $clean = [];
  72. foreach ($attrs as $name => $value) {
  73. $name = strtolower($name);
  74. $isData = preg_match('/^(data|aria)-[a-z0-9][a-z0-9-]*$/', $name) === 1;
  75. if (! $isData && ! in_array($name, $allowed, true)) {
  76. continue;
  77. }
  78. if (in_array($name, self::URL_ATTRS, true)) {
  79. $value = self::url($value);
  80. if ($value === null) {
  81. continue;
  82. }
  83. }
  84. $clean[$name] = $value;
  85. }
  86. return $clean;
  87. }
  88. /**
  89. * 地址白名单:http(s)、mailto、站内绝对 / 相对路径、锚点。其余(javascript:、data: 等)返回 null。
  90. */
  91. public static function url(string $url): ?string
  92. {
  93. $url = trim($url);
  94. // 去掉控制字符与空白后再判断协议,防 `java\tscript:` 之类绕过
  95. $probe = strtolower((string) preg_replace('/[\x00-\x20]+/', '', $url));
  96. if (preg_match('/^[a-z][a-z0-9+.-]*:/', $probe, $m) !== 1) {
  97. return $url;
  98. }
  99. return in_array($m[0], ['http:', 'https:', 'mailto:'], true) ? $url : null;
  100. }
  101. }