Skip to content

Commit c72eca5

Browse files
Fix stimulus-bundle recipe (#1468)
1 parent df07579 commit c72eca5

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

symfony/stimulus-bundle/2.24/assets/controllers/csrf_protection_controller.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const nameCheck = /^[-_a-zA-Z0-9]{4,22}$/;
2-
const tokenCheck = /^[-_\/+a-zA-Z0-9]{24,}$/;
2+
const tokenCheck = /^[-_/+a-zA-Z0-9]{24,}$/;
33

44
// Generate and double-submit a CSRF token in a form field and a cookie, as defined by Symfony's SameOriginCsrfTokenManager
5+
// Use `form.requestSubmit()` to ensure that the submit event is triggered. Using `form.submit()` will not trigger the event
6+
// and thus this event-listener will not be executed.
57
document.addEventListener('submit', function (event) {
68
generateCsrfToken(event.target);
79
}, true);
@@ -33,8 +35,8 @@ export function generateCsrfToken (formElement) {
3335
if (!csrfCookie && nameCheck.test(csrfToken)) {
3436
csrfField.setAttribute('data-csrf-protection-cookie-value', csrfCookie = csrfToken);
3537
csrfField.defaultValue = csrfToken = btoa(String.fromCharCode.apply(null, (window.crypto || window.msCrypto).getRandomValues(new Uint8Array(18))));
36-
csrfField.dispatchEvent(new Event('change', { bubbles: true }));
3738
}
39+
csrfField.dispatchEvent(new Event('change', { bubbles: true }));
3840

3941
if (csrfCookie && tokenCheck.test(csrfToken)) {
4042
const cookie = csrfCookie + '_' + csrfToken + '=' + csrfCookie + '; path=/; samesite=strict';

0 commit comments

Comments
 (0)