html-sanitization.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. .. _advanced.sanitization:
  2. Sanitization
  3. ============
  4. Most feeds embed :abbr:`HTML (HyperText Markup Language)` markup within feed
  5. elements. Some feeds even embed other types of markup, such as :abbr:`SVG
  6. (Scalable Vector Graphics)` or :abbr:`MathML (Mathematical Markup Language)`.
  7. Since many feed aggregators use a web browser (or browser component) to display
  8. content, :program:`Universal Feed Parser` sanitizes embedded markup to remove
  9. things that could pose security risks.
  10. These elements are sanitized by default:
  11. * :ref:`reference.entry.content`
  12. * :ref:`reference.entry.summary`
  13. * :ref:`reference.entry.title`
  14. * :ref:`reference.feed.info`
  15. * :ref:`reference.feed.rights`
  16. * :ref:`reference.feed.subtitle`
  17. * :ref:`reference.feed.title`
  18. .. note::
  19. If the content is declared to be (or is determined to be)
  20. :mimetype:`text/plain`, it will not be sanitized. This is to avoid data loss.
  21. It is recommended that you check the content type in e.g.
  22. :py:attr:`entries[i].summary_detail.type`. If it is :mimetype:`text/plain` then
  23. it has not been sanitized (and you should perform HTML escaping before
  24. rendering the content).
  25. .. _advanced.sanitization.html:
  26. :abbr:`HTML (HyperText Markup Language)` Sanitization
  27. -----------------------------------------------------
  28. The following :abbr:`HTML (HyperText Markup Language)` elements are allowed by
  29. default (all others are stripped):
  30. .. hlist::
  31. :columns: 3
  32. * a
  33. * abbr
  34. * acronym
  35. * address
  36. * area
  37. * article
  38. * aside
  39. * audio
  40. * b
  41. * big
  42. * blockquote
  43. * br
  44. * button
  45. * canvas
  46. * caption
  47. * center
  48. * cite
  49. * code
  50. * col
  51. * colgroup
  52. * command
  53. * datagrid
  54. * datalist
  55. * dd
  56. * del
  57. * details
  58. * dfn
  59. * dialog
  60. * dir
  61. * div
  62. * dl
  63. * dt
  64. * em
  65. * event-source
  66. * fieldset
  67. * figure
  68. * font
  69. * footer
  70. * form
  71. * h1
  72. * h2
  73. * h3
  74. * h4
  75. * h5
  76. * h6
  77. * header
  78. * hr
  79. * i
  80. * img
  81. * input
  82. * ins
  83. * kbd
  84. * keygen
  85. * label
  86. * legend
  87. * li
  88. * m
  89. * map
  90. * menu
  91. * meter
  92. * multicol
  93. * nav
  94. * nextid
  95. * noscript
  96. * ol
  97. * optgroup
  98. * option
  99. * output
  100. * p
  101. * pre
  102. * progress
  103. * q
  104. * s
  105. * samp
  106. * section
  107. * select
  108. * small
  109. * sound
  110. * source
  111. * spacer
  112. * span
  113. * strike
  114. * strong
  115. * sub
  116. * sup
  117. * table
  118. * tbody
  119. * td
  120. * textarea
  121. * tfoot
  122. * th
  123. * thead
  124. * time
  125. * tr
  126. * tt
  127. * u
  128. * ul
  129. * var
  130. * video
  131. The following :abbr:`HTML (HyperText Markup Language)` attributes are allowed
  132. by default (all others are stripped):
  133. .. hlist::
  134. :columns: 3
  135. * abbr
  136. * accept
  137. * accept-charset
  138. * accesskey
  139. * action
  140. * align
  141. * alt
  142. * autocomplete
  143. * autofocus
  144. * autoplay
  145. * axis
  146. * background
  147. * balance
  148. * bgcolor
  149. * bgproperties
  150. * border
  151. * bordercolor
  152. * bordercolordark
  153. * bordercolorlight
  154. * bottompadding
  155. * cellpadding
  156. * cellspacing
  157. * ch
  158. * challenge
  159. * char
  160. * charoff
  161. * charset
  162. * checked
  163. * choff
  164. * cite
  165. * class
  166. * clear
  167. * color
  168. * cols
  169. * colspan
  170. * compact
  171. * contenteditable
  172. * coords
  173. * data
  174. * datafld
  175. * datapagesize
  176. * datasrc
  177. * datetime
  178. * default
  179. * delay
  180. * dir
  181. * disabled
  182. * draggable
  183. * dynsrc
  184. * enctype
  185. * end
  186. * face
  187. * for
  188. * form
  189. * frame
  190. * galleryimg
  191. * gutter
  192. * headers
  193. * height
  194. * hidden
  195. * hidefocus
  196. * high
  197. * href
  198. * hreflang
  199. * hspace
  200. * icon
  201. * id
  202. * inputmode
  203. * ismap
  204. * keytype
  205. * label
  206. * lang
  207. * leftspacing
  208. * list
  209. * longdesc
  210. * loop
  211. * loopcount
  212. * loopend
  213. * loopstart
  214. * low
  215. * lowsrc
  216. * max
  217. * maxlength
  218. * media
  219. * method
  220. * min
  221. * multiple
  222. * name
  223. * nohref
  224. * noshade
  225. * nowrap
  226. * open
  227. * optimum
  228. * pattern
  229. * ping
  230. * point-size
  231. * poster
  232. * pqg
  233. * preload
  234. * prompt
  235. * radiogroup
  236. * readonly
  237. * rel
  238. * repeat-max
  239. * repeat-min
  240. * replace
  241. * required
  242. * rev
  243. * rightspacing
  244. * rows
  245. * rowspan
  246. * rules
  247. * scope
  248. * selected
  249. * shape
  250. * size
  251. * span
  252. * src
  253. * start
  254. * step
  255. * summary
  256. * suppress
  257. * tabindex
  258. * target
  259. * template
  260. * title
  261. * toppadding
  262. * type
  263. * unselectable
  264. * urn
  265. * usemap
  266. * valign
  267. * value
  268. * variable
  269. * volume
  270. * vrml
  271. * vspace
  272. * width
  273. * wrap
  274. * xml:lang
  275. .. _advanced.sanitization.svg:
  276. :abbr:`SVG (Scalable Vector Graphics)` Sanitization
  277. ---------------------------------------------------
  278. The following SVG elements are allowed by default (all others are stripped):
  279. .. hlist::
  280. :columns: 3
  281. * a
  282. * animate
  283. * animateColor
  284. * animateMotion
  285. * animateTransform
  286. * circle
  287. * defs
  288. * desc
  289. * ellipse
  290. * font-face
  291. * font-face-name
  292. * font-face-src
  293. * foreignObject
  294. * g
  295. * glyph
  296. * hkern
  297. * line
  298. * linearGradient
  299. * marker
  300. * metadata
  301. * missing-glyph
  302. * mpath
  303. * path
  304. * polygon
  305. * polyline
  306. * radialGradient
  307. * rect
  308. * set
  309. * stop
  310. * svg
  311. * switch
  312. * text
  313. * title
  314. * tspan
  315. * use
  316. The following :abbr:`SVG (Scalable Vector Graphics)` attributes are allowed by
  317. default (all others are stripped):
  318. .. hlist::
  319. :columns: 3
  320. * accent-height
  321. * accumulate
  322. * additive
  323. * alphabetic
  324. * arabic-form
  325. * ascent
  326. * attributeName
  327. * attributeType
  328. * baseProfile
  329. * bbox
  330. * begin
  331. * by
  332. * calcMode
  333. * cap-height
  334. * class
  335. * color
  336. * color-rendering
  337. * content
  338. * cx
  339. * cy
  340. * d
  341. * descent
  342. * display
  343. * dur
  344. * dx
  345. * dy
  346. * end
  347. * fill
  348. * fill-opacity
  349. * fill-rule
  350. * font-family
  351. * font-size
  352. * font-stretch
  353. * font-style
  354. * font-variant
  355. * font-weight
  356. * from
  357. * fx
  358. * fy
  359. * g1
  360. * g2
  361. * glyph-name
  362. * gradientUnits
  363. * hanging
  364. * height
  365. * horiz-adv-x
  366. * horiz-origin-x
  367. * id
  368. * ideographic
  369. * k
  370. * keyPoints
  371. * keySplines
  372. * keyTimes
  373. * lang
  374. * marker-end
  375. * marker-mid
  376. * marker-start
  377. * markerHeight
  378. * markerUnits
  379. * markerWidth
  380. * mathematical
  381. * max
  382. * min
  383. * name
  384. * offset
  385. * opacity
  386. * orient
  387. * origin
  388. * overline-position
  389. * overline-thickness
  390. * panose-1
  391. * path
  392. * pathLength
  393. * points
  394. * preserveAspectRatio
  395. * r
  396. * refX
  397. * refY
  398. * repeatCount
  399. * repeatDur
  400. * requiredExtensions
  401. * requiredFeatures
  402. * restart
  403. * rotate
  404. * rx
  405. * ry
  406. * slope
  407. * stemh
  408. * stemv
  409. * stop-color
  410. * stop-opacity
  411. * strikethrough-position
  412. * strikethrough-thickness
  413. * stroke
  414. * stroke-dasharray
  415. * stroke-dashoffset
  416. * stroke-linecap
  417. * stroke-linejoin
  418. * stroke-miterlimit
  419. * stroke-opacity
  420. * stroke-width
  421. * systemLanguage
  422. * target
  423. * text-anchor
  424. * to
  425. * transform
  426. * type
  427. * u1
  428. * u2
  429. * underline-position
  430. * underline-thickness
  431. * unicode
  432. * unicode-range
  433. * units-per-em
  434. * values
  435. * version
  436. * viewBox
  437. * visibility
  438. * width
  439. * widths
  440. * x
  441. * x-height
  442. * x1
  443. * x2
  444. * xlink:actuate
  445. * xlink:arcrole
  446. * xlink:href
  447. * xlink:role
  448. * xlink:show
  449. * xlink:title
  450. * xlink:type
  451. * xml:base
  452. * xml:lang
  453. * xml:space
  454. * xmlns
  455. * xmlns:xlink
  456. * y
  457. * y1
  458. * y2
  459. * zoomAndPan
  460. .. _advanced.sanitization.mathml:
  461. :abbr:`MathML (Mathematical Markup Language)` Sanitization
  462. ----------------------------------------------------------
  463. The following :abbr:`MathML (Mathematical Markup Language)` elements are
  464. allowed by default (all others are stripped):
  465. .. hlist::
  466. :columns: 3
  467. * annotation
  468. * annotation-xml
  469. * maction
  470. * maligngroup
  471. * malignmark
  472. * math
  473. * menclose
  474. * merror
  475. * mfenced
  476. * mfrac
  477. * mglyph
  478. * mi
  479. * mlabeledtr
  480. * mlongdiv
  481. * mmultiscripts
  482. * mn
  483. * mo
  484. * mover
  485. * mpadded
  486. * mphantom
  487. * mprescripts
  488. * mroot
  489. * mrow
  490. * ms
  491. * mscarries
  492. * mscarry
  493. * msgroup
  494. * msline
  495. * mspace
  496. * msqrt
  497. * msrow
  498. * mstack
  499. * mstyle
  500. * msub
  501. * msubsup
  502. * msup
  503. * mtable
  504. * mtd
  505. * mtext
  506. * mtr
  507. * munder
  508. * munderover
  509. * none
  510. * semantics
  511. The following :abbr:`MathML (Mathematical Markup Language)` attributes are
  512. allowed by default (all others are stripped):
  513. .. hlist::
  514. :columns: 3
  515. * accent
  516. * accentunder
  517. * actiontype
  518. * align
  519. * alignmentscope
  520. * altimg
  521. * altimg-height
  522. * altimg-valign
  523. * altimg-width
  524. * alttext
  525. * bevelled
  526. * charalign
  527. * close
  528. * columnalign
  529. * columnlines
  530. * columnspacing
  531. * columnspan
  532. * columnwidth
  533. * crossout
  534. * decimalpoint
  535. * denomalign
  536. * depth
  537. * dir
  538. * display
  539. * displaystyle
  540. * edge
  541. * encoding
  542. * equalcolumns
  543. * equalrows
  544. * fence
  545. * fontstyle
  546. * fontweight
  547. * form
  548. * frame
  549. * framespacing
  550. * groupalign
  551. * height
  552. * href
  553. * id
  554. * indentalign
  555. * indentalignfirst
  556. * indentalignlast
  557. * indentshift
  558. * indentshiftfirst
  559. * indentshiftlast
  560. * indenttarget
  561. * infixlinebreakstyle
  562. * largeop
  563. * length
  564. * linebreak
  565. * linebreakmultchar
  566. * linebreakstyle
  567. * lineleading
  568. * linethickness
  569. * location
  570. * longdivstyle
  571. * lquote
  572. * lspace
  573. * mathbackground
  574. * mathcolor
  575. * mathsize
  576. * mathvariant
  577. * maxsize
  578. * minlabelspacing
  579. * minsize
  580. * movablelimits
  581. * notation
  582. * numalign
  583. * open
  584. * other
  585. * overflow
  586. * position
  587. * rowalign
  588. * rowlines
  589. * rowspacing
  590. * rowspan
  591. * rquote
  592. * rspace
  593. * scriptlevel
  594. * scriptminsize
  595. * scriptsizemultiplier
  596. * selection
  597. * separator
  598. * separators
  599. * shift
  600. * side
  601. * src
  602. * stackalign
  603. * stretchy
  604. * subscriptshift
  605. * superscriptshift
  606. * symmetric
  607. * voffset
  608. * width
  609. * xlink:href
  610. * xlink:show
  611. * xlink:type
  612. * xmlns
  613. * xmlns:xlink
  614. .. _advanced.sanitization.css:
  615. :abbr:`CSS (Cascading Style Sheets)` Sanitization
  616. -------------------------------------------------
  617. The following :abbr:`CSS (Cascading Style Sheets)` properties are allowed by
  618. default in style attributes (all others are stripped):
  619. .. hlist::
  620. :columns: 3
  621. * azimuth
  622. * background-color
  623. * border-bottom-color
  624. * border-collapse
  625. * border-color
  626. * border-left-color
  627. * border-right-color
  628. * border-top-color
  629. * clear
  630. * color
  631. * cursor
  632. * direction
  633. * display
  634. * elevation
  635. * float
  636. * font
  637. * font-family
  638. * font-size
  639. * font-style
  640. * font-variant
  641. * font-weight
  642. * height
  643. * letter-spacing
  644. * line-height
  645. * overflow
  646. * pause
  647. * pause-after
  648. * pause-before
  649. * pitch
  650. * pitch-range
  651. * richness
  652. * speak
  653. * speak-header
  654. * speak-numeral
  655. * speak-punctuation
  656. * speech-rate
  657. * stress
  658. * text-align
  659. * text-decoration
  660. * text-indent
  661. * unicode-bidi
  662. * vertical-align
  663. * voice-family
  664. * volume
  665. * white-space
  666. * width
  667. .. note::
  668. Not all possible CSS values are allowed for these properties. The
  669. allowable values are restricted by a whitelist and a regular expression that
  670. allows color values and lengths. :abbr:`URI (Uniform Resource Identifier)`\s
  671. are not allowed, to prevent `platypus attacks <http://diveintomark.org/archives/2003/06/12/how_to_consume_rss_safely>`_.
  672. See the _HTMLSanitizer class for more details.
  673. Whitelist, Don't Blacklist
  674. --------------------------
  675. I am often asked why :program:`Universal Feed Parser` is so hard-assed about
  676. :abbr:`HTML (HyperText Markup Language)` and :abbr:`CSS (Cascading Style
  677. Sheets)` sanitizing. To illustrate the problem, here is an incomplete list of
  678. potentially dangerous :abbr:`HTML (HyperText Markup Language)` tags and
  679. attributes:
  680. * script, which can contain malicious script
  681. * applet, embed, and object, which can automatically download and execute malicious code
  682. * meta, which can contain malicious redirects
  683. * onload, onunload, and all other on* attributes, which can contain malicious script
  684. * style, link, and the style attribute, which can contain malicious script
  685. *style?* Yes, style. :abbr:`CSS (Cascading Style Sheets)` definitions can contain executable code.
  686. Embedding Javascript in :abbr:`CSS (Cascading Style Sheets)`
  687. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  688. This sample is taken from `http://feedparser.org/docs/examples/rss20.xml <http://feedparser.org/docs/examples/rss20.xml>`_:
  689. .. sourcecode:: html
  690. <description>Watch out for
  691. &lt;span style="background: url(javascript:window.location='http://example.org/')"&gt;
  692. nasty tricks&lt;/span&gt;</description>
  693. This sample is more advanced, and does not contain the keyword javascript: that
  694. many naive :abbr:`HTML (HyperText Markup Language)` sanitizers scan for:
  695. .. sourcecode:: html
  696. <description>Watch out for
  697. &lt;span style="any: expression(window.location='http://example.org/')"&gt;
  698. nasty tricks&lt;/span&gt;</description>
  699. Internet Explorer for Windows will execute the Javascript in both of these examples.
  700. Now consider that in :abbr:`HTML (HyperText Markup Language)`, attribute values may be entity-encoded in several different ways.
  701. Embedding encoded Javascript in :abbr:`CSS (Cascading Style Sheets)`
  702. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  703. To a browser, this:
  704. .. sourcecode:: html
  705. <span style="any: expression(window.location='http://example.org/')">
  706. is the same as this (without the line breaks):
  707. .. sourcecode:: html
  708. <span style="&#97;&#110;&#121;&#58;&#32;&#101;&#120;&#112;&#114;&#101;
  709. &#115;&#115;&#105;&#111;&#110;&#40;&#119;&#105;&#110;&#100;&#111;&#119;
  710. &#46;&#108;&#111;&#99;&#97;&#116;&#105;&#111;&#110;&#61;&#39;&#104;
  711. &#116;&#116;&#112;&#58;&#47;&#47;&#101;&#120;&#97;&#109;&#112;&#108;
  712. &#101;&#46;&#111;&#114;&#103;&#47;&#39;&#41;">
  713. which is the same as this (without the line breaks):
  714. .. sourcecode:: html
  715. <span style="&#x61;&#x6e;&#x79;&#x3a;&#x20;&#x65;&#x78;&#x70;&#x72;
  716. &#x65;&#x73;&#x73;&#x69;&#x6f;&#x6e;&#x28;&#x77;&#x69;&#x6e;
  717. &#x64;&#x6f;&#x77;&#x2e;&#x6c;&#x6f;&#x63;&#x61;&#x74;&#x69;
  718. &#x6f;&#x6e;&#x3d;&#x27;&#x68;&#x74;&#x74;&#x70;&#x3a;&#x2f;
  719. &#x2f;&#x65;&#x78;&#x61;&#x6d;&#x70;&#x6c;&#x65;&#x2e;&#x6f;
  720. &#x72;&#x67;&#x2f;&#x27;&#x29;">
  721. And so on, plus several other variations, plus every combination of every
  722. variation.
  723. The more I investigate, the more cases I find where Internet Explorer for
  724. Windows will treat seemingly innocuous markup as code and blithely execute it.
  725. This is why :program:`Universal Feed Parser` uses a whitelist and not a
  726. blacklist. I am reasonably confident that none of the elements or attributes on
  727. the whitelist are security risks. I am not at all confident about elements or
  728. attributes that I have not explicitly investigated. And I have no confidence at
  729. all in my ability to detect strings within attribute values that Internet
  730. Explorer for Windows will treat as executable code.
  731. .. seealso::
  732. `How to consume RSS safely <http://diveintomark.org/archives/2003/06/12/how_to_consume_rss_safely>`_
  733. Explains the platypus attack.