symfony/timulus-bundle

recipe
This commit is contained in:
2026-02-05 22:12:18 +01:00
parent 32c9c3f8c7
commit 6e78a22896
3 changed files with 11 additions and 7 deletions

View File

@@ -1,7 +1,9 @@
const nameCheck = /^[-_a-zA-Z0-9]{4,22}$/; 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 // 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) { document.addEventListener('submit', function (event) {
generateCsrfToken(event.target); generateCsrfToken(event.target);
}, true); }, true);
@@ -33,8 +35,8 @@ export function generateCsrfToken (formElement) {
if (!csrfCookie && nameCheck.test(csrfToken)) { if (!csrfCookie && nameCheck.test(csrfToken)) {
csrfField.setAttribute('data-csrf-protection-cookie-value', csrfCookie = 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.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)) { if (csrfCookie && tokenCheck.test(csrfToken)) {
const cookie = csrfCookie + '_' + csrfToken + '=' + csrfCookie + '; path=/; samesite=strict'; const cookie = csrfCookie + '_' + csrfToken + '=' + csrfCookie + '; path=/; samesite=strict';

View File

@@ -0,0 +1,2 @@
// register any custom, 3rd party controllers here
// app.register('some_controller_name', SomeImportedController);

View File

@@ -273,18 +273,18 @@
] ]
}, },
"symfony/stimulus-bundle": { "symfony/stimulus-bundle": {
"version": "2.26", "version": "2.32",
"recipe": { "recipe": {
"repo": "github.com/symfony/recipes", "repo": "github.com/symfony/recipes",
"branch": "main", "branch": "main",
"version": "2.20", "version": "2.24",
"ref": "3acc494b566816514a6873a89023a35440b6386d" "ref": "3357f2fa6627b93658d8e13baa416b2a94a50c5f"
}, },
"files": [ "files": [
"assets/bootstrap.js",
"assets/controllers.json", "assets/controllers.json",
"assets/controllers/csrf_protection_controller.js", "assets/controllers/csrf_protection_controller.js",
"assets/controllers/hello_controller.js" "assets/controllers/hello_controller.js",
"assets/stimulus_bootstrap.js"
] ]
}, },
"symfony/translation": { "symfony/translation": {