Skip to content

Commit 26d373d

Browse files
author
Alexander Kuehne
committed
psgi env: fix double encoding of path parts
Fix double encoding of path parts when parsing a PSGI env with a SCRIPT_NAME set. Make sure path is decoded and unescaped as expected by to_string. The test $env used in the regression test included is a stripped down version of the result of: use HTTP::Request::Common 'GET'; use HTTP::Message::PSGI 'req_to_psgi'; my $script_name = ...; my $path = ...; $env = req_to_psgi( GET( 'http://localhost:8080'.$script_name.$path ), SCRIPT_NAME => $script_name );
1 parent bd690df commit 26d373d

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

lib/Mojo/Message/Request.pm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ sub _parse_env {
197197
$base->path->parse($value =~ m!/$! ? $value : "$value/");
198198

199199
# Remove SCRIPT_NAME prefix if necessary
200+
$path->parts;
200201
my $buffer = $path->to_string;
201202
$value =~ s!^/|/$!!g;
202203
$buffer =~ s!^/?\Q$value\E/?!!;

t/mojo/request_cgi.t

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use Mojo::Base -strict;
22

33
use Test::More;
44
use Mojo::Message::Request;
5+
use Mojo::Util qw(encode url_escape);
56

67
subtest 'Parse Lighttpd CGI environment variables and body' => sub {
78
my $req = Mojo::Message::Request->new;
@@ -124,6 +125,25 @@ subtest 'Parse CGI environment with maximum message size' => sub {
124125
'right absolute URL';
125126
};
126127

128+
subtest 'Parse CGI environment with SCRIPT_NAME set and path part with UTF-8 character' => sub {
129+
my $req = Mojo::Message::Request->new;
130+
my $script_name = '/app/';
131+
my $path_stub = 'some/action/blub/';
132+
my $part_utf8 = encode('utf-8', "\x{1d120}");
133+
my $path = $path_stub.url_escape($part_utf8);
134+
$req->parse({
135+
CONTENT_LENGTH => 0,
136+
PATH_INFO => '/'.$path_stub.$part_utf8,
137+
REQUEST_URI => $script_name.$path,
138+
QUERY_STRING => '',
139+
REQUEST_METHOD => 'GET',
140+
SCRIPT_NAME => $script_name,
141+
HTTP_HOST => 'localhost:8080',
142+
SERVER_PROTOCOL => 'HTTP/1.1'
143+
});
144+
is $req->url->path->to_string, $path, 'round tripping yields same path';
145+
};
146+
127147
subtest 'Parse Apache CGI environment variables and body (file storage)' => sub {
128148
local $ENV{MOJO_MAX_MEMORY_SIZE} = 10;
129149
my $req = Mojo::Message::Request->new;

0 commit comments

Comments
 (0)