Skip to content

Commit 0124f0d

Browse files
kanongilMarsup
authored andcommitted
Don't try to set autoValue cookie when state parsing is disabled
Closes #4316
1 parent 2e418c4 commit 0124f0d

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

lib/headers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ exports.state = async function (response) {
7878
}
7979

8080
try {
81-
if (!clearOnly) {
81+
if (request.state && !clearOnly) {
8282
for (const name in request._core.states.cookies) {
8383
const autoValue = request._core.states.cookies[name].autoValue;
8484
if (!autoValue || name in request._states || name in request.state) {

test/state.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,17 @@ describe('state', () => {
197197
expect(res.headers['set-cookie']).to.equal(['always=sweet; Secure; HttpOnly; SameSite=Strict']);
198198
});
199199

200+
it('does not respond with automatic value when state parsing is disabled', async () => {
201+
202+
const server = Hapi.server();
203+
server.route({ method: 'GET', path: '/', handler: () => 'ok', options: { state: { parse: false } } });
204+
server.state('always', { autoValue: 'present' });
205+
206+
const res = await server.inject('/');
207+
expect(res.statusCode).to.equal(200);
208+
expect(res.headers['set-cookie']).to.not.exist();
209+
});
210+
200211
it('returns error 500 response when automatic value throws', async () => {
201212

202213
let called = 0;

0 commit comments

Comments
 (0)