Skip to content

Commit 6022a51

Browse files
authored
IBX-833: As a Developer I want to configure CSRF validation in REST endpoints (#76)
1 parent cbd83b7 commit 6022a51

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

src/bundle/EventListener/CsrfListener.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ public function onKernelRequest(RequestEvent $event)
104104
return;
105105
}
106106

107+
if (!$event->getRequest()->attributes->getBoolean('csrf_protection', true)) {
108+
return;
109+
}
110+
107111
if (!$this->checkCsrfToken($event->getRequest())) {
108112
throw new UnauthorizedException(
109113
'Missing or invalid CSRF token',
@@ -143,6 +147,8 @@ protected function isLoginRequest($route)
143147
* @param string $route
144148
*
145149
* @return bool
150+
*
151+
* @deprecated since Ibexa DXP 3.3.7. Add csrf_protection: false attribute to route definition instead.
146152
*/
147153
protected function isSessionRoute($route)
148154
{

src/bundle/Resources/config/routing.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,18 +1118,21 @@ ezpublish_rest_createSession:
11181118
path: /user/sessions
11191119
defaults:
11201120
_controller: ezpublish_rest.controller.session:createSessionAction
1121+
csrf_protection: false
11211122
methods: [POST]
11221123

11231124
ezpublish_rest_deleteSession:
11241125
path: /user/sessions/{sessionId}
11251126
defaults:
11261127
_controller: ezpublish_rest.controller.session:deleteSessionAction
1128+
csrf_protection: false
11271129
methods: [DELETE]
11281130

11291131
ezpublish_rest_refreshSession:
11301132
path: /user/sessions/{sessionId}/refresh
11311133
defaults:
11321134
_controller: ezpublish_rest.controller.session:refreshSessionAction
1135+
csrf_protection: false
11331136
methods: [POST]
11341137

11351138

tests/bundle/EventListener/CsrfListenerTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,15 @@ public static function provideSessionRoutes()
137137
];
138138
}
139139

140+
public function testSkipCsrfProtection(): void
141+
{
142+
$this->enableCsrfProtection = false;
143+
$this->csrfTokenHeaderValue = null;
144+
145+
$listener = $this->getEventListener();
146+
$listener->onKernelRequest($this->getEvent());
147+
}
148+
140149
public function testNoHeader()
141150
{
142151
$this->expectException(UnauthorizedException::class);

tests/bundle/EventListener/EventListenerTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ abstract class EventListenerTest extends TestCase
3434

3535
protected $requestMethod = false;
3636

37+
protected $enableCsrfProtection = true;
38+
3739
/**
3840
* @dataProvider provideExpectedSubscribedEventTypes
3941
*/
@@ -87,6 +89,11 @@ protected function getRequestAttributesMock()
8789
->method('get')
8890
->with('is_rest_request')
8991
->willReturn($this->isRestRequest);
92+
93+
$this->requestAttributesMock
94+
->method('getBoolean')
95+
->with('csrf_protection', true)
96+
->willReturn($this->enableCsrfProtection);
9097
}
9198

9299
return $this->requestAttributesMock;

0 commit comments

Comments
 (0)