1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace spec\Spatie\UrlSigner; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use DateTimeZone; |
7
|
|
|
use League\Url\UrlImmutable; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use Spatie\UrlSigner\Exceptions\InvalidExpiration; |
10
|
|
|
use Spatie\UrlSigner\Exceptions\InvalidSignatureKey; |
11
|
|
|
use Spatie\UrlSigner\MD5UrlSigner; |
12
|
|
|
use Spatie\UrlSigner\UrlSigner; |
13
|
|
|
|
14
|
|
|
class MD5UrlSignerTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
/** @test */ |
17
|
|
|
public function it_is_initialized() |
18
|
|
|
{ |
19
|
|
|
$urlSigner = new MD5UrlSigner('random_monkey'); |
20
|
|
|
|
21
|
|
|
$this->assertInstanceOf(MD5UrlSigner::class, $urlSigner); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** @test */ |
25
|
|
|
public function it_will_throw_an_exception_for_an_empty_signatureKey() |
26
|
|
|
{ |
27
|
|
|
$this->expectException(InvalidSignatureKey::class); |
28
|
|
|
|
29
|
|
|
$urlSigner = new MD5UrlSigner(''); |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** @test */ |
33
|
|
|
public function it_returns_false_when_validating_a_forged_url() |
34
|
|
|
{ |
35
|
|
|
$signedUrl = 'http://myapp.com/somewhereelse/?expires=4594900544&signature=41d5c3a92c6ef94e73cb70c7dcda0859'; |
36
|
|
|
$urlSigner = new MD5UrlSigner('random_monkey'); |
37
|
|
|
|
38
|
|
|
$this->assertFalse($urlSigner->validate($signedUrl)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** @test */ |
42
|
|
|
public function it_returns_false_when_validating_an_expired_url() |
43
|
|
|
{ |
44
|
|
|
$signedUrl = 'http://myapp.com/?expires=1123690544&signature=93e02326d7572632dd6edfa2665f2743'; |
45
|
|
|
$urlSigner = new MD5UrlSigner('random_monkey'); |
46
|
|
|
|
47
|
|
|
$this->assertFalse($urlSigner->validate($signedUrl)); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** @test */ |
51
|
|
|
public function it_returns_true_when_validating_an_non_expired_url() |
52
|
|
|
{ |
53
|
|
|
$url = 'http://myapp.com'; |
54
|
|
|
$expiration = 10000; |
55
|
|
|
$urlSigner = new MD5UrlSigner('random_monkey'); |
56
|
|
|
$signedUrl = $urlSigner->sign($url, $expiration); |
57
|
|
|
|
58
|
|
|
$this->assertTrue($urlSigner->validate($signedUrl)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function unsignedUrlProvider() |
62
|
|
|
{ |
63
|
|
|
return [ |
64
|
|
|
['http://myapp.com/?expires=4594900544'], |
65
|
|
|
['http://myapp.com/?signature=41d5c3a92c6ef94e73cb70c7dcda0859'], |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @test |
71
|
|
|
* @dataProvider unsignedUrlProvider |
72
|
|
|
*/ |
73
|
|
|
public function it_returns_false_when_validating_an_unsigned_url($unsignedUrl) |
74
|
|
|
{ |
75
|
|
|
$urlSigner = new MD5UrlSigner('random_monkey'); |
76
|
|
|
|
77
|
|
|
$this->assertFalse($urlSigner->validate($unsignedUrl)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** @test */ |
81
|
|
|
public function it_does_a_strict_check_on_expirations() |
82
|
|
|
{ |
83
|
|
|
$url = 'http://myapp.com'; |
84
|
|
|
$expiration = '30'; |
85
|
|
|
$urlSigner = new MD5UrlSigner('random_monkey'); |
86
|
|
|
|
87
|
|
|
$this->expectException(InvalidExpiration::class); |
88
|
|
|
|
89
|
|
|
$urlSigner->sign($url, $expiration); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function pastExpirationProvider() |
93
|
|
|
{ |
94
|
|
|
return [ |
95
|
|
|
[DateTime::createFromFormat('d/m/Y H:i:s', '10/08/2005 18:15:44')], |
96
|
|
|
[-10], |
97
|
|
|
]; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @test |
102
|
|
|
* @dataProvider pastExpirationProvider |
103
|
|
|
*/ |
104
|
|
|
public function it_doesnt_allow_expirations_in_the_past($pastExpiration) |
105
|
|
|
{ |
106
|
|
|
$url = 'http://myapp.com'; |
107
|
|
|
$urlSigner = new MD5UrlSigner('random_monkey'); |
108
|
|
|
|
109
|
|
|
$this->expectException(InvalidExpiration::class); |
110
|
|
|
|
111
|
|
|
$urlSigner->sign($url, $pastExpiration); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** @test */ |
115
|
|
|
public function it_keeps_the_urls_query_parameters_intact() |
116
|
|
|
{ |
117
|
|
|
$url = 'http://myapp.com/?foo=bar&baz=qux'; |
118
|
|
|
$expiration = DateTime::createFromFormat( |
119
|
|
|
'd/m/Y H:i:s', |
120
|
|
|
'10/08/2115 18:15:44', |
121
|
|
|
new DateTimeZone('Europe/Brussels') |
122
|
|
|
); |
123
|
|
|
$exepectedUrl = 'http://myapp.com/?foo=bar&baz=qux&expires=4594900544&signature=728971d9fd0682793d2a1e96b734d949'; |
124
|
|
|
|
125
|
|
|
$urlSigner = new MD5UrlSigner('random_monkey'); |
126
|
|
|
$signedUrl = $urlSigner->sign($url, $expiration); |
|
|
|
|
127
|
|
|
|
128
|
|
|
$this->assertSame($exepectedUrl, $signedUrl); |
129
|
|
|
$this->assertTrue($urlSigner->validate($signedUrl)); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.