Issue Bug Closed

#81351The CSRF protection token for the requested module is missing or invalid

David Brünner 05/26/17, 20:30 updated 10/22/26, 20:40

Description

In my module I have a list of all entries in Database.

$this->lebenslageRepository->findAll();

<div class="container" xmlns:f="http://www.w3.org/1999/html">
  <f:widget.paginate objects="{lebenslage}" as="lebenslage" configuration="{itemsPerPage: 25, insertAbove: 0, insertBelow: 1, maximumNumberOfLinks: 10}">
  <h2>Liste aller Lebenslagen</h2>
    <f:link.action action="addForm" class="btn btn-primary">Neu</f:link.action>
    <br/><br/>
    <table class="table table-striped table-hover">
      <thead>
        <tr>
          <th>
            Bezeichnung
      </th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <kk:widget.sort objects="{lebenslage}" as="lebenslage" property="bezeichnung">
          <f:for each="{lebenslage}" as="lebenslage">
            <tr>
              <td>{lebenslage.bezeichnung}</td>
              <td>
                <f:link.action action="updateForm" arguments="{lebenslage:lebenslage}" class="btn btn-primary">
                  Bearbeiten
                </f:link.action>
        <f:link.action action="deleteForm" arguments="{lebenslage:lebenslage}" class="btn btn-primary">
                  Löschen
        </f:link.action>
              </td>
            </tr>
       </f:for>
      </kk:widget.sort>
    </tbody>
      </table>
    </f:widget.paginate>

    <f:link.action action="addForm" class="btn btn-primary">Neu</f:link.action>
    <f:link.action controller="Backend\Index" action="index" class="btn btn-secondary">Zurück</f:link.action>
</div>

<script>
  var ajaxUrl = '<f:uri.action action="sortAjax" controller="{controllerName}" pageType="99"/>';
</script>

Next I wanted to sort all entries when I click on the .
With the Sort-Widget-ViewHelper I wrote, I’m not able to insert the sort-button in my . So I wanted to do it with ajax

var orderIcon = $(this).find('i.orderIcon');
if (!orderIcon.hasClass('ASC') && !orderIcon.hasClass('hide')) {
  orderIcon.addClass('ASC');
} else if (orderIcon.hasClass('ASC')) {
  orderIcon
    .removeClass('ASC')
    .addClass('hide');
} else if (orderIcon.hasClass('hide')) {
  orderIcon.removeClass('hide');
}

$.ajax({
  url: ajaxUrl,
  method: 'POST'
  ...
  ...
});

But when I now click on my , in WebDeveloper I get the message: ‘The CSRF protection token for the requested module is missing or invalid’

With debugging I found the solution:
In the file TYPO3\CMS\Core\FormProtection there’s a method ‘validateToken(…)’.
My change in the else-part:

$tokenId = $_REQUEST['amp;moduleToken'];
if ((string)$tokenId === $validTokenId) {
  $isValid = true;
} else {
  $isValid = false;
}

In TYPO3\CMS\Backend\Http, Fct. ‘isValidModuleRequest()’ the token is searched in

$this->request->getQueryParams()['moduleToken']

but the queryParams look like:
Array
(
[M] => web_KkbaybwKkbaybw
[amp;moduleToken] => fffc6cc341becfcc9aead8b76a887f19fab988f6
[amp;tx_kkbaybw_web_kkbaybwkkbaybw] => Array
(
[action] => sortAjax
[controller] => Backend\Lebenslage
)

)

Reviews 0

No change resolves or names this issue in its commit message, and no comment links one.

History 5

  1. Sylwester Szytula commented <span data-processor="relativetime" data-value="2017-09-07T17:44:46+00:00" > 09/07/17, 17:44 </span>

    I had the same problem. For me the solutions was to add f:format.htmlentitiesDecode, like:

    <script>
    var url: '<f:format.htmlentitiesDecode><f:uri.action action="someActionName" /></f:format.htmlentitiesDecode>';
    </script>
  2. Oliver Hader commented and changed status New In Progress <span data-processor="relativetime" data-value="2021-12-14T17:30:34+00:00" > 12/14/21, 17:30 </span>

    Looks like regular URL parameters like &moduleToken= is double-encoded to &amp;moduleToken= which explains getting amp;moduleToken as parameter-name. I’ll look into that and most probably create test cases for URI ActionViewHelper…

  3. Georg Ringer commented and changed status In Progress Closed <span data-processor="relativetime" data-value="2026-10-22T20:40:18+00:00" > 10/22/26, 20:40 </span>

    This works as designed. The URL was HTML-escaped by Fluid, not double-encoded by the UriBuilder.

    f:uri.action returns a string meant for HTML output and Fluid escapes it like every other ViewHelper output, so & becomes &amp; (see the expectations in ActionViewHelperTest, which assert &amp; between the query parameters). When such a URL is put into a <script> block and used for an AJAX request, it has to be unescaped again, for example with <f:format.raw> around the ViewHelper, or better by passing the URL in a data-* attribute, which the browser decodes automatically. The suggestion from the comment of 2017-09-07 (f:format.htmlentitiesDecode) is the same workaround.

    The reported moduleToken and M=… query parameters of the legacy module URLs do not exist anymore since backend modules are registered as regular routes (#82406, TYPO3 v9), and f:widget.paginate from the template of the report was removed in v12.

    Closing this issue as works as designed.

    If this is still valid for you in v14, please open a new issue and provide information how to reproduce it. Thanks!