1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WebStream\Annotation\Attributes; |
4
|
|
|
|
5
|
|
|
use WebStream\Annotation\Base\Annotation; |
6
|
|
|
use WebStream\Annotation\Base\IAnnotatable; |
7
|
|
|
use WebStream\Annotation\Base\IMethod; |
8
|
|
|
use WebStream\Container\Container; |
|
|
|
|
9
|
|
|
use WebStream\Exception\Extend\CsrfException; |
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* CsrfProtection |
13
|
|
|
* @author Ryuichi TANAKA. |
14
|
|
|
* @since 2015/05/08 |
15
|
|
|
* @version 0.7 |
16
|
|
|
* |
17
|
|
|
* @Annotation |
18
|
|
|
* @Target("METHOD") |
19
|
|
|
*/ |
20
|
|
|
class CsrfProtection extends Annotation implements IMethod |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var array<string> 注入アノテーション情報 |
24
|
|
|
*/ |
25
|
|
|
private $injectAnnotation; |
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array<string> 読み込みアノテーション情報 |
29
|
|
|
*/ |
30
|
|
|
private $readAnnotation; |
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array<string, string> CSRF定数定義 |
34
|
|
|
*/ |
35
|
|
|
private $csrfProtectionDefinitions = [ |
36
|
|
|
'tokenKey' => '__CSRF_TOKEN__', |
37
|
|
|
'tokenHeader' => 'X-CSRF-Token' |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
public function onInject(array $injectAnnotation) |
44
|
|
|
{ |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
6 |
|
public function onMethodInject(IAnnotatable $instance, \ReflectionMethod $method, Container $container) |
51
|
|
|
{ |
52
|
6 |
|
$tokenByRequest = null; |
53
|
6 |
|
if (array_key_exists($this->csrfProtectionDefinitions['tokenKey'], $container->post)) { |
54
|
2 |
|
$tokenByRequest = $container->post[$this->csrfProtectionDefinitions['tokenKey']]; |
55
|
4 |
|
} elseif (array_key_exists($this->csrfProtectionDefinitions['tokenHeader'], $container->header)) { |
56
|
2 |
|
$tokenByRequest = $container->header[$this->csrfProtectionDefinitions['tokenHeader']]; |
57
|
|
|
} |
58
|
|
|
|
59
|
6 |
|
$tokenInSession = $container->session->get($this->csrfProtectionDefinitions['tokenKey']); |
60
|
6 |
|
$container->session->delete($this->csrfProtectionDefinitions['tokenKey']); |
61
|
|
|
|
62
|
|
|
// POSTリクエスト以外はチェックしない |
63
|
6 |
|
if ($container->requestMethod !== 'POST') { |
64
|
1 |
|
return; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// リクエストトークン、セッショントークンが両方空はNG |
68
|
5 |
|
if ($tokenInSession === null && $tokenByRequest === null) { |
69
|
|
|
throw new CsrfException("Sent invalid CSRF token"); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// リクエストトークンとセッショントークンが一致しない場合NG |
73
|
5 |
|
if ($tokenInSession !== $tokenByRequest) { |
74
|
3 |
|
throw new CsrfException("Sent invalid CSRF token"); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths