Skip to content

Commit 3f21ef2

Browse files
committed
Update symfony/stimulus-bundle recipe
1 parent 2d6757a commit 3f21ef2

4 files changed

Lines changed: 12 additions & 8 deletions

File tree

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';

assets/stimulus_bootstrap.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// register any custom, 3rd party controllers here
2+
// app.register('some_controller_name', SomeImportedController);

symfony.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Encore
2525
// When enabled, Webpack "splits" your files into smaller pieces for greater optimization.
2626
.splitEntryChunks()
2727

28-
// enables the Symfony UX Stimulus bridge (used in assets/bootstrap.ts)
28+
// enables the Symfony UX Stimulus bridge (used in assets/stimulus_bootstrap.js)
2929
.enableStimulusBridge('./assets/controllers.json')
3030

3131
// will require an extra script tag for runtime.js

0 commit comments

Comments
 (0)