Bug 42360: [24.11] Move separator validation to C4::Context::csv_delimiter

So we protect other calls

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
This commit is contained in:
Jonathan Druart 2026-05-26 15:43:06 +02:00 committed by Fridolin Somers
parent a4a23224d0
commit bfe9cfa6f4
No known key found for this signature in database
GPG Key ID: 9A8E73E2A8B96537
4 changed files with 33 additions and 10 deletions

View File

@ -50,6 +50,10 @@ use Koha::Config::SysPrefs;
use Koha::Config;
use Koha;
# NOTE: The below list should match the corresponding options in the .pref file
# for CSVDelimiter
use constant ALLOWED_CSV_DELIMITERS => ( ';', 'tabulation', ',', '/', '\\', '#' );
=head1 NAME
C4::Context - Maintain and manipulate the context of a Koha script
@ -461,6 +465,13 @@ sub delete_preference {
sub csv_delimiter {
my ( $self, $value ) = @_;
# Validate input against allowed delimiters
if ($value) {
my %allowed = map { $_ => 1 } ALLOWED_CSV_DELIMITERS;
$value = undef unless $allowed{$value};
}
my $delimiter = $value || $self->preference('CSVDelimiter') || ',';
$delimiter = "\t" if $delimiter eq 'tabulation';
return $delimiter;

View File

@ -63,6 +63,10 @@ sub GetDelimiterChoices {
my ($choices, $default) = $sth->fetchrow;
my @dels = split /\|/, $choices;
#See BZ 41834 to see why this kludge is used here in older versions
# Use the centralized list of allowed delimiters from C4::Context
@dels = C4::Context::ALLOWED_CSV_DELIMITERS;
return {
values => \@dels,
default => $default,

View File

@ -119,14 +119,7 @@ if ($basename_input) {
#NOTE: Ideally it would be nice to validate the Filter parameter,
#but that would take a fair bit of work...
#NOTE: Validate sep parameter
our $sep = C4::Context->csv_delimiter();
my $sep_input = scalar $input->param("sep");
my $delimiter_choices = GetDelimiterChoices();
my %allowed_seps = map { $_ => 1 } @{ $delimiter_choices->{values} };
if ( $sep_input && $allowed_seps{$sep_input} ) {
$sep = $sep_input;
}
our $sep = C4::Context->csv_delimiter( scalar $input->param("sep") );
$template->param(
do_it => $do_it,

View File

@ -88,7 +88,7 @@ subtest 'needs_install() tests' => sub {
subtest 'csv_delimiter() tests' => sub {
plan tests => 4;
plan tests => 7;
t::lib::Mocks::mock_preference( 'CSVDelimiter', undef );
is( C4::Context->csv_delimiter, ',', "csv_delimiter returns comma if system preference CSVDelimiter is undefined" );
@ -100,7 +100,22 @@ subtest 'csv_delimiter() tests' => sub {
is( C4::Context->csv_delimiter, ';', "csv_delimiter returns semicolon if system preference CSVDelimiter is semicolon" );
t::lib::Mocks::mock_preference( 'CSVDelimiter', 'tabulation' );
is( C4::Context->csv_delimiter, "\t", "csv_delimiter returns '\t' if system preference CSVDelimiter is tabulation" );
is(
C4::Context->csv_delimiter, "\t",
"csv_delimiter returns '\t' if system preference CSVDelimiter is tabulation"
);
t::lib::Mocks::mock_preference( 'CSVDelimiter', ';' );
is( C4::Context->csv_delimiter(','), ',', "csv_delimiter returns comma if passed" );
is(
C4::Context->csv_delimiter(";"), ';',
"csv_delimiter returns semicolon if passed"
);
is(
C4::Context->csv_delimiter("£"), ';',
"csv_delimiter returns semicolon if not allowed"
);
};
subtest 'default_catalog_sort_by() tests' => sub {