]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/htmlscrubber.pm
25caa8a506cdf5ab8ea0c9b0d3e9e935f62bd6f8
[git.ikiwiki.info.git] / IkiWiki / Plugin / htmlscrubber.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::htmlscrubber;
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
8 sub import { #{{{
9         hook(type => "sanitize", id => "htmlscrubber", call => \&sanitize);
10 } # }}}
12 sub sanitize (@) { #{{{
13         my %params=@_;
14         return scrubber()->scrub($params{content});
15 } # }}}
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                 # data is a special case. Allow data:text/<image>, but
33                 # disallow data:text/javascript and everything else.
34                 qr/data:text\/(?:png|gif|jpeg)/,
35                 # Selected unofficial schemes
36                 "about", "aim", "callto", "cvs", "ed2k", "feed", "fish", "gg",
37                 "irc", "ircs", "lastfm", "ldaps", "magnet", "mms",
38                 "msnim", "notes", "rsync", "secondlife", "skype", "ssh",
39                 "sftp", "sms", "steam", "webcal", "ymsgr",
40         );
41         my $link=qr/^(?:$uri_schemes:|[^:]+$)/i;
43         eval q{use HTML::Scrubber};
44         error($@) if $@;
45         # Lists based on http://feedparser.org/docs/html-sanitization.html
46         # With html 5 video and audio tags added.
47         $_scrubber = HTML::Scrubber->new(
48                 allow => [qw{
49                         a abbr acronym address area b big blockquote br br/
50                         button caption center cite code col colgroup dd del
51                         dfn dir div dl dt em fieldset font form h1 h2 h3 h4
52                         h5 h6 hr hr/ i img input ins kbd label legend li map
53                         menu ol optgroup option p p/ pre q s samp select small
54                         span strike strong sub sup table tbody td textarea
55                         tfoot th thead tr tt u ul var
56                         video audio
57                 }],
58                 default => [undef, { (
59                         map { $_ => 1 } qw{
60                                 abbr accept accept-charset accesskey
61                                 align alt axis border cellpadding cellspacing
62                                 char charoff charset checked cite class
63                                 clear cols colspan color compact coords
64                                 datetime dir disabled enctype for frame
65                                 headers height hreflang hspace id ismap
66                                 label lang longdesc maxlength media method
67                                 multiple name nohref noshade nowrap prompt
68                                 readonly rel rev rows rowspan rules scope
69                                 selected shape size span start summary
70                                 tabindex target title type usemap valign
71                                 value vspace width
72                                 autoplay loopstart loopend end
73                                 playcount controls 
74                         } ),
75                         "/" => 1, # emit proper <hr /> XHTML
76                         href => $link,
77                         src => $link,
78                         action => $link,
79                         poster => $link,
80                 }],
81         );
82         return $_scrubber;
83 } # }}}
85 1