Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plack::Util::load_psgi: guard against hitting the identifier length limit #663

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions lib/Plack/Util.pm
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,40 @@ sub class_to_file {
$class . ".pm";
}

{
my $counter = 0;
my $file_to_generated_package = {};
sub _generate_sandbox_package {
my $abs_path = shift;
return $file_to_generated_package->{$abs_path}
if $file_to_generated_package->{$abs_path};

my $_package = $abs_path;
$_package =~ s/([^A-Za-z0-9_])/sprintf("_%2x", unpack("C", $1))/eg;

# Make sure our generated package won't pass Perl's identifier
# limit:
substr($_package, 0, 30, '') while length($_package) > 200;

# And to make up for the possibly less-unique path, add a unique
# prefix per file, so that two files with different early
# paths but similar later paths
# (think /foo/bar/baz/app.psgi vs /something/bar/baz/app.psgi)
# do not share namespaces:
$counter++;
my $prefix = "Guard$counter";
my $generated_package = $prefix . '::' . $_package;

# and in the rare case that this function gets called
# twice for the same file, make sure we return the same
# generated namespace for both invocations:
return $file_to_generated_package->{$abs_path} = $generated_package;
}
}
sub _load_sandbox {
my $_file = shift;

my $_package = $_file;
$_package =~ s/([^A-Za-z0-9_])/sprintf("_%2x", unpack("C", $1))/eg;
my $_package = _generate_sandbox_package($_file);

local $0 = $_file; # so FindBin etc. works
local @ARGV = (); # Some frameworks might try to parse @ARGV
Expand Down
8 changes: 8 additions & 0 deletions t/Plack-Util/load.t
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,12 @@ use Test::More;
chdir $cwd;
}

{
local $@;
# must be at least 250 characters long
my $very_long_path = join '/', map 1..300, 'very_long.psgi';
eval { Plack::Util::load_psgi($very_long_path) };
unlike($@, qr/Identifier too long/);
}

done_testing;