Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 59ca1eb

Browse files
[HttpFoundation] Add temporary URI signed
1 parent c71348a commit 59ca1eb

File tree

9 files changed

+350
-11
lines changed

9 files changed

+350
-11
lines changed

src/Symfony/Component/HttpFoundation/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ CHANGELOG
44
7.1
55
---
66

7+
* Add optional `$expirationParameter` argument to `UriSigner::__construct()`
8+
* Add optional `$expiration` argument to `UriSigner::sign()`
9+
* Rename `$parameter` argument of `UriSigner::__construct()` to `$hashParameter`
710
* Add `UploadedFile::getClientOriginalPath()`
811

912
7.0
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
/*
13+
* This file is part of the Symfony package.
14+
*
15+
* (c) Fabien Potencier <[email protected]>
16+
*
17+
* For the full copyright and license information, please view the LICENSE
18+
* file that was distributed with this source code.
19+
*/
20+
21+
namespace Symfony\Component\HttpFoundation\Exception;
22+
23+
interface ExceptionInterface extends \Throwable
24+
{
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
/*
13+
* This file is part of the Symfony package.
14+
*
15+
* (c) Fabien Potencier <[email protected]>
16+
*
17+
* For the full copyright and license information, please view the LICENSE
18+
* file that was distributed with this source code.
19+
*/
20+
21+
namespace Symfony\Component\HttpFoundation\Exception;
22+
23+
interface ExceptionInterface extends \Throwable
24+
{
25+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--- src/Symfony/Component/HttpFoundation/Exception/ExceptionInterface.php 2024-03-17 17:28:21.134766504 +0000
2+
+++ src/Symfony/Component/HttpFoundation/Exception/ExceptionInterface.php 2024-03-17 17:28:22.056798627 +0000
3+
@@ -1,16 +1,16 @@
4+
-<?php
5+
-
6+
-/*
7+
- * This file is part of the Symfony package.
8+
- *
9+
- * (c) Fabien Potencier <[email protected]>
10+
- *
11+
- * For the full copyright and license information, please view the LICENSE
12+
- * file that was distributed with this source code.
13+
- */
14+
-
15+
-namespace Symfony\Component\HttpFoundation\Exception;
16+
-
17+
-interface ExceptionInterface extends \Throwable
18+
-{
19+
-}
20+
+<?php
21+
+
22+
+/*
23+
+ * This file is part of the Symfony package.
24+
+ *
25+
+ * (c) Fabien Potencier <[email protected]>
26+
+ *
27+
+ * For the full copyright and license information, please view the LICENSE
28+
+ * file that was distributed with this source code.
29+
+ */
30+
+
31+
+namespace Symfony\Component\HttpFoundation\Exception;
32+
+
33+
+interface ExceptionInterface extends \Throwable
34+
+{
35+
+}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
/*
13+
* This file is part of the Symfony package.
14+
*
15+
* (c) Fabien Potencier <[email protected]>
16+
*
17+
* For the full copyright and license information, please view the LICENSE
18+
* file that was distributed with this source code.
19+
*/
20+
21+
namespace Symfony\Component\HttpFoundation\Exception;
22+
23+
/**
24+
* Base LogicException for Http Foundation component.
25+
*/
26+
class LogicException extends \LogicException implements ExceptionInterface
27+
{
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
/*
13+
* This file is part of the Symfony package.
14+
*
15+
* (c) Fabien Potencier <[email protected]>
16+
*
17+
* For the full copyright and license information, please view the LICENSE
18+
* file that was distributed with this source code.
19+
*/
20+
21+
namespace Symfony\Component\HttpFoundation\Exception;
22+
23+
/**
24+
* Base LogicException for Http Foundation component.
25+
*/
26+
class LogicException extends \LogicException implements ExceptionInterface
27+
{
28+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--- src/Symfony/Component/HttpFoundation/Exception/LogicException.php 2024-03-17 17:28:21.346773890 +0000
2+
+++ src/Symfony/Component/HttpFoundation/Exception/LogicException.php 2024-03-17 17:28:22.090799811 +0000
3+
@@ -1,19 +1,19 @@
4+
-<?php
5+
-
6+
-/*
7+
- * This file is part of the Symfony package.
8+
- *
9+
- * (c) Fabien Potencier <[email protected]>
10+
- *
11+
- * For the full copyright and license information, please view the LICENSE
12+
- * file that was distributed with this source code.
13+
- */
14+
-
15+
-namespace Symfony\Component\HttpFoundation\Exception;
16+
-
17+
-/**
18+
- * Base LogicException for Http Foundation component.
19+
- */
20+
-class LogicException extends \LogicException implements ExceptionInterface
21+
-{
22+
-}
23+
+<?php
24+
+
25+
+/*
26+
+ * This file is part of the Symfony package.
27+
+ *
28+
+ * (c) Fabien Potencier <[email protected]>
29+
+ *
30+
+ * For the full copyright and license information, please view the LICENSE
31+
+ * file that was distributed with this source code.
32+
+ */
33+
+
34+
+namespace Symfony\Component\HttpFoundation\Exception;
35+
+
36+
+/**
37+
+ * Base LogicException for Http Foundation component.
38+
+ */
39+
+class LogicException extends \LogicException implements ExceptionInterface
40+
+{
41+
+}

src/Symfony/Component/HttpFoundation/Tests/UriSignerTest.php

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\HttpFoundation\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpFoundation\Exception\LogicException;
1516
use Symfony\Component\HttpFoundation\Request;
1617
use Symfony\Component\HttpFoundation\UriSigner;
1718

@@ -24,21 +25,38 @@ public function testSign()
2425
$this->assertStringContainsString('?_hash=', $signer->sign('http://example.com/foo'));
2526
$this->assertStringContainsString('?_hash=', $signer->sign('http://example.com/foo?foo=bar'));
2627
$this->assertStringContainsString('&foo=', $signer->sign('http://example.com/foo?foo=bar'));
28+
29+
$this->assertStringContainsString('?_expiration=', $signer->sign('http://example.com/foo', 1));
30+
$this->assertStringContainsString('&_hash=', $signer->sign('http://example.com/foo', 1));
31+
$this->assertStringContainsString('?_expiration=', $signer->sign('http://example.com/foo?foo=bar', 1));
32+
$this->assertStringContainsString('&_hash=', $signer->sign('http://example.com/foo?foo=bar', 1));
33+
$this->assertStringContainsString('&foo=', $signer->sign('http://example.com/foo?foo=bar', 1));
2734
}
2835

2936
public function testCheck()
3037
{
3138
$signer = new UriSigner('foobar');
3239

40+
$this->assertFalse($signer->check('http://example.com/foo'));
3341
$this->assertFalse($signer->check('http://example.com/foo?_hash=foo'));
3442
$this->assertFalse($signer->check('http://example.com/foo?foo=bar&_hash=foo'));
3543
$this->assertFalse($signer->check('http://example.com/foo?foo=bar&_hash=foo&bar=foo'));
3644

45+
$this->assertFalse($signer->check('http://example.com/foo?_expiration=4070908800'));
46+
$this->assertFalse($signer->check('http://example.com/foo?_expiration=4070908800?_hash=foo'));
47+
$this->assertFalse($signer->check('http://example.com/foo?_expiration=4070908800&foo=bar&_hash=foo'));
48+
$this->assertFalse($signer->check('http://example.com/foo?_expiration=4070908800&foo=bar&_hash=foo&bar=foo'));
49+
3750
$this->assertTrue($signer->check($signer->sign('http://example.com/foo')));
3851
$this->assertTrue($signer->check($signer->sign('http://example.com/foo?foo=bar')));
3952
$this->assertTrue($signer->check($signer->sign('http://example.com/foo?foo=bar&0=integer')));
4053

54+
$this->assertTrue($signer->check($signer->sign('http://example.com/foo', new \DateTimeImmutable('2099-01-01 00:00:00'))));
55+
$this->assertTrue($signer->check($signer->sign('http://example.com/foo?foo=bar', new \DateTimeImmutable('2099-01-01 00:00:00'))));
56+
$this->assertTrue($signer->check($signer->sign('http://example.com/foo?foo=bar&0=integer', new \DateTimeImmutable('2099-01-01 00:00:00'))));
57+
4158
$this->assertSame($signer->sign('http://example.com/foo?foo=bar&bar=foo'), $signer->sign('http://example.com/foo?bar=foo&foo=bar'));
59+
$this->assertSame($signer->sign('http://example.com/foo?foo=bar&bar=foo', 1), $signer->sign('http://example.com/foo?bar=foo&foo=bar', 1));
4260
}
4361

4462
public function testCheckWithDifferentArgSeparator()
@@ -51,6 +69,12 @@ public function testCheckWithDifferentArgSeparator()
5169
$signer->sign('http://example.com/foo?foo=bar&baz=bay')
5270
);
5371
$this->assertTrue($signer->check($signer->sign('http://example.com/foo?foo=bar&baz=bay')));
72+
73+
$this->assertSame(
74+
'http://example.com/foo?_expiration=4070908800&_hash=xfui5FoP0vbD9Cp7pI0tHnqR1Fmj2UARqkIUw7SZVfQ%3D&baz=bay&foo=bar',
75+
$signer->sign('http://example.com/foo?foo=bar&baz=bay', new \DateTimeImmutable('2099-01-01 00:00:00'))
76+
);
77+
$this->assertTrue($signer->check($signer->sign('http://example.com/foo?foo=bar&baz=bay', new \DateTimeImmutable('2099-01-01 00:00:00'))));
5478
}
5579

5680
public function testCheckWithRequest()
@@ -60,17 +84,27 @@ public function testCheckWithRequest()
6084
$this->assertTrue($signer->checkRequest(Request::create($signer->sign('http://example.com/foo'))));
6185
$this->assertTrue($signer->checkRequest(Request::create($signer->sign('http://example.com/foo?foo=bar'))));
6286
$this->assertTrue($signer->checkRequest(Request::create($signer->sign('http://example.com/foo?foo=bar&0=integer'))));
87+
88+
$this->assertTrue($signer->checkRequest(Request::create($signer->sign('http://example.com/foo', new \DateTimeImmutable('2099-01-01 00:00:00')))));
89+
$this->assertTrue($signer->checkRequest(Request::create($signer->sign('http://example.com/foo?foo=bar', new \DateTimeImmutable('2099-01-01 00:00:00')))));
90+
$this->assertTrue($signer->checkRequest(Request::create($signer->sign('http://example.com/foo?foo=bar&0=integer', new \DateTimeImmutable('2099-01-01 00:00:00')))));
6391
}
6492

