2 package IkiWiki::Plugin::htmlscrubber;
8 # This regexp matches urls that are in a known safe scheme.
9 # Feel free to use it from other plugins.
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",
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",
33 # data is a special case. Allow data:image/*, but
34 # disallow data:text/javascript and everything else.
35 $safe_url_regexp=qr/^(?:(?:$uri_schemes):|data:image\/|[^:]+(?:$|\/))/i;
44 htmlscrubber_skip => {
46 example => "!*/Discussion",
47 description => "PageSpec specifying pages not to scrub",
48 link => "ikiwiki/PageSpec",
57 if (exists $config{htmlscrubber_skip} &&
58 length $config{htmlscrubber_skip} &&
59 exists $params{destpage} &&
60 pagespec_match($params{destpage}, $config{htmlscrubber_skip})) {
61 return $params{content};
64 return scrubber()->scrub($params{content});
69 return $_scrubber if defined $_scrubber;
71 eval q{use HTML::Scrubber};
73 # Lists based on http://feedparser.org/docs/html-sanitization.html
74 # With html 5 video and audio tags added.
75 $_scrubber = HTML::Scrubber->new(
77 a abbr acronym address area b big blockquote br br/
78 button caption center cite code col colgroup dd del
79 dfn dir div dl dt em fieldset font form h1 h2 h3 h4
80 h5 h6 hr hr/ i img input ins kbd label legend li map
81 menu ol optgroup option p p/ pre q s samp select small
82 span strike strong sub sup table tbody td textarea
83 tfoot th thead tr tt u ul var
86 default => [undef, { (
88 abbr accept accept-charset accesskey
89 align alt axis border cellpadding cellspacing
90 char charoff charset checked class
91 clear cols colspan color compact coords
92 datetime dir disabled enctype for frame
93 headers height hreflang hspace id ismap
94 label lang maxlength media method
95 multiple name nohref noshade nowrap prompt
96 readonly rel rev rows rowspan rules scope
97 selected shape size span start summary
98 tabindex target title type valign
100 autoplay loopstart loopend end
103 "/" => 1, # emit proper <hr /> XHTML
104 href => $safe_url_regexp,
105 src => $safe_url_regexp,
106 action => $safe_url_regexp,
107 cite => $safe_url_regexp,
108 longdesc => $safe_url_regexp,
109 poster => $safe_url_regexp,
110 usemap => $safe_url_regexp,