forked from javascript-tutorial/server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsrfCheck.js
More file actions
executable file
·84 lines (62 loc) · 1.77 KB
/
Copy pathcsrfCheck.js
File metadata and controls
executable file
·84 lines (62 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const koaCsrf = require('koa-csrf');
const PathListCheck = require('pathListCheck');
function CsrfChecker() {
this.ignore = new PathListCheck();
}
CsrfChecker.prototype.middleware = function() {
var self = this;
return function*(next) {
// skip these methods
if (this.method === 'GET' || this.method === 'HEAD' || this.method === 'OPTIONS') {
return yield* next;
}
var checkCsrf = true;
if (!this.user) {
checkCsrf = false;
}
if (self.ignore.check(this.path)) {
checkCsrf = false;
}
// If test check CSRF only when "X-Test-Csrf" header is set
if (process.env.NODE_ENV == 'test') {
if (!this.get('X-Test-Csrf')) {
checkCsrf = false;
}
}
if (checkCsrf) {
this.assertCSRF(this.request.body);
} else {
this.log.debug("csrf skip");
}
yield* next;
};
};
// every request gets different this._csrf to use in POST
// but ALL tokens are valid
exports.init = function(app) {
koaCsrf(app);
app.csrfChecker = new CsrfChecker();
app.use(app.csrfChecker.middleware());
app.use(function*(next) {
try {
// first, do the middleware, maybe authorize user in the process
yield* next;
} finally {
// then if we have a user, set XSRF token
if (this.req.user) {
setCsrfCookie.call(this);
}
}
});
};
// XSRF-TOKEN cookie name is used in angular by default
function setCsrfCookie() {
try {
// if this doesn't throw, the user has a valid token in cookie already
this.assertCsrf({_csrf: this.cookies.get('XSRF-TOKEN') });
} catch(e) {
// error occurs if no token or invalid token (old session)
// then we set a new (valid) one
this.cookies.set('XSRF-TOKEN', this.csrf, { httpOnly: false, signed: false });
}
}