Skip to content

Prevent conflict between CsrfComponent and CsrfProtectionMiddleware#591

Merged
steinkel merged 1 commit into
CakeDC:developfrom
jtraulle:patch-2
Aug 15, 2017
Merged

Prevent conflict between CsrfComponent and CsrfProtectionMiddleware#591
steinkel merged 1 commit into
CakeDC:developfrom
jtraulle:patch-2

Conversation

@jtraulle

Copy link
Copy Markdown
Contributor

I am currently playing with next version of Cake (3.5.x). 🎉

Since CsrfComponent is deprecated starting from 3.5, I have switched my app to Cake\Http\Middleware\CsrfProtectionMiddleware.

CakeDC/Users makes use of CsrfComponent, so, when CsrfProtectionMiddleware is enabled application wide, CsrfComponent enabled in CakeDC/Users overrides CsrfProtectionMiddleware injected _csrfToken causing CsrfProtectionMiddleware to throw a InvalidCsrfTokenException .

Currently, i am using this code in my AppController to disable CsrfComponent for CakeDC/Users plugin

if ($event->getSubject()->request->getParam('plugin') === 'CakeDC/Users') {
    $this->getEventManager()->off($this->Csrf);
}

However, it would be better to check if _csrfToken is already defined in request param before loading CsrfComponent to prevent overriding CsrfProtectionMiddleware injected _csrfToken. 😉

if ($this->request->getParam('_csrfToken') === false) {
    $this->loadComponent('Csrf');
}

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.03%) to 79.584% when pulling c687459 on jtraulle:patch-2 into 2b65802 on CakeDC:develop.

@steinkel
steinkel merged commit b6b1681 into CakeDC:develop Aug 15, 2017
@jtraulle
jtraulle deleted the patch-2 branch August 23, 2017 14:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants