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