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

fix cookies' secure detect #614

Merged
merged 1 commit into from
Mar 1, 2016
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
5 changes: 4 additions & 1 deletion lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ app.createContext = function(req, res){
response.request = request;
context.onerror = context.onerror.bind(context);
context.originalUrl = request.originalUrl = req.url;
context.cookies = new Cookies(req, res, this.keys);
context.cookies = new Cookies(req, res, {
keys: this.keys,
secure: request.secure
});
context.accept = request.accept = accepts(req);
context.state = {};
return context;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"composition": "^2.1.1",
"content-disposition": "~0.5.0",
"content-type": "^1.0.0",
"cookies": "~0.5.0",
"cookies": "~0.6.1",
"debug": "*",
"delegates": "^1.0.0",
"destroy": "^1.0.3",
Expand Down
2 changes: 2 additions & 0 deletions test/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ exports = module.exports = function(req, res){
var socket = new Stream.Duplex();
req = req || { headers: {}, socket: socket, __proto__: Stream.Readable.prototype };
res = res || { _headers: {}, socket: socket, __proto__: Stream.Writable.prototype };
req.socket = req.socket || socket;
res.socket = res.socket || socket;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exactly does this do? res.socket is already defined as socket 2 lines up, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some test case will init context with specific req without socket like https://github.com/koajs/koa/blob/master/test/request/path.js#L26

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh oops, I missed that. Thanks for the help.

res.getHeader = function(k){ return res._headers[k.toLowerCase()] };
res.setHeader = function(k, v){ res._headers[k.toLowerCase()] = v };
res.removeHeader = function(k, v){ delete res._headers[k.toLowerCase()] };
Expand Down
39 changes: 39 additions & 0 deletions test/context/cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,43 @@ describe('ctx.cookies.set()', function(){
})
})
})

describe('with secure', function(){
it('should get secure from request', function(done){
var app = koa();

app.proxy = true;
app.keys = ['a', 'b'];

app.use(function *(next){
this.cookies.set('name', 'jon', { signed: true });
this.status = 204;
})

var server = app.listen();

request(server)
.get('/')
.set('x-forwarded-proto', 'https') // mock secure
.expect(204)
.end(function(err, res){
if (err) return done(err);

var cookies = res.headers['set-cookie'];
cookies.some(function(cookie){
return /^name=/.test(cookie);
}).should.be.ok;

cookies.some(function(cookie){
return /^name\.sig=/.test(cookie);
}).should.be.ok;

cookies.every(function(cookie){
return /secure/.test(cookie);
}).should.be.ok;

done();
})
})
})
})