Skip to content

Commit

Permalink
fix(cors): add cookie to vary if credentials option is true
Browse files Browse the repository at this point in the history
  • Loading branch information
aaharu committed Oct 10, 2024
1 parent 92031aa commit 1825940
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/types/utils/cors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export type H3AccessControlAllowOriginHeader =
| {
"access-control-allow-origin": "*";
}
| {
"access-control-allow-origin": "*";
vary: "cookie, origin";
}
| {
"access-control-allow-origin": "null" | string;
vary: "origin";
Expand Down
8 changes: 6 additions & 2 deletions src/utils/internal/cors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,12 @@ export function createOriginHeaders(
const { origin: originOption, credentials } = options;
const origin = event.request.headers.get("origin");

if ((!originOption || originOption === "*") && !credentials) {
return { "access-control-allow-origin": "*" };
if (!originOption || originOption === "*") {
if (!credentials) {
return { "access-control-allow-origin": "*" };
}
// https://w3c.github.io/webappsec-cors-for-developers/#use-vary
return { "access-control-allow-origin": "*", vary: "cookie, origin" };
}

if (originOption === "null") {
Expand Down
4 changes: 2 additions & 2 deletions test/unit/cors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ describe("cors (unit)", () => {
};

expect(createOriginHeaders(eventMock, options)).toEqual({
"access-control-allow-origin": "https://example.com",
vary: "origin",
"access-control-allow-origin": "*",
vary: "cookie, origin",
});
});

Expand Down

0 comments on commit 1825940

Please sign in to comment.