File: xss.t

package info (click to toggle)
libhtml-restrict-perl 3.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 364 kB
  • sloc: perl: 842; makefile: 7
file content (35 lines) | stat: -rwxr-xr-x 941 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env perl

use warnings;
use strict;

use Test::More;
use HTML::Restrict ();

my $hr = HTML::Restrict->new;
$hr->debug(0);
$hr->set_rules( { a => [ 'href', 'class' ] } );

my $text = q{<a href="javascript:alert(1)">oops!</a>};

my $clean = $hr->process($text);
is $clean, '<a>oops!</a>', 'bad scheme removed';

is $hr->process(q{<a href="javascript&#58;evil_script()">evil</a>}),
    '<a>evil</a>', 'bad scheme removed';

foreach my $uri (
    'http://vilerichard.com', 'https://vilerichard.com',
    '//vilerichard.com',      '/music'
) {
    my $img = qq[<a href="$uri">click</a>];
    is $hr->process($img), $img, 'good uri scheme preserved';
}

is $hr->process(
    q{<a class="&quot;&gt;&lt;script&gt;alert(&quot;oops&quot;);&lt;/script&gt;&lt;a href=&quot;"></a>}
    ),
    q{<a class="&quot;&gt;&lt;script&gt;alert(&quot;oops&quot;);&lt;/script&gt;&lt;a href=&quot;"></a>},
    'attribute value filtered';

done_testing();