Skip to content

Commit 6ee9b93

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.
1 parent bd690df commit 6ee9b93

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

lib/Mojo/Message/Request.pm

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

199199
# Remove SCRIPT_NAME prefix if necessary
200+
# Make sure $path is decoded and unescaped as expected by to_string
201+
$path->parts;
200202
my $buffer = $path->to_string;
201203
$value =~ s!^/|/$!!g;
202204
$buffer =~ s!^/?\Q$value\E/?!!;

t/mojo/request_cgi.t

Lines changed: 24 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 'encode', 'url_escape';
56

67
subtest 'Parse Lighttpd CGI environment variables and body' => sub {
78
my $req = Mojo::Message::Request->new;
@@ -124,6 +125,29 @@ 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 UTF8 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(q(utf-8), "\x{1d120}");
133+
my $path = $path_stub.url_escape($part_utf8);
134+
# test $env is a stripped down version of the result of:
135+
# use HTTP::Request::Common 'GET';
136+
# use HTTP::Message::PSGI 'req_to_psgi';
137+
# $env = req_to_psgi( GET( 'http://www.example.com'.$script_name.$path ), SCRIPT_NAME => $script_name );
138+
$req->parse({
139+
CONTENT_LENGTH => 0,
140+
PATH_INFO => '/'.$path_stub.$part_utf8,
141+
REQUEST_URI => $script_name.$path,
142+
QUERY_STRING => '',
143+
REQUEST_METHOD => 'GET',
144+
SCRIPT_NAME => $script_name,
145+
HTTP_HOST => 'localhost:8080',
146+
SERVER_PROTOCOL => 'HTTP/1.1'
147+
});
148+
is $req->url->path->to_string, $path, 'round tripping yields same path';
149+
};
150+
127151
subtest 'Parse Apache CGI environment variables and body (file storage)' => sub {
128152
local $ENV{MOJO_MAX_MEMORY_SIZE} = 10;
129153
my $req = Mojo::Message::Request->new;

0 commit comments

Comments
 (0)