loofah 2.2.3 → 2.6.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of loofah might be problematic. Click here for more details.

@@ -0,0 +1,804 @@
1
+ # frozen_string_literal: true
2
+ require "set"
3
+
4
+ module Loofah
5
+ module HTML5 # :nodoc:
6
+ #
7
+ # HTML safelist lifted from HTML5lib sanitizer code:
8
+ #
9
+ # http://code.google.com/p/html5lib/
10
+ #
11
+ # <html5_license>
12
+ #
13
+ # Copyright (c) 2006-2008 The Authors
14
+ #
15
+ # Contributors:
16
+ # James Graham - jg307@cam.ac.uk
17
+ # Anne van Kesteren - annevankesteren@gmail.com
18
+ # Lachlan Hunt - lachlan.hunt@lachy.id.au
19
+ # Matt McDonald - kanashii@kanashii.ca
20
+ # Sam Ruby - rubys@intertwingly.net
21
+ # Ian Hickson (Google) - ian@hixie.ch
22
+ # Thomas Broyer - t.broyer@ltgt.net
23
+ # Jacques Distler - distler@golem.ph.utexas.edu
24
+ # Henri Sivonen - hsivonen@iki.fi
25
+ # The Mozilla Foundation (contributions from Henri Sivonen since 2008)
26
+ #
27
+ # Permission is hereby granted, free of charge, to any person
28
+ # obtaining a copy of this software and associated documentation
29
+ # files (the "Software"), to deal in the Software without
30
+ # restriction, including without limitation the rights to use, copy,
31
+ # modify, merge, publish, distribute, sublicense, and/or sell copies
32
+ # of the Software, and to permit persons to whom the Software is
33
+ # furnished to do so, subject to the following conditions:
34
+ #
35
+ # The above copyright notice and this permission notice shall be
36
+ # included in all copies or substantial portions of the Software.
37
+ #
38
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
39
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
40
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
41
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
42
+ # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
43
+ # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
44
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
45
+ # DEALINGS IN THE SOFTWARE.
46
+ #
47
+ # </html5_license>
48
+ module SafeList
49
+ ACCEPTABLE_ELEMENTS = Set.new([
50
+ "a",
51
+ "abbr",
52
+ "acronym",
53
+ "address",
54
+ "area",
55
+ "article",
56
+ "aside",
57
+ "audio",
58
+ "b",
59
+ "bdi",
60
+ "bdo",
61
+ "big",
62
+ "blockquote",
63
+ "br",
64
+ "button",
65
+ "canvas",
66
+ "caption",
67
+ "center",
68
+ "cite",
69
+ "code",
70
+ "col",
71
+ "colgroup",
72
+ "command",
73
+ "datalist",
74
+ "dd",
75
+ "del",
76
+ "details",
77
+ "dfn",
78
+ "dir",
79
+ "div",
80
+ "dl",
81
+ "dt",
82
+ "em",
83
+ "fieldset",
84
+ "figcaption",
85
+ "figure",
86
+ "font",
87
+ "footer",
88
+ "form",
89
+ "h1",
90
+ "h2",
91
+ "h3",
92
+ "h4",
93
+ "h5",
94
+ "h6",
95
+ "header",
96
+ "hr",
97
+ "i",
98
+ "img",
99
+ "input",
100
+ "ins",
101
+ "kbd",
102
+ "label",
103
+ "legend",
104
+ "li",
105
+ "main",
106
+ "map",
107
+ "mark",
108
+ "menu",
109
+ "meter",
110
+ "nav",
111
+ "ol",
112
+ "optgroup",
113
+ "option",
114
+ "output",
115
+ "p",
116
+ "pre",
117
+ "q",
118
+ "s",
119
+ "samp",
120
+ "section",
121
+ "select",
122
+ "small",
123
+ "span",
124
+ "strike",
125
+ "strong",
126
+ "sub",
127
+ "summary",
128
+ "sup",
129
+ "table",
130
+ "tbody",
131
+ "td",
132
+ "textarea",
133
+ "tfoot",
134
+ "th",
135
+ "thead",
136
+ "time",
137
+ "tr",
138
+ "tt",
139
+ "u",
140
+ "ul",
141
+ "var",
142
+ "video",
143
+ ])
144
+
145
+ MATHML_ELEMENTS = Set.new([
146
+ "annotation",
147
+ "annotation-xml",
148
+ "maction",
149
+ "math",
150
+ "merror",
151
+ "mfenced",
152
+ "mfrac",
153
+ "mi",
154
+ "mmultiscripts",
155
+ "mn",
156
+ "mo",
157
+ "mover",
158
+ "mpadded",
159
+ "mphantom",
160
+ "mprescripts",
161
+ "mroot",
162
+ "mrow",
163
+ "mspace",
164
+ "msqrt",
165
+ "mstyle",
166
+ "msub",
167
+ "msubsup",
168
+ "msup",
169
+ "mtable",
170
+ "mtd",
171
+ "mtext",
172
+ "mtr",
173
+ "munder",
174
+ "munderover",
175
+ "none",
176
+ "semantics",
177
+ ])
178
+
179
+ SVG_ELEMENTS = Set.new([
180
+ "a",
181
+ "animate",
182
+ "animateColor",
183
+ "animateMotion",
184
+ "animateTransform",
185
+ "circle",
186
+ "clipPath",
187
+ "defs",
188
+ "desc",
189
+ "ellipse",
190
+ "feGaussianBlur",
191
+ "filter",
192
+ "font-face",
193
+ "font-face-name",
194
+ "font-face-src",
195
+ "foreignObject",
196
+ "g",
197
+ "glyph",
198
+ "hkern",
199
+ "line",
200
+ "linearGradient",
201
+ "marker",
202
+ "mask",
203
+ "metadata",
204
+ "missing-glyph",
205
+ "mpath",
206
+ "path",
207
+ "polygon",
208
+ "polyline",
209
+ "radialGradient",
210
+ "rect",
211
+ "set",
212
+ "stop",
213
+ "svg",
214
+ "switch",
215
+ "symbol",
216
+ "text",
217
+ "textPath",
218
+ "title",
219
+ "tspan",
220
+ "use",
221
+ ])
222
+
223
+ ACCEPTABLE_ATTRIBUTES = Set.new([
224
+ "abbr",
225
+ "accept",
226
+ "accept-charset",
227
+ "accesskey",
228
+ "action",
229
+ "align",
230
+ "alt",
231
+ "axis",
232
+ "border",
233
+ "cellpadding",
234
+ "cellspacing",
235
+ "char",
236
+ "charoff",
237
+ "charset",
238
+ "checked",
239
+ "cite",
240
+ "class",
241
+ "clear",
242
+ "color",
243
+ "cols",
244
+ "colspan",
245
+ "compact",
246
+ "contenteditable",
247
+ "coords",
248
+ "datetime",
249
+ "dir",
250
+ "disabled",
251
+ "enctype",
252
+ "for",
253
+ "frame",
254
+ "headers",
255
+ "height",
256
+ "href",
257
+ "hreflang",
258
+ "hspace",
259
+ "id",
260
+ "ismap",
261
+ "label",
262
+ "lang",
263
+ "longdesc",
264
+ "loop",
265
+ "loopcount",
266
+ "loopend",
267
+ "loopstart",
268
+ "maxlength",
269
+ "media",
270
+ "method",
271
+ "multiple",
272
+ "name",
273
+ "nohref",
274
+ "noshade",
275
+ "nowrap",
276
+ "poster",
277
+ "preload",
278
+ "prompt",
279
+ "readonly",
280
+ "rel",
281
+ "rev",
282
+ "rows",
283
+ "rowspan",
284
+ "rules",
285
+ "scope",
286
+ "selected",
287
+ "shape",
288
+ "size",
289
+ "span",
290
+ "src",
291
+ "start",
292
+ "style",
293
+ "summary",
294
+ "tabindex",
295
+ "target",
296
+ "title",
297
+ "type",
298
+ "usemap",
299
+ "valign",
300
+ "value",
301
+ "vspace",
302
+ "width",
303
+ "xml:lang",
304
+ ])
305
+
306
+ MATHML_ATTRIBUTES = Set.new([
307
+ "actiontype",
308
+ "align",
309
+ "close",
310
+ "columnalign",
311
+ "columnlines",
312
+ "columnspacing",
313
+ "columnspan",
314
+ "depth",
315
+ "display",
316
+ "displaystyle",
317
+ "encoding",
318
+ "equalcolumns",
319
+ "equalrows",
320
+ "fence",
321
+ "fontstyle",
322
+ "fontweight",
323
+ "frame",
324
+ "height",
325
+ "linethickness",
326
+ "lspace",
327
+ "mathbackground",
328
+ "mathcolor",
329
+ "mathvariant",
330
+ "maxsize",
331
+ "minsize",
332
+ "open",
333
+ "other",
334
+ "rowalign",
335
+ "rowlines",
336
+ "rowspacing",
337
+ "rowspan",
338
+ "rspace",
339
+ "scriptlevel",
340
+ "selection",
341
+ "separator",
342
+ "separators",
343
+ "stretchy",
344
+ "width",
345
+ "xlink:href",
346
+ "xlink:show",
347
+ "xlink:type",
348
+ "xmlns",
349
+ "xmlns:xlink",
350
+ ])
351
+
352
+ SVG_ATTRIBUTES = Set.new([
353
+ "accent-height",
354
+ "accumulate",
355
+ "additive",
356
+ "alphabetic",
357
+ "arabic-form",
358
+ "ascent",
359
+ "attributeName",
360
+ "attributeType",
361
+ "baseProfile",
362
+ "bbox",
363
+ "begin",
364
+ "calcMode",
365
+ "cap-height",
366
+ "class",
367
+ "clip-path",
368
+ "clip-rule",
369
+ "color",
370
+ "color-interpolation-filters",
371
+ "color-rendering",
372
+ "content",
373
+ "cx",
374
+ "cy",
375
+ "d",
376
+ "descent",
377
+ "display",
378
+ "dur",
379
+ "dx",
380
+ "dy",
381
+ "end",
382
+ "fill",
383
+ "fill-opacity",
384
+ "fill-rule",
385
+ "filterRes",
386
+ "filterUnits",
387
+ "font-family",
388
+ "font-size",
389
+ "font-stretch",
390
+ "font-style",
391
+ "font-variant",
392
+ "font-weight",
393
+ "fx",
394
+ "fy",
395
+ "g1",
396
+ "g2",
397
+ "glyph-name",
398
+ "gradientUnits",
399
+ "hanging",
400
+ "height",
401
+ "horiz-adv-x",
402
+ "horiz-origin-x",
403
+ "id",
404
+ "ideographic",
405
+ "k",
406
+ "keyPoints",
407
+ "keySplines",
408
+ "keyTimes",
409
+ "lang",
410
+ "marker-end",
411
+ "marker-mid",
412
+ "marker-start",
413
+ "markerHeight",
414
+ "markerUnits",
415
+ "markerWidth",
416
+ "maskContentUnits",
417
+ "maskUnits",
418
+ "mathematical",
419
+ "max",
420
+ "method",
421
+ "min",
422
+ "name",
423
+ "offset",
424
+ "opacity",
425
+ "orient",
426
+ "origin",
427
+ "overline-position",
428
+ "overline-thickness",
429
+ "panose-1",
430
+ "path",
431
+ "pathLength",
432
+ "patternContentUnits",
433
+ "patternTransform",
434
+ "patternUnits",
435
+ "points",
436
+ "preserveAspectRatio",
437
+ "primitiveUnits",
438
+ "r",
439
+ "refX",
440
+ "refY",
441
+ "repeatCount",
442
+ "repeatDur",
443
+ "requiredExtensions",
444
+ "requiredFeatures",
445
+ "restart",
446
+ "rotate",
447
+ "rx",
448
+ "ry",
449
+ "slope",
450
+ "spacing",
451
+ "startOffset",
452
+ "stdDeviation",
453
+ "stemh",
454
+ "stemv",
455
+ "stop-color",
456
+ "stop-opacity",
457
+ "strikethrough-position",
458
+ "strikethrough-thickness",
459
+ "stroke",
460
+ "stroke-dasharray",
461
+ "stroke-dashoffset",
462
+ "stroke-linecap",
463
+ "stroke-linejoin",
464
+ "stroke-miterlimit",
465
+ "stroke-opacity",
466
+ "stroke-width",
467
+ "systemLanguage",
468
+ "target",
469
+ "text-anchor",
470
+ "transform",
471
+ "type",
472
+ "u1",
473
+ "u2",
474
+ "underline-position",
475
+ "underline-thickness",
476
+ "unicode",
477
+ "unicode-range",
478
+ "units-per-em",
479
+ "version",
480
+ "viewBox",
481
+ "visibility",
482
+ "width",
483
+ "widths",
484
+ "x",
485
+ "x-height",
486
+ "x1",
487
+ "x2",
488
+ "xlink:actuate",
489
+ "xlink:arcrole",
490
+ "xlink:href",
491
+ "xlink:role",
492
+ "xlink:show",
493
+ "xlink:title",
494
+ "xlink:type",
495
+ "xml:base",
496
+ "xml:lang",
497
+ "xml:space",
498
+ "xmlns",
499
+ "xmlns:xlink",
500
+ "y",
501
+ "y1",
502
+ "y2",
503
+ "zoomAndPan",
504
+ ])
505
+
506
+ ATTR_VAL_IS_URI = Set.new([
507
+ "action",
508
+ "cite",
509
+ "href",
510
+ "longdesc",
511
+ "poster",
512
+ "preload",
513
+ "src",
514
+ "xlink:href",
515
+ "xml:base",
516
+ ])
517
+
518
+ SVG_ATTR_VAL_ALLOWS_REF = Set.new([
519
+ "clip-path",
520
+ "color-profile",
521
+ "cursor",
522
+ "fill",
523
+ "filter",
524
+ "marker",
525
+ "marker-end",
526
+ "marker-mid",
527
+ "marker-start",
528
+ "mask",
529
+ "stroke",
530
+ ])
531
+
532
+ SVG_ALLOW_LOCAL_HREF = Set.new([
533
+ "altGlyph",
534
+ "animate",
535
+ "animateColor",
536
+ "animateMotion",
537
+ "animateTransform",
538
+ "cursor",
539
+ "feImage",
540
+ "filter",
541
+ "linearGradient",
542
+ "pattern",
543
+ "radialGradient",
544
+ "set",
545
+ "textpath",
546
+ "tref",
547
+ "use",
548
+ ])
549
+
550
+ ACCEPTABLE_CSS_PROPERTIES = Set.new([
551
+ "azimuth",
552
+ "background-color",
553
+ "border-bottom-color",
554
+ "border-collapse",
555
+ "border-color",
556
+ "border-left-color",
557
+ "border-right-color",
558
+ "border-top-color",
559
+ "clear",
560
+ "color",
561
+ "cursor",
562
+ "direction",
563
+ "display",
564
+ "elevation",
565
+ "float",
566
+ "font",
567
+ "font-family",
568
+ "font-size",
569
+ "font-style",
570
+ "font-variant",
571
+ "font-weight",
572
+ "height",
573
+ "letter-spacing",
574
+ "line-height",
575
+ "list-style",
576
+ "list-style-type",
577
+ "max-width",
578
+ "overflow",
579
+ "pause",
580
+ "pause-after",
581
+ "pause-before",
582
+ "pitch",
583
+ "pitch-range",
584
+ "richness",
585
+ "speak",
586
+ "speak-header",
587
+ "speak-numeral",
588
+ "speak-punctuation",
589
+ "speech-rate",
590
+ "stress",
591
+ "text-align",
592
+ "text-decoration",
593
+ "text-indent",
594
+ "unicode-bidi",
595
+ "vertical-align",
596
+ "voice-family",
597
+ "volume",
598
+ "white-space",
599
+ "width",
600
+ ])
601
+
602
+ ACCEPTABLE_CSS_KEYWORDS = Set.new([
603
+ "!important",
604
+ "aqua",
605
+ "auto",
606
+ "black",
607
+ "block",
608
+ "blue",
609
+ "bold",
610
+ "both",
611
+ "bottom",
612
+ "brown",
613
+ "center",
614
+ "collapse",
615
+ "dashed",
616
+ "dotted",
617
+ "double",
618
+ "fuchsia",
619
+ "gray",
620
+ "green",
621
+ "groove",
622
+ "hidden",
623
+ "inset",
624
+ "italic",
625
+ "left",
626
+ "lime",
627
+ "maroon",
628
+ "medium",
629
+ "navy",
630
+ "none",
631
+ "normal",
632
+ "nowrap",
633
+ "olive",
634
+ "outset",
635
+ "pointer",
636
+ "purple",
637
+ "red",
638
+ "ridge",
639
+ "right",
640
+ "silver",
641
+ "solid",
642
+ "teal",
643
+ "thin",
644
+ "thick",
645
+ "top",
646
+ "transparent",
647
+ "underline",
648
+ "white",
649
+ "yellow",
650
+ ])
651
+
652
+ # see https://www.quackit.com/css/functions/
653
+ # omit `url` and `image` from that list
654
+ ACCEPTABLE_CSS_FUNCTIONS = Set.new([
655
+ "attr",
656
+ "blur",
657
+ "brightness",
658
+ "calc",
659
+ "circle",
660
+ "contrast",
661
+ "counter",
662
+ "counters",
663
+ "cubic-bezier",
664
+ "drop-shadow",
665
+ "ellipse",
666
+ "grayscale",
667
+ "hsl",
668
+ "hsla",
669
+ "hue-rotate",
670
+ "hwb",
671
+ "inset",
672
+ "invert",
673
+ "linear-gradient",
674
+ "matrix",
675
+ "matrix3d",
676
+ "opacity",
677
+ "perspective",
678
+ "polygon",
679
+ "radial-gradient",
680
+ "repeating-linear-gradient",
681
+ "repeating-radial-gradient",
682
+ "rgb",
683
+ "rgba",
684
+ "rotate",
685
+ "rotate3d",
686
+ "rotateX",
687
+ "rotateY",
688
+ "rotateZ",
689
+ "saturate",
690
+ "sepia",
691
+ "scale",
692
+ "scale3d",
693
+ "scaleX",
694
+ "scaleY",
695
+ "scaleZ",
696
+ "skew",
697
+ "skewX",
698
+ "skewY",
699
+ "symbols",
700
+ "translate",
701
+ "translate3d",
702
+ "translateX",
703
+ "translateY",
704
+ "translateZ",
705
+ ])
706
+
707
+ SHORTHAND_CSS_PROPERTIES = Set.new([
708
+ "background",
709
+ "border",
710
+ "margin",
711
+ "padding",
712
+ ])
713
+
714
+ ACCEPTABLE_SVG_PROPERTIES = Set.new([
715
+ "fill",
716
+ "fill-opacity",
717
+ "fill-rule",
718
+ "stroke",
719
+ "stroke-width",
720
+ "stroke-linecap",
721
+ "stroke-linejoin",
722
+ "stroke-opacity",
723
+ ])
724
+
725
+ PROTOCOL_SEPARATOR = /:|(&#0*58)|(&#x70)|(&#x0*3a)|(%|&#37;)3A/i
726
+
727
+ ACCEPTABLE_PROTOCOLS = Set.new([
728
+ "afs",
729
+ "aim",
730
+ "callto",
731
+ "data",
732
+ "ed2k",
733
+ "feed",
734
+ "ftp",
735
+ "gopher",
736
+ "http",
737
+ "https",
738
+ "irc",
739
+ "line",
740
+ "mailto",
741
+ "news",
742
+ "nntp",
743
+ "rsync",
744
+ "rtsp",
745
+ "sftp",
746
+ "ssh",
747
+ "tag",
748
+ "tel",
749
+ "telnet",
750
+ "urn",
751
+ "webcal",
752
+ "xmpp",
753
+ ])
754
+
755
+ ACCEPTABLE_URI_DATA_MEDIATYPES = Set.new([
756
+ "image/gif",
757
+ "image/jpeg",
758
+ "image/png",
759
+ "image/svg+xml",
760
+ "text/css",
761
+ "text/plain",
762
+ ])
763
+
764
+ # subclasses may define their own versions of these constants
765
+ ALLOWED_ELEMENTS = ACCEPTABLE_ELEMENTS + MATHML_ELEMENTS + SVG_ELEMENTS
766
+ ALLOWED_ATTRIBUTES = ACCEPTABLE_ATTRIBUTES + MATHML_ATTRIBUTES + SVG_ATTRIBUTES
767
+ ALLOWED_CSS_PROPERTIES = ACCEPTABLE_CSS_PROPERTIES
768
+ ALLOWED_CSS_KEYWORDS = ACCEPTABLE_CSS_KEYWORDS
769
+ ALLOWED_CSS_FUNCTIONS = ACCEPTABLE_CSS_FUNCTIONS
770
+ ALLOWED_SVG_PROPERTIES = ACCEPTABLE_SVG_PROPERTIES
771
+ ALLOWED_PROTOCOLS = ACCEPTABLE_PROTOCOLS
772
+ ALLOWED_URI_DATA_MEDIATYPES = ACCEPTABLE_URI_DATA_MEDIATYPES
773
+
774
+ VOID_ELEMENTS = Set.new([
775
+ "area",
776
+ "base",
777
+ "br",
778
+ "col",
779
+ "embed",
780
+ "hr",
781
+ "img",
782
+ "input",
783
+ "link",
784
+ "meta",
785
+ "param",
786
+ ])
787
+
788
+ # additional tags we should consider safe since we have libxml2 fixing up our documents.
789
+ TAGS_SAFE_WITH_LIBXML2 = Set.new([
790
+ "body",
791
+ "head",
792
+ "html",
793
+ ])
794
+ ALLOWED_ELEMENTS_WITH_LIBXML2 = ALLOWED_ELEMENTS + TAGS_SAFE_WITH_LIBXML2
795
+ end
796
+
797
+ WhiteList = SafeList
798
+ if Object.respond_to?(:deprecate_constant)
799
+ deprecate_constant :WhiteList
800
+ end
801
+
802
+ ::Loofah::MetaHelpers.add_downcased_set_members_to_all_set_constants ::Loofah::HTML5::SafeList
803
+ end
804
+ end