Add CSRF tokens to actions rendered as POST forms - #7806
Open
ERuban wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Actions configured with
renderAsForm()are submitted asPOSTrequests, but the rendered<form>contains no CSRF token, so there is no way to protect these state-changing actions against CSRF attacks. The built-indeleteaction already ships its own token (ea-delete); this PR brings the same protection to custom actions.Changes:
Buttoncomponent and by dropdown items withrenderAsFormnow include a hiddentokenfield with a CSRF token (token ID:ea-action).delete()):$this->isCsrfTokenValid('ea-action', $request->getPayload()->getString('token')). Documented in a new "Validating CSRF Tokens in Custom Actions" section.Backward compatibility:
{% guard function csrf_token %}) or when rendering outside a web request with a session (e.g. CLI/email contexts), so nothing breaks in those setups.tokenfield in the POST payload is harmless for existing actions that don't validate it.Tests: component-level rendering tests (token present with a session, absent without a request), assertions in the existing
renderAsForm()functional tests, and an end-to-end test with a CSRF-validating action in the test app (empty/invalid token →400and no side effect, valid token → action executed).