Skip to content

Commit dc6a452

Browse files
Enable double-submit CSRF protection
1 parent c26ec2b commit dc6a452

File tree

6 files changed

+100
-0
lines changed

6 files changed

+100
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# see https://symfony.com/doc/current/reference/configuration/framework.html
22
framework:
33
secret: '%env(APP_SECRET)%'
4+
form:
5+
csrf_protection: x-csrf-token
46

57
# Note that the session will be started ONLY if you read or write from it.
68
session: true
Lines changed: 2 additions & 0 deletions
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);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"controllers": [],
3+
"entrypoints": []
4+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
document.addEventListener('submit', function (event) {
2+
let csrfField = event.target.querySelector('input[data-controller="csrf-protection"]');
3+
4+
if (!csrfField) {
5+
return;
6+
}
7+
8+
let csrfCookie = csrfField.getAttribute('data-csrf-protection-cookie');
9+
10+
if (!csrfCookie) {
11+
csrfField.setAttribute('data-csrf-protection-cookie', csrfCookie = csrfField.value);
12+
}
13+
14+
15+
if (!/^[-a-zA-Z0-9_]+$/.test(csrfCookie)) {
16+
return
17+
}
18+
19+
let csrfToken = btoa(String.fromCharCode.apply(null, (window.crypto || window.msCrypto).getRandomValues(new Uint8Array(18))));
20+
csrfField.value = csrfToken;
21+
let cookie = csrfCookie + '_' + csrfToken + '=' + csrfCookie + '; path=/; samesite=strict';
22+
23+
document.cookie = window.location.protocol === 'https:' ? '__Host-' + cookie + '; secure' : cookie;
24+
});
25+
26+
document.addEventListener('turbo:submit-start', function (event) {
27+
let csrfField = event.detail.formSubmission.formElement.querySelector('input[data-controller="csrf-protection"]');
28+
29+
if (csrfField) {
30+
event.detail.formSubmission.fetchRequest.headers[csrfField.getAttribute('data-csrf-protection-cookie')] = csrfField.value;
31+
}
32+
});
33+
34+
/* stimulusFetch: 'lazy' */
35+
export default 'csrf-protection-controller';
Lines changed: 16 additions & 0 deletions
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+
}
Lines changed: 41 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)