Skip to content

Commit 638fdd5

Browse files
Enable stateless CSRF protection for forms, logins and logouts
1 parent c26ec2b commit 638fdd5

File tree

7 files changed

+135
-0
lines changed

7 files changed

+135
-0
lines changed

Diff for: 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

Diff for: symfony/stimulus-bundle/2.20/assets/bootstrap.js

+2
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);

Diff for: symfony/stimulus-bundle/2.20/assets/controllers.json

+4
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,53 @@
1+
document.addEventListener('submit', function (event) {
2+
var csrfField = event.target.querySelector('input[data-controller="csrf-protection"]');
3+
4+
if (!csrfField) {
5+
return;
6+
}
7+
8+
var csrfCookie = csrfField.getAttribute('data-csrf-protection-cookie-value');
9+
var csrfToken = csrfField.value;
10+
11+
if (!csrfCookie && /^[-_a-zA-Z0-9]{4,22}$/.test(csrfToken)) {
12+
csrfField.setAttribute('data-csrf-protection-cookie-value', csrfCookie = csrfToken);
13+
csrfField.value = csrfToken = btoa(String.fromCharCode.apply(null, (window.crypto || window.msCrypto).getRandomValues(new Uint8Array(18))));
14+
}
15+
16+
if (csrfCookie && /^[-_/+a-zA-Z0-9]{24,}$/.test(csrfToken)) {
17+
var cookie = csrfCookie + '_' + csrfToken + '=' + csrfCookie + '; path=/; samesite=strict';
18+
document.cookie = window.location.protocol === 'https:' ? '__Host-' + cookie + '; secure' : cookie;
19+
}
20+
});
21+
22+
document.addEventListener('turbo:submit-start', function (event) {
23+
var csrfField = event.detail.formSubmission.formElement.querySelector('input[data-controller="csrf-protection"]');
24+
25+
if (!csrfField) {
26+
return;
27+
}
28+
29+
var csrfCookie = csrfField.getAttribute('data-csrf-protection-cookie-value');
30+
31+
if (/^[-_/+a-zA-Z0-9]{24,}$/.test(csrfField.value) && /^[-_a-zA-Z0-9]{4,22}$/.test(csrfCookie)) {
32+
event.detail.formSubmission.fetchRequest.headers[csrfCookie] = csrfField.value;
33+
}
34+
});
35+
36+
document.addEventListener('turbo:submit-end', function (event) {
37+
var csrfField = event.detail.formSubmission.formElement.querySelector('input[data-controller="csrf-protection"]');
38+
39+
if (!csrfField) {
40+
return;
41+
}
42+
43+
var csrfCookie = csrfField.getAttribute('data-csrf-protection-cookie-value');
44+
45+
if (/^[-_/+a-zA-Z0-9]{24,}$/.test(csrfField.value) && /^[-_a-zA-Z0-9]{4,22}$/.test(csrfCookie)) {
46+
var cookie = csrfCookie + '_' + csrfField.value + '=0; path=/; samesite=strict; max-age=0';
47+
48+
document.cookie = window.location.protocol === 'https:' ? '__Host-' + cookie + '; secure' : cookie;
49+
}
50+
});
51+
52+
/* stimulusFetch: 'lazy' */
53+
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+
}

Diff for: symfony/stimulus-bundle/2.20/manifest.json

+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+
}

Diff for: 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)