By twod on
Change record status:
Project:
Introduced in branch:
10.3.x
Introduced in version:
10.3.0
Description:

The _csrf_token route access requirement always needs a valid token, which means a user will always get a 403 access denied when the token is missing or invalid.

The new _csrf_confirm_form_route route option allows the user to be redirected to a different route when they receive a 403 on the CSRF protected route.

Example route definition:

my_module.some_action:
 path: '/perform_action'
 defaults:
 _controller: '\Drupal\my_module\Controller\MyController::doAction'
 requirements:
 _user_is_logged_in: 'TRUE'
 _csrf_token: 'TRUE'
 options:
 _csrf_confirm_form_route: 'my_module.some_action.confirm'
my_module.some_action.confirm:
 path: '/perform_action/confirm'
 defaults:
 _form: '\Drupal\my_module\Form\MyActionConfirm'
 requirements:
 _user_is_logged_in: 'TRUE'

MyController:

public function doAction() {
 // Perform the action here.
}

Confirm form:

class MyActionConfirm extends ConfirmFormBase {
 // ...
 public function getConfirmText() {
 return $this->t('Just do it!');
 }
 public function getQuestion() {
 return $this->t('Are you sure you want to perform the action?');
 }
 public function submitForm(array &$form, FormStateInterface $form_state) {
 // Perform the action here.
 }
 // ...
}

This is used in core by the user.logout and user.logout.confirm routes.

Impacts:
Module developers