Skip to content

Open-source status page, incident response and uptime monitoring

chore: add trpc source guard

Maximilian Kaske2a7f75c1481a
diff --git a/apps/dashboard/src/app/api/trpc/edge/[trpc]/route.ts b/apps/dashboard/src/app/api/trpc/edge/[trpc]/route.ts
index ee69b118c..078d4c4d8 100644
--- a/apps/dashboard/src/app/api/trpc/edge/[trpc]/route.ts
+++ b/apps/dashboard/src/app/api/trpc/edge/[trpc]/route.ts
@@ -2,13 +2,17 @@ import { fetchRequestHandler } from "@trpc/server/adapters/fetch";
import type { NextRequest } from "next/server";
import { auth } from "@/lib/auth";
+import { guardTRPCSource } from "@/lib/trpc/shared";
import { createTRPCContext } from "@openstatus/api";
import { edgeRouter } from "@openstatus/api/src/edge";
export const runtime = "edge";
-const handler = (req: NextRequest) =>
- fetchRequestHandler({
+const handler = (req: NextRequest) => {
+ const blocked = guardTRPCSource(req);
+ if (blocked) return blocked;
+
+ return fetchRequestHandler({
endpoint: "/api/trpc/edge",
router: edgeRouter,
req: req,
@@ -18,5 +22,6 @@ const handler = (req: NextRequest) =>
console.error(error);
},
});
+};
export { handler as GET, handler as POST };
diff --git a/apps/dashboard/src/app/api/trpc/lambda/[trpc]/route.ts b/apps/dashboard/src/app/api/trpc/lambda/[trpc]/route.ts
index 1352dcf0c..3192adb41 100644
--- a/apps/dashboard/src/app/api/trpc/lambda/[trpc]/route.ts
+++ b/apps/dashboard/src/app/api/trpc/lambda/[trpc]/route.ts
@@ -2,14 +2,18 @@ import { fetchRequestHandler } from "@trpc/server/adapters/fetch";
import type { NextRequest } from "next/server";
import { auth } from "@/lib/auth";
+import { guardTRPCSource } from "@/lib/trpc/shared";
import { createTRPCContext } from "@openstatus/api";
import { lambdaRouter } from "@openstatus/api/src/lambda";
// Stripe is incompatible with Edge runtimes due to using Node.js events
// export const runtime = "edge";
-const handler = (req: NextRequest) =>
- fetchRequestHandler({
+const handler = (req: NextRequest) => {
+ const blocked = guardTRPCSource(req);
+ if (blocked) return blocked;
+
+ return fetchRequestHandler({
endpoint: "/api/trpc/lambda",
router: lambdaRouter,
req: req,
@@ -19,5 +23,6 @@ const handler = (req: NextRequest) =>
console.error(error);
},
});
+};
export { handler as GET, handler as POST };
diff --git a/apps/dashboard/src/lib/trpc/shared.ts b/apps/dashboard/src/lib/trpc/shared.ts
index 23af94147..74a88d12d 100644
--- a/apps/dashboard/src/lib/trpc/shared.ts
+++ b/apps/dashboard/src/lib/trpc/shared.ts
@@ -4,6 +4,19 @@ import { httpBatchLink } from "@trpc/client";
import type { AppRouter } from "@openstatus/api";
import superjson from "superjson";
+/**
+ * Reject requests that don't come from our tRPC clients.
+ * Our server and client links always set `x-trpc-source`.
+ * Returns a 401 Response if the header is missing, or null if valid.
+ */
+export function guardTRPCSource(req: Request): Response | null {
+ const source = req.headers.get("x-trpc-source");
+ if (source !== "server" && source !== "client") {
+ return new Response(null, { status: 401 });
+ }
+ return null;
+}
+
const getBaseUrl = () => {
if (typeof window !== "undefined") return "";
// Note: dashboard has its own tRPC API routes
diff --git a/apps/status-page/src/app/api/trpc/edge/[trpc]/route.ts b/apps/status-page/src/app/api/trpc/edge/[trpc]/route.ts
index cf4f2382b..a30251986 100644
--- a/apps/status-page/src/app/api/trpc/edge/[trpc]/route.ts
+++ b/apps/status-page/src/app/api/trpc/edge/[trpc]/route.ts
@@ -2,19 +2,23 @@ import { fetchRequestHandler } from "@trpc/server/adapters/fetch";
import type { NextRequest } from "next/server";
import { auth } from "@/lib/auth";
-import { createOnError } from "@/lib/trpc/shared";
+import { createOnError, guardTRPCSource } from "@/lib/trpc/shared";
import { createTRPCContext } from "@openstatus/api";
import { edgeRouter } from "@openstatus/api/src/edge";
export const runtime = "edge";
-const handler = (req: NextRequest) =>
- fetchRequestHandler({
+const handler = (req: NextRequest) => {
+ const blocked = guardTRPCSource(req);
+ if (blocked) return blocked;
+
+ return fetchRequestHandler({
endpoint: "/api/trpc/edge",
router: edgeRouter,
req: req,
createContext: () => createTRPCContext({ req, auth }),
onError: createOnError(req, "edge"),
});
+};
export { handler as GET, handler as POST };
diff --git a/apps/status-page/src/app/api/trpc/lambda/[trpc]/route.ts b/apps/status-page/src/app/api/trpc/lambda/[trpc]/route.ts
index 416a78262..be9db45d2 100644
--- a/apps/status-page/src/app/api/trpc/lambda/[trpc]/route.ts
+++ b/apps/status-page/src/app/api/trpc/lambda/[trpc]/route.ts
@@ -2,20 +2,24 @@ import { fetchRequestHandler } from "@trpc/server/adapters/fetch";
import type { NextRequest } from "next/server";
import { auth } from "@/lib/auth";
-import { createOnError } from "@/lib/trpc/shared";
+import { createOnError, guardTRPCSource } from "@/lib/trpc/shared";
import { createTRPCContext } from "@openstatus/api";
import { lambdaRouter } from "@openstatus/api/src/lambda";
// Stripe is incompatible with Edge runtimes due to using Node.js events
// export const runtime = "edge";
-const handler = (req: NextRequest) =>
- fetchRequestHandler({
+const handler = (req: NextRequest) => {
+ const blocked = guardTRPCSource(req);
+ if (blocked) return blocked;
+
+ return fetchRequestHandler({
endpoint: "/api/trpc/lambda",
router: lambdaRouter,
req: req,
createContext: () => createTRPCContext({ req, auth }),
onError: createOnError(req, "lambda"),
});
+};
export { handler as GET, handler as POST };
diff --git a/apps/status-page/src/lib/trpc/shared.ts b/apps/status-page/src/lib/trpc/shared.ts
index c5dd92c2f..a70309aad 100644
--- a/apps/status-page/src/lib/trpc/shared.ts
+++ b/apps/status-page/src/lib/trpc/shared.ts
@@ -23,6 +23,19 @@ export function createOnError(req: Request, label: string) {
};
}
+/**
+ * Reject requests that don't come from our tRPC clients.
+ * Our server and client links always set `x-trpc-source`.
+ * Returns a 401 Response if the header is missing, or null if valid.
+ */
+export function guardTRPCSource(req: Request): Response | null {
+ const source = req.headers.get("x-trpc-source");
+ if (source !== "server" && source !== "client") {
+ return new Response(null, { status: 401 });
+ }
+ return null;
+}
+
const getBaseUrl = () => {
if (typeof window !== "undefined") return "";
// Note: status-page has its own tRPC API routes
diff --git a/apps/web/src/app/api/trpc/edge/[trpc]/route.ts b/apps/web/src/app/api/trpc/edge/[trpc]/route.ts
index 2c1fc5285..65f784175 100644
--- a/apps/web/src/app/api/trpc/edge/[trpc]/route.ts
+++ b/apps/web/src/app/api/trpc/edge/[trpc]/route.ts
@@ -1,13 +1,17 @@
import { fetchRequestHandler } from "@trpc/server/adapters/fetch";
import type { NextRequest } from "next/server";
+import { guardTRPCSource } from "@/trpc/shared";
import { createTRPCContext } from "@openstatus/api";
import { edgeRouter } from "@openstatus/api/src/edge";
export const runtime = "edge";
-const handler = (req: NextRequest) =>
- fetchRequestHandler({
+const handler = (req: NextRequest) => {
+ const blocked = guardTRPCSource(req);
+ if (blocked) return blocked;
+
+ return fetchRequestHandler({
endpoint: "/api/trpc/edge",
router: edgeRouter,
req: req,
@@ -17,5 +21,6 @@ const handler = (req: NextRequest) =>
console.error(error);
},
});
+};
export { handler as GET, handler as POST };
diff --git a/apps/web/src/app/api/trpc/lambda/[trpc]/route.ts b/apps/web/src/app/api/trpc/lambda/[trpc]/route.ts
index b36568088..9a75341be 100644
--- a/apps/web/src/app/api/trpc/lambda/[trpc]/route.ts
+++ b/apps/web/src/app/api/trpc/lambda/[trpc]/route.ts
@@ -1,14 +1,18 @@
import { fetchRequestHandler } from "@trpc/server/adapters/fetch";
import type { NextRequest } from "next/server";
+import { guardTRPCSource } from "@/trpc/shared";
import { createTRPCContext } from "@openstatus/api";
import { lambdaRouter } from "@openstatus/api/src/lambda";
// Stripe is incompatible with Edge runtimes due to using Node.js events
// export const runtime = "edge";
-const handler = (req: NextRequest) =>
- fetchRequestHandler({
+const handler = (req: NextRequest) => {
+ const blocked = guardTRPCSource(req);
+ if (blocked) return blocked;
+
+ return fetchRequestHandler({
endpoint: "/api/trpc/lambda",
router: lambdaRouter,
req: req,
@@ -18,5 +22,6 @@ const handler = (req: NextRequest) =>
console.error(error);
},
});
+};
export { handler as GET, handler as POST };
diff --git a/apps/web/src/trpc/shared.ts b/apps/web/src/trpc/shared.ts
index d6c8c89c2..5d3dcd6e9 100644
--- a/apps/web/src/trpc/shared.ts
+++ b/apps/web/src/trpc/shared.ts
@@ -4,6 +4,19 @@ import { httpBatchLink } from "@trpc/client";
import type { AppRouter } from "@openstatus/api";
import superjson from "superjson";
+/**
+ * Reject requests that don't come from our tRPC clients.
+ * Our server and client links always set `x-trpc-source`.
+ * Returns a 401 Response if the header is missing, or null if valid.
+ */
+export function guardTRPCSource(req: Request): Response | null {
+ const source = req.headers.get("x-trpc-source");
+ if (source !== "server" && source !== "client") {
+ return new Response(null, { status: 401 });
+ }
+ return null;
+}
+
const getBaseUrl = () => {
if (typeof window !== "undefined") return "";
if (process.env.VERCEL_ENV === "production")
diff --git a/packages/api/src/trpc.ts b/packages/api/src/trpc.ts
index db7b1dc03..ba5401d6d 100644
--- a/packages/api/src/trpc.ts
+++ b/packages/api/src/trpc.ts
@@ -73,18 +73,6 @@ export const createTRPCContext = async (opts: {
}) => {
// Use provided auth function or return null session
const session = opts.auth ? await opts.auth() : null;
-
- console.log("[createTRPCContext debug]", {
- hasAuthFn: !!opts.auth,
- sessionResult: session ? "exists" : "null",
- hasReq: !!opts.req,
- hasCookie: !!opts.req?.headers.get("cookie"),
- hasSessionToken:
- !!opts.req?.cookies.get("authjs.session-token")?.value ||
- !!opts.req?.cookies.get("__Secure-authjs.session-token")?.value,
- userAgent: opts.req?.headers.get("user-agent"),
- });
-
const workspace = null;
const user = null;