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