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 => "sanitize", id => "htmlscrubber", call => \&sanitize);
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",
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",
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;
37 sub sanitize (@) { #{{{
39 return scrubber()->scrub($params{content});
44 return $_scrubber if defined $_scrubber;
46 eval q{use HTML::Scrubber};
48 # Lists based on http://feedparser.org/docs/html-sanitization.html
49 $_scrubber = HTML::Scrubber->new(
51 a abbr acronym address area b big blockquote br
52 button caption center cite code col colgroup dd del
53 dfn dir div dl dt em fieldset font form h1 h2 h3 h4
54 h5 h6 hr i img input ins kbd label legend li map
55 menu ol optgroup option p pre q s samp select small
56 span strike strong sub sup table tbody td textarea
57 tfoot th thead tr tt u ul var
59 default => [undef, { ( 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
73 "/" => 1, # emit proper <hr /> XHTML
74 href => $safe_url_regexp,
75 src => $safe_url_regexp,
76 action => $safe_url_regexp,