Skip to content

Commit fbc5853

Browse files
veryrustyxsawyerx
authored andcommitted
Remove behind_proxy handling from D2::Core::Request
1 parent 5b495cd commit fbc5853

3 files changed

Lines changed: 84 additions & 51 deletions

File tree

lib/Dancer2/Core/Request.pm

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -130,40 +130,17 @@ sub _set_route_params {
130130
# XXX: incompatible with Plack::Request
131131
sub uploads { $_[0]->{'uploads'} }
132132

133-
sub is_behind_proxy { $_[0]->{'is_behind_proxy'} || 0 }
134-
135133
sub host {
136-
my ($self) = @_;
137-
138-
if ( $self->is_behind_proxy and exists $self->env->{'HTTP_X_FORWARDED_HOST'} ) {
139-
my @hosts = split /\s*,\s*/, $self->env->{'HTTP_X_FORWARDED_HOST'}, 2;
140-
return $hosts[0];
141-
} else {
142-
return $self->env->{'HTTP_HOST'};
143-
}
134+
return shift->env->{'HTTP_HOST'};
144135
}
145136

146137
# aliases, kept for backward compat
147138
sub agent { shift->user_agent }
148139
sub remote_address { shift->address }
140+
149141
sub forwarded_for_address { shift->env->{'HTTP_X_FORWARDED_FOR'} }
150142
sub forwarded_host { shift->env->{'HTTP_X_FORWARDED_HOST'} }
151-
152-
# there are two options
153-
sub forwarded_protocol {
154-
$_[0]->env->{'HTTP_X_FORWARDED_PROTO'} ||
155-
$_[0]->env->{'HTTP_X_FORWARDED_PROTOCOL'} ||
156-
$_[0]->env->{'HTTP_FORWARDED_PROTO'}
157-
}
158-
159-
sub scheme {
160-
my ($self) = @_;
161-
my $scheme = $self->is_behind_proxy
162-
? $self->forwarded_protocol
163-
: '';
164-
165-
return $scheme || $self->env->{'psgi.url_scheme'};
166-
}
143+
sub forwarded_protocol { shift->env->{'HTTP_X_FORWARDED_PROTO'} }
167144

168145
sub serializer { $_[0]->{'serializer'} }
169146

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
use strict;
2+
use warnings;
3+
use Test::More 'tests' => 6;
4+
use File::Spec;
5+
use Path::Tiny ();
6+
use Dancer2::FileUtils qw< path >;
7+
8+
# This module tests merge_paths can be properly replaced by Path::Tiny::path()
9+
# It addresses the following comment by @wchristian, written bellow.
10+
11+
# We removed this subroutine but still need to verify we converted correctly
12+
sub merge_paths {
13+
my ( undef, $path, $public_dir ) = @_;
14+
15+
my ( $volume, $dirs, $file ) = File::Spec->splitpath( $path );
16+
my @tokens = File::Spec->splitdir( "$dirs$file" );
17+
my $updir = File::Spec->updir;
18+
return if grep $_ eq $updir, @tokens;
19+
20+
my ( $pub_vol, $pub_dirs, $pub_file ) = File::Spec->splitpath( $public_dir );
21+
my @pub_tokens = File::Spec->splitdir( "$pub_dirs$pub_file" );
22+
return if length $volume and length $pub_vol and $volume ne $pub_vol;
23+
24+
my @final_vol = ( length $pub_vol ? $pub_vol : length $volume ? $volume : () );
25+
my @file_path = ( @final_vol, @pub_tokens, @tokens );
26+
my $file_path = path( @file_path );
27+
return $file_path;
28+
}
29+
30+
# From GH #1264:
31+
#
32+
# That merge_paths commit is no good in that form, i'm afraid.
33+
#
34+
# All of these should throw errors instead of "succeeding", otherwise they present huge security problems and/or break tests on systems with more than one file system tree. @haarg probably has other ideas of how this could break as well.
35+
#
36+
# C:\Users\Mithaldu>perl -e "use Path::Tiny; print Path::Tiny::path( 'c:/meep', 'c:/marp' )->stringify"
37+
# C:/meep/c:/marp
38+
# C:\Users\Mithaldu>perl -e "use Path::Tiny; print Path::Tiny::path( 'c:/meep', 'd:/marp' )->stringify"
39+
# C:/meep/d:/marp
40+
# C:\Users\Mithaldu>perl -e "use Path::Tiny; print Path::Tiny::path( 'c:/meep', '../marp' )->stringify"
41+
# C:/marp
42+
#
43+
# The first two are both straight-up non-sense in the vein of merge_path( "~/.ssh", "ftp://some.server/whatever" ) so they should error out, nothing else.
44+
# The third is "merely" dangerous and should be prevented.
45+
46+
47+
is(
48+
merge_paths( undef, 'c:/meep', 'c:/marp' ),
49+
'c:/marp/c:/meep',
50+
'merge_paths: c:/meep + c:/marp',
51+
);
52+
53+
is(
54+
Path::Tiny::path( 'c:/meep', 'c:/marp' ),
55+
'c:/meep/c:/marp',
56+
'Path::Tiny: c:/meep + c:/marp',
57+
);
58+
59+
is(
60+
merge_paths( undef, 'c:/meep', 'd:/marp' ),
61+
'd:/marp/c:/meep',
62+
'merge_paths: c:/meep + d:/marp',
63+
);
64+
65+
is(
66+
Path::Tiny::path( 'c:/meep', 'd:/marp' ),
67+
'c:/meep/d:/marp',
68+
'Path::Tiny: c:/meep + d:/marp',
69+
);
70+
71+
is(
72+
merge_paths( undef, 'c:/meep', '../marp' ),
73+
'../marp/c:/meep',
74+
'merge_paths: c:/meep + ../marp',
75+
);
76+
77+
is(
78+
Path::Tiny::path( 'c:/meep', '../marp' ),
79+
'c:/meep/../marp',
80+
'Path::Tiny: c:/meep + ../marp',
81+
);

t/request.t

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -97,31 +97,6 @@ sub run_test {
9797
is $req->base, 'http://oddhostname:5000/foo';
9898
}
9999

100-
note "testing behind proxy"; {
101-
my $req = Dancer2::Core::Request->new(
102-
env => $env,
103-
is_behind_proxy => 1
104-
);
105-
is $req->secure, 1;
106-
is $req->host, $env->{HTTP_X_FORWARDED_HOST};
107-
is $req->scheme, 'https';
108-
}
109-
110-
note "testing behind proxy when optional headers are not set"; {
111-
# local modifications to env:
112-
local $env->{HTTP_HOST} = 'oddhostname:5000';
113-
delete local $env->{'HTTP_X_FORWARDED_FOR'};
114-
delete local $env->{'HTTP_X_FORWARDED_HOST'};
115-
delete local $env->{'HTTP_X_FORWARDED_PROTOCOL'};
116-
my $req = Dancer2::Core::Request->new(
117-
env => $env,
118-
is_behind_proxy => 1
119-
);
120-
is ! $req->secure, 1;
121-
is $req->host, 'oddhostname:5000';
122-
is $req->scheme, 'http';
123-
}
124-
125100
note "testing path and uri_base"; {
126101
# Base env used for path and uri_base tests
127102
my $base = {

0 commit comments

Comments
 (0)