chore: cleanup cors middleware (#3083)
- Remove some indirections - Make tests self contained - Switch tests to `Deno.test` as the LSP doesn't yet recognise `describe/it`
This commit is contained in:
+81
-90
@@ -1,11 +1,14 @@
|
|||||||
import type { FreshContext } from "../context.ts";
|
import type { FreshContext } from "../context.ts";
|
||||||
import type { MiddlewareFn } from "./mod.ts";
|
import type { MiddlewareFn } from "./mod.ts";
|
||||||
|
|
||||||
export type CORSOptions = {
|
export type CORSOptions<State> = {
|
||||||
origin:
|
origin:
|
||||||
| string
|
| string
|
||||||
| string[]
|
| string[]
|
||||||
| ((requestOrigin: string, ctx: FreshContext) => string | undefined | null);
|
| ((
|
||||||
|
requestOrigin: string,
|
||||||
|
ctx: FreshContext<State>,
|
||||||
|
) => string | undefined | null);
|
||||||
allowMethods?: string[];
|
allowMethods?: string[];
|
||||||
allowHeaders?: string[];
|
allowHeaders?: string[];
|
||||||
maxAge?: number;
|
maxAge?: number;
|
||||||
@@ -51,8 +54,8 @@ export type CORSOptions = {
|
|||||||
* // ];
|
* // ];
|
||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
export function cors<T>(options?: CORSOptions): MiddlewareFn<T> {
|
export function cors<State>(options?: CORSOptions<State>): MiddlewareFn<State> {
|
||||||
const opts: CORSOptions = {
|
const opts: CORSOptions<State> = {
|
||||||
origin: "*",
|
origin: "*",
|
||||||
allowMethods: ["GET", "HEAD", "PUT", "POST", "DELETE", "PATCH"],
|
allowMethods: ["GET", "HEAD", "PUT", "POST", "DELETE", "PATCH"],
|
||||||
allowHeaders: [],
|
allowHeaders: [],
|
||||||
@@ -60,27 +63,10 @@ export function cors<T>(options?: CORSOptions): MiddlewareFn<T> {
|
|||||||
...options,
|
...options,
|
||||||
};
|
};
|
||||||
|
|
||||||
const findAllowOrigin = ((optsOrigin: CORSOptions["origin"]) => {
|
|
||||||
if (typeof optsOrigin === "string") {
|
|
||||||
if (optsOrigin === "*") {
|
|
||||||
return (_requestOrigin: string, _ctx: FreshContext) => optsOrigin;
|
|
||||||
} else {
|
|
||||||
return (requestOrigin: string, _ctx: FreshContext) =>
|
|
||||||
optsOrigin === requestOrigin ? requestOrigin : null;
|
|
||||||
}
|
|
||||||
} else if (typeof optsOrigin === "function") {
|
|
||||||
return (requestOrigin: string, ctx: FreshContext) =>
|
|
||||||
optsOrigin(requestOrigin, ctx);
|
|
||||||
} else {
|
|
||||||
return (requestOrigin: string, _ctx: FreshContext) =>
|
|
||||||
optsOrigin.includes(requestOrigin) ? requestOrigin : null;
|
|
||||||
}
|
|
||||||
})(opts.origin);
|
|
||||||
|
|
||||||
const addHeaderProperties = (
|
const addHeaderProperties = (
|
||||||
headers: Headers,
|
headers: Headers,
|
||||||
allowOrigin: string | null | undefined,
|
allowOrigin: string | null | undefined,
|
||||||
opts: CORSOptions,
|
opts: CORSOptions<State>,
|
||||||
) => {
|
) => {
|
||||||
if (allowOrigin) {
|
if (allowOrigin) {
|
||||||
headers.set("Access-Control-Allow-Origin", allowOrigin);
|
headers.set("Access-Control-Allow-Origin", allowOrigin);
|
||||||
@@ -98,77 +84,82 @@ export function cors<T>(options?: CORSOptions): MiddlewareFn<T> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const OptionsResponse = (
|
const optsOrigin = opts.origin;
|
||||||
ctx: FreshContext,
|
|
||||||
allowOrigin: string | null | undefined,
|
|
||||||
opts: CORSOptions,
|
|
||||||
varyValues: Set<string>,
|
|
||||||
) => {
|
|
||||||
const headers = new Headers();
|
|
||||||
|
|
||||||
addHeaderProperties(
|
return async (ctx) => {
|
||||||
headers,
|
|
||||||
allowOrigin,
|
|
||||||
opts,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (opts.maxAge != null) {
|
|
||||||
headers.set("Access-Control-Max-Age", opts.maxAge.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (opts.allowMethods?.length) {
|
|
||||||
headers.set(
|
|
||||||
"Access-Control-Allow-Methods",
|
|
||||||
opts.allowMethods.join(","),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
let effectiveAllowHeaders = opts.allowHeaders;
|
|
||||||
if (!effectiveAllowHeaders?.length) {
|
|
||||||
const reqHeaders = ctx.req.headers.get(
|
|
||||||
"Access-Control-Request-Headers",
|
|
||||||
);
|
|
||||||
if (reqHeaders) {
|
|
||||||
effectiveAllowHeaders = reqHeaders.split(/\s*,\s*/);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (effectiveAllowHeaders?.length) {
|
|
||||||
headers.set(
|
|
||||||
"Access-Control-Allow-Headers",
|
|
||||||
effectiveAllowHeaders.join(","),
|
|
||||||
);
|
|
||||||
varyValues.add("Access-Control-Request-Headers");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (varyValues.size > 0) {
|
|
||||||
headers.set("Vary", Array.from(varyValues).join(", "));
|
|
||||||
} else {
|
|
||||||
headers.delete("Vary"); // Ensure Vary is not set if no conditions met
|
|
||||||
}
|
|
||||||
|
|
||||||
headers.delete("Content-Length");
|
|
||||||
headers.delete("Content-Type");
|
|
||||||
|
|
||||||
return new Response(null, {
|
|
||||||
status: 204,
|
|
||||||
statusText: "No Content",
|
|
||||||
headers,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
return async (ctx: FreshContext): Promise<Response> => {
|
|
||||||
const requestOrigin = ctx.req.headers.get("origin") || "";
|
const requestOrigin = ctx.req.headers.get("origin") || "";
|
||||||
const allowOrigin = findAllowOrigin(requestOrigin, ctx);
|
|
||||||
|
|
||||||
const varyValues = new Set<string>();
|
let allowOrigin: string | null = null;
|
||||||
|
if (typeof optsOrigin === "string") {
|
||||||
|
if (optsOrigin === "*") {
|
||||||
|
allowOrigin = optsOrigin;
|
||||||
|
} else {
|
||||||
|
allowOrigin = optsOrigin === requestOrigin ? requestOrigin : null;
|
||||||
|
}
|
||||||
|
} else if (typeof optsOrigin === "function") {
|
||||||
|
allowOrigin = optsOrigin(requestOrigin, ctx) ?? null;
|
||||||
|
} else {
|
||||||
|
allowOrigin = optsOrigin.includes(requestOrigin) ? requestOrigin : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const vary = new Set<string>();
|
||||||
// Add 'Origin' to Vary if a specific origin is allowed, not '*'
|
// Add 'Origin' to Vary if a specific origin is allowed, not '*'
|
||||||
if (opts.origin !== "*" && allowOrigin && allowOrigin !== "*") {
|
if (opts.origin !== "*" && allowOrigin && allowOrigin !== "*") {
|
||||||
varyValues.add("Origin");
|
vary.add("Origin");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ctx.req.method === "OPTIONS") {
|
if (ctx.req.method === "OPTIONS") {
|
||||||
return OptionsResponse(ctx, allowOrigin, opts, varyValues);
|
const headers = new Headers();
|
||||||
|
|
||||||
|
addHeaderProperties(
|
||||||
|
headers,
|
||||||
|
allowOrigin,
|
||||||
|
opts,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (opts.maxAge != null) {
|
||||||
|
headers.set("Access-Control-Max-Age", opts.maxAge.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (opts.allowMethods?.length) {
|
||||||
|
headers.set(
|
||||||
|
"Access-Control-Allow-Methods",
|
||||||
|
opts.allowMethods.join(","),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let allowHeaders = opts.allowHeaders;
|
||||||
|
if (!allowHeaders?.length) {
|
||||||
|
const reqHeaders = ctx.req.headers.get(
|
||||||
|
"Access-Control-Request-Headers",
|
||||||
|
);
|
||||||
|
if (reqHeaders) {
|
||||||
|
allowHeaders = reqHeaders.split(/\s*,\s*/);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (allowHeaders?.length) {
|
||||||
|
headers.set(
|
||||||
|
"Access-Control-Allow-Headers",
|
||||||
|
allowHeaders.join(","),
|
||||||
|
);
|
||||||
|
vary.add("Access-Control-Request-Headers");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (vary.size > 0) {
|
||||||
|
headers.set("Vary", Array.from(vary).join(", "));
|
||||||
|
} else {
|
||||||
|
headers.delete("Vary"); // Ensure Vary is not set if no conditions met
|
||||||
|
}
|
||||||
|
|
||||||
|
headers.delete("Content-Length");
|
||||||
|
headers.delete("Content-Type");
|
||||||
|
|
||||||
|
return new Response(null, {
|
||||||
|
status: 204,
|
||||||
|
statusText: "No Content",
|
||||||
|
headers,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// For non-OPTIONS requests
|
// For non-OPTIONS requests
|
||||||
@@ -181,13 +172,13 @@ export function cors<T>(options?: CORSOptions): MiddlewareFn<T> {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Merge our calculated varyValues with any existing Vary from downstream response
|
// Merge our calculated varyValues with any existing Vary from downstream response
|
||||||
if (varyValues.size > 0) {
|
if (vary.size > 0) {
|
||||||
const existingVary = res.headers.get("Vary");
|
const existing = res.headers.get("Vary");
|
||||||
if (existingVary) {
|
if (existing) {
|
||||||
existingVary.split(/\s*,\s*/).forEach((v) => varyValues.add(v));
|
existing.split(/\s*,\s*/).forEach((v) => vary.add(v));
|
||||||
}
|
}
|
||||||
|
|
||||||
res.headers.set("Vary", Array.from(varyValues).join(", "));
|
res.headers.set("Vary", Array.from(vary).join(", "));
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
|
|||||||
+209
-203
@@ -1,249 +1,255 @@
|
|||||||
import { App } from "../app.ts";
|
import { App } from "../app.ts";
|
||||||
import type { FreshContext } from "../context.ts";
|
|
||||||
import { cors } from "./cors.ts";
|
import { cors } from "./cors.ts";
|
||||||
import { describe, it } from "@std/testing/bdd";
|
|
||||||
import { expect } from "@std/expect";
|
import { expect } from "@std/expect";
|
||||||
|
|
||||||
describe("CORS by Middleware", () => {
|
Deno.test("CORS - GET default", async () => {
|
||||||
const app = new App();
|
const handler = new App()
|
||||||
|
.use(cors())
|
||||||
|
.get("/", () => new Response("ok"))
|
||||||
|
.handler();
|
||||||
|
|
||||||
app.all("/api/*", cors());
|
const res = await handler(new Request("https://localhost/"));
|
||||||
|
|
||||||
app.all(
|
expect(res.status).toBe(200);
|
||||||
"/api2/*",
|
expect(res.headers.get("Access-Control-Allow-Origin")).toBe("*");
|
||||||
cors({
|
expect(res.headers.get("Vary")).toBeNull();
|
||||||
origin: "http://example.com",
|
});
|
||||||
allowHeaders: ["X-Custom-Header", "Upgrade-Insecure-Requests"],
|
|
||||||
allowMethods: ["POST", "GET", "OPTIONS"],
|
Deno.test("CORS - Preflight default", async () => {
|
||||||
exposeHeaders: ["Content-Length", "X-Kuma-Revision"],
|
const handler = new App()
|
||||||
maxAge: 600,
|
.use(cors())
|
||||||
credentials: true,
|
.get("/", () => new Response("ok"))
|
||||||
|
.handler();
|
||||||
|
|
||||||
|
const req = new Request("https://localhost/", { method: "OPTIONS" });
|
||||||
|
req.headers.append(
|
||||||
|
"Access-Control-Request-Headers",
|
||||||
|
"X-PINGOTHER, Content-Type",
|
||||||
|
);
|
||||||
|
|
||||||
|
const res = await handler(req);
|
||||||
|
|
||||||
|
expect(res.status).toBe(204);
|
||||||
|
expect(res.statusText).toBe("No Content");
|
||||||
|
expect(res.headers.get("Access-Control-Allow-Methods")?.split(",")[0]).toBe(
|
||||||
|
"GET",
|
||||||
|
);
|
||||||
|
expect(res.headers.get("Access-Control-Allow-Headers")?.split(",")).toEqual(
|
||||||
|
[
|
||||||
|
"X-PINGOTHER",
|
||||||
|
"Content-Type",
|
||||||
|
],
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.test("CORS - Preflight with options", async () => {
|
||||||
|
const handler = new App()
|
||||||
|
.use(
|
||||||
|
cors({
|
||||||
|
origin: "http://example.com",
|
||||||
|
allowHeaders: ["X-Custom-Header", "Upgrade-Insecure-Requests"],
|
||||||
|
allowMethods: ["POST", "GET", "OPTIONS"],
|
||||||
|
exposeHeaders: ["Content-Length", "X-Kuma-Revision"],
|
||||||
|
maxAge: 600,
|
||||||
|
credentials: true,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.get("/", () => new Response("ok"))
|
||||||
|
.handler();
|
||||||
|
|
||||||
|
const res = await handler(
|
||||||
|
new Request("https://localhost/", {
|
||||||
|
method: "OPTIONS",
|
||||||
|
headers: { origin: "http://example.com" },
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
app.all(
|
expect(res.headers.get("Access-Control-Allow-Origin")).toBe(
|
||||||
"/api3/*",
|
"http://example.com",
|
||||||
cors({
|
);
|
||||||
|
expect(res.headers.get("Vary")?.split(/\s*,\s*/)).toEqual(
|
||||||
|
expect.arrayContaining(["Origin"]),
|
||||||
|
);
|
||||||
|
expect(res.headers.get("Access-Control-Allow-Headers")?.split(/\s*,\s*/))
|
||||||
|
.toEqual([
|
||||||
|
"X-Custom-Header",
|
||||||
|
"Upgrade-Insecure-Requests",
|
||||||
|
]);
|
||||||
|
expect(res.headers.get("Access-Control-Allow-Methods")?.split(/\s*,\s*/))
|
||||||
|
.toEqual([
|
||||||
|
"POST",
|
||||||
|
"GET",
|
||||||
|
"OPTIONS",
|
||||||
|
]);
|
||||||
|
expect(res.headers.get("Access-Control-Expose-Headers")?.split(/\s*,\s*/))
|
||||||
|
.toEqual([
|
||||||
|
"Content-Length",
|
||||||
|
"X-Kuma-Revision",
|
||||||
|
]);
|
||||||
|
expect(res.headers.get("Access-Control-Max-Age")).toBe("600");
|
||||||
|
expect(res.headers.get("Access-Control-Allow-Credentials")).toBe("true");
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.test("CORS - Disallow an unmatched origin", async () => {
|
||||||
|
const handler = new App()
|
||||||
|
.use(
|
||||||
|
cors({
|
||||||
|
origin: "http://example.com",
|
||||||
|
allowHeaders: ["X-Custom-Header", "Upgrade-Insecure-Requests"],
|
||||||
|
allowMethods: ["POST", "GET", "OPTIONS"],
|
||||||
|
exposeHeaders: ["Content-Length", "X-Kuma-Revision"],
|
||||||
|
maxAge: 600,
|
||||||
|
credentials: true,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.get("/", () => new Response("ok"))
|
||||||
|
.handler();
|
||||||
|
|
||||||
|
const res = await handler(
|
||||||
|
new Request("https://localhost/", {
|
||||||
|
method: "OPTIONS",
|
||||||
|
headers: { origin: "http://example.net" },
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(res.headers.has("Access-Control-Allow-Origin")).toBeFalsy();
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.test("CORS - Allow multiple origins", async () => {
|
||||||
|
const handler = new App()
|
||||||
|
.use(cors({
|
||||||
origin: [
|
origin: [
|
||||||
"http://example.com",
|
"http://example.com",
|
||||||
"http://example.org",
|
"http://example.org",
|
||||||
"http://example.dev",
|
"http://example.dev",
|
||||||
],
|
],
|
||||||
}),
|
}))
|
||||||
);
|
.get("/", () => new Response("ok"))
|
||||||
|
.handler();
|
||||||
|
|
||||||
app.all(
|
let res = await handler(
|
||||||
"/api4/*",
|
new Request("http://localhost/", {
|
||||||
cors({
|
|
||||||
origin: (
|
|
||||||
origin,
|
|
||||||
) => (origin.endsWith(".example.com") ? origin : "http://example.com"),
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
app.all("/api5/*", cors());
|
|
||||||
|
|
||||||
app.all(
|
|
||||||
"/api6/*",
|
|
||||||
cors({
|
|
||||||
origin: "http://example.com",
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
//
|
|
||||||
app.get("/api/abc", (_ctx: FreshContext) => {
|
|
||||||
return Response.json({ success: true });
|
|
||||||
});
|
|
||||||
|
|
||||||
app.get("/api2/abc", (_ctx: FreshContext) => {
|
|
||||||
return Response.json({ success: true });
|
|
||||||
});
|
|
||||||
|
|
||||||
app.get("/api3/abc", (_ctx: FreshContext) => {
|
|
||||||
return Response.json({ success: true });
|
|
||||||
});
|
|
||||||
|
|
||||||
app.get("/api4/abc", (_ctx: FreshContext) => {
|
|
||||||
return Response.json({ success: true });
|
|
||||||
});
|
|
||||||
|
|
||||||
app.get("/api5/abc", () => {
|
|
||||||
return new Response(JSON.stringify({ success: true }));
|
|
||||||
});
|
|
||||||
|
|
||||||
app.get("/api6/abc", (_ctx: FreshContext) => {
|
|
||||||
return Response.json({ success: true });
|
|
||||||
});
|
|
||||||
|
|
||||||
const handler = app.handler();
|
|
||||||
|
|
||||||
it("GET default", async () => {
|
|
||||||
const res = await handler(
|
|
||||||
new Request("https://localhost/api/abc"),
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(res.status).toBe(200);
|
|
||||||
expect(res.headers.get("Access-Control-Allow-Origin")).toBe("*");
|
|
||||||
expect(res.headers.get("Vary")).toBeNull();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("Preflight default", async () => {
|
|
||||||
const req = new Request("https://localhost/api/abc", { method: "OPTIONS" });
|
|
||||||
req.headers.append(
|
|
||||||
"Access-Control-Request-Headers",
|
|
||||||
"X-PINGOTHER, Content-Type",
|
|
||||||
);
|
|
||||||
|
|
||||||
const res = await handler(req);
|
|
||||||
|
|
||||||
expect(res.status).toBe(204);
|
|
||||||
expect(res.statusText).toBe("No Content");
|
|
||||||
expect(res.headers.get("Access-Control-Allow-Methods")?.split(",")[0]).toBe(
|
|
||||||
"GET",
|
|
||||||
);
|
|
||||||
expect(res.headers.get("Access-Control-Allow-Headers")?.split(",")).toEqual(
|
|
||||||
[
|
|
||||||
"X-PINGOTHER",
|
|
||||||
"Content-Type",
|
|
||||||
],
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("Preflight with options", async () => {
|
|
||||||
const req = new Request("https://localhost/api2/abc", {
|
|
||||||
method: "OPTIONS",
|
|
||||||
headers: { origin: "http://example.com" },
|
|
||||||
});
|
|
||||||
|
|
||||||
const res = await handler(req);
|
|
||||||
|
|
||||||
expect(res.headers.get("Access-Control-Allow-Origin")).toBe(
|
|
||||||
"http://example.com",
|
|
||||||
);
|
|
||||||
expect(res.headers.get("Vary")?.split(/\s*,\s*/)).toEqual(
|
|
||||||
expect.arrayContaining(["Origin"]),
|
|
||||||
);
|
|
||||||
expect(res.headers.get("Access-Control-Allow-Headers")?.split(/\s*,\s*/))
|
|
||||||
.toEqual([
|
|
||||||
"X-Custom-Header",
|
|
||||||
"Upgrade-Insecure-Requests",
|
|
||||||
]);
|
|
||||||
expect(res.headers.get("Access-Control-Allow-Methods")?.split(/\s*,\s*/))
|
|
||||||
.toEqual([
|
|
||||||
"POST",
|
|
||||||
"GET",
|
|
||||||
"OPTIONS",
|
|
||||||
]);
|
|
||||||
expect(res.headers.get("Access-Control-Expose-Headers")?.split(/\s*,\s*/))
|
|
||||||
.toEqual([
|
|
||||||
"Content-Length",
|
|
||||||
"X-Kuma-Revision",
|
|
||||||
]);
|
|
||||||
expect(res.headers.get("Access-Control-Max-Age")).toBe("600");
|
|
||||||
expect(res.headers.get("Access-Control-Allow-Credentials")).toBe("true");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("Disallow an unmatched origin", async () => {
|
|
||||||
const req = new Request("https://localhost/api2/abc", {
|
|
||||||
method: "OPTIONS",
|
|
||||||
headers: { origin: "http://example.net" },
|
|
||||||
});
|
|
||||||
|
|
||||||
const res = await handler(req);
|
|
||||||
expect(res.headers.has("Access-Control-Allow-Origin")).toBeFalsy();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("Allow multiple origins", async () => {
|
|
||||||
let req = new Request("http://localhost/api3/abc", {
|
|
||||||
headers: {
|
headers: {
|
||||||
Origin: "http://example.org",
|
Origin: "http://example.org",
|
||||||
},
|
},
|
||||||
});
|
}),
|
||||||
let res = await handler(req);
|
);
|
||||||
expect(res.headers.get("Access-Control-Allow-Origin")).toBe(
|
expect(res.headers.get("Access-Control-Allow-Origin")).toBe(
|
||||||
"http://example.org",
|
"http://example.org",
|
||||||
);
|
);
|
||||||
|
|
||||||
req = new Request("http://localhost/api3/abc");
|
res = await handler(new Request("http://localhost/"));
|
||||||
res = await handler(req);
|
expect(
|
||||||
expect(
|
res.headers.has("Access-Control-Allow-Origin"),
|
||||||
res.headers.has("Access-Control-Allow-Origin"),
|
"An unmatched origin should be disallowed",
|
||||||
"An unmatched origin should be disallowed",
|
).toBeFalsy();
|
||||||
).toBeFalsy();
|
|
||||||
|
|
||||||
req = new Request("http://localhost/api3/abc", {
|
res = await handler(
|
||||||
|
new Request("http://localhost/api3/abc", {
|
||||||
headers: {
|
headers: {
|
||||||
Referer: "http://example.net/",
|
Referer: "http://example.net/",
|
||||||
},
|
},
|
||||||
});
|
}),
|
||||||
res = await handler(req);
|
);
|
||||||
expect(
|
expect(
|
||||||
res.headers.has("Access-Control-Allow-Origin"),
|
res.headers.has("Access-Control-Allow-Origin"),
|
||||||
"An unmatched origin should be disallowed",
|
"An unmatched origin should be disallowed",
|
||||||
).toBeFalsy();
|
).toBeFalsy();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Allow different Vary header value", async () => {
|
Deno.test("CORS - Allow different Vary header value", async () => {
|
||||||
const req = new Request("http://localhost/api3/abc", {
|
const handler = new App()
|
||||||
|
.use(cors({
|
||||||
|
origin: [
|
||||||
|
"http://example.com",
|
||||||
|
"http://example.org",
|
||||||
|
"http://example.dev",
|
||||||
|
],
|
||||||
|
}))
|
||||||
|
.get("/", () => new Response("ok"))
|
||||||
|
.handler();
|
||||||
|
|
||||||
|
const res = await handler(
|
||||||
|
new Request("http://localhost/", {
|
||||||
headers: {
|
headers: {
|
||||||
Vary: "accept-encoding",
|
Vary: "accept-encoding",
|
||||||
Origin: "http://example.com",
|
Origin: "http://example.com",
|
||||||
},
|
},
|
||||||
});
|
}),
|
||||||
const res = await handler(req);
|
);
|
||||||
|
|
||||||
expect(res.status).toBe(200);
|
expect(res.status).toBe(200);
|
||||||
expect(res.headers.get("Access-Control-Allow-Origin")).toBe(
|
expect(res.headers.get("Access-Control-Allow-Origin")).toBe(
|
||||||
"http://example.com",
|
"http://example.com",
|
||||||
);
|
);
|
||||||
expect(res.headers.get("Vary")).toBe("Origin");
|
expect(res.headers.get("Vary")).toBe("Origin");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Allow origins by function", async () => {
|
Deno.test("CORS - Allow origins by function", async () => {
|
||||||
let req = new Request("http://localhost/api4/abc", {
|
const handler = new App()
|
||||||
|
.use(cors({
|
||||||
|
origin: (
|
||||||
|
origin,
|
||||||
|
) => (origin.endsWith(".example.com") ? origin : "http://example.com"),
|
||||||
|
}))
|
||||||
|
.get("/", () => new Response("ok"))
|
||||||
|
.handler();
|
||||||
|
|
||||||
|
let res = await handler(
|
||||||
|
new Request("http://localhost/", {
|
||||||
headers: {
|
headers: {
|
||||||
Origin: "http://subdomain.example.com",
|
Origin: "http://subdomain.example.com",
|
||||||
},
|
},
|
||||||
});
|
}),
|
||||||
let res = await handler(req);
|
);
|
||||||
expect(res.headers.get("Access-Control-Allow-Origin")).toBe(
|
expect(res.headers.get("Access-Control-Allow-Origin")).toBe(
|
||||||
"http://subdomain.example.com",
|
"http://subdomain.example.com",
|
||||||
);
|
);
|
||||||
|
|
||||||
req = new Request("http://localhost/api4/abc");
|
res = await handler(new Request("http://localhost/"));
|
||||||
res = await handler(req);
|
expect(res.headers.get("Access-Control-Allow-Origin")).toBe(
|
||||||
expect(res.headers.get("Access-Control-Allow-Origin")).toBe(
|
"http://example.com",
|
||||||
"http://example.com",
|
);
|
||||||
);
|
|
||||||
|
|
||||||
req = new Request("http://localhost/api4/abc", {
|
res = await handler(
|
||||||
|
new Request("http://localhost/", {
|
||||||
headers: {
|
headers: {
|
||||||
Referer: "http://evil-example.com/",
|
Referer: "http://evil-example.com/",
|
||||||
},
|
},
|
||||||
});
|
}),
|
||||||
res = await handler(req);
|
);
|
||||||
expect(res.headers.get("Access-Control-Allow-Origin")).toBe(
|
expect(res.headers.get("Access-Control-Allow-Origin")).toBe(
|
||||||
"http://example.com",
|
"http://example.com",
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("With raw Response object", async () => {
|
Deno.test("CORS - With raw Response object", async () => {
|
||||||
const req = new Request("http://localhost/api5/abc");
|
const handler = new App()
|
||||||
const res = await handler(req);
|
.use(cors())
|
||||||
|
.get("/", () => new Response("ok"))
|
||||||
|
.handler();
|
||||||
|
|
||||||
expect(res.headers.get("Access-Control-Allow-Origin")).toBe("*");
|
const res = await handler(new Request("http://localhost/"));
|
||||||
expect(res.headers.get("Vary")).toBeNull();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("Should not return duplicate header values", async () => {
|
expect(res.headers.get("Access-Control-Allow-Origin")).toBe("*");
|
||||||
const req = new Request("http://localhost/api6/abc", {
|
expect(res.headers.get("Vary")).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.test("CORS - Should not return duplicate header values", async () => {
|
||||||
|
const handler = new App()
|
||||||
|
.use(cors({ origin: "http://example.com" }))
|
||||||
|
.get("/", () => new Response("ok"))
|
||||||
|
.handler();
|
||||||
|
|
||||||
|
const res = await handler(
|
||||||
|
new Request("http://localhost/", {
|
||||||
headers: {
|
headers: {
|
||||||
origin: "http://example.com",
|
origin: "http://example.com",
|
||||||
},
|
},
|
||||||
});
|
}),
|
||||||
const res = await handler(req);
|
);
|
||||||
|
|
||||||
expect(res.headers.get("Access-Control-Allow-Origin")).toBe(
|
expect(res.headers.get("Access-Control-Allow-Origin")).toBe(
|
||||||
"http://example.com",
|
"http://example.com",
|
||||||
);
|
);
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user