1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
|
.. _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 <https://web.archive.org/web/20080826033749/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.
Disabling HTML Sanitization
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Though not recommended, it is possible to disable :program:`Universal Feed Parser`\'s
HTML sanitization by passing ``sanitize_html=False`` to :func:`feedparser.parse()`.
When passing this flag you are responsible for manually sanitizing HTML from the feed.
.. seealso::
`How to consume RSS safely <https://web.archive.org/web/20080826033749/http://diveintomark.org/archives/2003/06/12/how_to_consume_rss_safely>`_
Explains the platypus attack.
|