View | Details | Raw Unified | Return to bug 24695
Collapse All | Expand All

(-)a/C4/Reports/Guided.pm (-2 / +2 lines)
Lines 549-556 sub execute_query { Link Here
549
    $limit  = 999999 unless $limit;
549
    $limit  = 999999 unless $limit;
550
    $debug and print STDERR "execute_query($sql, $offset, $limit)\n";
550
    $debug and print STDERR "execute_query($sql, $offset, $limit)\n";
551
551
552
    my $errors = Koha::Reports->validate_sql($sql);
552
    my ( $is_sql_valid, $errors ) = Koha::Report->new({ savedsql => $sql })->is_sql_valid;
553
    return (undef, @{$errors}[0]) if (scalar(@$errors));
553
    return (undef, @{$errors}[0]) unless $is_sql_valid;
554
554
555
    my ($useroffset, $userlimit);
555
    my ($useroffset, $userlimit);
556
556
(-)a/Koha/Report.pm (+30 lines)
Lines 35-40 Koha::Report - Koha Report Object class Link Here
35
35
36
=cut
36
=cut
37
37
38
# FIXME We could only return an error code instead of the arrayref
39
# Only 1 error is returned
40
# TODO Koha::Report->store should check this before saving
41
=head3 is_sql_valid
42
43
my ( $is_sql_valid, $errors ) = $report->is_sql_valid;
44
45
$errors is a arrayref of hashrefs, keys can be sqlerr or queryerr.
46
47
Validate SQL query string so it only contains a select,
48
not any of the harmful queries.
49
50
=cut
51
52
sub is_sql_valid {
53
    my ($self) = @_;
54
55
    my $sql = $self->savedsql;
56
    $sql //= '';
57
    my @errors = ();
58
59
    if ($sql =~ /;?\W?(UPDATE|DELETE|DROP|INSERT|SHOW|CREATE)\W/i) {
60
        push @errors, { sqlerr => $1 };
61
    } elsif ($sql !~ /^\s*SELECT\b\s*/i) {
62
        push @errors, { queryerr => 'Missing SELECT' };
63
    }
64
65
    return ( @errors ? 0 : 1, \@errors );
66
}
67
38
=head3 get_search_info
68
=head3 get_search_info
39
69
40
Return search info
70
Return search info
(-)a/Koha/Reports.pm (-22 lines)
Lines 35-62 Koha::Reports - Koha Report Object set class Link Here
35
35
36
=cut
36
=cut
37
37
38
=head3 validate_sql
39
40
Validate SQL query string so it only contains a select,
41
not any of the harmful queries.
42
43
=cut
44
45
sub validate_sql {
46
    my ($self, $sql) = @_;
47
48
    $sql //= '';
49
    my @errors = ();
50
51
    if ($sql =~ /;?\W?(UPDATE|DELETE|DROP|INSERT|SHOW|CREATE)\W/i) {
52
        push @errors, { sqlerr => $1 };
53
    } elsif ($sql !~ /^\s*SELECT\b\s*/i) {
54
        push @errors, { queryerr => 'Missing SELECT' };
55
    }
56
57
    return \@errors;
58
}
59
60
=head3 _type
38
=head3 _type
61
39
62
Returns name of corresponding DBIC resultset
40
Returns name of corresponding DBIC resultset
(-)a/reports/guided_reports.pl (-2 / +4 lines)
Lines 240-246 elsif ( $phase eq 'Update SQL'){ Link Here
240
240
241
    create_non_existing_group_and_subgroup($input, $group, $subgroup);
241
    create_non_existing_group_and_subgroup($input, $group, $subgroup);
242
242
243
    push(@errors, @{Koha::Reports->validate_sql($sql)});
243
    my ( $is_sql_valid, $validation_errors ) = Koha::Report->new({ savedsql => $sql })->is_sql_valid;
244
    push(@errors, @$validation_errors) unless $is_sql_valid;
244
245
245
    if (@errors) {
246
    if (@errors) {
246
        $template->param(
247
        $template->param(
Lines 596-602 elsif ( $phase eq 'Save Report' ) { Link Here
596
597
597
    create_non_existing_group_and_subgroup($input, $group, $subgroup);
598
    create_non_existing_group_and_subgroup($input, $group, $subgroup);
598
    ## FIXME this is AFTER entering a name to save the report under
599
    ## FIXME this is AFTER entering a name to save the report under
599
    push(@errors, @{Koha::Reports->validate_sql($sql)});
600
    my ( $is_sql_valid, $validation_errors ) = Koha::Report->new({ savedsql => $sql })->is_sql_valid;
601
    push(@errors, @$validation_errors) unless $is_sql_valid;
600
602
601
    if (@errors) {
603
    if (@errors) {
602
        $template->param(
604
        $template->param(
(-)a/t/db_dependent/Koha/Reports.t (-10 / +36 lines)
Lines 50-63 is( Koha::Reports->search->count, $nb_of_reports + 1, 'Delete should have delete Link Here
50
50
51
$schema->storage->txn_rollback;
51
$schema->storage->txn_rollback;
52
52
53
subtest 'validate_sql' => sub {
53
subtest 'is_sql_valid' => sub {
54
    plan tests => 3 + 6*2;
54
    plan tests => 3 + 6 * 2;
55
    my @badwords = ('UPDATE', 'DELETE', 'DROP', 'INSERT', 'SHOW', 'CREATE');
55
    my @badwords = ( 'UPDATE', 'DELETE', 'DROP', 'INSERT', 'SHOW', 'CREATE' );
56
    is_deeply( Koha::Reports->validate_sql(), [{ queryerr => 'Missing SELECT'}], 'Empty sql is missing SELECT' );
56
    is_deeply(
57
    is_deeply( Koha::Reports->validate_sql('FOO'), [{ queryerr => 'Missing SELECT'}], 'Nonsense sql is missing SELECT' );
57
        [ Koha::Report->new( { savedsql => '' } )->is_sql_valid ],
58
    is_deeply( Koha::Reports->validate_sql('select FOO'), [], 'select FOO is good' );
58
        [ 0, [ { queryerr => 'Missing SELECT' } ] ],
59
        'Empty sql is missing SELECT'
60
    );
61
    is_deeply(
62
        [ Koha::Report->new( { savedsql => 'FOO' } )->is_sql_valid ],
63
        [ 0, [ { queryerr => 'Missing SELECT' } ] ],
64
        'Nonsense sql is missing SELECT'
65
    );
66
    is_deeply(
67
        [ Koha::Report->new( { savedsql => 'select FOO' } )->is_sql_valid ],
68
        [ 1, [] ],
69
        'select FOO is good'
70
    );
59
    foreach my $word (@badwords) {
71
    foreach my $word (@badwords) {
60
        is_deeply( Koha::Reports->validate_sql('select FOO;'.$word.' BAR'), [{ sqlerr => $word}], 'select FOO with '.$word.' BAR' );
72
        is_deeply(
61
        is_deeply( Koha::Reports->validate_sql($word.' qux'), [{ sqlerr => $word}], $word.' qux' );
73
            [
74
                Koha::Report->new(
75
                    { savedsql => 'select FOO;' . $word . ' BAR' }
76
                )->is_sql_valid
77
            ],
78
            [ 0, [ { sqlerr => $word } ] ],
79
            'select FOO with ' . $word . ' BAR'
80
        );
81
        is_deeply(
82
            [
83
                Koha::Report->new( { savedsql => $word . ' qux' } )
84
                  ->is_sql_valid
85
            ],
86
            [ 0, [ { sqlerr => $word } ] ],
87
            $word . ' qux'
88
        );
62
    }
89
    }
63
}
90
  }
64
- 

Return to bug 24695