6593
public function testCheckWithDifferentParameter()
6694
{
67-
$signer = new UriSigner('foobar', 'qux');
95+
$signer = new UriSigner('foobar', 'qux', 'abc');
6896

6997
$this->assertSame(
7098
'http://example.com/foo?baz=bay&foo=bar&qux=rIOcC%2FF3DoEGo%2FvnESjSp7uU9zA9S%2F%2BOLhxgMexoPUM%3D',
7199
$signer->sign('http://example.com/foo?foo=bar&baz=bay')
72100
);
73101
$this->assertTrue($signer->check($signer->sign('http://example.com/foo?foo=bar&baz=bay')));
102+
103+
$this->assertSame(
104+
'http://example.com/foo?abc=4070908800&baz=bay&foo=bar&qux=hdhUhBVPpzKJdz5ZjC%2FkLvtOYdGKOvKVOczmmMIZK0A%3D',
105+
$signer->sign('http://example.com/foo?foo=bar&baz=bay', new \DateTimeImmutable('2099-01-01 00:00:00'))
106+
);
107+
$this->assertTrue($signer->check($signer->sign('http://example.com/foo?foo=bar&baz=bay', new \DateTimeImmutable('2099-01-01 00:00:00'))));
74108
}
75109

76110
public function testSignerWorksWithFragments()
@@ -81,6 +115,79 @@ public function testSignerWorksWithFragments()
81115
'http://example.com/foo?_hash=EhpAUyEobiM3QTrKxoLOtQq5IsWyWedoXDPqIjzNj5o%3D&bar=foo&foo=bar#foobar',
82116
$signer->sign('http://example.com/foo?bar=foo&foo=bar#foobar')
83117
);
118+
84119
$this->assertTrue($signer->check($signer->sign('http://example.com/foo?bar=foo&foo=bar#foobar')));
120+
121+
$this->assertSame(
122+
'http://example.com/foo?_expiration=4070908800&_hash=qHl626U5d7LMsVtBxPt9GNzysdSxyOQ1fHA59Y1ib0Y%3D&bar=foo&foo=bar#foobar',
123+
$signer->sign('http://example.com/foo?bar=foo&foo=bar#foobar', new \DateTimeImmutable('2099-01-01 00:00:00'))
124+
);
125+
126+
$this->assertTrue($signer->check($signer->sign('http://example.com/foo?bar=foo&foo=bar#foobar', new \DateTimeImmutable('2099-01-01 00:00:00'))));
127+
}
128+
129+
public function testSignWithUriExpiration()
130+
{
131+
$signer = new UriSigner('foobar');
132+
133+
$this->assertSame($signer->sign('http://example.com/foo?foo=bar&bar=foo', new \DateTimeImmutable('2099-01-01 00:00:00')), $signer->sign('http://example.com/foo?bar=foo&foo=bar', 4070908800));
134+
}
135+
136+
public function testSignWithoutExpirationAndWithReservedHashParameter()
137+
{
138+
$signer = new UriSigner('foobar');
139+
140+
$this->expectException(LogicException::class);
141+
142+
$signer->sign('http://example.com/foo?_hash=bar');
143+
}
144+
145+
public function testSignWithoutExpirationAndWithReservedParameter()
146+
{
147+
$signer = new UriSigner('foobar');
148+
149+
$this->expectException(LogicException::class);
150+
151+
$signer->sign('http://example.com/foo?_expiration=4070908800');
152+
}
153+
154+
public function testSignWithExpirationAndWithReservedHashParameter()
155+
{
156+
$signer = new UriSigner('foobar');
157+
158+
$this->expectException(LogicException::class);
159+
160+
$signer->sign('http://example.com/foo?_hash=bar', new \DateTimeImmutable('2099-01-01 00:00:00'));
161+
}
162+
163+
public function testSignWithExpirationAndWithReservedParameter()
164+
{
165+
$signer = new UriSigner('foobar');
166+
167+
$this->expectException(LogicException::class);
168+
169+
$signer->sign('http://example.com/foo?_expiration=4070908800', new \DateTimeImmutable('2099-01-01 00:00:00'));
170+
}
171+
172+
public function testCheckWithUriExpiration()
173+
{
174+
$signer = new UriSigner('foobar');
175+
176+
$this->assertFalse($signer->check($signer->sign('http://example.com/foo', new \DateTimeImmutable('2000-01-01 00:00:00'))));
177+
$this->assertFalse($signer->check($signer->sign('http://example.com/foo?foo=bar', new \DateTimeImmutable('2000-01-01 00:00:00'))));
178+
$this->assertFalse($signer->check($signer->sign('http://example.com/foo?foo=bar&0=integer', new \DateTimeImmutable('2000-01-01 00:00:00'))));
179+
180+
$this->assertFalse($signer->check($signer->sign('http://example.com/foo', 1577836800))); // 2000-01-01
181+
$this->assertFalse($signer->check($signer->sign('http://example.com/foo?foo=bar', 1577836800))); // 2000-01-01
182+
$this->assertFalse($signer->check($signer->sign('http://example.com/foo?foo=bar&0=integer', 1577836800))); // 2000-01-01
183+
184+
$relativeUriFromNow1 = $signer->sign('http://example.com/foo', new \DateInterval('PT3S'));
185+
$relativeUriFromNow2 = $signer->sign('http://example.com/foo?foo=bar', new \DateInterval('PT3S'));
186+
$relativeUriFromNow3 = $signer->sign('http://example.com/foo?foo=bar&0=integer', new \DateInterval('PT3S'));
187+
sleep(10);
188+
189+
$this->assertFalse($signer->check($relativeUriFromNow1));
190+
$this->assertFalse($signer->check($relativeUriFromNow2));
191+
$this->assertFalse($signer->check($relativeUriFromNow3));
85192
}
86193
}

0 commit comments

Comments
 (0)