Skip to content

Commit b97b2b2

Browse files
authored
feat: csrf cookie support cookieOptions (#80)
1 parent 742f3f2 commit b97b2b2

File tree

9 files changed

+54
-3
lines changed

9 files changed

+54
-3
lines changed

.github/workflows/nodejs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
node-version: ${{ matrix.node-version }}
3636

3737
- name: Install Dependencies
38-
run: npm i -g npminstall && npminstall
38+
run: npm i -g npminstall@5 && npminstall
3939

4040
- name: Continuous Integration
4141
run: npm run ci

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ run/
88
.vscode
99
package-lock.json
1010
.travis.yml
11+
.idea

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ exports.security = {
219219
supportedRequests: [ // supported URL path and method, the package will match URL path regex patterns one by one until path matched. We recommend you set {path: /^\//, methods:['POST','PATCH','DELETE','PUT','CONNECT']} as the last rule in the list, which is also the default config.
220220
{path: /^\//, methods:['POST','PATCH','DELETE','PUT','CONNECT']}
221221
],
222+
cookieOptions: {}, // csrf token's cookie options
222223
},
223224
}
224225
```

app/extend/context.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,18 @@ module.exports = {
105105
debug('ensure csrf secret, exists: %s, rotate; %s', this[CSRF_SECRET], rotate);
106106
const secret = tokens.secretSync();
107107
this[NEW_CSRF_SECRET] = secret;
108-
let { useSession, sessionName, cookieDomain, cookieName } = this.app.config.security.csrf;
108+
let { useSession, sessionName, cookieDomain, cookieName, cookieOptions = {} } = this.app.config.security.csrf;
109109

110110
if (useSession) {
111111
this.session[sessionName] = secret;
112112
} else {
113-
const cookieOpts = {
113+
const defaultOpts = {
114114
domain: cookieDomain && cookieDomain(this),
115115
signed: false,
116116
httpOnly: false,
117117
overwrite: true,
118118
};
119+
const cookieOpts = utils.merge(defaultOpts, cookieOptions);
119120
// cookieName support array. so we can change csrf cookie name smoothly
120121
if (!Array.isArray(cookieName)) cookieName = [ cookieName ];
121122
for (const name of cookieName) {

test/csrf_cookieDomain.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,24 @@ describe('test/csrf_cookieDomain.test.js', () => {
4444
.expect('Set-Cookie', /csrfToken=[\w\-]+; path=\/; domain=\.string\.com/);
4545
});
4646
});
47+
48+
describe('cookieOptions = object', () => {
49+
let app;
50+
before(() => {
51+
app = mm.app({
52+
baseDir: 'apps/csrf-cookieOptions',
53+
});
54+
return app.ready();
55+
});
56+
after(() => app.close());
57+
58+
it('should auto set csrfToken with cookie options on GET request', () => {
59+
return app.httpRequest()
60+
.get('/hello')
61+
.set('Host', 'abc.aaaa.ddd.string.com')
62+
.expect('hello csrfToken cookieOptions')
63+
.expect(200)
64+
.expect('Set-Cookie', /csrfToken=[\w\-]+; path=\/; httponly/);
65+
});
66+
});
4767
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
module.exports = app => {
4+
return class Home extends app.Controller {
5+
* index() {
6+
this.ctx.body = 'hello csrfToken cookieOptions';
7+
}
8+
};
9+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
module.exports = app => {
4+
app.get('/hello', 'home.index');
5+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
exports.keys = 'cookie options';
4+
5+
exports.security = {
6+
csrf: {
7+
cookieOptions: {
8+
httpOnly: true,
9+
},
10+
},
11+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "csrf-cookieOptions"
3+
}

0 commit comments

Comments
 (0)