Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: CSRF cookies allow the use of signatures #88

Merged
merged 1 commit into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/extend/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ module.exports = {
*/
get [CSRF_SECRET]() {
if (this[_CSRF_SECRET]) return this[_CSRF_SECRET];
let { useSession, cookieName, sessionName } = this.app.config.security.csrf;
let { useSession, cookieName, sessionName, cookieOptions = {} } = this.app.config.security.csrf;
// get secret from session or cookie
if (useSession) {
this[_CSRF_SECRET] = this.session[sessionName] || '';
} else {
// cookieName support array. so we can change csrf cookie name smoothly
if (!Array.isArray(cookieName)) cookieName = [ cookieName ];
for (const name of cookieName) {
this[_CSRF_SECRET] = this.cookies.get(name, { signed: false }) || '';
this[_CSRF_SECRET] = this.cookies.get(name, { signed: cookieOptions.signed || false }) || '';
if (this[_CSRF_SECRET]) break;
}
}
Expand Down
4 changes: 4 additions & 0 deletions config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ module.exports = () => {
refererWhiteList: [
// 'eggjs.org'
],
// csrf token's cookie options
cookieOptions: {
signed: false,
},
},

xframe: {
Expand Down
20 changes: 20 additions & 0 deletions test/csrf_cookieDomain.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,24 @@ describe('test/csrf_cookieDomain.test.js', () => {
.expect('Set-Cookie', /csrfToken=[\w\-]+; path=\/; httponly/);
});
});

describe('cookieOptions use signed', () => {
let app;
before(() => {
app = mm.app({
baseDir: 'apps/csrf-cookieOptions-signed',
});
return app.ready();
});
after(() => app.close());

it('should auto set csrfToken and csrfToken.sig with cookie options on GET request', () => {
return app.httpRequest()
.get('/hello')
.set('Host', 'abc.aaaa.ddd.string.com')
.expect('hello csrfToken cookieOptions signed')
.expect(200)
.expect('Set-Cookie', /csrfToken=[\w\-]+; path=\/,csrfToken\.sig=[\w\-]+; path=\//);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

module.exports = app => {
return class Home extends app.Controller {
* index() {
this.ctx.body = 'hello csrfToken cookieOptions signed';
}
};
};
5 changes: 5 additions & 0 deletions test/fixtures/apps/csrf-cookieOptions-signed/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = app => {
app.get('/hello', 'home.index');
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

exports.keys = 'cookie options';

exports.security = {
csrf: {
cookieOptions: {
signed: true
},
},
};
3 changes: 3 additions & 0 deletions test/fixtures/apps/csrf-cookieOptions-signed/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "csrf-cookieOptions-signed"
}
Loading