HtmlSanitizerDefaults.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. using AngleSharp.Css.Dom;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.Immutable;
  5. namespace Ganss.Xss;
  6. /// <summary>
  7. /// Default options.
  8. /// </summary>
  9. public static class HtmlSanitizerDefaults
  10. {
  11. /// <summary>
  12. /// The default allowed CSS at-rules.
  13. /// </summary>
  14. public static ISet<CssRuleType> AllowedAtRules { get; } = new HashSet<CssRuleType>()
  15. {
  16. CssRuleType.Style, CssRuleType.Namespace
  17. }.ToImmutableHashSet();
  18. /// <summary>
  19. /// The default allowed URI schemes.
  20. /// </summary>
  21. public static ISet<string> AllowedSchemes { get; } = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
  22. {
  23. "http", "https"
  24. }.ToImmutableHashSet(StringComparer.OrdinalIgnoreCase);
  25. /// <summary>
  26. /// The default allowed HTML tag names.
  27. /// </summary>
  28. public static ISet<string> AllowedTags { get; } = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
  29. {
  30. // https://developer.mozilla.org/en/docs/Web/Guide/HTML/HTML5/HTML5_element_list
  31. "a", "abbr", "acronym", "address", "area", "b",
  32. "big", "blockquote", "br", "button", "caption", "center", "cite",
  33. "code", "col", "colgroup", "dd", "del", "dfn", "dir", "div", "dl", "dt",
  34. "em", "fieldset", "font", "form", "h1", "h2", "h3", "h4", "h5", "h6",
  35. "hr", "i", "img", "input", "ins", "kbd", "label", "legend", "li", "map",
  36. "menu", "ol", "optgroup", "option", "p", "pre", "q", "s", "samp",
  37. "select", "small", "span", "strike", "strong", "sub", "sup", "table",
  38. "tbody", "td", "textarea", "tfoot", "th", "thead", "tr", "tt", "u",
  39. "ul", "var",
  40. // HTML5
  41. // Sections
  42. "section", "nav", "article", "aside", "header", "footer", "main",
  43. // Grouping content
  44. "figure", "figcaption",
  45. // Text-level semantics
  46. "data", "time", "mark", "ruby", "rt", "rp", "bdi", "wbr",
  47. // Forms
  48. "datalist", "keygen", "output", "progress", "meter",
  49. // Interactive elements
  50. "details", "summary", "menuitem",
  51. // document elements
  52. "html", "head", "body"
  53. }.ToImmutableHashSet(StringComparer.OrdinalIgnoreCase);
  54. /// <summary>
  55. /// The default allowed HTML attributes.
  56. /// </summary>
  57. public static ISet<string> AllowedAttributes { get; } = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
  58. {
  59. // https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes
  60. "abbr", "accept", "accept-charset", "accesskey",
  61. "action", "align", "alt", "axis", "bgcolor", "border", "cellpadding",
  62. "cellspacing", "char", "charoff", "charset", "checked", "cite", /* "class", */
  63. "clear", "cols", "colspan", "color", "compact", "coords", "datetime",
  64. "dir", "disabled", "enctype", "for", "frame", "headers", "height",
  65. "href", "hreflang", "hspace", /* "id", */ "ismap", "label", "lang",
  66. "longdesc", "maxlength", "media", "method", "multiple", "name",
  67. "nohref", "noshade", "nowrap", "prompt", "readonly", "rel", "rev",
  68. "rows", "rowspan", "rules", "scope", "selected", "shape", "size",
  69. "span", "src", "start", "style", "summary", "tabindex", "target", "title",
  70. "type", "usemap", "valign", "value", "vspace", "width",
  71. // HTML5
  72. "high", // <meter>
  73. "keytype", // <keygen>
  74. "list", // <input>
  75. "low", // <meter>
  76. "max", // <input>, <meter>, <progress>
  77. "min", // <input>, <meter>
  78. "novalidate", // <form>
  79. "open", // <details>
  80. "optimum", // <meter>
  81. "pattern", // <input>
  82. "placeholder", // <input>, <textarea>
  83. "pubdate", // <time>
  84. "radiogroup", // <menuitem>
  85. "required", // <input>, <select>, <textarea>
  86. "reversed", // <ol>
  87. "spellcheck", // Global attribute
  88. "step", // <input>
  89. "wrap", // <textarea>
  90. "challenge", // <keygen>
  91. "contenteditable", // Global attribute
  92. "draggable", // Global attribute
  93. "dropzone", // Global attribute
  94. "autocomplete", // <form>, <input>
  95. "autosave", // <input>
  96. }.ToImmutableHashSet(StringComparer.OrdinalIgnoreCase);
  97. /// <summary>
  98. /// The default URI attributes.
  99. /// </summary>
  100. public static ISet<string> UriAttributes { get; } = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
  101. {
  102. "action", "background", "dynsrc", "href", "lowsrc", "src"
  103. }.ToImmutableHashSet(StringComparer.OrdinalIgnoreCase);
  104. /// <summary>
  105. /// The default allowed CSS properties.
  106. /// </summary>
  107. public static ISet<string> AllowedCssProperties { get; } = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
  108. {
  109. // CSS 3 properties <http://www.w3.org/TR/CSS/#properties>
  110. "align-content",
  111. "align-items",
  112. "align-self",
  113. "all",
  114. "animation",
  115. "animation-delay",
  116. "animation-direction",
  117. "animation-duration",
  118. "animation-fill-mode",
  119. "animation-iteration-count",
  120. "animation-name",
  121. "animation-play-state",
  122. "animation-timing-function",
  123. "backface-visibility",
  124. "background",
  125. "background-attachment",
  126. "background-blend-mode",
  127. "background-clip",
  128. "background-color",
  129. "background-image",
  130. "background-origin",
  131. "background-position",
  132. "background-position-x",
  133. "background-position-y",
  134. "background-repeat",
  135. "background-repeat-x", // see https://github.com/mganss/HtmlSanitizer/issues/243
  136. "background-repeat-y",
  137. "background-size",
  138. "border",
  139. "border-bottom",
  140. "border-bottom-color",
  141. "border-bottom-left-radius",
  142. "border-bottom-right-radius",
  143. "border-bottom-style",
  144. "border-bottom-width",
  145. "border-collapse",
  146. "border-color",
  147. "border-image",
  148. "border-image-outset",
  149. "border-image-repeat",
  150. "border-image-slice",
  151. "border-image-source",
  152. "border-image-width",
  153. "border-left",
  154. "border-left-color",
  155. "border-left-style",
  156. "border-left-width",
  157. "border-radius",
  158. "border-right",
  159. "border-right-color",
  160. "border-right-style",
  161. "border-right-width",
  162. "border-spacing",
  163. "border-style",
  164. "border-top",
  165. "border-top-color",
  166. "border-top-left-radius",
  167. "border-top-right-radius",
  168. "border-top-style",
  169. "border-top-width",
  170. "border-width",
  171. "bottom",
  172. "box-decoration-break",
  173. "box-shadow",
  174. "box-sizing",
  175. "break-after",
  176. "break-before",
  177. "break-inside",
  178. "caption-side",
  179. "caret-color",
  180. "clear",
  181. "clip",
  182. "color",
  183. "column-count",
  184. "column-fill",
  185. "column-gap",
  186. "column-rule",
  187. "column-rule-color",
  188. "column-rule-style",
  189. "column-rule-width",
  190. "column-span",
  191. "column-width",
  192. "columns",
  193. "content",
  194. "counter-increment",
  195. "counter-reset",
  196. "cursor",
  197. "direction",
  198. "display",
  199. "empty-cells",
  200. "filter",
  201. "flex",
  202. "flex-basis",
  203. "flex-direction",
  204. "flex-flow",
  205. "flex-grow",
  206. "flex-shrink",
  207. "flex-wrap",
  208. "float",
  209. "font",
  210. "font-family",
  211. "font-feature-settings",
  212. "font-kerning",
  213. "font-language-override",
  214. "font-size",
  215. "font-size-adjust",
  216. "font-stretch",
  217. "font-style",
  218. "font-synthesis",
  219. "font-variant",
  220. "font-variant-alternates",
  221. "font-variant-caps",
  222. "font-variant-east-asian",
  223. "font-variant-ligatures",
  224. "font-variant-numeric",
  225. "font-variant-position",
  226. "font-weight",
  227. "gap",
  228. "grid",
  229. "grid-area",
  230. "grid-auto-columns",
  231. "grid-auto-flow",
  232. "grid-auto-rows",
  233. "grid-column",
  234. "grid-column-end",
  235. "grid-column-gap",
  236. "grid-column-start",
  237. "grid-gap",
  238. "grid-row",
  239. "grid-row-end",
  240. "grid-row-gap",
  241. "grid-row-start",
  242. "grid-template",
  243. "grid-template-areas",
  244. "grid-template-columns",
  245. "grid-template-rows",
  246. "hanging-punctuation",
  247. "height",
  248. "hyphens",
  249. "image-rendering",
  250. "isolation",
  251. "justify-content",
  252. "left",
  253. "letter-spacing",
  254. "line-break",
  255. "line-height",
  256. "list-style",
  257. "list-style-image",
  258. "list-style-position",
  259. "list-style-type",
  260. "margin",
  261. "margin-bottom",
  262. "margin-left",
  263. "margin-right",
  264. "margin-top",
  265. "mask",
  266. "mask-clip",
  267. "mask-composite",
  268. "mask-image",
  269. "mask-mode",
  270. "mask-origin",
  271. "mask-position",
  272. "mask-repeat",
  273. "mask-size",
  274. "mask-type",
  275. "max-height",
  276. "max-width",
  277. "min-height",
  278. "min-width",
  279. "mix-blend-mode",
  280. "object-fit",
  281. "object-position",
  282. "opacity",
  283. "order",
  284. "orphans",
  285. "outline",
  286. "outline-color",
  287. "outline-offset",
  288. "outline-style",
  289. "outline-width",
  290. "overflow",
  291. "overflow-wrap",
  292. "overflow-x",
  293. "overflow-y",
  294. "padding",
  295. "padding-bottom",
  296. "padding-left",
  297. "padding-right",
  298. "padding-top",
  299. "page-break-after",
  300. "page-break-before",
  301. "page-break-inside",
  302. "perspective",
  303. "perspective-origin",
  304. "pointer-events",
  305. "position",
  306. "quotes",
  307. "resize",
  308. "right",
  309. "row-gap",
  310. "scroll-behavior",
  311. "tab-size",
  312. "table-layout",
  313. "text-align",
  314. "text-align-last",
  315. "text-combine-upright",
  316. "text-decoration",
  317. "text-decoration-color",
  318. "text-decoration-line",
  319. "text-decoration-skip",
  320. "text-decoration-style",
  321. "text-indent",
  322. "text-justify",
  323. "text-orientation",
  324. "text-overflow",
  325. "text-shadow",
  326. "text-transform",
  327. "text-underline-position",
  328. "top",
  329. "transform",
  330. "transform-origin",
  331. "transform-style",
  332. "transition",
  333. "transition-delay",
  334. "transition-duration",
  335. "transition-property",
  336. "transition-timing-function",
  337. "unicode-bidi",
  338. "user-select",
  339. "vertical-align",
  340. "visibility",
  341. "white-space",
  342. "widows",
  343. "width",
  344. "word-break",
  345. "word-spacing",
  346. "word-wrap",
  347. "writing-mode",
  348. "z-index"
  349. }.ToImmutableHashSet(StringComparer.OrdinalIgnoreCase);
  350. /// <summary>
  351. /// The default allowed CSS classes.
  352. /// </summary>
  353. public static ISet<string> AllowedClasses { get; } = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
  354. .ToImmutableHashSet(StringComparer.OrdinalIgnoreCase);
  355. }