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

(-)a/C4/Reports/Guided.pm (-5 / +3 lines)
Lines 551-561 sub execute_query { Link Here
551
    $offset = 0    unless $offset;
551
    $offset = 0    unless $offset;
552
    $limit  = 999999 unless $limit;
552
    $limit  = 999999 unless $limit;
553
    $debug and print STDERR "execute_query($sql, $offset, $limit)\n";
553
    $debug and print STDERR "execute_query($sql, $offset, $limit)\n";
554
    if ($sql =~ /;?\W?(UPDATE|DELETE|DROP|INSERT|SHOW|CREATE)\W/i) {
554
555
        return (undef, {  sqlerr => $1} );
555
    my $errors = Koha::Reports->validate_sql($sql);
556
    } elsif ($sql !~ /^\s*SELECT\b\s*/i) {
556
    return (undef, @{$errors}[0]) if (scalar(@$errors));
557
        return (undef, { queryerr => 'Missing SELECT'} );
558
    }
559
557
560
    foreach my $sql_param ( @$sql_params ){
558
    foreach my $sql_param ( @$sql_params ){
561
        if ( $sql_param =~ m/\n/ ){
559
        if ( $sql_param =~ m/\n/ ){
(-)a/Koha/Reports.pm (+22 lines)
Lines 35-40 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
38
=head3 _type
60
=head3 _type
39
61
40
Returns name of corresponding DBIC resultset
62
Returns name of corresponding DBIC resultset
(-)a/reports/guided_reports.pl (-12 / +2 lines)
Lines 239-250 elsif ( $phase eq 'Update SQL'){ Link Here
239
239
240
    create_non_existing_group_and_subgroup($input, $group, $subgroup);
240
    create_non_existing_group_and_subgroup($input, $group, $subgroup);
241
241
242
    if ($sql =~ /;?\W?(UPDATE|DELETE|DROP|INSERT|SHOW|CREATE)\W/i) {
242
    push(@errors, @{Koha::Reports->validate_sql($sql)});
243
        push @errors, {sqlerr => $1};
244
    }
245
    elsif ($sql !~ /^(SELECT)/i) {
246
        push @errors, {queryerr => "No SELECT"};
247
    }
248
243
249
    if (@errors) {
244
    if (@errors) {
250
        $template->param(
245
        $template->param(
Lines 600-611 elsif ( $phase eq 'Save Report' ) { Link Here
600
595
601
    create_non_existing_group_and_subgroup($input, $group, $subgroup);
596
    create_non_existing_group_and_subgroup($input, $group, $subgroup);
602
    ## FIXME this is AFTER entering a name to save the report under
597
    ## FIXME this is AFTER entering a name to save the report under
603
    if ($sql =~ /;?\W?(UPDATE|DELETE|DROP|INSERT|SHOW|CREATE)\W/i) {
598
    push(@errors, @{Koha::Reports->validate_sql($sql)});
604
        push @errors, {sqlerr => $1};
605
    }
606
    elsif ($sql !~ /^(SELECT)/i) {
607
        push @errors, {queryerr => "No SELECT"};
608
    }
609
599
610
    if (@errors) {
600
    if (@errors) {
611
        $template->param(
601
        $template->param(
(-)a/t/db_dependent/Koha/Reports.t (-1 / +12 lines)
Lines 70-72 subtest 'prep_report' => sub { Link Here
70
};
70
};
71
71
72
$schema->storage->txn_rollback;
72
$schema->storage->txn_rollback;
73
- 
73
74
subtest 'validate_sql' => sub {
75
    plan tests => 3 + 6*2;
76
    my @badwords = ('UPDATE', 'DELETE', 'DROP', 'INSERT', 'SHOW', 'CREATE');
77
    is_deeply( Koha::Reports->validate_sql(), [{ queryerr => 'Missing SELECT'}], 'Empty sql is missing SELECT' );
78
    is_deeply( Koha::Reports->validate_sql('FOO'), [{ queryerr => 'Missing SELECT'}], 'Nonsense sql is missing SELECT' );
79
    is_deeply( Koha::Reports->validate_sql('select FOO'), [], 'select FOO is good' );
80
    foreach my $word (@badwords) {
81
        is_deeply( Koha::Reports->validate_sql('select FOO;'.$word.' BAR'), [{ sqlerr => $word}], 'select FOO with '.$word.' BAR' );
82
        is_deeply( Koha::Reports->validate_sql($word.' qux'), [{ sqlerr => $word}], $word.' qux' );
83
    }
84
}

Return to bug 24695