Skip to content

Commit

Permalink
fix(cors)!: doesn't return access-control-allow-origin header when …
Browse files Browse the repository at this point in the history
…dynamic
  • Loading branch information
aaharu committed Oct 4, 2024
1 parent 7324eee commit db0bb01
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 20 deletions.
14 changes: 8 additions & 6 deletions src/utils/internal/cors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,19 @@ export function createOriginHeaders(
const { origin: originOption } = options;
const origin = event.request.headers.get("origin");

if (!origin || !originOption || originOption === "*") {
if (!originOption || originOption === "*") {
return { "access-control-allow-origin": "*" };
}

if (typeof originOption === "string") {
return { "access-control-allow-origin": originOption, vary: "origin" };
if (originOption === "null") {
return { "access-control-allow-origin": "null", vary: "origin" };
}

return isCorsOriginAllowed(origin, options)
? { "access-control-allow-origin": origin, vary: "origin" }
: {};
if (origin && isCorsOriginAllowed(origin, options)) {
return { "access-control-allow-origin": origin, vary: "origin" };
}

return {};
}

/**
Expand Down
54 changes: 40 additions & 14 deletions test/unit/cors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,33 +180,35 @@ describe("cors (unit)", () => {

describe("createOriginHeaders", () => {
it('returns an object whose `access-control-allow-origin` is `"*"` if `origin` option is not defined, or `"*"`', () => {
const eventMock = mockEvent("/", {
const hasOriginEventMock = mockEvent("/", {
method: "OPTIONS",
headers: {
origin: "https://example.com",
},
});
const options1: H3CorsOptions = {};
const options2: H3CorsOptions = {
const noOriginEventMock = mockEvent("/", {
method: "OPTIONS",
headers: {},
});
const defaultOptions: H3CorsOptions = {};
const originWildcardOptions: H3CorsOptions = {
origin: "*",
};

expect(createOriginHeaders(eventMock, options1)).toEqual({
expect(createOriginHeaders(hasOriginEventMock, defaultOptions)).toEqual({
"access-control-allow-origin": "*",
});
expect(createOriginHeaders(eventMock, options2)).toEqual({
expect(
createOriginHeaders(hasOriginEventMock, originWildcardOptions),
).toEqual({
"access-control-allow-origin": "*",
});
});

it('returns an object whose `access-control-allow-origin` is `"*"` if `origin` header is not defined', () => {
const eventMock = mockEvent("/", {
method: "OPTIONS",
headers: {},
expect(createOriginHeaders(noOriginEventMock, defaultOptions)).toEqual({
"access-control-allow-origin": "*",
});
const options: H3CorsOptions = {};

expect(createOriginHeaders(eventMock, options)).toEqual({
expect(
createOriginHeaders(noOriginEventMock, originWildcardOptions),
).toEqual({
"access-control-allow-origin": "*",
});
});
Expand Down Expand Up @@ -235,6 +237,12 @@ describe("cors (unit)", () => {
origin: "http://example.com",
},
});
const noMatchEventMock = mockEvent("/", {
method: "OPTIONS",
headers: {
origin: "http://example.test",
},
});
const options1: H3CorsOptions = {
origin: ["http://example.com"],
};
Expand All @@ -246,10 +254,12 @@ describe("cors (unit)", () => {
"access-control-allow-origin": "http://example.com",
vary: "origin",
});
expect(createOriginHeaders(noMatchEventMock, options1)).toEqual({});
expect(createOriginHeaders(eventMock, options2)).toEqual({
"access-control-allow-origin": "http://example.com",
vary: "origin",
});
expect(createOriginHeaders(noMatchEventMock, options2)).toEqual({});
});

it("returns an empty object if `origin` option is one that is not allowed", () => {
Expand All @@ -269,6 +279,22 @@ describe("cors (unit)", () => {
expect(createOriginHeaders(eventMock, options1)).toEqual({});
expect(createOriginHeaders(eventMock, options2)).toEqual({});
});

it("returns an empty object if `origin` option is not wildcard and `origin` header is not defined", () => {
const eventMock = mockEvent("/", {
method: "OPTIONS",
headers: {},
});
const options1: H3CorsOptions = {
origin: ["http://example.com"],
};
const options2: H3CorsOptions = {
origin: () => false,
};

expect(createOriginHeaders(eventMock, options1)).toEqual({});
expect(createOriginHeaders(eventMock, options2)).toEqual({});
});
});

describe("createMethodsHeaders", () => {
Expand Down

0 comments on commit db0bb01

Please sign in to comment.