| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815 |
- .. _advanced.sanitization:
- Sanitization
- ============
- Most feeds embed :abbr:`HTML (HyperText Markup Language)` markup within feed
- elements. Some feeds even embed other types of markup, such as :abbr:`SVG
- (Scalable Vector Graphics)` or :abbr:`MathML (Mathematical Markup Language)`.
- Since many feed aggregators use a web browser (or browser component) to display
- content, :program:`Universal Feed Parser` sanitizes embedded markup to remove
- things that could pose security risks.
- These elements are sanitized by default:
- * :ref:`reference.entry.content`
- * :ref:`reference.entry.summary`
- * :ref:`reference.entry.title`
- * :ref:`reference.feed.info`
- * :ref:`reference.feed.rights`
- * :ref:`reference.feed.subtitle`
- * :ref:`reference.feed.title`
- .. note::
- If the content is declared to be (or is determined to be)
- :mimetype:`text/plain`, it will not be sanitized. This is to avoid data loss.
- It is recommended that you check the content type in e.g.
- :py:attr:`entries[i].summary_detail.type`. If it is :mimetype:`text/plain` then
- it has not been sanitized (and you should perform HTML escaping before
- rendering the content).
- .. _advanced.sanitization.html:
- :abbr:`HTML (HyperText Markup Language)` Sanitization
- -----------------------------------------------------
- The following :abbr:`HTML (HyperText Markup Language)` elements are allowed by
- default (all others are stripped):
- .. hlist::
- :columns: 3
- * a
- * abbr
- * acronym
- * address
- * area
- * article
- * aside
- * audio
- * b
- * big
- * blockquote
- * br
- * button
- * canvas
- * caption
- * center
- * cite
- * code
- * col
- * colgroup
- * command
- * datagrid
- * datalist
- * dd
- * del
- * details
- * dfn
- * dialog
- * dir
- * div
- * dl
- * dt
- * em
- * event-source
- * fieldset
- * figure
- * font
- * footer
- * form
- * h1
- * h2
- * h3
- * h4
- * h5
- * h6
- * header
- * hr
- * i
- * img
- * input
- * ins
- * kbd
- * keygen
- * label
- * legend
- * li
- * m
- * map
- * menu
- * meter
- * multicol
- * nav
- * nextid
- * noscript
- * ol
- * optgroup
- * option
- * output
- * p
- * pre
- * progress
- * q
- * s
- * samp
- * section
- * select
- * small
- * sound
- * source
- * spacer
- * span
- * strike
- * strong
- * sub
- * sup
- * table
- * tbody
- * td
- * textarea
- * tfoot
- * th
- * thead
- * time
- * tr
- * tt
- * u
- * ul
- * var
- * video
- The following :abbr:`HTML (HyperText Markup Language)` attributes are allowed
- by default (all others are stripped):
- .. hlist::
- :columns: 3
- * abbr
- * accept
- * accept-charset
- * accesskey
- * action
- * align
- * alt
- * autocomplete
- * autofocus
- * autoplay
- * axis
- * background
- * balance
- * bgcolor
- * bgproperties
- * border
- * bordercolor
- * bordercolordark
- * bordercolorlight
- * bottompadding
- * cellpadding
- * cellspacing
- * ch
- * challenge
- * char
- * charoff
- * charset
- * checked
- * choff
- * cite
- * class
- * clear
- * color
- * cols
- * colspan
- * compact
- * contenteditable
- * coords
- * data
- * datafld
- * datapagesize
- * datasrc
- * datetime
- * default
- * delay
- * dir
- * disabled
- * draggable
- * dynsrc
- * enctype
- * end
- * face
- * for
- * form
- * frame
- * galleryimg
- * gutter
- * headers
- * height
- * hidden
- * hidefocus
- * high
- * href
- * hreflang
- * hspace
- * icon
- * id
- * inputmode
- * ismap
- * keytype
- * label
- * lang
- * leftspacing
- * list
- * longdesc
- * loop
- * loopcount
- * loopend
- * loopstart
- * low
- * lowsrc
- * max
- * maxlength
- * media
- * method
- * min
- * multiple
- * name
- * nohref
- * noshade
- * nowrap
- * open
- * optimum
- * pattern
- * ping
- * point-size
- * poster
- * pqg
- * preload
- * prompt
- * radiogroup
- * readonly
- * rel
- * repeat-max
- * repeat-min
- * replace
- * required
- * rev
- * rightspacing
- * rows
- * rowspan
- * rules
- * scope
- * selected
- * shape
- * size
- * span
- * src
- * start
- * step
- * summary
- * suppress
- * tabindex
- * target
- * template
- * title
- * toppadding
- * type
- * unselectable
- * urn
- * usemap
- * valign
- * value
- * variable
- * volume
- * vrml
- * vspace
- * width
- * wrap
- * xml:lang
- .. _advanced.sanitization.svg:
- :abbr:`SVG (Scalable Vector Graphics)` Sanitization
- ---------------------------------------------------
- The following SVG elements are allowed by default (all others are stripped):
- .. hlist::
- :columns: 3
- * a
- * animate
- * animateColor
- * animateMotion
- * animateTransform
- * circle
- * defs
- * desc
- * ellipse
- * font-face
- * font-face-name
- * font-face-src
- * foreignObject
- * g
- * glyph
- * hkern
- * line
- * linearGradient
- * marker
- * metadata
- * missing-glyph
- * mpath
- * path
- * polygon
- * polyline
- * radialGradient
- * rect
- * set
- * stop
- * svg
- * switch
- * text
- * title
- * tspan
- * use
- The following :abbr:`SVG (Scalable Vector Graphics)` attributes are allowed by
- default (all others are stripped):
- .. hlist::
- :columns: 3
- * accent-height
- * accumulate
- * additive
- * alphabetic
- * arabic-form
- * ascent
- * attributeName
- * attributeType
- * baseProfile
- * bbox
- * begin
- * by
- * calcMode
- * cap-height
- * class
- * color
- * color-rendering
- * content
- * cx
- * cy
- * d
- * descent
- * display
- * dur
- * dx
- * dy
- * end
- * fill
- * fill-opacity
- * fill-rule
- * font-family
- * font-size
- * font-stretch
- * font-style
- * font-variant
- * font-weight
- * from
- * fx
- * fy
- * g1
- * g2
- * glyph-name
- * gradientUnits
- * hanging
- * height
- * horiz-adv-x
- * horiz-origin-x
- * id
- * ideographic
- * k
- * keyPoints
- * keySplines
- * keyTimes
- * lang
- * marker-end
- * marker-mid
- * marker-start
- * markerHeight
- * markerUnits
- * markerWidth
- * mathematical
- * max
- * min
- * name
- * offset
- * opacity
- * orient
- * origin
- * overline-position
- * overline-thickness
- * panose-1
- * path
- * pathLength
- * points
- * preserveAspectRatio
- * r
- * refX
- * refY
- * repeatCount
- * repeatDur
- * requiredExtensions
- * requiredFeatures
- * restart
- * rotate
- * rx
- * ry
- * slope
- * stemh
- * stemv
- * stop-color
- * stop-opacity
- * strikethrough-position
- * strikethrough-thickness
- * stroke
- * stroke-dasharray
- * stroke-dashoffset
- * stroke-linecap
- * stroke-linejoin
- * stroke-miterlimit
- * stroke-opacity
- * stroke-width
- * systemLanguage
- * target
- * text-anchor
- * to
- * transform
- * type
- * u1
- * u2
- * underline-position
- * underline-thickness
- * unicode
- * unicode-range
- * units-per-em
- * values
- * version
- * viewBox
- * visibility
- * width
- * widths
- * x
- * x-height
- * x1
- * x2
- * xlink:actuate
- * xlink:arcrole
- * xlink:href
- * xlink:role
- * xlink:show
- * xlink:title
- * xlink:type
- * xml:base
- * xml:lang
- * xml:space
- * xmlns
- * xmlns:xlink
- * y
- * y1
- * y2
- * zoomAndPan
- .. _advanced.sanitization.mathml:
- :abbr:`MathML (Mathematical Markup Language)` Sanitization
- ----------------------------------------------------------
- The following :abbr:`MathML (Mathematical Markup Language)` elements are
- allowed by default (all others are stripped):
- .. hlist::
- :columns: 3
- * annotation
- * annotation-xml
- * maction
- * maligngroup
- * malignmark
- * math
- * menclose
- * merror
- * mfenced
- * mfrac
- * mglyph
- * mi
- * mlabeledtr
- * mlongdiv
- * mmultiscripts
- * mn
- * mo
- * mover
- * mpadded
- * mphantom
- * mprescripts
- * mroot
- * mrow
- * ms
- * mscarries
- * mscarry
- * msgroup
- * msline
- * mspace
- * msqrt
- * msrow
- * mstack
- * mstyle
- * msub
- * msubsup
- * msup
- * mtable
- * mtd
- * mtext
- * mtr
- * munder
- * munderover
- * none
- * semantics
- The following :abbr:`MathML (Mathematical Markup Language)` attributes are
- allowed by default (all others are stripped):
- .. hlist::
- :columns: 3
- * accent
- * accentunder
- * actiontype
- * align
- * alignmentscope
- * altimg
- * altimg-height
- * altimg-valign
- * altimg-width
- * alttext
- * bevelled
- * charalign
- * close
- * columnalign
- * columnlines
- * columnspacing
- * columnspan
- * columnwidth
- * crossout
- * decimalpoint
- * denomalign
- * depth
- * dir
- * display
- * displaystyle
- * edge
- * encoding
- * equalcolumns
- * equalrows
- * fence
- * fontstyle
- * fontweight
- * form
- * frame
- * framespacing
- * groupalign
- * height
- * href
- * id
- * indentalign
- * indentalignfirst
- * indentalignlast
- * indentshift
- * indentshiftfirst
- * indentshiftlast
- * indenttarget
- * infixlinebreakstyle
- * largeop
- * length
- * linebreak
- * linebreakmultchar
- * linebreakstyle
- * lineleading
- * linethickness
- * location
- * longdivstyle
- * lquote
- * lspace
- * mathbackground
- * mathcolor
- * mathsize
- * mathvariant
- * maxsize
- * minlabelspacing
- * minsize
- * movablelimits
- * notation
- * numalign
- * open
- * other
- * overflow
- * position
- * rowalign
- * rowlines
- * rowspacing
- * rowspan
- * rquote
- * rspace
- * scriptlevel
- * scriptminsize
- * scriptsizemultiplier
- * selection
- * separator
- * separators
- * shift
- * side
- * src
- * stackalign
- * stretchy
- * subscriptshift
- * superscriptshift
- * symmetric
- * voffset
- * width
- * xlink:href
- * xlink:show
- * xlink:type
- * xmlns
- * xmlns:xlink
- .. _advanced.sanitization.css:
- :abbr:`CSS (Cascading Style Sheets)` Sanitization
- -------------------------------------------------
- The following :abbr:`CSS (Cascading Style Sheets)` properties are allowed by
- default in style attributes (all others are stripped):
- .. hlist::
- :columns: 3
- * azimuth
- * background-color
- * border-bottom-color
- * border-collapse
- * border-color
- * border-left-color
- * border-right-color
- * border-top-color
- * clear
- * color
- * cursor
- * direction
- * display
- * elevation
- * float
- * font
- * font-family
- * font-size
- * font-style
- * font-variant
- * font-weight
- * height
- * letter-spacing
- * line-height
- * overflow
- * pause
- * pause-after
- * pause-before
- * pitch
- * pitch-range
- * richness
- * speak
- * speak-header
- * speak-numeral
- * speak-punctuation
- * speech-rate
- * stress
- * text-align
- * text-decoration
- * text-indent
- * unicode-bidi
- * vertical-align
- * voice-family
- * volume
- * white-space
- * width
- .. note::
- Not all possible CSS values are allowed for these properties. The
- allowable values are restricted by a whitelist and a regular expression that
- allows color values and lengths. :abbr:`URI (Uniform Resource Identifier)`\s
- are not allowed, to prevent `platypus attacks <http://diveintomark.org/archives/2003/06/12/how_to_consume_rss_safely>`_.
- See the _HTMLSanitizer class for more details.
- Whitelist, Don't Blacklist
- --------------------------
- I am often asked why :program:`Universal Feed Parser` is so hard-assed about
- :abbr:`HTML (HyperText Markup Language)` and :abbr:`CSS (Cascading Style
- Sheets)` sanitizing. To illustrate the problem, here is an incomplete list of
- potentially dangerous :abbr:`HTML (HyperText Markup Language)` tags and
- attributes:
- * script, which can contain malicious script
- * applet, embed, and object, which can automatically download and execute malicious code
- * meta, which can contain malicious redirects
- * onload, onunload, and all other on* attributes, which can contain malicious script
- * style, link, and the style attribute, which can contain malicious script
- *style?* Yes, style. :abbr:`CSS (Cascading Style Sheets)` definitions can contain executable code.
- Embedding Javascript in :abbr:`CSS (Cascading Style Sheets)`
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- This sample is taken from `http://feedparser.org/docs/examples/rss20.xml <http://feedparser.org/docs/examples/rss20.xml>`_:
- .. sourcecode:: html
- <description>Watch out for
- <span style="background: url(javascript:window.location='http://example.org/')">
- nasty tricks</span></description>
- This sample is more advanced, and does not contain the keyword javascript: that
- many naive :abbr:`HTML (HyperText Markup Language)` sanitizers scan for:
- .. sourcecode:: html
- <description>Watch out for
- <span style="any: expression(window.location='http://example.org/')">
- nasty tricks</span></description>
- Internet Explorer for Windows will execute the Javascript in both of these examples.
- Now consider that in :abbr:`HTML (HyperText Markup Language)`, attribute values may be entity-encoded in several different ways.
- Embedding encoded Javascript in :abbr:`CSS (Cascading Style Sheets)`
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- To a browser, this:
- .. sourcecode:: html
- <span style="any: expression(window.location='http://example.org/')">
- is the same as this (without the line breaks):
- .. sourcecode:: html
- <span style="any: expre
- ssion(window
- .location='h
- ttp://exampl
- e.org/')">
- which is the same as this (without the line breaks):
- .. sourcecode:: html
- <span style="any: expr
- ession(win
- dow.locati
- on='http:/
- /example.o
- rg/')">
- And so on, plus several other variations, plus every combination of every
- variation.
- The more I investigate, the more cases I find where Internet Explorer for
- Windows will treat seemingly innocuous markup as code and blithely execute it.
- This is why :program:`Universal Feed Parser` uses a whitelist and not a
- blacklist. I am reasonably confident that none of the elements or attributes on
- the whitelist are security risks. I am not at all confident about elements or
- attributes that I have not explicitly investigated. And I have no confidence at
- all in my ability to detect strings within attribute values that Internet
- Explorer for Windows will treat as executable code.
- .. seealso::
- `How to consume RSS safely <http://diveintomark.org/archives/2003/06/12/how_to_consume_rss_safely>`_
- Explains the platypus attack.
|