From 8cf34b63ba39051b43431019b8034314179920a7 Mon Sep 17 00:00:00 2001 From: Hammat Wele Date: Fri, 21 Jun 2024 17:07:47 +0000 Subject: [PATCH] Bug 37157: Fix Malformed UTF-8 character in JSON string before decode_json MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When we add a new identity provider and put some special characters in the Config or Mapping field, we got 500 error when we list the identity providers To test: 1. Apply this patch. 2. Add a new identity provider 2.1. fill the form 2.2. click on «Add default Oauth configuration» and on «Add default Oauth mapping» 2.3. put some special characters in Configuration and Mapping field 3. Save the form => Confirm the identity providers list is shown correctly Also prove t/db_dependent/api/v1/provider.t. Signed-off-by: David Nind --- Koha/Auth/Identity/Provider.pm | 4 +- t/db_dependent/api/v1/provider.t | 66 ++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) create mode 100755 t/db_dependent/api/v1/provider.t diff --git a/Koha/Auth/Identity/Provider.pm b/Koha/Auth/Identity/Provider.pm index e9dfc2fddb..d617b2449a 100644 --- a/Koha/Auth/Identity/Provider.pm +++ b/Koha/Auth/Identity/Provider.pm @@ -62,7 +62,7 @@ sub get_config { my ($self) = @_; return try { - return decode_json( $self->config ); + return decode_json( Encode::encode_utf8( $self->config ) ); } catch { Koha::Exceptions::Object::BadValue->throw("Error reading JSON data: $_"); @@ -127,7 +127,7 @@ sub get_mapping { my ($self) = @_; return try { - return decode_json( $self->mapping ); + return decode_json( Encode::encode_utf8( $self->mapping ) ); } catch { Koha::Exceptions::Object::BadValue->throw("Error reading JSON data: $_"); diff --git a/t/db_dependent/api/v1/provider.t b/t/db_dependent/api/v1/provider.t new file mode 100755 index 0000000000..d505fc99d9 --- /dev/null +++ b/t/db_dependent/api/v1/provider.t @@ -0,0 +1,66 @@ +#!/usr/bin/env perl + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Test::More tests => 1; +use Test::Mojo; +use t::lib::TestBuilder; +use t::lib::Mocks; + +use JSON qw(encode_json); + +use C4::Budgets; + +use Koha::Database; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new(); + +t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); + +my $t = Test::Mojo->new('Koha::REST::V1'); + +subtest 'list() tests' => sub { + + plan tests => 4; + + $schema->storage->txn_begin; + + my $superlibrarian = + $builder->build_object( { class => 'Koha::Patrons', value => { flags => 1 } } ); + my $password = 'thePassword123'; + $superlibrarian->set_password( { password => $password, skip_validation => 1 } ); + $superlibrarian->discard_changes; + + my $userid = $superlibrarian->userid; + + $t->get_ok("//$userid:$password@/api/v1/auth/identity_providers")->status_is(200); + + my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers' } ); + + $provider->set( + { + config => '{"some":"value","and":"élève"}', + mapping => '{"some":"value","and":"tréma"}' + } + )->store; + + $t->get_ok("//$userid:$password@/api/v1/auth/identity_providers")->status_is(200); + + $schema->storage->txn_rollback; +}; -- 2.39.2