Skip to content

Commit 94371fb

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

File tree

8 files changed

+134
-0
lines changed

8 files changed

+134
-0
lines changed

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

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

58
# Note that the session will be started ONLY if you read or write from it.
69
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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
if (!/^[-a-zA-Z0-9_]+$/.test(csrfCookie)) {
15+
return
16+
}
17+
18+
let csrfToken = btoa(String.fromCharCode.apply(null, (window.crypto || window.msCrypto).getRandomValues(new Uint8Array(18))));
19+
csrfField.value = csrfToken;
20+
let cookie = csrfCookie + '_' + csrfToken + '=' + csrfCookie + '; path=/; samesite=strict';
21+
22+
document.cookie = window.location.protocol === 'https:' ? '__Host-' + cookie + '; secure' : cookie;
23+
});
24+
25+
document.addEventListener('turbo:submit-start', function (event) {
26+
let csrfField = event.detail.formSubmission.formElement.querySelector('input[data-controller="csrf-protection"]');
27+
28+
if (csrfField) {
29+
event.detail.formSubmission.fetchRequest.headers[csrfField.getAttribute('data-csrf-protection-cookie')] = csrfField.value;
30+
}
31+
});
32+
33+
document.addEventListener('turbo:submit-end', function (event) {
34+
let csrfField = event.detail.formSubmission.formElement.querySelector('input[data-controller="csrf-protection"]');
35+
36+
if (csrfField) {
37+
let csrfCookie = csrfField.getAttribute('data-csrf-protection-cookie');
38+
let cookie = csrfCookie + '_' + csrfField.value + '=0; path=/; samesite=strict; max-age=0';
39+
40+
document.cookie = window.location.protocol === 'https:' ? '__Host-' + cookie + '; secure' : cookie;
41+
}
42+
});
43+
44+
/* stimulusFetch: 'lazy' */
45+
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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<template>
2+
<div>Hello {{ name }}!</div>
3+
</template>
4+
5+
<script setup>
6+
defineProps({
7+
name: String
8+
});
9+
</script>

symfony/ux-turbo/2.19/manifest.json

Lines changed: 14 additions & 0 deletions
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)