question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CSRF tokens in multiple components on first cached request

See original GitHub issue

Using CSRF tokens in multiple components on the first cached request can result in CSRF requests failing. This is because the Sprig components are loaded asynchronously, without a session cookie existing for the user, as reported in https://github.com/putyourlightson/craft-sprig/issues/201 and related to https://github.com/putyourlightson/craft-blitz/issues/329.

The current suggested workaround is to use a dummy init component that on page load will force a session cookie to be generated and will then refresh all other components on the page.

{# _components/init #} 

{% if sprig.isRequest %}
    {{ csrfInput() }}
    <script>
        for (const component of htmx.findAll('.sprig-component')) {
            htmx.trigger(component, 'refresh');
        }
    </script>
{% endif %}
{# Main page #} 

{{ sprig('_components/init', {}, { 's-trigger': 'load' }) }}

{{ sprig('_components/form') }}
{{ sprig('_components/form') }}
{{ sprig('_components/form') }}

{{ sprig.script }}

This is a feature request for Sprig to add a convenience method for handling this use-case.

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:15

github_iconTop GitHub Comments

1reaction
bencrokercommented, Dec 20, 2022

Features are only added to new versions of the plugin, so you’ll have to update to get it.

1reaction
bencrokercommented, Dec 8, 2022

Released in version 2.4.0.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Avoiding CSRF tokens - caching - Stack Overflow
On an ecommerce, the product page is cached by Varnish, so if the user lands directly here, he won't have a session ID...
Read more >
A Deep Dive into CSRF Protection in Rails | by Alex Taylor
There are two components to CSRF. First, a unique token is embedded in your site's HTML. That same token is also stored in...
Read more >
The Curious Case of Caching CSRF Tokens
On the first request, the user is anonymous and their request indistinguishable from any other - their page can be served from the...
Read more >
Multiple {{ craft.blitz.csrfInput }} should not make ... - GitHub
I have a page with multiple {{ craft.blitz.csrfInput }} on it, for different forms. I noticed that triggers as many XHR requests as...
Read more >
CSRF tokens | Web Security Academy - PortSwigger
A CSRF token is a unique, secret, unpredictable value that is generated by the server-side application and transmitted to the client in such...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found