# t/clean-comment.t # # Test LJ::CleanHTML::clean_comment. # # Authors: # Afuna # Jen Griffin # # Copyright (c) 2013 by Dreamwidth Studios, LLC. # # This program is free software; you may redistribute it and/or modify it under # the same terms as Perl itself. For a copy of the license, please reference # 'perldoc perlartistic' or 'perldoc perlgpl'. # use strict; use warnings; use Test::More tests => 28; BEGIN { require "$ENV{LJHOME}/t/lib/ljtestlib.pl"; } use LJ::CleanHTML; my $orig_comment; my $clean_comment; my $clean = sub { my $opts = shift; LJ::CleanHTML::clean_comment( \$orig_comment, $opts ); }; # remove various positioning and display rules $orig_comment = qq{}; $clean_comment = qq{<\\/span>}; $clean->( { remove_positioning => 1 } ); ok( $orig_comment =~ /^$clean_comment$/, "Removed display:none ($orig_comment)" ); $orig_comment = qq{}; $clean_comment = qq{<\\/span>}; $clean->( { remove_positioning => 1 } ); ok( $orig_comment =~ /^$clean_comment$/, "Removed margin ($orig_comment)" ); $orig_comment = qq{}; $clean_comment = qq{<\\/span>}; $clean->( { remove_positioning => 1 } ); ok( $orig_comment =~ /^$clean_comment$/, "Removed height" ); # handle unreasonably large padding values $orig_comment = qq{}; $clean_comment = qq{<\\/span>}; $clean->( { remove_positioning => 1 } ); ok( $orig_comment =~ /^$clean_comment$/, "All padding removed. (Multiple rules, all too large)" ); $orig_comment = qq{}; $clean_comment = qq{<\\/span>}; $clean->( { remove_positioning => 1 } ); ok( $orig_comment =~ /^$clean_comment$/, "All padding removed. (Combined into one rule, all too large)" ); $orig_comment = qq{}; $clean_comment = qq{<\\/span>}; $clean->( { remove_positioning => 1 } ); ok( $orig_comment =~ /^$clean_comment$/, "All padding removed. (Multiple rules, mixed too large and small enough)" ); $orig_comment = qq{}; $clean_comment = qq{<\\/span>}; $clean->( { remove_positioning => 1 } ); ok( $orig_comment =~ /^$clean_comment$/, "All padding removed. (One dimension in a combined rule, mixed too large and small enough)" ); $orig_comment = qq{}; $clean_comment = qq{<\\/span>}; $clean->( { remove_positioning => 1 } ); ok( $orig_comment =~ /^$clean_comment$/, "Padding not removed; of reasonable size." ); $orig_comment = qq{test}; $clean_comment = qq{test}; $clean->(); ok( $orig_comment eq $clean_comment, "Font tag closed." ); $orig_comment = qq{test}; $clean_comment = qq{test}; $clean->(); ok( $orig_comment eq $clean_comment, "Spurious closing div stripped." ); $orig_comment = qq{
test}; $clean_comment = qq{
test
}; $clean->(); ok( $orig_comment eq $clean_comment, "Closing div inserted." ); $orig_comment = qq{
test}; $clean_comment = qq{
test}; $clean->(); ok( $orig_comment eq $clean_comment, "Bad open/closes fixed." ); $orig_comment = qq{

}; $clean_comment = qq{

}; $clean->(); ok( $orig_comment eq $clean_comment, "Aggressively close things." ); $orig_comment = qq{

}; $clean_comment = qq{

}; $clean->(); ok( $orig_comment eq $clean_comment, "Aggressive close with eaten extra close." ); note("Remove absolute sizes when logged out"); { $orig_comment = qq{foo}; $clean_comment = qq{foo}; $clean->( { anon_comment => 1 } ); is( $orig_comment, $clean_comment, "Retain relative font sizes" ); $orig_comment = qq{foo}; $clean_comment = qq{foo}; $clean->( { anon_comment => 1 } ); is( $orig_comment, $clean_comment, "Strip absolute font sizes" ); $orig_comment = qq{foo}; $clean_comment = qq{foo}; $clean->( { anon_comment => 1 } ); is( $orig_comment, $clean_comment, "Strip absolute font sizes" ); } note("Don't remove absolute sizes when logged in"); { $orig_comment = qq{foo}; $clean_comment = $orig_comment; $clean->(); is( $orig_comment, $clean_comment, "Retain relative font sizes" ); $orig_comment = qq{foo}; $clean_comment = $orig_comment; $clean->(); is( $orig_comment, $clean_comment, "Retain absolute font sizes" ); $orig_comment = qq{foo}; $clean_comment = $orig_comment; $clean->(); is( $orig_comment, $clean_comment, "Retain absolute font sizes" ); } # remove background urls from logged out users $orig_comment = qq{}; $clean_comment = qq{<\\/span>}; $clean->(); ok( $orig_comment =~ /^$clean_comment$/, "Background URL not cleaned: logged-in user" ); $orig_comment = qq{}; $clean_comment = qq{<\\/span>}; $clean->( { anon_comment => 1 } ); ok( $orig_comment =~ /^$clean_comment$/, "Background URL removed: anonymous comment" ); $orig_comment = qq{pre post}; $clean_comment = qq{pre post (asdf)}; $clean->( { anon_comment => 1 } ); is( $orig_comment, $clean_comment, "Full href bold escape" ); $orig_comment = qq{pre post}; $clean_comment = qq{pre post ()}; $clean->( { anon_comment => 1 } ); is( $orig_comment, $clean_comment, "Empty href bold escape" ); # another table exploit involving a tags. $orig_comment = q{}; $clean_comment = q{ (mailto:blah@blah.com)}; $clean->( { anon_comment => 1 } ); is( $orig_comment, $clean_comment, "Anonymous comment bold escape" ); note("various allowed/disallowed tags"); { $orig_comment = qq{abc}; $clean_comment = qq{abc}; $clean->(); is( $orig_comment, $clean_comment, "em tag allowed" ); $orig_comment = qq{abc}; $clean_comment = qq{abc}; $clean->(); is( $orig_comment, $clean_comment, "marquee tag not allowed" ); $orig_comment = qq{abc}; $clean_comment = qq{abc}; $clean->(); is( $orig_comment, $clean_comment, "blink tag not allowed" ); } 1;