Koha-QA

 view release on metacpan or  search on metacpan

lib/Koha/QA/Security/CSRF.pm  view on Meta::CPAN

package Koha::QA::Security::CSRF;

use Modern::Perl;
use base 'Koha::QA::Base';

=head1 NAME

Koha::QA::Security::CSRF - Check for missing CSRF tokens and op parameters in forms

=head1 SYNOPSIS

  use Koha::QA::Security::CSRF;

  # With a file
  my $checker = Koha::QA::Security::CSRF->new({file => $file_path});

  # Or with content directly
  my $checker = Koha::QA::Security::CSRF->new({content => $template_content});

  my $is_valid = $checker->check;
  my @errors = $checker->errors;

=head1 DESCRIPTION

This module checks Template Toolkit files for forms that are missing CSRF tokens
or have missing/improper op parameters.

=head1 METHODS

=head2 new

  my $checker = Koha::QA::Security::CSRF->new({file => $file_path});

Creates a new CSRF checker instance.

=head2 check

  my $is_valid = $checker->check;

Checks the file for missing CSRF tokens and op parameters.

=head2 errors

  my @errors = $checker->errors;

Returns array of error hashrefs, each containing:
  - line: the form tag content
  - line_number: the line number
  - error: the error type (missing_csrf_token, missing_op, invalid_op_value)

=cut

sub check {
    my ($self) = @_;
    my $content = $self->content;

    my @errors;
    my @lines = split /\n/, $content;
    return 1 unless grep { $_ =~ m|<form| } @lines;

    my ( $in_form, $form_start_line, $form_line_content, $found_csrf, $has_op, $op_value );
    my $line_number = 0;
    for my $line (@lines) {



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