25
25
26
26
class HttpUtilsTest extends TestCase
27
27
{
28
- public function testCreateRedirectResponseWithPath ()
28
+ /**
29
+ * @dataProvider validRequestDomainUrls
30
+ */
31
+ public function testCreateRedirectResponseWithPath (?string $ domainRegexp , string $ path , string $ expectedRedirectUri )
29
32
{
30
- $ utils = new HttpUtils ($ this ->getUrlGenerator ());
31
- $ response = $ utils ->createRedirectResponse ($ this ->getRequest (), ' /foobar ' );
33
+ $ utils = new HttpUtils ($ this ->getUrlGenerator (), null , $ domainRegexp );
34
+ $ response = $ utils ->createRedirectResponse ($ this ->getRequest (), $ path );
32
35
33
- $ this ->assertTrue ($ response ->isRedirect (' http://localhost/foobar ' ));
36
+ $ this ->assertTrue ($ response ->isRedirect ($ expectedRedirectUri ));
34
37
$ this ->assertEquals (302 , $ response ->getStatusCode ());
35
38
}
36
39
37
- public function testCreateRedirectResponseWithAbsoluteUrl ()
40
+ public static function validRequestDomainUrls ()
38
41
{
39
- $ utils = new HttpUtils ($ this ->getUrlGenerator ());
40
- $ response = $ utils ->createRedirectResponse ($ this ->getRequest (), 'http://symfony.com/ ' );
41
-
42
- $ this ->assertTrue ($ response ->isRedirect ('http://symfony.com/ ' ));
43
- }
44
-
45
- public function testCreateRedirectResponseWithDomainRegexp ()
46
- {
47
- $ utils = new HttpUtils ($ this ->getUrlGenerator (), null , '#^https?://symfony\.com$#i ' );
48
- $ response = $ utils ->createRedirectResponse ($ this ->getRequest (), 'http://symfony.com/blog ' );
49
-
50
- $ this ->assertTrue ($ response ->isRedirect ('http://symfony.com/blog ' ));
51
- }
52
-
53
- public function testCreateRedirectResponseWithRequestsDomain ()
54
- {
55
- $ utils = new HttpUtils ($ this ->getUrlGenerator (), null , '#^https?://%s$#i ' );
56
- $ response = $ utils ->createRedirectResponse ($ this ->getRequest (), 'http://localhost/blog ' );
57
-
58
- $ this ->assertTrue ($ response ->isRedirect ('http://localhost/blog ' ));
42
+ return [
43
+ '/foobar ' => [
44
+ null ,
45
+ '/foobar ' ,
46
+ 'http://localhost/foobar ' ,
47
+ ],
48
+ 'http://symfony.com/ without domain regex ' => [
49
+ null ,
50
+ 'http://symfony.com/ ' ,
51
+ 'http://symfony.com/ ' ,
52
+ ],
53
+ 'http://localhost/blog with #^https?://symfony\.com$#i ' => [
54
+ '#^https?://symfony\.com$#i ' ,
55
+ 'http://symfony.com/blog ' ,
56
+ 'http://symfony.com/blog ' ,
57
+ ],
58
+ 'http://localhost/blog with #^https?://%s$#i ' => [
59
+ '#^https?://%s$#i ' ,
60
+ 'http://localhost/blog ' ,
61
+ 'http://localhost/blog ' ,
62
+ ],
63
+ 'custom scheme ' => [
64
+ null ,
65
+ 'android-app://com.google.android.gm/ ' ,
66
+ 'android-app://com.google.android.gm/ ' ,
67
+ ],
68
+ 'custom scheme with all URL components ' => [
69
+ null ,
70
+ 'android-app://foo:[email protected] :8080/software/index.html?lite=true#section1 ' ,
71
+ 'android-app://foo:[email protected] :8080/software/index.html?lite=true#section1 ' ,
72
+ ],
73
+ ];
59
74
}
60
75
61
76
/**
@@ -72,22 +87,17 @@ public function testCreateRedirectResponseWithBadRequestsDomain($url)
72
87
public static function badRequestDomainUrls ()
73
88
{
74
89
return [
90
+ ['http:///foo ' ],
75
91
['http://pirate.net/foo ' ],
92
+ ['//evil.com/do-bad-things ' ],
93
+ ['http://localhost/foo/../bar ' ],
76
94
['http: \\\\pirate.net/foo ' ],
77
95
['http:/ \\pirate.net/foo ' ],
78
96
['http: \\/pirate.net/foo ' ],
79
97
['http://////pirate.net/foo ' ],
80
98
];
81
99
}
82
100
83
- public function testCreateRedirectResponseWithProtocolRelativeTarget ()
84
- {
85
- $ utils = new HttpUtils ($ this ->getUrlGenerator (), null , '#^https?://%s$#i ' );
86
- $ response = $ utils ->createRedirectResponse ($ this ->getRequest (), '//evil.com/do-bad-things ' );
87
-
88
- $ this ->assertTrue ($ response ->isRedirect ('http://localhost//evil.com/do-bad-things ' ), 'Protocol-relative redirection should not be supported for security reasons ' );
89
- }
90
-
91
101
public function testCreateRedirectResponseWithRouteName ()
92
102
{
93
103
$ utils = new HttpUtils ($ urlGenerator = $ this ->createMock (UrlGeneratorInterface::class));
0 commit comments