-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Hello!


How do i run and pass arguments to an anonymous subroutine that i shared
on a safe compartment?

My goal is to have a function that perform "dangerous" operations, that
must be available to untrusted code, however this function needs some
parameters that are only available when the code is executing, and the
untrusted code doesn't know them (hence the anonymous subroutine).

Example (35 lines):

### Example.pl file
use strict;
use warnings;
use Safe;
use 5.020;

my $UNTRUSTED_SCRIPT="untrusted_script.pl";

sub run_me{

  my $sandbox = Safe->new("Sandbox");
  $sandbox->permit(qw(:base_core :base_loop :base_mem :base_io :load 
:base_orig));
  $sandbox->deny(qw(die exit));

  my $operation = "Dangerous";
  
  my $anon_func = sub{
    my $param = shift;
    print "$operation $param operation!\n";
  };

  $sandbox->share($anon_func);
  $sandbox->rdo($UNTRUSTED_SCRIPT);
  $sandbox->reval('func1()');
}

run_me();

### untrusted_script.pl
use 5.020;
use warnings;
use utf8;


sub func1{
  say "About to run shared function!";
  $anon_func->("func1");
}

######

The output of the Example.pl script is:

Error while executing the sandbox: Global symbol "$anon_func" requires
explicit package name at untrusted_script.pl line 5. 

I was expecting of the output of the Example.pl script to be
"Dangerous func1 operation!". What am i doing wrong?


Best regards,
David Santiago
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQIcBAEBCAAGBQJUoE1jAAoJEP84J6veUQ8lC1kQAIohVyMk3XSru1SP+fP1z+EE
YhhvERKKDCM4ccZ53qVlcc0t8XurOIAStjh2h0MTdn9ntwF9I9mAYFeINIPoac4h
yyb0eqD8Qxr18HTmQ6Q+eikLR6opO7Q7f6gzBqXnhf0rrk2FhU8BEWnZRkmOUIsN
b1ZRRKDiVFdD82usAsrNqatmO2/SCBufMF7pA7jClRlP8us8tHVm0eZTlTdMNE+A
mcxNSmpP2ExiEcSZKrDGlagiZqKsbD2d93pBuau0k6cvQHUugrQ/JVTVNBqm9mey
yDXzcZNuzhD60N9i5ubquzUFFfD6gNQWN/fzLMOCxj0DlxuBHfQlygEzhMbJl/HQ
dsEl7POmZLdkmJ6Xp7k8GjEAoBYPAycNHje11NcITZmiKk1yBg/il+yBE44e+o35
klrAGJvQAPw+ljt9OAkdh1riENd7e0dqUqFbb1M4NZxvunchagOdkl5Ckb86eL18
vQ/C1zuZ6KRsNSLL8bZ8rnQk2LG4ye54rohV/EMC5aVmjYsWNe85CIA1SG51A8xV
JlvyrA4hXfrSKZmhlKdHDau+PbFuSIhN/batVUGVAqbuEH13dGvpWbg3+39VPRTa
s0f0cDFFrAGhqyMqqEDjM3TUNlD9ZBS4IfjA8ykQhjy9K6NRsdH7xyIQfElCmCyl
4cdhwvFCfYMgmvKGcWJl
=PbOC
-----END PGP SIGNATURE-----

Reply via email to