1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace TheCodingMachine\Middlewares; |
5
|
|
|
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
9
|
|
|
use TheCodingMachine\Middlewares\SafeRequests\IsSafeHttpMethod; |
10
|
|
|
use Zend\Diactoros\Request; |
11
|
|
|
use Zend\Diactoros\Response; |
12
|
|
|
use Zend\Diactoros\ServerRequest; |
13
|
|
|
|
14
|
|
|
class CsrfHeaderCheckMiddlewareTest extends AbstractMiddlewareTest |
15
|
|
|
{ |
16
|
|
View Code Duplication |
public function testGetRequest() |
17
|
|
|
{ |
18
|
|
|
$request = new ServerRequest([], [], "http://alice.com/hello", "Get"); |
19
|
|
|
|
20
|
|
|
$middleware = CsrfHeaderCheckMiddlewareFactory::createDefault(); |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
$response = $middleware->process($request, $this->getDelegate()); |
24
|
|
|
|
25
|
|
|
$this->assertSame('foobar', (string) $response->getBody()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
View Code Duplication |
public function testFailingPostRequestNoHost() |
29
|
|
|
{ |
30
|
|
|
$request = new ServerRequest([], [], "http://alice.com/hello", "Post"); |
31
|
|
|
$request = $request->withHeader('Origin', "http://alice.com"); |
32
|
|
|
$request = $request->withoutHeader('Host'); |
33
|
|
|
|
34
|
|
|
$middleware = CsrfHeaderCheckMiddlewareFactory::createDefault(); |
35
|
|
|
|
36
|
|
|
$this->expectException(CsrfHeaderCheckMiddlewareException::class); |
37
|
|
|
$this->expectExceptionMessage('Could not find the HOST header in the HTTP request.'); |
38
|
|
|
|
39
|
|
|
$response = $middleware->process($request, $this->getDelegate()); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
View Code Duplication |
public function testSuccessfullPostWithOriginAndHost() |
43
|
|
|
{ |
44
|
|
|
$request = new ServerRequest([], [], "http://alice.com/hello", "Post"); |
45
|
|
|
$request = $request->withHeader('Origin', "http://alice.com"); |
46
|
|
|
|
47
|
|
|
$middleware = CsrfHeaderCheckMiddlewareFactory::createDefault(); |
48
|
|
|
|
49
|
|
|
$response = $middleware->process($request, $this->getDelegate()); |
50
|
|
|
|
51
|
|
|
$this->assertSame('foobar', (string) $response->getBody()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
View Code Duplication |
public function testSuccessfullPostWithOriginAndHostAndPort() |
55
|
|
|
{ |
56
|
|
|
$request = new ServerRequest([], [], "http://alice.com:8080/hello", "Post"); |
57
|
|
|
$request = $request->withHeader('Origin', "http://alice.com:8080"); |
58
|
|
|
|
59
|
|
|
$middleware = CsrfHeaderCheckMiddlewareFactory::createDefault(); |
60
|
|
|
|
61
|
|
|
$response = $middleware->process($request, $this->getDelegate()); |
62
|
|
|
|
63
|
|
|
$this->assertSame('foobar', (string) $response->getBody()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
View Code Duplication |
public function testAttackPostWithOriginAndHost() |
67
|
|
|
{ |
68
|
|
|
$request = new ServerRequest([], [], "http://alice.com/hello", "Post"); |
69
|
|
|
$request = $request->withHeader('Origin', "http://eve.com"); |
70
|
|
|
|
71
|
|
|
$middleware = CsrfHeaderCheckMiddlewareFactory::createDefault(); |
72
|
|
|
|
73
|
|
|
$this->expectException(CsrfHeaderCheckMiddlewareException::class); |
74
|
|
|
$this->expectExceptionMessage('Potential CSRF attack stopped. Source origin and target origin do not match.'); |
75
|
|
|
$response = $middleware->process($request, $this->getDelegate()); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
View Code Duplication |
public function testExceptionOnWeirdRequests() |
79
|
|
|
{ |
80
|
|
|
$request = new ServerRequest([], [], "http://alice.com/hello", "Post"); |
81
|
|
|
$request = $request->withHeader('Origin', "http://eve.com"); |
82
|
|
|
$request = $request->withAddedHeader('Origin', "http://alice.com"); |
83
|
|
|
|
84
|
|
|
$middleware = CsrfHeaderCheckMiddlewareFactory::createDefault(); |
85
|
|
|
|
86
|
|
|
$this->expectException(CsrfHeaderCheckMiddlewareException::class); |
87
|
|
|
$this->expectExceptionMessage('Unexpected request: more than one ORIGIN header sent.'); |
88
|
|
|
$response = $middleware->process($request, $this->getDelegate()); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
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.