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