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