diff --git a/assets/controllers/csrf_protection_controller.js b/assets/controllers/csrf_protection_controller.js index 2811f21..511fffa 100644 --- a/assets/controllers/csrf_protection_controller.js +++ b/assets/controllers/csrf_protection_controller.js @@ -1,7 +1,9 @@ const nameCheck = /^[-_a-zA-Z0-9]{4,22}$/; -const tokenCheck = /^[-_\/+a-zA-Z0-9]{24,}$/; +const tokenCheck = /^[-_/+a-zA-Z0-9]{24,}$/; // Generate and double-submit a CSRF token in a form field and a cookie, as defined by Symfony's SameOriginCsrfTokenManager +// Use `form.requestSubmit()` to ensure that the submit event is triggered. Using `form.submit()` will not trigger the event +// and thus this event-listener will not be executed. document.addEventListener('submit', function (event) { generateCsrfToken(event.target); }, true); @@ -33,8 +35,8 @@ export function generateCsrfToken (formElement) { if (!csrfCookie && nameCheck.test(csrfToken)) { csrfField.setAttribute('data-csrf-protection-cookie-value', csrfCookie = csrfToken); csrfField.defaultValue = csrfToken = btoa(String.fromCharCode.apply(null, (window.crypto || window.msCrypto).getRandomValues(new Uint8Array(18)))); - csrfField.dispatchEvent(new Event('change', { bubbles: true })); } + csrfField.dispatchEvent(new Event('change', { bubbles: true })); if (csrfCookie && tokenCheck.test(csrfToken)) { const cookie = csrfCookie + '_' + csrfToken + '=' + csrfCookie + '; path=/; samesite=strict'; diff --git a/assets/stimulus_bootstrap.js b/assets/stimulus_bootstrap.js new file mode 100644 index 0000000..2689398 --- /dev/null +++ b/assets/stimulus_bootstrap.js @@ -0,0 +1,2 @@ +// register any custom, 3rd party controllers here +// app.register('some_controller_name', SomeImportedController); diff --git a/symfony.lock b/symfony.lock index bda13dd..9d1f209 100644 --- a/symfony.lock +++ b/symfony.lock @@ -273,18 +273,18 @@ ] }, "symfony/stimulus-bundle": { - "version": "2.26", + "version": "2.32", "recipe": { "repo": "github.com/symfony/recipes", "branch": "main", - "version": "2.20", - "ref": "3acc494b566816514a6873a89023a35440b6386d" + "version": "2.24", + "ref": "3357f2fa6627b93658d8e13baa416b2a94a50c5f" }, "files": [ - "assets/bootstrap.js", "assets/controllers.json", "assets/controllers/csrf_protection_controller.js", - "assets/controllers/hello_controller.js" + "assets/controllers/hello_controller.js", + "assets/stimulus_bootstrap.js" ] }, "symfony/translation": {