|
| 1 | +var nameCheck = /^[-_a-zA-Z0-9]{4,22}$/; |
| 2 | +var tokenCheck = /^[-_/+a-zA-Z0-9]{24,}$/; |
| 3 | + |
| 4 | +// Generate and double-submit a CSRF token in a form field and a cookie, as defined by Symfony's SameOriginCsrfTokenManager |
| 5 | +document.addEventListener('submit', function (event) { |
| 6 | + var csrfField = event.target.querySelector('input[data-controller="csrf-protection"]'); |
| 7 | + |
| 8 | + if (!csrfField) { |
| 9 | + return; |
| 10 | + } |
| 11 | + |
| 12 | + var csrfCookie = csrfField.getAttribute('data-csrf-protection-cookie-value'); |
| 13 | + var csrfToken = csrfField.value; |
| 14 | + |
| 15 | + if (!csrfCookie && nameCheck.test(csrfToken)) { |
| 16 | + csrfField.setAttribute('data-csrf-protection-cookie-value', csrfCookie = csrfToken); |
| 17 | + csrfField.value = csrfToken = btoa(String.fromCharCode.apply(null, (window.crypto || window.msCrypto).getRandomValues(new Uint8Array(18)))); |
| 18 | + } |
| 19 | + |
| 20 | + if (csrfCookie && tokenCheck.test(csrfToken)) { |
| 21 | + var cookie = csrfCookie + '_' + csrfToken + '=' + csrfCookie + '; path=/; samesite=strict'; |
| 22 | + document.cookie = window.location.protocol === 'https:' ? '__Host-' + cookie + '; secure' : cookie; |
| 23 | + } |
| 24 | +}); |
| 25 | + |
| 26 | +// When @hotwired/turbo handles form submissions, send the CSRF token in a header in addition to a cookie |
| 27 | +// The `framework.csrf_protection.check_header` config option needs to be enabled for the header to be checked |
| 28 | +document.addEventListener('turbo:submit-start', function (event) { |
| 29 | + var csrfField = event.detail.formSubmission.formElement.querySelector('input[data-controller="csrf-protection"]'); |
| 30 | + |
| 31 | + if (!csrfField) { |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + var csrfCookie = csrfField.getAttribute('data-csrf-protection-cookie-value'); |
| 36 | + |
| 37 | + if (tokenCheck.test(csrfField.value) && nameCheck.test(csrfCookie)) { |
| 38 | + event.detail.formSubmission.fetchRequest.headers[csrfCookie] = csrfField.value; |
| 39 | + } |
| 40 | +}); |
| 41 | + |
| 42 | +// When @hotwired/turbo handles form submissions, remove the CSRF cookie once a form has been submitted |
| 43 | +document.addEventListener('turbo:submit-end', function (event) { |
| 44 | + var csrfField = event.detail.formSubmission.formElement.querySelector('input[data-controller="csrf-protection"]'); |
| 45 | + |
| 46 | + if (!csrfField) { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + var csrfCookie = csrfField.getAttribute('data-csrf-protection-cookie-value'); |
| 51 | + |
| 52 | + if (tokenCheck.test(csrfField.value) && nameCheck.test(csrfCookie)) { |
| 53 | + var cookie = csrfCookie + '_' + csrfField.value + '=0; path=/; samesite=strict; max-age=0'; |
| 54 | + |
| 55 | + document.cookie = window.location.protocol === 'https:' ? '__Host-' + cookie + '; secure' : cookie; |
| 56 | + } |
| 57 | +}); |
| 58 | + |
| 59 | +/* stimulusFetch: 'lazy' */ |
| 60 | +export default 'csrf-protection-controller'; |
0 commit comments