Skip to content

Open-source status page, incident response and uptime monitoring

fix: trpc tinybird issue (#1894)

Thibault Le Ouay7dd77385bcad
diff --git a/packages/api/src/router/tinybird/index.test.ts b/packages/api/src/router/tinybird/index.test.ts
new file mode 100644
index 000000000..3ed27a8e4
--- /dev/null
+++ b/packages/api/src/router/tinybird/index.test.ts
@@ -0,0 +1,80 @@
+import { expect, test } from "bun:test";
+import { TRPCError } from "@trpc/server";
+
+import { edgeRouter } from "../../edge";
+import { createInnerTRPCContext } from "../../trpc";
+
+// Workspace 1 owns monitor 1; monitor 5 belongs to workspace 3.
+// These IDs come from the seed data (packages/db/src/seed.mts).
+
+function callerForWorkspace(workspaceId: number) {
+ const ctx = createInnerTRPCContext({
+ req: undefined,
+ session: { user: { id: "1" } },
+ // @ts-expect-error - minimal workspace for test
+ workspace: { id: workspaceId, plan: "team" },
+ });
+ return edgeRouter.createCaller(ctx);
+}
+
+// ─── metricsLatency ──────────────────────────────────────────────
+
+test("tinybird.metricsLatency rejects monitor from another workspace", async () => {
+ const caller = callerForWorkspace(1);
+
+ try {
+ // Monitor 5 belongs to workspace 3
+ await caller.tinybird.metricsLatency({
+ monitorId: "5",
+ period: "1d",
+ type: "http",
+ });
+ throw new Error("Should have thrown");
+ } catch (e) {
+ expect(e).toBeInstanceOf(TRPCError);
+ expect((e as TRPCError).code).toBe("NOT_FOUND");
+ }
+});
+
+test("tinybird.metricsLatency succeeds for own workspace monitor", async () => {
+ const caller = callerForWorkspace(1);
+
+ // Monitor 1 belongs to workspace 1 — should not throw
+ const result = await caller.tinybird.metricsLatency({
+ monitorId: "1",
+ period: "1d",
+ type: "http",
+ });
+ expect(result).toBeDefined();
+});
+
+// ─── metricsTimingPhases ─────────────────────────────────────────
+
+test("tinybird.metricsTimingPhases rejects monitor from another workspace", async () => {
+ const caller = callerForWorkspace(1);
+
+ try {
+ // Monitor 5 belongs to workspace 3
+ await caller.tinybird.metricsTimingPhases({
+ monitorId: "5",
+ period: "1d",
+ type: "http",
+ });
+ throw new Error("Should have thrown");
+ } catch (e) {
+ expect(e).toBeInstanceOf(TRPCError);
+ expect((e as TRPCError).code).toBe("NOT_FOUND");
+ }
+});
+
+test("tinybird.metricsTimingPhases succeeds for own workspace monitor", async () => {
+ const caller = callerForWorkspace(1);
+
+ // Monitor 1 belongs to workspace 1 — should not throw
+ const result = await caller.tinybird.metricsTimingPhases({
+ monitorId: "1",
+ period: "1d",
+ type: "http",
+ });
+ expect(result).toBeDefined();
+});
diff --git a/packages/api/src/router/tinybird/index.ts b/packages/api/src/router/tinybird/index.ts
index 184230702..55897d022 100644
--- a/packages/api/src/router/tinybird/index.ts
+++ b/packages/api/src/router/tinybird/index.ts
@@ -430,6 +430,22 @@ export const tinybirdRouter = createTRPCRouter({
}),
)
.query(async (opts) => {
+ const whereConditions: SQL[] = [
+ eq(monitor.id, Number.parseInt(opts.input.monitorId)),
+ eq(monitor.workspaceId, opts.ctx.workspace.id),
+ ];
+
+ const _monitor = await db.query.monitor.findFirst({
+ where: and(...whereConditions),
+ });
+
+ if (!_monitor) {
+ throw new TRPCError({
+ code: "NOT_FOUND",
+ message: "Monitor not found",
+ });
+ }
+
if (opts.ctx.workspace.plan === "free") {
opts.input.regions = undefined;
}
@@ -452,6 +468,22 @@ export const tinybirdRouter = createTRPCRouter({
}),
)
.query(async (opts) => {
+ const whereConditions: SQL[] = [
+ eq(monitor.id, Number.parseInt(opts.input.monitorId)),
+ eq(monitor.workspaceId, opts.ctx.workspace.id),
+ ];
+
+ const _monitor = await db.query.monitor.findFirst({
+ where: and(...whereConditions),
+ });
+
+ if (!_monitor) {
+ throw new TRPCError({
+ code: "NOT_FOUND",
+ message: "Monitor not found",
+ });
+ }
+
if (opts.ctx.workspace.plan === "free") {
opts.input.regions = undefined;
}