Skip to content

Commit f614c44

Browse files
authored
Merge pull request #1337 from symfony/csrf-double-submit
Enable stateless headers/cookies-based CSRF protection
2 parents 6c43cb8 + 21a9094 commit f614c44

File tree

7 files changed

+142
-0
lines changed

7 files changed

+142
-0
lines changed

symfony/framework-bundle/7.2/config/packages/framework.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ framework:
88
#esi: true
99
#fragments: true
1010

11+
# Enable stateless CSRF protection for forms and logins/logouts
12+
form: { csrf_protection: { token_id: submit } }
13+
csrf_protection:
14+
stateless_token_ids: [submit, authenticate, logout]
15+
1116
when@test:
1217
framework:
1318
test: true
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);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"controllers": [],
3+
"entrypoints": []
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Controller } from '@hotwired/stimulus';
2+
3+
/*
4+
* This is an example Stimulus controller!
5+
*
6+
* Any element with a data-controller="hello" attribute will cause
7+
* this controller to be executed. The name "hello" comes from the filename:
8+
* hello_controller.js -> "hello"
9+
*
10+
* Delete this file or adapt it for your use!
11+
*/
12+
export default class extends Controller {
13+
connect() {
14+
this.element.textContent = 'Hello Stimulus! Edit me in assets/controllers/hello_controller.js';
15+
}
16+
}
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"bundles": {
3+
"Symfony\\UX\\StimulusBundle\\StimulusBundle": ["all"]
4+
},
5+
"copy-from-recipe": {
6+
"assets/": "assets/"
7+
},
8+
"aliases": ["stimulus", "stimulus-bundle"],
9+
"conflict": {
10+
"symfony/framework-bundle": "<7.2",
11+
"symfony/security-csrf": "<7.2",
12+
"symfony/webpack-encore-bundle": "<2.0",
13+
"symfony/flex": "<1.20.0 || >=2.0.0,<2.3.0"
14+
},
15+
"add-lines": [
16+
{
17+
"file": "webpack.config.js",
18+
"content": "\n // enables the Symfony UX Stimulus bridge (used in assets/bootstrap.js)\n .enableStimulusBridge('./assets/controllers.json')",
19+
"position": "after_target",
20+
"target": ".splitEntryChunks()"
21+
},
22+
{
23+
"file": "assets/app.js",
24+
"content": "import './bootstrap.js';",
25+
"position": "top",
26+
"warn_if_missing": true
27+
},
28+
{
29+
"file": "assets/bootstrap.js",
30+
"content": "import { startStimulusApp } from '@symfony/stimulus-bridge';\n\n// Registers Stimulus controllers from controllers.json and in the controllers/ directory\nexport const app = startStimulusApp(require.context(\n '@symfony/stimulus-bridge/lazy-controller-loader!./controllers',\n true,\n /\\.[jt]sx?$/\n));",
31+
"position": "top",
32+
"requires": "symfony/webpack-encore-bundle"
33+
},
34+
{
35+
"file": "assets/bootstrap.js",
36+
"content": "import { startStimulusApp } from '@symfony/stimulus-bundle';\n\nconst app = startStimulusApp();",
37+
"position": "top",
38+
"requires": "symfony/asset-mapper"
39+
}
40+
]
41+
}

symfony/ux-turbo/2.20/manifest.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"conflict": {
3+
"symfony/framework-bundle": "<7.2",
4+
"symfony/security-csrf": "<7.2"
5+
},
6+
"add-lines": [
7+
{
8+
"file": "config/packages/framework.yaml",
9+
"position": "after_target",
10+
"target": " csrf_protection:",
11+
"content": " check_header: true"
12+
}
13+
]
14+
}

0 commit comments

Comments
 (0)