1 | <?php |
||
23 | class CsrfListener implements EventSubscriberInterface |
||
24 | { |
||
25 | /** |
||
26 | * Name of the HTTP header containing CSRF token. |
||
27 | */ |
||
28 | const CSRF_TOKEN_HEADER = 'X-CSRF-Token'; |
||
29 | |||
30 | /** |
||
31 | * @var null|CsrfTokenManagerInterface |
||
32 | */ |
||
33 | private $csrfTokenManager; |
||
34 | |||
35 | /** |
||
36 | * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface |
||
37 | */ |
||
38 | private $eventDispatcher; |
||
39 | |||
40 | /** |
||
41 | * @var bool |
||
42 | */ |
||
43 | private $csrfEnabled; |
||
44 | |||
45 | /** |
||
46 | * @var bool |
||
47 | */ |
||
48 | private $csrfTokenIntention; |
||
49 | |||
50 | /** |
||
51 | * Note that CSRF provider needs to be optional as it will not be available |
||
52 | * when CSRF protection is disabled. |
||
53 | * |
||
54 | * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher |
||
55 | * @param bool $csrfEnabled |
||
56 | * @param string $csrfTokenIntention |
||
57 | * @param null|CsrfTokenManagerInterface $csrfTokenManager |
||
58 | */ |
||
59 | public function __construct( |
||
70 | |||
71 | /** |
||
72 | * @return array |
||
73 | */ |
||
74 | public static function getSubscribedEvents() |
||
80 | |||
81 | /** |
||
82 | * This method validates CSRF token if CSRF protection is enabled. |
||
83 | * |
||
84 | * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event |
||
85 | * |
||
86 | * @throws \eZ\Publish\Core\Base\Exceptions\UnauthorizedException |
||
87 | */ |
||
88 | public function onKernelRequest(GetResponseEvent $event) |
||
121 | |||
122 | /** |
||
123 | * @param string $method |
||
124 | * |
||
125 | * @return bool |
||
126 | */ |
||
127 | protected function isMethodSafe($method) |
||
131 | |||
132 | /** |
||
133 | * @param string $route |
||
134 | * |
||
135 | * @return bool |
||
136 | * |
||
137 | * @deprecated Deprecated since 6.5. Use isSessionRoute() instead. |
||
138 | */ |
||
139 | protected function isLoginRequest($route) |
||
143 | |||
144 | /** |
||
145 | * Tests if a given $route is a session management one. |
||
146 | * |
||
147 | * @param string $route |
||
148 | * |
||
149 | * @return bool |
||
150 | */ |
||
151 | protected function isSessionRoute($route) |
||
152 | { |
||
153 | return in_array( |
||
154 | $route, |
||
155 | ['ezpublish_rest_createSession', 'ezpublish_rest_refreshSession', 'ezpublish_rest_deleteSession'] |
||
156 | ); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Checks the validity of the request's csrf token header. |
||
161 | * |
||
162 | * @param Request $request |
||
163 | * |
||
164 | * @return bool true/false if the token is valid/invalid, false if none was found in the request's headers. |
||
165 | */ |
||
166 | protected function checkCsrfToken(Request $request) |
||
179 | } |
||
180 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.