Skip to content

Commit a805cc5

Browse files
author
Florian Weber
committed
feat(csrf): csrf can now be disabled
By setting the new configuration parameter 'csrf' to true or false, you are able to disable or enable it.
1 parent b9bd5f4 commit a805cc5

File tree

6 files changed

+22
-15
lines changed

6 files changed

+22
-15
lines changed

packages/config/configs/server.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export default () => ({
22
csrfExclude: [],
3+
csrf: true,
34
createRenderer: {}
45
});

packages/core/server.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import WWW from './www';
1818
export default class Server extends WWW {
1919
constructor(hooks, config) {
2020
super(hooks, config);
21-
this.csrfProtection = csrf({ cookie: true });
2221
this.renderer = null;
2322
this.readyPromise = null;
2423
this.isProd = process.env.NODE_ENV === 'production';
@@ -111,10 +110,13 @@ export default class Server extends WWW {
111110

112111
this.middlewares.push(bodyParser.json());
113112
this.middlewares.push(bodyParser.urlencoded({ extended: false }));
114-
this.middlewares.push((req, res, next) => {
115-
if (indexOf(this.config.csrfExclude, req.path) !== -1) return next();
116-
csrf({ cookie: true })(req, res, next);
117-
});
113+
114+
if (this.config.csrf) {
115+
this.middlewares.push((req, res, next) => {
116+
if (indexOf(this.config.csrfExclude, req.path) !== -1) return next();
117+
csrf({ cookie: true })(req, res, next);
118+
});
119+
}
118120

119121
for (const middleware of this.hooks.middlewares) {
120122
middleware({
@@ -210,7 +212,7 @@ export default class Server extends WWW {
210212
}
211213
});
212214

213-
this.app.get('*', this.csrfProtection, this.isProd ? this.render.bind(this) : (req, res) => {
215+
this.app.get('*', this.isProd ? this.render.bind(this) : (req, res) => {
214216
self.readyPromise.then(() => self.render(req, res));
215217
});
216218
}
@@ -221,10 +223,11 @@ export default class Server extends WWW {
221223
const context = {
222224
title: process.env.APP_NAME,
223225
url: req.url,
224-
csrfToken: req.csrfToken(),
225226
cookies: req.cookies
226227
};
227228

229+
if (this.config.csrf) Object.assign(context, { csrfToken: req.csrfToken() });
230+
228231
if (typeof req.flash === 'function') Object.assign(context, { flash: req.flash() });
229232
if (typeof req.isAuthenticated === 'function') Object.assign(context, { isAuthenticated: req.isAuthenticated(), user: req.user });
230233

packages/renderer/src/builder.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ export default class Builder {
3636
const compiledApp = compiled({
3737
config: {
3838
progressbar: this.globalConfig.progressbar,
39-
i18n: this.globalConfig.i18n
39+
i18n: this.globalConfig.i18n,
40+
csrf: this.globalConfig.csrf
4041
}
4142
});
4243

packages/vue-app/lib/entry-client.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import { createApp } from './app';
2-
import Vue from 'vue';
3-
import axios from 'axios';
4-
import forEach from 'lodash/forEach';
1+
import { createApp } from './app';
2+
import Vue from 'vue';
3+
import axios from 'axios';
4+
import forEach from 'lodash/forEach';
55
const { app, router, store } = createApp({ isServer: false });
66

77
// eslint-disable-next-line no-unused-vars
88
class ClientEntry {
99
constructor() {
1010
this.addMobileCheck();
11-
this.configureCSRF();
11+
<% if (config.csrf) { %> this.configureCSRF(); <% } %>
1212
this.setRouterMixins();
1313
this.initMixin();
1414

@@ -60,6 +60,7 @@ class ClientEntry {
6060
};
6161
}
6262

63+
<% if (config.csrf) { %>
6364
configureCSRF() {
6465
let token = document.querySelector('meta[name="csrf-token"]');
6566

@@ -70,6 +71,7 @@ class ClientEntry {
7071
console.error('CSRF token not found');
7172
}
7273
}
74+
<% } %>
7375

7476
setRouterMixins() {
7577
Vue.mixin({

packages/vue-app/lib/entry-server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { createApp } from './app';
44

55
Vue.prototype.$auth = null;
66
Vue.prototype.$modernizr = {};
7-
Vue.prototype.$csrf = '';
7+
<% if (config.csrf) { %> Vue.prototype.$csrf = ''; <% } %>
88

99
const mixinContext = require.context('@/', false, /^\.\/entry-server\.js$/i);
1010

packages/vue-app/lib/index.template.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!DOCTYPE html>{{ meta.inject() }}
22
<html data-vue-meta-server-rendered {{{ meta.htmlAttrs.text() }}}>
33
<head>
4-
<meta name="csrf-token" content="{{ csrfToken }}">
4+
<% if (config.csrf) { %> <meta name="csrf-token" content="{{ csrfToken }}"> <% } %>
55
{{{ meta.meta.text() + meta.title.text() + meta.link.text() + meta.style.text() + meta.script.text() + meta.noscript.text() }}}
66
</head>
77
<body {{{ meta.bodyAttrs.text() }}}>

0 commit comments

Comments
 (0)