Bug 42360: Move separator validation to C4::Context::csv_delimiter

So we protect other calls

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
This commit is contained in:
Jonathan Druart 2026-05-26 15:43:06 +02:00 committed by Lucas Gass
parent cd8608ab4e
commit a9f2bec7a4
No known key found for this signature in database
GPG Key ID: 60B447398B0D36CA
4 changed files with 27 additions and 11 deletions

View File

@ -52,6 +52,10 @@ use Koha::Config::SysPrefs;
use Koha::Config;
use Koha::Database;
# 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
@ -485,6 +489,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

@ -51,8 +51,8 @@ This will return a list of all the available delimiters.
sub GetDelimiterChoices {
my $default = C4::Context->preference('CSVDelimiter');
# NOTE: The below list should match the corresponding options in the .pref file
my @dels = ( ';', 'tabulation', ',', '/', '\\', '#' );
# Use the centralized list of allowed delimiters from C4::Context
my @dels = C4::Context::ALLOWED_CSV_DELIMITERS;
return {
values => \@dels,

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

@ -89,7 +89,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" );
@ -111,6 +111,18 @@ subtest 'csv_delimiter() tests' => sub {
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 {