forked from ezsystems/ezpublish-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CsrfListener.php
164 lines (145 loc) · 4.42 KB
/
CsrfListener.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/**
* File containing the CsrfListener class.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
* @version //autogentag//
*/
namespace eZ\Bundle\EzPublishRestBundle\EventListener;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use eZ\Publish\Core\Base\Exceptions\UnauthorizedException;
use eZ\Bundle\EzPublishRestBundle\RestEvents;
use Symfony\Component\Security\Csrf\CsrfToken;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
class CsrfListener implements EventSubscriberInterface
{
/**
* Name of the HTTP header containing CSRF token.
*/
const CSRF_TOKEN_HEADER = "X-CSRF-Token";
/**
* @var null|CsrfTokenManagerInterface
*/
private $csrfTokenManager;
/**
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
private $eventDispatcher;
/**
* @var bool
*/
private $csrfEnabled;
/**
* @var bool
*/
private $csrfTokenIntention;
/**
* Note that CSRF provider needs to be optional as it will not be available
* when CSRF protection is disabled.
*
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
* @param bool $csrfEnabled
* @param string $csrfTokenIntention
* @param null|CsrfTokenManagerInterface $csrfTokenManager
*/
public function __construct(
EventDispatcherInterface $eventDispatcher,
$csrfEnabled,
$csrfTokenIntention,
CsrfTokenManagerInterface $csrfTokenManager = null
)
{
$this->eventDispatcher = $eventDispatcher;
$this->csrfEnabled = $csrfEnabled;
$this->csrfTokenIntention = $csrfTokenIntention;
$this->csrfTokenManager = $csrfTokenManager;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return array(
KernelEvents::REQUEST => 'onKernelRequest',
);
}
/**
* This method validates CSRF token if CSRF protection is enabled.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
*
* @throws \eZ\Publish\Core\Base\Exceptions\UnauthorizedException
*/
public function onKernelRequest( GetResponseEvent $event )
{
if ( !$event->getRequest()->attributes->get( 'is_rest_request' ) )
{
return;
}
if ( !$this->csrfEnabled )
{
return;
}
// skip CSRF validation if no session is running
if ( !$event->getRequest()->getSession()->isStarted() )
{
return;
}
if ( $this->isMethodSafe( $event->getRequest()->getMethod() ) )
{
return;
}
if ( $this->isLoginRequest( $event->getRequest()->get( "_route" ) ) )
{
return;
}
if ( !$this->checkCsrfToken( $event->getRequest() ) )
{
throw new UnauthorizedException(
"Missing or invalid CSRF token",
$event->getRequest()->getMethod() . " " . $event->getRequest()->getPathInfo()
);
}
// Dispatching event so that CSRF token intention can be injected into Legacy Stack
$this->eventDispatcher->dispatch( RestEvents::REST_CSRF_TOKEN_VALIDATED );
}
/**
* @param string $method
* @return bool
*/
protected function isMethodSafe( $method )
{
return in_array( $method, array( 'GET', 'HEAD', 'OPTIONS' ) );
}
/**
* @param string $route
* @return bool
*/
protected function isLoginRequest( $route )
{
return $route === "ezpublish_rest_createSession";
}
/**
* @param GetResponseEvent $event
*
* @return bool
*/
protected function checkCsrfToken( Request $request )
{
if ( !$request->headers->has( self::CSRF_TOKEN_HEADER ) )
{
return false;
}
return $this->csrfTokenManager->isTokenValid(
new CsrfToken(
$this->csrfTokenIntention,
$request->headers->get( self::CSRF_TOKEN_HEADER )
)
);
}
}