From d46a2f5c28862d95b6ca7068c559b62a53be50a9 Mon Sep 17 00:00:00 2001 From: Maxime Besson Date: Mon, 26 Jul 2021 16:44:58 +0200 Subject: [PATCH 1/2] Fix error reporting in Jail (#2568) --- .../lib/Lemonldap/NG/Handler/Main/Jail.pm | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Main/Jail.pm b/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Main/Jail.pm index 486ccccf66..272b48b1d2 100644 --- a/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Main/Jail.pm +++ b/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Main/Jail.pm @@ -129,13 +129,7 @@ sub token { # Fake reval method if useSafeJail is off sub reval { my ( $self, $e ) = @_; - - my $res = eval $e; - if ($@) { - $self->error($@); - return undef; - } - return $res; + return eval $e; } ## @method wrap_code_ref @@ -183,8 +177,7 @@ sub jail_reval { # if nothing is returned by reval, add the return statement to # the "no safe wrap" reval - my $res; - eval { $res = ( $self->jail->reval($reval) ) }; + my $res = $self->jail->reval($reval); if ($@) { $self->error($@); return undef; -- GitLab From c1b6ca940d335b647f62b181c96be278237931d7 Mon Sep 17 00:00:00 2001 From: Maxime Besson Date: Mon, 26 Jul 2021 16:45:17 +0200 Subject: [PATCH 2/2] Unit tests for jail error reporting (#2568) --- .../t/12-Lemonldap-NG-Handler-Jail.t | 12 ++++++++++-- .../t/13-Lemonldap-NG-Handler-Fake-Safe.t | 14 ++++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lemonldap-ng-handler/t/12-Lemonldap-NG-Handler-Jail.t b/lemonldap-ng-handler/t/12-Lemonldap-NG-Handler-Jail.t index 25c6329d6b..55ed31859f 100644 --- a/lemonldap-ng-handler/t/12-Lemonldap-NG-Handler-Jail.t +++ b/lemonldap-ng-handler/t/12-Lemonldap-NG-Handler-Jail.t @@ -6,7 +6,7 @@ # change 'tests => 1' to 'tests => last_test_to_print'; use strict; -use Test::More tests => 20; +use Test::More tests => 22; require 't/test.pm'; BEGIN { use_ok('Lemonldap::NG::Handler::Main::Jail') } @@ -60,7 +60,7 @@ ok( ok( $res = &$code, "Function works" ); ok( $res == 1, 'Get good result' ); -$sub = "sub { return(checkDate('20000101000000+0100','21000101000000+0100')) }"; +$sub = "sub { return(checkDate('20000101000000+0100','21000101000000+0100')) }"; $code = $jail->jail_reval($sub); ok( ( defined($code) and ref($code) eq 'CODE' ), @@ -105,3 +105,11 @@ is( "Function works" ); +$sub = "sub { return("; +$code = $jail->jail_reval($sub); +ok( ( not defined($code) ), 'Syntax error yields undef result' ); +like( + $jail->error, + qr/Missing right curly or square bracket/, + 'Found correct error message' +); diff --git a/lemonldap-ng-handler/t/13-Lemonldap-NG-Handler-Fake-Safe.t b/lemonldap-ng-handler/t/13-Lemonldap-NG-Handler-Fake-Safe.t index b6584b7655..c2911f736b 100644 --- a/lemonldap-ng-handler/t/13-Lemonldap-NG-Handler-Fake-Safe.t +++ b/lemonldap-ng-handler/t/13-Lemonldap-NG-Handler-Fake-Safe.t @@ -5,7 +5,7 @@ # change 'tests => 1' to 'tests => last_test_to_print'; -use Test::More tests => 14; +use Test::More tests => 16; require 't/test.pm'; BEGIN { use_ok('Lemonldap::NG::Handler::Main::Jail') } @@ -43,7 +43,8 @@ my $checkDate = $jail->jail_reval($sub3); ok( &$checkDate == "1", 'checkDate extended function working without Safe Jail' ); -my $sub4 = "sub { return(checkDate('20000101000000+0100','21000101000000+0100')) }"; +my $sub4 = + "sub { return(checkDate('20000101000000+0100','21000101000000+0100')) }"; my $checkDate = $jail->jail_reval($sub4); ok( &$checkDate == "1", 'checkDate extended function working without Safe Jail' ); @@ -96,3 +97,12 @@ is( 0, "Function works" ); + +$sub = "sub { return("; +$code = $jail->jail_reval($sub); +ok( ( not defined($code) ), 'Syntax error yields undef result' ); +like( + $jail->error, + qr/Missing right curly or square bracket/, + 'Found correct error message' +); -- GitLab