Create a Common::Safe module, to be able to disable Safe jail with useSafeJail in Portal (#270)
parent
b7b2681967
commit
0865635a7b
@ -0,0 +1,82 @@ |
||||
## @file |
||||
# LL::NG module for Safe jail |
||||
|
||||
## @package |
||||
# LL::NG module for Safe jail |
||||
package Lemonldap::NG::Common::Safe; |
||||
|
||||
use strict; |
||||
use base qw(Safe); |
||||
use constant SAFEWRAP => ( Safe->can("wrap_code_ref") ? 1 : 0 ); |
||||
|
||||
our $VERSION = 1.0.2; |
||||
|
||||
our $self; # Safe cannot share a variable declared with my |
||||
|
||||
## @constructor Lemonldap::NG::Common::Safe new(Lemonldap::NG::Portal::Simple portal) |
||||
# Build a new Safe object |
||||
# @param portal Lemonldap::NG::Portal::Simple object |
||||
# @return Lemonldap::NG::Common::Safe object |
||||
sub new { |
||||
my ( $class, $portal ) = splice @_; |
||||
my $self = {}; |
||||
|
||||
unless ( $portal->{useSafeJail} ) { |
||||
|
||||
# Fake jail |
||||
$portal->lmLog( "Creating a fake Safe jail", 'debug' ); |
||||
bless $self, $class; |
||||
} |
||||
else { |
||||
|
||||
# Safe jail |
||||
$self = $class->SUPER::new(); |
||||
$portal->lmLog( "Creating a real Safe jail", 'debug' ); |
||||
} |
||||
|
||||
# Store portal object |
||||
$self->{p} = $portal; |
||||
|
||||
return $self; |
||||
} |
||||
|
||||
## @method reval(string $e) |
||||
# Evaluate an expression, inside or outside jail |
||||
# @param e Expression to evaluate |
||||
sub reval { |
||||
local $self = shift; |
||||
my ($e) = splice @_; |
||||
my $result; |
||||
|
||||
# Replace $date |
||||
$e =~ s/\$date/&POSIX::strftime("%Y%m%d%H%M%S",localtime())/e; |
||||
|
||||
# Replace variables by session content |
||||
$e =~ s/\$(?!ENV)(\w+)/\$self->{p}->{sessionInfo}->{$1}/g; |
||||
|
||||
$self->{p}->lmLog( "Evaluate expression: $e", 'debug' ); |
||||
|
||||
if ( $self->{p}->{useSafeJail} ) { |
||||
|
||||
# Share $self to access sessionInfo HASH |
||||
$self->SUPER::share('$self'); |
||||
|
||||
# Test SAFEWRAP and run reval |
||||
$result = ( |
||||
SAFEWRAP |
||||
? $self->SUPER::wrap_code_ref( $self->SUPER::reval($e) ) |
||||
: $self->SUPER::reval($e) |
||||
); |
||||
} |
||||
else { |
||||
|
||||
# Use a standard eval |
||||
$result = eval $e; |
||||
} |
||||
|
||||
$self->{p}->lmLog( "Evaluation result: $result", 'debug' ); |
||||
|
||||
return $result; |
||||
} |
||||
|
||||
1; |
@ -0,0 +1,49 @@ |
||||
# Before `make install' is performed this script should be runnable with |
||||
# `make test'. After `make install' it should work as `perl Lemonldap-NG-Portal.t' |
||||
|
||||
######################### |
||||
|
||||
# change 'tests => 1' to 'tests => last_test_to_print'; |
||||
|
||||
use Test::More tests => 8; |
||||
|
||||
BEGIN { use_ok( 'Lemonldap::NG::Portal::Simple', ':all' ) } |
||||
|
||||
######################### |
||||
|
||||
# Insert your test code below, the Test::More module is use()ed here so read |
||||
# its man page ( perldoc Test::More ) for help writing this test script. |
||||
|
||||
|
||||
|
||||
# Create portal object with Safe jail (the default) |
||||
my $p; |
||||
$ENV{REQUEST_METHOD} = "GET"; |
||||
ok( |
||||
$p = Lemonldap::NG::Portal::Simple->new( |
||||
{ |
||||
globalStorage => 'Apache::Session::File', |
||||
domain => 'example.com', |
||||
} |
||||
), |
||||
'Portal object' |
||||
); |
||||
|
||||
# Fake data |
||||
my $sessionData = "coudot"; |
||||
$p->{sessionInfo}->{uid} = $sessionData; |
||||
my $envData = "127.0.0.1"; |
||||
$ENV{REMOTE_ADDR} = $envData; |
||||
|
||||
# Real Safe jail |
||||
ok( $p->{useSafeJail} == 1, 'Safe jail on' ); |
||||
ok( $p->safe->reval('$uid') eq $sessionData, 'Safe jail on - session data' ); |
||||
ok( $p->safe->reval('$ENV{REMOTE_ADDR}') eq $envData, 'Safe jail on - env data' ); |
||||
|
||||
# Fake Safe jail |
||||
$p->{useSafeJail} = 0; |
||||
ok( $p->{useSafeJail} == 0, 'Safe jail off' ); |
||||
ok( $p->safe->reval('$uid') eq $sessionData, 'Safe jail off - session data' ); |
||||
ok( $p->safe->reval('$ENV{REMOTE_ADDR}') eq $envData, 'Safe jail off - env data' ); |
||||
|
||||
|
Loading…
Reference in new issue