Files
outline/server/middlewares/csp.ts
T
Tom MoorandClaude Opus 5.5 00b124d03c chore: Rename APIContext to Context and flatten its fields (#14684)
* chore: Rename APIContext to Context and flatten its fields

Auth and transaction now live directly on the context instead of in
ctx.state and a nested ctx.context, and model helpers read them directly.
APIContext remains as a deprecated alias.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* chore: Remove deprecated APIContext alias

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* fix: Read auth from context in OAuth provider callbacks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* chore: Add withUser and withTransaction context helpers

Replaces manual createContext rebuilds that copy fields from an existing context.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* chore: Remove arbitrary property access from Koa context types

AppContext and Context no longer inherit the any index signature from
Koa's DefaultContext, custom fields are declared explicitly, and importing
Koa's loose Context type is disallowed by lint.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-23 23:17:06 -05:00

186 lines
5.8 KiB
TypeScript

import crypto from "node:crypto";
import type { Next } from "koa";
import helmet from "koa-helmet";
import { asyncNoop } from "es-toolkit";
import { uniq } from "es-toolkit/compat";
import env from "@server/env";
import type { AppContext } from "@server/types";
const getBucketOrigins = (): string[] => {
if (env.AWS_S3_ACCELERATE_URL) {
return [new URL(env.AWS_S3_ACCELERATE_URL).origin];
}
const url = env.AWS_S3_UPLOAD_BUCKET_URL || "";
if (!url) {
return [];
}
try {
const parsedUrl = new URL(url);
const bucket = env.AWS_S3_UPLOAD_BUCKET_NAME;
const host =
bucket && parsedUrl.host.startsWith(`${bucket}.`)
? parsedUrl.host.substring(bucket.length + 1) // +1 for the dot
: parsedUrl.host;
const origins = [`${parsedUrl.protocol}//${host}`];
// Without path-style the S3 client signs urls on the bucket subdomain.
if (bucket && !env.AWS_S3_FORCE_PATH_STYLE) {
origins.push(`${parsedUrl.protocol}//${bucket}.${host}`);
}
return origins;
} catch {
return [];
}
};
const getCloudFrontOrigin = () => {
if (!env.AWS_CLOUDFRONT_URL) {
return;
}
try {
return new URL(env.AWS_CLOUDFRONT_URL).origin;
} catch {
return;
}
};
interface CSPOptions {
/** Additional origins to allow as script sources. */
extraScriptSrc?: string[];
}
/**
* Allow additional origins as script sources in the Content Security Policy of
* the current response. Must be called before the response is sent.
*
* @param ctx The Koa context of the current request.
* @param sources The origins to allow.
*/
export function allowScriptSrc(ctx: AppContext, sources: string[]) {
ctx.state.cspScriptSrc = [...(ctx.state.cspScriptSrc ?? []), ...sources];
}
/**
* Allow additional origins as style sources in the Content Security Policy of
* the current response. Must be called before the response is sent.
*
* @param ctx The Koa context of the current request.
* @param sources The origins to allow.
*/
export function allowStyleSrc(ctx: AppContext, sources: string[]) {
ctx.state.cspStyleSrc = [...(ctx.state.cspStyleSrc ?? []), ...sources];
}
/**
* Allow the current response to be rendered in a frame on any origin. Must be
* called before the response is sent.
*
* @param ctx The Koa context of the current request.
*/
export function allowFraming(ctx: AppContext) {
ctx.state.cspAllowFraming = true;
ctx.remove("X-Frame-Options");
}
/**
* Create a Content Security Policy middleware for the application.
*
* @param options Optional configuration for the CSP middleware.
* @returns A Koa middleware function that applies the CSP headers.
*/
export default function createCSPMiddleware(options?: CSPOptions) {
// Construct scripts CSP based on options in use
const defaultSrc: string[] = ["'self'"];
const scriptSrc: string[] = [];
const styleSrc: string[] = ["'self'", "'unsafe-inline'"];
const objectSrc: string[] = [env.URL, "'self'"];
if (env.isCloudHosted) {
scriptSrc.push("www.googletagmanager.com");
scriptSrc.push("cdn.zapier.com");
styleSrc.push("cdn.zapier.com");
}
// Allow to load assets from Vite
if (!env.isProduction) {
scriptSrc.push(env.URL.replace(`:${env.PORT}`, `:${env.vitePort}`));
scriptSrc.push(`localhost:${env.vitePort}`);
} else {
scriptSrc.push(env.URL);
}
if (env.GOOGLE_ANALYTICS_ID) {
scriptSrc.push("www.googletagmanager.com");
scriptSrc.push("www.google-analytics.com");
}
if (env.CDN_URL) {
scriptSrc.push(env.CDN_URL);
styleSrc.push(env.CDN_URL);
defaultSrc.push(env.CDN_URL);
}
objectSrc.push(...getBucketOrigins());
const cloudFrontOrigin = getCloudFrontOrigin();
if (cloudFrontOrigin) {
objectSrc.push(cloudFrontOrigin);
}
return async function cspMiddleware(ctx: AppContext, next: Next) {
ctx.state.cspNonce = crypto.randomBytes(16).toString("hex");
ctx.state.cspScriptSrc = [];
ctx.state.cspStyleSrc = [];
try {
await next();
} finally {
// The policy is written once downstream middleware has had the chance to
// allow additional sources for the response, see `allowScriptSrc` and
// `allowStyleSrc`.
if (!ctx.res.headersSent) {
// The service worker is served from the same origin as the document,
// which may be a custom domain that is not present in scriptSrc.
const directives = {
baseUri: ["'none'"],
defaultSrc,
styleSrc: uniq([...styleSrc, ...(ctx.state.cspStyleSrc as string[])]),
scriptSrc: uniq([
...scriptSrc,
// Allow the service worker to importScripts the workbox runtime,
// which is served under /static on the document host.. Scoped to
// the /static path so only immutable build assets are permitted,
// not ugc served elsewhere on the same origin.
`${ctx.host}/static/`,
...(options?.extraScriptSrc ?? []),
...(ctx.state.cspScriptSrc as string[]),
env.DEVELOPMENT_UNSAFE_INLINE_CSP
? "'unsafe-inline'"
: `'nonce-${ctx.state.cspNonce}'`,
]),
mediaSrc: ["*", "data:", "blob:"],
imgSrc: ["*", "data:", "blob:"],
frameSrc: ["*", "data:"],
workerSrc: ["'self'"],
objectSrc,
// Do not use connect-src: because self + websockets does not work in
// Safari, ref: https://bugs.webkit.org/show_bug.cgi?id=201591
connectSrc: ["*"],
...(ctx.state.cspAllowFraming ? {} : { frameAncestors: ["'self'"] }),
};
// Helmet's default directives are not merged in, they include
// form-action restrictions that break OAuth redirects.
await helmet.contentSecurityPolicy({ useDefaults: false, directives })(
ctx,
asyncNoop
);
}
}
};
}