]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/htmlscrubber.pm
a few thoughts on data: security
[ikiwiki.git] / IkiWiki / Plugin / htmlscrubber.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::htmlscrubber;
3
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
7
8 sub import { #{{{
9         hook(type => "sanitize", id => "htmlscrubber", call => \&sanitize);
10 } # }}}
11
12 sub sanitize (@) { #{{{
13         my %params=@_;
14         return scrubber()->scrub($params{content});
15 } # }}}
16
17 my $_scrubber;
18 sub scrubber { #{{{
19         return $_scrubber if defined $_scrubber;
20         
21         # Only known uri schemes are allowed to avoid all the ways of
22         # embedding javascrpt.
23         # List at http://en.wikipedia.org/wiki/URI_scheme
24         my $uri_schemes=join("|",
25                 # IANA registered schemes
26                 "http", "https", "ftp", "mailto", "file", "telnet", "gopher",
27                 "aaa", "aaas", "acap",  "cap", "cid", "crid", 
28                 "dav", "dict", "dns", "fax", "go", "h323", "im", "imap",
29                 "ldap", "mid", "news", "nfs", "nntp", "pop", "pres",
30                 "sip", "sips", "snmp", "tel", "urn", "wais", "xmpp",
31                 "z39.50r", "z39.50s",
32                 # Selected unofficial schemes
33                 "about", "aim", "callto", "cvs", "ed2k", "feed", "fish", "gg",
34                 "irc", "ircs", "lastfm", "ldaps", "magnet", "mms",
35                 "msnim", "notes", "rsync", "secondlife", "skype", "ssh",
36                 "sftp", "sms", "steam", "webcal", "ymsgr",
37         );
38         # data is a special case. Allow data:image/*, but
39         # disallow data:text/javascript and everything else.
40         my $link=qr/^(?:$uri_schemes:|data:image\/|[^:]+$)/i;
41
42         eval q{use HTML::Scrubber};
43         error($@) if $@;
44         # Lists based on http://feedparser.org/docs/html-sanitization.html
45         # With html 5 video and audio tags added.
46         $_scrubber = HTML::Scrubber->new(
47                 allow => [qw{
48                         a abbr acronym address area b big blockquote br br/
49                         button caption center cite code col colgroup dd del
50                         dfn dir div dl dt em fieldset font form h1 h2 h3 h4
51                         h5 h6 hr hr/ i img input ins kbd label legend li map
52                         menu ol optgroup option p p/ pre q s samp select small
53                         span strike strong sub sup table tbody td textarea
54                         tfoot th thead tr tt u ul var
55                         video audio
56                 }],
57                 default => [undef, { (
58                         map { $_ => 1 } qw{
59                                 abbr accept accept-charset accesskey
60                                 align alt axis border cellpadding cellspacing
61                                 char charoff charset checked cite class
62                                 clear cols colspan color compact coords
63                                 datetime dir disabled enctype for frame
64                                 headers height hreflang hspace id ismap
65                                 label lang longdesc maxlength media method
66                                 multiple name nohref noshade nowrap prompt
67                                 readonly rel rev rows rowspan rules scope
68                                 selected shape size span start summary
69                                 tabindex target title type usemap valign
70                                 value vspace width
71                                 autoplay loopstart loopend end
72                                 playcount controls 
73                         } ),
74                         "/" => 1, # emit proper <hr /> XHTML
75                         href => $link,
76                         src => $link,
77                         action => $link,
78                         poster => $link,
79                 }],
80         );
81         return $_scrubber;
82 } # }}}
83
84 1