Passed
Push — master ( fb814c...01670e )
by Ryuichi
36:14 queued 34:17
created

CsrfProtection   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 8
eloc 19
c 0
b 0
f 0
dl 0
loc 55
ccs 13
cts 14
cp 0.9286
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A onInject() 0 2 1
B onMethodInject() 0 25 7
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;
0 ignored issues
show
Bug introduced by
The type WebStream\Container\Container was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use WebStream\Exception\Extend\CsrfException;
0 ignored issues
show
Bug introduced by
The type WebStream\Exception\Extend\CsrfException was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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;
0 ignored issues
show
introduced by
The private property $injectAnnotation is not used, and could be removed.
Loading history...
26
27
    /**
28
     * @var array<string> 読み込みアノテーション情報
29
     */
30
    private $readAnnotation;
0 ignored issues
show
introduced by
The private property $readAnnotation is not used, and could be removed.
Loading history...
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