csrf_protection_controller.js 3.1 KB

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