Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[4.x][5.x] Missing CSRF in paymentForm.js -> _callPayAction() #334

Open
craig-lt opened this issue Feb 28, 2025 · 3 comments
Open

[4.x][5.x] Missing CSRF in paymentForm.js -> _callPayAction() #334

craig-lt opened this issue Feb 28, 2025 · 3 comments

Comments

@craig-lt
Copy link

Description

Seeing a bunch of 400 (Bad Request) errors in console using the plugin on the checkout page.

It wouldn't allow me to create a PR but the fix is just to add this line:
form.append(window.csrfTokenName, window.csrfTokenValue);
at ~line 222 in paymentForm.js so that CSRF is added before the post. It looks like all the other places where formData() is gotten has this already but this one place is missing.

Copy link

linear bot commented Feb 28, 2025

@nfourtythree
Copy link
Contributor

Hi @craig-lt

Thank you for your message.

I can't replicate this issue locally using the example templates, I assuming this could be being caused because of missing the csrf input in your templates.

The first thing that happens in the _callPayAction() method is to grab all the data from the <form> tag. This means is the CSRF token is in there it will be added to the post data.

Could you check to see if the input is being added? Or maybe you are interacting with the payment form in a different manner?

Thanks!

@nfourtythree nfourtythree self-assigned this Mar 3, 2025
@craig-lt
Copy link
Author

craig-lt commented Mar 3, 2025

Hi @nfourtythree

It's very possible that we are doing something unique here as I've inherited a rather custom Craft site. There is custom code calling _callPayAction() indirectly from some other JS logic. I haven't determined what the need is for doing this but there is a lot of code around this functionality so I don't want to remove it entirely yet. The CSRF input is in the Twig form and we are using asyncCSRF.

The best I can tell from some ad hoc testing and investigation is that initStripe() is firing before the CSRF tokens have been added to the form via the asyncCSRF code. If I delay calling it a few hundred milliseconds then everything works correctly too (as another potential fix to the problem).

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

2 participants