1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
|
=head1 NAME
HTML::Scrubber - Perl extension for scrubbing/sanitizing html
=head1 SYNOPSIS
=for example begin
#!/usr/bin/perl -w
use HTML::Scrubber;
use strict;
#
my $html = q[
<style type="text/css"> BAD { background: #666; color: #666;} </style>
<script language="javascript"> alert("Hello, I am EVIL!"); </script>
<HR>
a => <a href=1>link </a>
br => <br>
b => <B> bold </B>
u => <U> UNDERLINE </U>
];
#
my $scrubber = HTML::Scrubber->new( allow => [ qw[ p b i u hr br ] ] ); #
#
print $scrubber->scrub($html); #
#
$scrubber->deny( qw[ p b i u hr br ] ); #
#
print $scrubber->scrub($html); #
#
=for example end
=head1 DESCRIPTION
If you wanna "scrub" or "sanitize" html input
in a reliable an flexible fashion,
then this module is for you.
I wasn't satisfied with HTML::Sanitizer because it is
based on HTML::TreeBuilder,
so I thought I'd write something similar
that works directly with HTML::Parser.
=head1 METHODS
First a note on documentation: just study the L<EXAMPLE|"EXAMPLE"> below.
It's all the documentation you could need
Also, be sure to read all the comments as well as
L<How does it work?|"How does it work?">.
If you're new to perl, good luck to you.
=cut
package HTML::Scrubber;
use HTML::Parser();
use HTML::Entities;
use vars qw[ $VERSION @_scrub @_scrub_fh ];
use strict;
$VERSION = '0.08';
# my my my my, these here to prevent foolishness like
# http://perlmonks.org/index.pl?node_id=251127#Stealing+Lexicals
(@_scrub )= ( \&_scrub, "self, event, tagname, attr, attrseq, text");
(@_scrub_fh )= ( \&_scrub_fh, "self, event, tagname, attr, attrseq, text");
sub new {
my $package = shift;
my $p = HTML::Parser->new(
api_version => 3,
default_h => \@_scrub,
marked_sections => 0,
strict_comment => 0,
unbroken_text => 1,
case_sensitive => 0,
boolean_attribute_value => undef,
);
my $self = {
_p => $p,
_rules => {
'*' => 0,
},
_comment => 0,
_process => 0,
_r => "",
_optimize => 1,
_script => 0,
_style => 0,
};
$p->{"\0_s"} = bless $self, $package;
return $self unless @_;
my(%args)= @_;
for my $f( qw[ default allow deny rules process comment ] ) {
next unless exists $args{$f};
if( ref $args{$f} ) {
$self->$f( @{ $args{$f} } ) ;
} else {
$self->$f( $args{$f} ) ;
}
}
return $self;
}
=head2 comment
warn "comments are ", $p->comment ? 'allowed' : 'not allowed';
$p->comment(0); # off by default
=cut
sub comment {
return
$_[0]->{_comment}
if @_ == 1;
$_[0]->{_comment} = $_[1];
return;
}
=head2 process
warn "process instructions are ", $p->process ? 'allowed' : 'not allowed';
$p->process(0); # off by default
=cut
sub process {
return
$_[0]->{_process}
if @_ == 1;
$_[0]->{_process} = $_[1];
return;
}
=head2 script
warn "script tags (and everything in between) are supressed"
if $p->script; # off by default
$p->script( 0 || 1 );
B<**> Please note that this is implemented
using HTML::Parser's ignore_elements function,
so if C<script> is set to true,
all script tags encountered will be validated like all other tags.
=cut
sub script {
return
$_[0]->{_script}
if @_ == 1;
$_[0]->{_script} = $_[1];
return;
}
=head2 style
warn "style tags (and everything in between) are supressed"
if $p->style; # off by default
$p->style( 0 || 1 );
B<**> Please note that this is implemented
using HTML::Parser's ignore_elements function,
so if C<style> is set to true,
all style tags encountered will be validated like all other tags.
=cut
sub style {
return
$_[0]->{_style}
if @_ == 1;
$_[0]->{_style} = $_[1];
return;
}
=head2 allow
$p->allow(qw[ t a g s ]);
=cut
sub allow {
my $self = shift;
for my $k(@_){
$self->{_rules}{lc $k}=1;
}
$self->{_optimize} = 1; # each time a rule changes, reoptimize when parse
return;
}
=head2 deny
$p->deny(qw[ t a g s ]);
=cut
sub deny {
my $self = shift;
for my $k(@_){
$self->{_rules}{lc $k} = 0;
}
$self->{_optimize} = 1; # each time a rule changes, reoptimize when parse
return;
}
=head2 rules
$p->rules(
img => {
src => qr{^(?!http://)}i, # only relative image links allowed
alt => 1, # alt attribute allowed
'*' => 0, # deny all other attributes
},
b => 1,
...
);
=cut
sub rules{
my $self = shift;
my(%rules)= @_;
for my $k(keys %rules) {
$self->{_rules}{lc $k} = $rules{$k};
}
$self->{_optimize} = 1; # each time a rule changes, reoptimize when parse
return;
}
=head2 default
print "default is ", $p->default();
$p->default(1); # allow tags by default
$p->default(
undef, # don't change
{ # default attribute rules
'*' => 1, # allow attributes by default
}
);
=cut
sub default {
return
$_[0]->{_rules}{'*'}
if @_ == 1;
$_[0]->{_rules}{'*'} = $_[1] if defined $_[1];
$_[0]->{_rules}{'_'} = $_[2] if defined $_[2] and ref $_[2];
$_[0]->{_optimize} = 1; # each time a rule changes, reoptimize when parse
return;
}
=head2 scrub_file
$html = $scrubber->scrub_file('foo.html'); ## returns giant string
die "Eeek $!" unless defined $html; ## opening foo.html may have failed
$scrubber->scrub_file('foo.html', 'new.html') or die "Eeek $!";
$scrubber->scrub_file('foo.html', *STDOUT)
or die "Eeek $!"
if fileno STDOUT;
=cut
sub scrub_file {
if(@_ > 2){
return unless defined $_[0]->_out($_[2]);
} else {
$_[0]->{_p}->handler( default => @_scrub );
}
$_[0]->_optimize() ;#if $_[0]->{_optimize};
$_[0]->{_p}->parse_file($_[1]);
return delete $_[0]->{_r} unless exists $_[0]->{_out};
delete $_[0]->{_out};
return 1;
}
=head2 scrub
print $scrubber->scrub($html); ## returns giant string
$scrubber->scrub($html, 'new.html') or die "Eeek $!";
$scrubber->scrub($html', *STDOUT)
or die "Eeek $!"
if fileno STDOUT;
=cut
sub scrub {
if(@_ > 2){
return unless defined $_[0]->_out($_[2]);
} else {
$_[0]->{_p}->handler( default => @_scrub );
}
$_[0]->_optimize();# if $_[0]->{_optimize};
$_[0]->{_p}->parse($_[1]);
$_[0]->{_p}->eof();
return delete $_[0]->{_r} unless exists $_[0]->{_out};
delete $_[0]->{_out};
return 1;
}
=for comment _out
$scrubber->_out(*STDOUT) if fileno STDOUT;
$scrubber->_out('foo.html') or die "eeek $!";
=cut
sub _out {
my($self, $o ) = @_;
unless( ref $o and ref \$o ne 'GLOB') {
local *F;
open F, ">$o" or return undef;
binmode F;
$self->{_out} = *F;
} else {
$self->{_out} = $o;
}
$self->{_p}->handler( default => @_scrub_fh );
return 1;
}
=for comment _validate
Uses $self->{_rules} to do attribute validation.
Takes tag, rule('_' || $tag), attrref.
=cut
sub _validate {
my($s, $t, $r, $a, $as) = @_;
return "<$t>" unless %$a;
$r = $s->{_rules}->{$r};
my %f;
for my $k( keys %$a ) {
if( exists $r->{$k} ) {
if( ref $r->{$k} || length($r->{$k}) > 1 ) {
$f{$k} = $a->{$k} if $a->{$k} =~ m{$r->{$k}};
} elsif( $r->{$k} ) {
$f{$k} = $a->{$k};
}
} elsif( exists $r->{'*'} and $r->{'*'} ) {
$f{$k} = $a->{$k};
}
}
if( %f ){
my %seen;
return "<$t $r>"
if $r = join ' ',
map {
defined $f{$_}
? qq[$_="].encode_entities($f{$_}).q["]
: $_; # boolean attribute (TODO?)
} grep {
exists $f{$_} and !$seen{$_}++;
} @$as;
}
return "<$t>";
}
=for comment _scrub_fh
I<default> handler, does the scrubbing if we're scrubbing out to a file.
=cut
sub _scrub_fh {
my( $p, $e, $t, $a, $as, $text ) = @_;
my $s = $p->{"\0_s"} ;
if ( $e eq 'start' )
{
if( exists $s->{_rules}->{$t} ) # is there a specific rule
{
if( ref $s->{_rules}->{$t} ) # is it complicated?(not simple;)
{
print
{$s->{_out}}
$s->_validate($t, $t, $a, $as);
}
elsif( $s->{_rules}->{$t} ) # validate using default attribute rule
{
print
{$s->{_out}}
$s->_validate($t, '_', $a, $as);
}
}
elsif( $s->{_rules}->{'*'} ) # default allow tags
{
print
{$s->{_out}}
$s->_validate($t, '_', $a, $as);
}
}
elsif ( $e eq 'end' )
{
if( exists $s->{_rules}->{$t} )
{
print
{$s->{_out}}
"</$t>"
if $s->{_rules}->{$t};
}
elsif( $s->{_rules}->{'*'} )
{
print {$s->{_out}} "</$t>";
}
}
elsif ( $e eq 'comment' )
{
print
{$s->{_out}}
$text
if $s->{_comment};
}
elsif ( $e eq 'process' )
{
print
{$s->{_out}}
$text
if $s->{_process};
}
elsif ( $e eq 'text' or $e eq 'default')
{
$text =~ s/</</g; #https://rt.cpan.org/Ticket/Attachment/8716/10332/scrubber.patch
$text =~ s/>/>/g;
print
{$s->{_out}}
$text;
}
}
=for comment _scrub
I<default> handler, does the scrubbing if we're returning a giant string.
=cut
sub _scrub {
my( $p, $e, $t, $a, $as, $text ) = @_;
my $s = $p->{"\0_s"} ;
if ( $e eq 'start' )
{
if( exists $s->{_rules}->{$t} ) # is there a specific rule
{
if( ref $s->{_rules}->{$t} ) # is it complicated?(not simple;)
{
$s->{_r} .= $s->_validate($t, $t, $a, $as);
}
elsif( $s->{_rules}->{$t} ) # validate using default attribute rule
{
$s->{_r} .= $s->_validate($t, '_', $a, $as);
}
}
elsif( $s->{_rules}->{'*'} ) # default allow tags
{
$s->{_r} .= $s->_validate($t, '_', $a, $as);
}
}
elsif ( $e eq 'end' )
{
if( exists $s->{_rules}->{$t} )
{
$s->{_r} .= "</$t>" if $s->{_rules}->{$t};
}
elsif( $s->{_rules}->{'*'} )
{
$s->{_r} .= "</$t>";
}
}
elsif ( $e eq 'comment' )
{
$s->{_r} .= $text if $s->{_comment};
}
elsif ( $e eq 'process' )
{
$s->{_r} .= $text if $s->{_process};
}
elsif ( $e eq 'text' or $e eq 'default')
{
$text =~ s/</</g; #https://rt.cpan.org/Ticket/Attachment/8716/10332/scrubber.patch
$text =~ s/>/>/g;
$s->{_r} .= $text;
}
elsif ( $e eq 'start_document' )
{
$s->{_r} = "";
}
}
sub _optimize {
my($self) = @_;
my( @ignore_elements ) = grep { not $self->{"_$_"} } qw(script style);
$self->{_p}->ignore_elements(@ignore_elements); # if @ is empty, we reset ;)
return unless $self->{_optimize};
#sub allow
# return unless $self->{_optimize}; # till I figure it out (huh)
if( $self->{_rules}{'*'} ){ # default allow
$self->{_p}->report_tags(); # so clear it
} else {
my(@reports) =
grep { # report only tags we want
$self->{_rules}{$_}
} keys %{
$self->{_rules}
};
$self->{_p}->report_tags( # default deny, so optimize
@reports
) if @reports;
}
# sub deny
# return unless $self->{_optimize}; # till I figure it out (huh)
my(@ignores)=
grep {
not $self->{_rules}{$_}
} grep {
$_ ne '*'
} keys %{
$self->{_rules}
};
$self->{_p}->ignore_tags( # always ignore stuff we don't want
@ignores
) if @ignores;
$self->{_optimize}=0;
return;
}
sub DESTROY {
delete $_[0]->{_p}->{"\0_s"}; # break circular reference
}
1;
#print sprintf q[ '%-12s => %s,], "$_'", $h{$_} for sort keys %h;# perl!
#perl -ne"chomp;print $_;print qq'\t\t# test ', ++$a if /ok\(/;print $/" test.pl >test2.pl
#perl -ne"chomp;print $_;if( /ok\(/ ){s/\#test \d+$//;print qq'\t\t# test ', ++$a }print $/" test.pl >test2.pl
#perl -ne"chomp;if(/ok\(/){s/# test .*$//;print$_,qq'\t\t# test ',++$a}else{print$_}print$/" test.pl >test2.pl
=head1 How does it work?
When a tag is encountered, HTML::Scrubber
allows/denies the tag using the explicit rule if one exists.
If no explicit rule exists, Scrubber applies the default rule.
If an explicit rule exists,
but it's a simple rule(1),
the default attribute rule is applied.
=head2 EXAMPLE
=for example begin
#!/usr/bin/perl -w
use HTML::Scrubber;
use strict;
#
my @allow = qw[ br hr b a ];
#
my @rules = (
script => 0,
img => {
src => qr{^(?!http://)}i, # only relative image links allowed
alt => 1, # alt attribute allowed
'*' => 0, # deny all other attributes
},
);
#
my @default = (
0 => # default rule, deny all tags
{
'*' => 1, # default rule, allow all attributes
'href' => qr{^(?!(?:java)?script)}i,
'src' => qr{^(?!(?:java)?script)}i,
# If your perl doesn't have qr
# just use a string with length greater than 1
'cite' => '(?i-xsm:^(?!(?:java)?script))',
'language' => 0,
'name' => 1, # could be sneaky, but hey ;)
'onblur' => 0,
'onchange' => 0,
'onclick' => 0,
'ondblclick' => 0,
'onerror' => 0,
'onfocus' => 0,
'onkeydown' => 0,
'onkeypress' => 0,
'onkeyup' => 0,
'onload' => 0,
'onmousedown' => 0,
'onmousemove' => 0,
'onmouseout' => 0,
'onmouseover' => 0,
'onmouseup' => 0,
'onreset' => 0,
'onselect' => 0,
'onsubmit' => 0,
'onunload' => 0,
'src' => 0,
'type' => 0,
}
);
#
my $scrubber = HTML::Scrubber->new();
$scrubber->allow( @allow );
$scrubber->rules( @rules ); # key/value pairs
$scrubber->default( @default );
$scrubber->comment(1); # 1 allow, 0 deny
#
## preferred way to create the same object
$scrubber = HTML::Scrubber->new(
allow => \@allow,
rules => \@rules,
default => \@default,
comment => 1,
process => 0,
);
#
require Data::Dumper,die Data::Dumper::Dumper($scrubber) if @ARGV;
#
my $it = q[
<?php echo(" EVIL EVIL EVIL "); ?> <!-- asdf -->
<hr>
<I FAKE="attribute" > IN ITALICS WITH FAKE="attribute" </I><br>
<B> IN BOLD </B><br>
<A NAME="evil">
<A HREF="javascript:alert('die die die');">HREF=JAVA <!></A>
<br>
<A HREF="image/bigone.jpg" ONMOUSEOVER="alert('die die die');">
<IMG SRC="image/smallone.jpg" ALT="ONMOUSEOVER JAVASCRIPT">
</A>
</A> <br>
];
#
print "#original text",$/, $it, $/;
print
"#scrubbed text (default ",
$scrubber->default(), # no arguments returns the current value
" comment ",
$scrubber->comment(),
" process ",
$scrubber->process(),
" )",
$/,
$scrubber->scrub($it),
$/;
#
$scrubber->default(1); # allow all tags by default
$scrubber->comment(0); # deny comments
#
print
"#scrubbed text (default ",
$scrubber->default(),
" comment ",
$scrubber->comment(),
" process ",
$scrubber->process(),
" )",
$/,
$scrubber->scrub($it),
$/;
#
$scrubber->process(1); # allow process instructions (dangerous)
$default[0] = 1; # allow all tags by default
$default[1]->{'*'} = 0; # deny all attributes by default
$scrubber->default(@default); # set the default again
#
print
"#scrubbed text (default ",
$scrubber->default(),
" comment ",
$scrubber->comment(),
" process ",
$scrubber->process(),
" )",
$/,
$scrubber->scrub($it),
$/;
=for example end
=head2 FUN
If you have Test::Inline (and you've installed HTML::Scrubber), try
pod2test Scrubber.pm >scrubber.t
perl scrubber.t
=head1 SEE ALSO
L<HTML::Parser>, L<Test::Inline>, L<HTML::Sanitizer>.
=head1 BUGS/SUGGESTIONS/ETC
Please use
https://rt.cpan.org/NoAuth/Bugs.html?Dist=HTML-Scrubber
to report I<bugs>/additions/etc
or send mail to <bug-HTML-Scrubber#rt.cpan.org>.
=head1 AUTHOR
D. H. (PodMaster)
=head1 LICENSE
Copyright (c) 2003-2004 by D.H. (PodMaster). All rights reserved.
This module is free software;
you can redistribute it and/or modify it under
the same terms as Perl itself.
The LICENSE file contains the full text of the license.
=cut
|