Skip to content

fix: invalid cookie error#1885

Merged
dherault merged 9 commits into
dherault:masterfrom
blasttoys:master
May 27, 2026
Merged

fix: invalid cookie error#1885
dherault merged 9 commits into
dherault:masterfrom
blasttoys:master

Conversation

@blasttoys

@blasttoys blasttoys commented Apr 29, 2026

Copy link
Copy Markdown
Contributor

Description

added @Hapi cookie generator, to fulfill .state() cookie requirement of @Hapi

Motivation and Context

this change fixes issue #1866 by setting the @Hapi cookie value correctly as per the new @Hapi regexp validation. and setting @Hapi cookie property as per desired.

How Has This Been Tested?

ran the serverless-offline fix with @hapi/statehood 8.2.1, and invalid cookie error is resolved.
Sample cookie used:
cookieID=test-3121359fb6ced7c2ca330e2783be73b748a847f0; Max-Age=1800; Expires=Wed, 29 Apr 2026 09:01:55 GMT; Secure; HttpOnly; Domain=localhost; Path=/

@dherault dherault requested a review from Copilot May 11, 2026 07:15
@dherault

Copy link
Copy Markdown
Owner

Not sure about this. Let's see what copilot has to say. Merging this today if good.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses “invalid cookie” errors introduced by stricter @hapi/statehood cookie validation by changing how Set-Cookie response headers are applied to Hapi responses (using h.state() with properly separated cookie value vs. attributes).

Changes:

  • Added a new generateHapiCookie utility to parse Set-Cookie strings into { name, value, options }.
  • Updated HttpServer response handling to call h.state() using the parsed cookie name/value/options instead of passing attributes inside the value.
  • Exported the new utility via src/utils/index.js.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.

File Description
src/utils/index.js Exports the new generateHapiCookie utility for use by the HTTP server.
src/utils/generateHapiCookie.js Introduces parsing logic to split cookie name/value from attributes and map attributes into Hapi cookie options.
src/events/http/HttpServer.js Uses generateHapiCookie() when translating Lambda Set-Cookie output into Hapi h.state() calls.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/utils/generateHapiCookie.js Outdated
Comment thread src/utils/generateHapiCookie.js Outdated
Comment thread src/utils/generateHapiCookie.js Outdated
Comment thread src/utils/generateHapiCookie.js Outdated
Comment thread src/utils/generateHapiCookie.js Outdated
Comment on lines +2 to +28
const name = cookieString.slice(0, cookieString.indexOf("="));
const value = cookieString.slice(cookieString.indexOf("=") + 1, cookieString.indexOf(";"));

// Parse attributes into a map
const attributes = cookieString
.split(";")
.slice(1) // skip the name=value part
.reduce((acc, part) => {
const [key, val] = part.trim().split("=");
acc[key.trim().toLowerCase()] = val ? val.trim() : true;
return acc;
}, {});

return {
name,
value,
options: {
ttl: attributes["max-age"] ? parseInt(attributes["max-age"]) * 1000 : undefined,
isSecure: attributes["secure"] === true,
isHttpOnly: attributes["httponly"] === true,
path: attributes["path"],
domain: attributes["domain"],
isSameSite: attributes["samesite"] || false,
encoding: "none",
strictHeader: false,
},
};
Comment thread src/events/http/HttpServer.js
@dherault

Copy link
Copy Markdown
Owner

Hi, thank you for the PR.

Please rebase/merge master, pass the tests and address some (if not all) of the review comments.
Sorry for the copilot review, but I felt like this PR could be improved.

@dherault

Copy link
Copy Markdown
Owner

Great job, let's ship it!

@dherault dherault merged commit c457469 into dherault:master May 27, 2026
3 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Version 8.2.1 of @hapijs/statehood make a regression on serverless-offline

3 participants