Skip to content

Open-source document signature platform. Not affiliated with DocuSign.

TypeScript

Move the proxy into src so it runs, and fix its policy

Claude19ac46a43406
diff --git a/src/proxy.ts b/src/proxy.ts
index c603b3d..5dabc36 100644
--- a/src/proxy.ts
+++ b/src/proxy.ts
@@ -2,6 +2,8 @@ import type { NextRequest } from "next/server";
import { getCookieCache, getSessionCookie } from "better-auth/cookies";
import { NextResponse } from "next/server";
+import { resolveAuthSecret, resolveAuthUrl } from "@/lib/utils/env";
+
const DEFAULT_PERMISSIONS_POLICY = [
"accelerometer=()",
"camera=()",
@@ -18,27 +20,70 @@ function isProductionEnvironment() {
return process.env.NODE_ENV === "production";
}
+// The comments toolbar that Vercel adds to preview deployments loads from
+// these hosts. They are allowed on previews only, never in production.
+// https://vercel.com/docs/vercel-toolbar/managing-toolbar
+function isVercelPreview() {
+ return process.env.VERCEL === "1" && process.env.VERCEL_ENV === "preview";
+}
+
function buildContentSecurityPolicy() {
- const scriptSrc = [
- "'self'",
- "'unsafe-inline'",
- ...(isProductionEnvironment() ? [] : ["'unsafe-eval'"]),
- ].join(" ");
- const r2ConnectSource = "https://*.r2.cloudflarestorage.com";
-
- return [
- "default-src 'self'",
- "base-uri 'self'",
- "frame-ancestors 'none'",
- "form-action 'self'",
- "object-src 'none'",
- "img-src 'self' data: blob:",
- "font-src 'self' data:",
- `connect-src 'self' ${r2ConnectSource}`,
- "worker-src 'self' blob:",
- "style-src 'self' 'unsafe-inline'",
- `script-src ${scriptSrc}`,
- ].join("; ");
+ const preview = isVercelPreview();
+ const directives: Array<[name: string, sources: Array<string | false>]> = [
+ ["default-src", ["'self'"]],
+ ["base-uri", ["'self'"]],
+ ["frame-ancestors", ["'none'"]],
+ ["form-action", ["'self'"]],
+ ["object-src", ["'none'"]],
+ [
+ "img-src",
+ [
+ "'self'",
+ "data:",
+ "blob:",
+ preview && "https://vercel.live https://vercel.com",
+ ],
+ ],
+ [
+ "font-src",
+ [
+ "'self'",
+ "data:",
+ preview && "https://vercel.live https://assets.vercel.com",
+ ],
+ ],
+ [
+ "connect-src",
+ [
+ "'self'",
+ // The field editor reads the just-uploaded PDF back from a blob: URL.
+ "blob:",
+ preview && "https://vercel.live wss://ws-us3.pusher.com",
+ ],
+ ],
+ ["frame-src", [preview ? "https://vercel.live" : "'none'"]],
+ ["worker-src", ["'self'", "blob:"]],
+ [
+ "style-src",
+ ["'self'", "'unsafe-inline'", preview && "https://vercel.live"],
+ ],
+ [
+ "script-src",
+ [
+ "'self'",
+ "'unsafe-inline'",
+ !isProductionEnvironment() && "'unsafe-eval'",
+ preview && "https://vercel.live",
+ ],
+ ],
+ ];
+
+ return directives
+ .map(
+ ([name, sources]) =>
+ `${name} ${sources.filter((source) => source !== false).join(" ")}`,
+ )
+ .join("; ");
}
function applySecurityHeaders(response: NextResponse, pathname: string) {
@@ -74,10 +119,15 @@ function applySecurityHeaders(response: NextResponse, pathname: string) {
}
async function hasOptimisticSession(request: NextRequest) {
- if (process.env.BETTER_AUTH_SECRET) {
+ // Resolved the same way the auth server does, so this keeps working when
+ // the secret is derived rather than set.
+ const secret = resolveAuthSecret();
+
+ if (secret) {
try {
const session = await getCookieCache(request, {
- secret: process.env.BETTER_AUTH_SECRET,
+ isSecure: resolveAuthUrl()?.startsWith("https://") ?? false,
+ secret,
});
if (session) {
@@ -124,29 +174,6 @@ async function maybeRedirectAuthenticatedAuthRoute(request: NextRequest) {
export async function proxy(request: NextRequest) {
const pathname = request.nextUrl.pathname;
- const forwardedProto = request.headers
- .get("x-forwarded-proto")
- ?.split(",")[0]
- ?.trim();
- const requestProtocol =
- forwardedProto ?? request.nextUrl.protocol.replace(":", "");
- const isLocalHostname =
- request.nextUrl.hostname === "localhost" ||
- request.nextUrl.hostname === "127.0.0.1" ||
- request.nextUrl.hostname === "::1";
-
- if (
- isProductionEnvironment() &&
- !isLocalHostname &&
- requestProtocol !== "https"
- ) {
- const httpsUrl = request.nextUrl.clone();
-
- httpsUrl.protocol = "https:";
-
- return applySecurityHeaders(NextResponse.redirect(httpsUrl), pathname);
- }
-
const authRedirect = await maybeRedirectAuthenticatedAuthRoute(request);
if (authRedirect) {