Koha-QA

 view release on metacpan or  search on metacpan

t/security/csrf.t  view on Meta::CPAN

use Modern::Perl;
use Test::NoWarnings;
use Test::More tests => 11;
use File::Temp qw(tempfile);
use Koha::QA::Security::CSRF;

my $expected_messages = {
    missing_csrf_token => q{Some forms with POST method are missing CSRF token (bug 22990)},
    missing_op         => q{Form with POST method is missing op parameter (see bug 34478)},
    invalid_op_value   => q{op parameter value should start with "cud-" or be a TT variable (see bug 34478)},
};

sub check_csrf {
    my ($content) = @_;
    my $checker = Koha::QA::Security::CSRF->new( { content => $content } );
    $checker->check();
    return $checker->errors();
}

subtest 'Forms with POST method missing CSRF token and op' => sub {
    plan tests => 1;
    my $input = <<INPUT;
<form method="post" action="/cgi-bin/koha/foo.pl">
    <input type="text" name="bar" />
</form>
INPUT

    my @errors = check_csrf($input);
    is_deeply(
        \@errors,
        [
            {
                error       => 'missing_csrf_token',
                message     => $expected_messages->{missing_csrf_token},
                line        => '<form method="post" action="/cgi-bin/koha/foo.pl">',
                line_number => 1,
            },
            {
                error       => 'missing_op',
                message     => $expected_messages->{missing_op},
                line        => '<form method="post" action="/cgi-bin/koha/foo.pl">',
                line_number => 1,
            }
        ],
        'POST form without CSRF token and op parameter detected'
    );
};

subtest 'Forms with POST method and CSRF token but missing op' => sub {
    plan tests => 2;
    my $input = <<INPUT;
<form method="post" action="/cgi-bin/koha/foo.pl">
    [% INCLUDE 'csrf-token.inc' %]
    <input type="text" name="bar" />
</form>
INPUT

    my @errors = check_csrf($input);
    is_deeply(
        \@errors,
        [



( run in 1.003 second using v1.01-cache-2.11-cpan-eb134014566 )