MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / withCorsHeaders

Function withCorsHeaders

apps/local/src/serve.ts:292–317  ·  view source on GitHub ↗
(
  req: Request,
  response: Response,
  allowedHosts: ReadonlySet<string>,
)

Source from the content-addressed store, hash-verified

290} as const;
291
292const withCorsHeaders = (
293 req: Request,
294 response: Response,
295 allowedHosts: ReadonlySet<string>,
296): Response => {
297 const origin = req.headers.get("origin");
298 // Same-origin requests carry no Origin header — nothing to do. Cross-origin
299 // requests only get credentialed CORS if their Origin is an allowed loopback
300 // host; an arbitrary web page (e.g. https://evil.example) gets no ACAO, so
301 // the browser blocks it reading the response even if it knew the token.
302 if (!origin || !isAllowedOrigin(origin, allowedHosts)) return response;
303 const headers = new Headers(response.headers);
304 headers.set("access-control-allow-origin", origin);
305 for (const [key, value] of Object.entries(corsHeaders)) headers.set(key, value);
306 headers.set(
307 "access-control-allow-headers",
308 req.headers.get("access-control-request-headers") ??
309 corsHeaders["access-control-allow-headers"],
310 );
311 headers.append("vary", "Origin");
312 return new Response(response.body, {
313 status: response.status,
314 statusText: response.statusText,
315 headers,
316 });
317};
318
319const corsPreflightResponse = (req: Request, allowedHosts: ReadonlySet<string>): Response =>
320 withCorsHeaders(req, new Response(null, { status: 204 }), allowedHosts);

Callers 2

corsPreflightResponseFunction · 0.85
withCorsFunction · 0.85

Calls 4

isAllowedOriginFunction · 0.90
getMethod · 0.65
setMethod · 0.65
appendMethod · 0.65

Tested by

no test coverage detected