| 1 |
NAME |
| 2 |
|
| 3 |
HTML::Scrubber - Perl extension for scrubbing/sanitizing HTML |
| 4 |
|
| 5 |
VERSION |
| 6 |
|
| 7 |
version 0.19 |
| 8 |
|
| 9 |
SYNOPSIS |
| 10 |
|
| 11 |
use HTML::Scrubber; |
| 12 |
|
| 13 |
my $scrubber = HTML::Scrubber->new( allow => [ qw[ p b i u hr br ] ] ); |
| 14 |
print $scrubber->scrub('<p><b>bold</b> <em>missing</em></p>'); |
| 15 |
# output is: <p><b>bold</b> </p> |
| 16 |
|
| 17 |
# more complex input |
| 18 |
my $html = q[ |
| 19 |
<style type="text/css"> BAD { background: #666; color: #666;} </style> |
| 20 |
<script language="javascript"> alert("Hello, I am EVIL!"); </script> |
| 21 |
<HR> |
| 22 |
a => <a href=1>link </a> |
| 23 |
br => <br> |
| 24 |
b => <B> bold </B> |
| 25 |
u => <U> UNDERLINE </U> |
| 26 |
]; |
| 27 |
|
| 28 |
print $scrubber->scrub($html); |
| 29 |
|
| 30 |
$scrubber->deny( qw[ p b i u hr br ] ); |
| 31 |
|
| 32 |
print $scrubber->scrub($html); |
| 33 |
|
| 34 |
DESCRIPTION |
| 35 |
|
| 36 |
If you want to "scrub" or "sanitize" html input in a reliable and |
| 37 |
flexible fashion, then this module is for you. |
| 38 |
|
| 39 |
I wasn't satisfied with HTML::Sanitizer because it is based on |
| 40 |
HTML::TreeBuilder, so I thought I'd write something similar that works |
| 41 |
directly with HTML::Parser. |
| 42 |
|
| 43 |
METHODS |
| 44 |
|
| 45 |
First a note on documentation: just study the EXAMPLE below. It's all |
| 46 |
the documentation you could need. |
| 47 |
|
| 48 |
Also, be sure to read all the comments as well as How does it work?. |
| 49 |
|
| 50 |
If you're new to perl, good luck to you. |
| 51 |
|
| 52 |
new |
| 53 |
|
| 54 |
my $scrubber = HTML::Scrubber->new( allow => [ qw[ p b i u hr br ] ] ); |
| 55 |
|
| 56 |
Build a new HTML::Scrubber. The arguments are the initial values for |
| 57 |
the following directives:- |
| 58 |
|
| 59 |
* default |
| 60 |
|
| 61 |
* allow |
| 62 |
|
| 63 |
* deny |
| 64 |
|
| 65 |
* rules |
| 66 |
|
| 67 |
* process |
| 68 |
|
| 69 |
* comment |
| 70 |
|
| 71 |
comment |
| 72 |
|
| 73 |
warn "comments are ", $p->comment ? 'allowed' : 'not allowed'; |
| 74 |
$p->comment(0); # off by default |
| 75 |
|
| 76 |
process |
| 77 |
|
| 78 |
warn "process instructions are ", $p->process ? 'allowed' : 'not allowed'; |
| 79 |
$p->process(0); # off by default |
| 80 |
|
| 81 |
script |
| 82 |
|
| 83 |
warn "script tags (and everything in between) are supressed" |
| 84 |
if $p->script; # off by default |
| 85 |
$p->script( 0 || 1 ); |
| 86 |
|
| 87 |
** Please note that this is implemented using HTML::Parser's |
| 88 |
ignore_elements function, so if script is set to true, all script tags |
| 89 |
encountered will be validated like all other tags. |
| 90 |
|
| 91 |
style |
| 92 |
|
| 93 |
warn "style tags (and everything in between) are supressed" |
| 94 |
if $p->style; # off by default |
| 95 |
$p->style( 0 || 1 ); |
| 96 |
|
| 97 |
** Please note that this is implemented using HTML::Parser's |
| 98 |
ignore_elements function, so if style is set to true, all style tags |
| 99 |
encountered will be validated like all other tags. |
| 100 |
|
| 101 |
allow |
| 102 |
|
| 103 |
$p->allow(qw[ t a g s ]); |
| 104 |
|
| 105 |
deny |
| 106 |
|
| 107 |
$p->deny(qw[ t a g s ]); |
| 108 |
|
| 109 |
rules |
| 110 |
|
| 111 |
$p->rules( |
| 112 |
img => { |
| 113 |
src => qr{^(?!http://)}i, # only relative image links allowed |
| 114 |
alt => 1, # alt attribute allowed |
| 115 |
'*' => 0, # deny all other attributes |
| 116 |
}, |
| 117 |
a => { |
| 118 |
href => sub { ... }, # check or adjust with a callback |
| 119 |
}, |
| 120 |
b => 1, |
| 121 |
... |
| 122 |
); |
| 123 |
|
| 124 |
Updates a set of attribute rules. Each rule can be 1/0, a regular |
| 125 |
expression or a callback. Values longer than 1 char are treated as |
| 126 |
regexps. The callback is called with the following arguments: the |
| 127 |
current object, tag name, attribute name, and attribute value; the |
| 128 |
callback should return an empty list to drop the attribute, undef to |
| 129 |
keep it without a value, or a new scalar value. |
| 130 |
|
| 131 |
default |
| 132 |
|
| 133 |
print "default is ", $p->default(); |
| 134 |
$p->default(1); # allow tags by default |
| 135 |
$p->default( |
| 136 |
undef, # don't change |
| 137 |
{ # default attribute rules |
| 138 |
'*' => 1, # allow attributes by default |
| 139 |
} |
| 140 |
); |
| 141 |
|
| 142 |
scrub_file |
| 143 |
|
| 144 |
$html = $scrubber->scrub_file('foo.html'); ## returns giant string |
| 145 |
die "Eeek $!" unless defined $html; ## opening foo.html may have failed |
| 146 |
$scrubber->scrub_file('foo.html', 'new.html') or die "Eeek $!"; |
| 147 |
$scrubber->scrub_file('foo.html', *STDOUT) |
| 148 |
or die "Eeek $!" |
| 149 |
if fileno STDOUT; |
| 150 |
|
| 151 |
scrub |
| 152 |
|
| 153 |
print $scrubber->scrub($html); ## returns giant string |
| 154 |
$scrubber->scrub($html, 'new.html') or die "Eeek $!"; |
| 155 |
$scrubber->scrub($html', *STDOUT) |
| 156 |
or die "Eeek $!" |
| 157 |
if fileno STDOUT; |
| 158 |
|
| 159 |
default handler, used by both _scrub and _scrub_fh. Moved all the |
| 160 |
common code (basically all of it) into a single routine for ease of |
| 161 |
maintenance. |
| 162 |
|
| 163 |
default handler, does the scrubbing if we're scrubbing out to a file. |
| 164 |
Now calls _scrub_str and pushes that out to a file. |
| 165 |
|
| 166 |
default handler, does the scrubbing if we're returning a giant string. |
| 167 |
Now calls _scrub_str and appends that to the output string. |
| 168 |
|
| 169 |
How does it work? |
| 170 |
|
| 171 |
When a tag is encountered, HTML::Scrubber allows/denies the tag using |
| 172 |
the explicit rule if one exists. |
| 173 |
|
| 174 |
If no explicit rule exists, Scrubber applies the default rule. |
| 175 |
|
| 176 |
If an explicit rule exists, but it's a simple rule(1), then the default |
| 177 |
attribute rule is applied. |
| 178 |
|
| 179 |
EXAMPLE |
| 180 |
|
| 181 |
#!/usr/bin/perl -w |
| 182 |
use HTML::Scrubber; |
| 183 |
use strict; |
| 184 |
|
| 185 |
my @allow = qw[ br hr b a ]; |
| 186 |
|
| 187 |
my @rules = ( |
| 188 |
script => 0, |
| 189 |
img => { |
| 190 |
src => qr{^(?!http://)}i, # only relative image links allowed |
| 191 |
alt => 1, # alt attribute allowed |
| 192 |
'*' => 0, # deny all other attributes |
| 193 |
}, |
| 194 |
); |
| 195 |
|
| 196 |
my @default = ( |
| 197 |
0 => # default rule, deny all tags |
| 198 |
{ |
| 199 |
'*' => 1, # default rule, allow all attributes |
| 200 |
'href' => qr{^(?:http|https|ftp)://}i, |
| 201 |
'src' => qr{^(?:http|https|ftp)://}i, |
| 202 |
|
| 203 |
# If your perl doesn't have qr |
| 204 |
# just use a string with length greater than 1 |
| 205 |
'cite' => '(?i-xsm:^(?:http|https|ftp):)', |
| 206 |
'language' => 0, |
| 207 |
'name' => 1, # could be sneaky, but hey ;) |
| 208 |
'onblur' => 0, |
| 209 |
'onchange' => 0, |
| 210 |
'onclick' => 0, |
| 211 |
'ondblclick' => 0, |
| 212 |
'onerror' => 0, |
| 213 |
'onfocus' => 0, |
| 214 |
'onkeydown' => 0, |
| 215 |
'onkeypress' => 0, |
| 216 |
'onkeyup' => 0, |
| 217 |
'onload' => 0, |
| 218 |
'onmousedown' => 0, |
| 219 |
'onmousemove' => 0, |
| 220 |
'onmouseout' => 0, |
| 221 |
'onmouseover' => 0, |
| 222 |
'onmouseup' => 0, |
| 223 |
'onreset' => 0, |
| 224 |
'onselect' => 0, |
| 225 |
'onsubmit' => 0, |
| 226 |
'onunload' => 0, |
| 227 |
'src' => 0, |
| 228 |
'type' => 0, |
| 229 |
} |
| 230 |
); |
| 231 |
|
| 232 |
my $scrubber = HTML::Scrubber->new(); |
| 233 |
$scrubber->allow(@allow); |
| 234 |
$scrubber->rules(@rules); # key/value pairs |
| 235 |
$scrubber->default(@default); |
| 236 |
$scrubber->comment(1); # 1 allow, 0 deny |
| 237 |
|
| 238 |
## preferred way to create the same object |
| 239 |
$scrubber = HTML::Scrubber->new( |
| 240 |
allow => \@allow, |
| 241 |
rules => \@rules, |
| 242 |
default => \@default, |
| 243 |
comment => 1, |
| 244 |
process => 0, |
| 245 |
); |
| 246 |
|
| 247 |
require Data::Dumper, die Data::Dumper::Dumper($scrubber) if @ARGV; |
| 248 |
|
| 249 |
my $it = q[ |
| 250 |
<?php echo(" EVIL EVIL EVIL "); ?> <!-- asdf --> |
| 251 |
<hr> |
| 252 |
<I FAKE="attribute" > IN ITALICS WITH FAKE="attribute" </I><br> |
| 253 |
<B> IN BOLD </B><br> |
| 254 |
<A NAME="evil"> |
| 255 |
<A HREF="javascript:alert('die die die');">HREF=JAVA <!></A> |
| 256 |
<br> |
| 257 |
<A HREF="image/bigone.jpg" ONMOUSEOVER="alert('die die die');"> |
| 258 |
<IMG SRC="image/smallone.jpg" ALT="ONMOUSEOVER JAVASCRIPT"> |
| 259 |
</A> |
| 260 |
</A> <br> |
| 261 |
]; |
| 262 |
|
| 263 |
print "#original text", $/, $it, $/; |
| 264 |
print |
| 265 |
"#scrubbed text (default ", $scrubber->default(), # no arguments returns the current value |
| 266 |
" comment ", $scrubber->comment(), " process ", $scrubber->process(), " )", $/, $scrubber->scrub($it), $/; |
| 267 |
|
| 268 |
$scrubber->default(1); # allow all tags by default |
| 269 |
$scrubber->comment(0); # deny comments |
| 270 |
|
| 271 |
print |
| 272 |
"#scrubbed text (default ", |
| 273 |
$scrubber->default(), |
| 274 |
" comment ", |
| 275 |
$scrubber->comment(), |
| 276 |
" process ", |
| 277 |
$scrubber->process(), |
| 278 |
" )", $/, |
| 279 |
$scrubber->scrub($it), |
| 280 |
$/; |
| 281 |
|
| 282 |
$scrubber->process(1); # allow process instructions (dangerous) |
| 283 |
$default[0] = 1; # allow all tags by default |
| 284 |
$default[1]->{'*'} = 0; # deny all attributes by default |
| 285 |
$scrubber->default(@default); # set the default again |
| 286 |
|
| 287 |
print |
| 288 |
"#scrubbed text (default ", |
| 289 |
$scrubber->default(), |
| 290 |
" comment ", |
| 291 |
$scrubber->comment(), |
| 292 |
" process ", |
| 293 |
$scrubber->process(), |
| 294 |
" )", $/, |
| 295 |
$scrubber->scrub($it), |
| 296 |
$/; |
| 297 |
|
| 298 |
FUN |
| 299 |
|
| 300 |
If you have Test::Inline (and you've installed HTML::Scrubber), try |
| 301 |
|
| 302 |
pod2test Scrubber.pm >scrubber.t |
| 303 |
perl scrubber.t |
| 304 |
|
| 305 |
SEE ALSO |
| 306 |
|
| 307 |
HTML::Parser, Test::Inline. |
| 308 |
|
| 309 |
The HTML::Sanitizer module is no longer available on CPAN. |
| 310 |
|
| 311 |
VERSION REQUIREMENTS |
| 312 |
|
| 313 |
As of version 0.14 I have added a perl minimum version requirement of |
| 314 |
5.8. This is basically due to failures on the smokers perl 5.6 |
| 315 |
installations - which appears to be down to installation mechanisms and |
| 316 |
requirements. |
| 317 |
|
| 318 |
Since I don't want to spend the time supporting a version that is so |
| 319 |
old (and may not work for reasons on UTF support etc), I have added a |
| 320 |
use 5.008; to the main module. |
| 321 |
|
| 322 |
If this is problematic I am very willing to accept patches to fix this |
| 323 |
up, although I do not personally see a good reason to support a release |
| 324 |
that has been obsolete for 13 years. |
| 325 |
|
| 326 |
CONTRIBUTING |
| 327 |
|
| 328 |
If you want to contribute to the development of this module, the code |
| 329 |
is on GitHub <http://github.com/nigelm/html-scrubber>. You'll need a |
| 330 |
perl environment with Dist::Zilla, and if you're just getting started, |
| 331 |
there's some documentation on using Vagrant and Perlbrew here |
| 332 |
<http://mrcaron.github.io/2015/03/06/Perl-CPAN-Pull-Request.html>. |
| 333 |
|
| 334 |
There is now a .perltidyrc and a .tidyallrc file within the repository |
| 335 |
for the standard perltidy settings used - I will apply these before new |
| 336 |
releases. Please do not let formatting prevent you from sending in |
| 337 |
patches etc - this can be sorted out as part of the release process. |
| 338 |
Info on tidyall can be found at |
| 339 |
https://metacpan.org/pod/distribution/Code-TidyAll/bin/tidyall. |
| 340 |
|
| 341 |
AUTHORS |
| 342 |
|
| 343 |
* Ruslan Zakirov <[email protected]> |
| 344 |
|
| 345 |
* Nigel Metheringham <[email protected]> |
| 346 |
|
| 347 |
* D. H. <[email protected]> |
| 348 |
|
| 349 |
COPYRIGHT AND LICENSE |
| 350 |
|
| 351 |
This software is copyright (c) 2018 by Ruslan Zakirov, Nigel |
| 352 |
Metheringham, 2003-2004 D. H. |
| 353 |
|
| 354 |
This is free software; you can redistribute it and/or modify it under |
| 355 |
the same terms as the Perl 5 programming language system itself. |
| 356 |
|
| 357 |
SUPPORT |
| 358 |
|
| 359 |
Perldoc |
| 360 |
|
| 361 |
You can find documentation for this module with the perldoc command. |
| 362 |
|
| 363 |
perldoc HTML::Scrubber |
| 364 |
|
| 365 |
Websites |
| 366 |
|
| 367 |
The following websites have more information about this module, and may |
| 368 |
be of help to you. As always, in addition to those websites please use |
| 369 |
your favorite search engine to discover more resources. |
| 370 |
|
| 371 |
* MetaCPAN |
| 372 |
|
| 373 |
A modern, open-source CPAN search engine, useful to view POD in HTML |
| 374 |
format. |
| 375 |
|
| 376 |
https://metacpan.org/release/HTML-Scrubber |
| 377 |
|
| 378 |
* Search CPAN |
| 379 |
|
| 380 |
The default CPAN search engine, useful to view POD in HTML format. |
| 381 |
|
| 382 |
http://search.cpan.org/dist/HTML-Scrubber |
| 383 |
|
| 384 |
* RT: CPAN's Bug Tracker |
| 385 |
|
| 386 |
The RT ( Request Tracker ) website is the default bug/issue tracking |
| 387 |
system for CPAN. |
| 388 |
|
| 389 |
https://rt.cpan.org/Public/Dist/Display.html?Name=HTML-Scrubber |
| 390 |
|
| 391 |
* AnnoCPAN |
| 392 |
|
| 393 |
The AnnoCPAN is a website that allows community annotations of Perl |
| 394 |
module documentation. |
| 395 |
|
| 396 |
http://annocpan.org/dist/HTML-Scrubber |
| 397 |
|
| 398 |
* CPAN Ratings |
| 399 |
|
| 400 |
The CPAN Ratings is a website that allows community ratings and |
| 401 |
reviews of Perl modules. |
| 402 |
|
| 403 |
http://cpanratings.perl.org/d/HTML-Scrubber |
| 404 |
|
| 405 |
* CPANTS |
| 406 |
|
| 407 |
The CPANTS is a website that analyzes the Kwalitee ( code metrics ) |
| 408 |
of a distribution. |
| 409 |
|
| 410 |
http://cpants.cpanauthors.org/dist/HTML-Scrubber |
| 411 |
|
| 412 |
* CPAN Testers |
| 413 |
|
| 414 |
The CPAN Testers is a network of smoke testers who run automated |
| 415 |
tests on uploaded CPAN distributions. |
| 416 |
|
| 417 |
http://www.cpantesters.org/distro/H/HTML-Scrubber |
| 418 |
|
| 419 |
* CPAN Testers Matrix |
| 420 |
|
| 421 |
The CPAN Testers Matrix is a website that provides a visual overview |
| 422 |
of the test results for a distribution on various Perls/platforms. |
| 423 |
|
| 424 |
http://matrix.cpantesters.org/?dist=HTML-Scrubber |
| 425 |
|
| 426 |
* CPAN Testers Dependencies |
| 427 |
|
| 428 |
The CPAN Testers Dependencies is a website that shows a chart of the |
| 429 |
test results of all dependencies for a distribution. |
| 430 |
|
| 431 |
http://deps.cpantesters.org/?module=HTML::Scrubber |
| 432 |
|
| 433 |
Bugs / Feature Requests |
| 434 |
|
| 435 |
Please report any bugs or feature requests by email to |
| 436 |
bug-html-scrubber at rt.cpan.org, or through the web interface at |
| 437 |
https://rt.cpan.org/Public/Bug/Report.html?Queue=HTML-Scrubber. You |
| 438 |
will be automatically notified of any progress on the request by the |
| 439 |
system. |
| 440 |
|
| 441 |
Source Code |
| 442 |
|
| 443 |
The code is open to the world, and available for you to hack on. Please |
| 444 |
feel free to browse it and play with it, or whatever. If you want to |
| 445 |
contribute patches, please send me a diff or prod me to pull from your |
| 446 |
repository :) |
| 447 |
|
| 448 |
https://github.com/nigelm/html-scrubber |
| 449 |
|
| 450 |
git clone https://github.com/nigelm/html-scrubber.git |
| 451 |
|
| 452 |
CONTRIBUTORS |
| 453 |
|
| 454 |
* Andrei Vereha <[email protected]> |
| 455 |
|
| 456 |
* Lee Johnson <[email protected]> |
| 457 |
|
| 458 |
* Michael Caron <[email protected]> |
| 459 |
|
| 460 |
* Michael Caron <[email protected]> |
| 461 |
|
| 462 |
* Nigel Metheringham <[email protected]> |
| 463 |
|
| 464 |
* Paul Cochrane <[email protected]> |
| 465 |
|
| 466 |
* Ruslan Zakirov <[email protected]> |
| 467 |
|
| 468 |
* Sergey Romanov <[email protected]> |
| 469 |
|
| 470 |
* vagrant <vagrant@precise64.(none)> |
| 471 |
|