Skip to content

Open-source status page, incident response and uptime monitoring

fix: trpc issue (#1881)

Thibault Le Ouayf55561367920
diff --git a/.gitignore b/.gitignore
index d7994c702..7dabd15da 100644
--- a/.gitignore
+++ b/.gitignore
@@ -76,3 +76,5 @@ packages/ui/public/r
# For vibing
research.md
plan.md
+
+.zed
diff --git a/.zed/settings.json b/.zed/settings.json
new file mode 100644
index 000000000..a10b829ce
--- /dev/null
+++ b/.zed/settings.json
@@ -0,0 +1,172 @@
+{
+ "lsp": {
+ "oxlint": {
+ "initialization_options": {
+ "settings": {
+ "configPath": null,
+ "run": "onType",
+ "disableNestedConfig": false,
+ "fixKind": "safe_fix",
+ "typeAware": true,
+ "unusedDisableDirectives": "deny"
+ }
+ }
+ },
+ "oxfmt": {
+ "initialization_options": {
+ "settings": {
+ "fmt.configPath": null,
+ "run": "onSave"
+ }
+ }
+ }
+ },
+ "languages": {
+ "CSS": {
+ "format_on_save": "on",
+ "prettier": {
+ "allowed": false
+ },
+ "formatter": [
+ {
+ "language_server": {
+ "name": "oxfmt"
+ }
+ }
+ ]
+ },
+ "HTML": {
+ "format_on_save": "on",
+ "prettier": {
+ "allowed": false
+ },
+ "formatter": [
+ {
+ "language_server": {
+ "name": "oxfmt"
+ }
+ }
+ ]
+ },
+ "JavaScript": {
+ "format_on_save": "on",
+ "prettier": {
+ "allowed": false
+ },
+ "formatter": [
+ {
+ "language_server": {
+ "name": "oxfmt"
+ }
+ },
+ {
+ "code_action": "source.fixAll.oxc"
+ }
+ ]
+ },
+ "JSON": {
+ "format_on_save": "on",
+ "prettier": {
+ "allowed": false
+ },
+ "formatter": [
+ {
+ "language_server": {
+ "name": "oxfmt"
+ }
+ }
+ ]
+ },
+ "JSONC": {
+ "format_on_save": "on",
+ "prettier": {
+ "allowed": false
+ },
+ "formatter": [
+ {
+ "language_server": {
+ "name": "oxfmt"
+ }
+ }
+ ]
+ },
+ "Markdown": {
+ "format_on_save": "on",
+ "prettier": {
+ "allowed": false
+ },
+ "formatter": [
+ {
+ "language_server": {
+ "name": "oxfmt"
+ }
+ }
+ ]
+ },
+ "MDX": {
+ "format_on_save": "on",
+ "prettier": {
+ "allowed": false
+ },
+ "formatter": [
+ {
+ "language_server": {
+ "name": "oxfmt"
+ }
+ }
+ ]
+ },
+ "SCSS": {
+ "format_on_save": "on",
+ "prettier": {
+ "allowed": false
+ },
+ "formatter": [
+ {
+ "language_server": {
+ "name": "oxfmt"
+ }
+ }
+ ]
+ },
+ "TypeScript": {
+ "format_on_save": "on",
+ "prettier": {
+ "allowed": false
+ },
+ "formatter": [
+ {
+ "language_server": {
+ "name": "oxfmt"
+ }
+ }
+ ]
+ },
+ "TSX": {
+ "format_on_save": "on",
+ "prettier": {
+ "allowed": false
+ },
+ "formatter": [
+ {
+ "language_server": {
+ "name": "oxfmt"
+ }
+ }
+ ]
+ },
+ "YAML": {
+ "format_on_save": "on",
+ "prettier": {
+ "allowed": false
+ },
+ "formatter": [
+ {
+ "language_server": {
+ "name": "oxfmt"
+ }
+ }
+ ]
+ }
+ }
+}
diff --git a/packages/api/src/router/monitor.test.ts b/packages/api/src/router/monitor.test.ts
new file mode 100644
index 000000000..4bab71d80
--- /dev/null
+++ b/packages/api/src/router/monitor.test.ts
@@ -0,0 +1,95 @@
+import { afterAll, beforeAll, expect, test } from "bun:test";
+import { db, eq } from "@openstatus/db";
+import { monitorTag, monitorTagsToMonitors } from "@openstatus/db/src/schema";
+import { TRPCError } from "@trpc/server";
+
+import { edgeRouter } from "../edge";
+import { createInnerTRPCContext } from "../trpc";
+
+let tagId: number;
+
+beforeAll(async () => {
+ const tag = await db
+ .insert(monitorTag)
+ .values({
+ workspaceId: 1,
+ name: "idor-test-tag",
+ color: "#ff0000",
+ })
+ .returning()
+ .get();
+ tagId = tag.id;
+});
+
+afterAll(async () => {
+ await db
+ .delete(monitorTagsToMonitors)
+ .where(eq(monitorTagsToMonitors.monitorTagId, tagId));
+ await db.delete(monitorTag).where(eq(monitorTag.id, tagId));
+});
+
+test("monitor.updateTags rejects monitor from another workspace", async () => {
+ const ctx = createInnerTRPCContext({
+ req: undefined,
+ session: { user: { id: "1" } },
+ // @ts-expect-error - minimal workspace for test
+ workspace: { id: 1 },
+ });
+ const caller = edgeRouter.createCaller(ctx);
+
+ try {
+ await caller.monitor.updateTags({ id: 5, tags: [] });
+ throw new Error("Should have thrown");
+ } catch (e) {
+ expect(e).toBeInstanceOf(TRPCError);
+ expect((e as TRPCError).code).toBe("NOT_FOUND");
+ }
+});
+
+test("monitor.updateTags succeeds for own workspace monitor", async () => {
+ const ctx = createInnerTRPCContext({
+ req: undefined,
+ session: { user: { id: "1" } },
+ // @ts-expect-error - minimal workspace for test
+ workspace: { id: 1 },
+ });
+ const caller = edgeRouter.createCaller(ctx);
+
+ await caller.monitor.updateTags({ id: 1, tags: [tagId] });
+
+ const result = await db.query.monitorTagsToMonitors.findFirst({
+ where: eq(monitorTagsToMonitors.monitorId, 1),
+ });
+ expect(result).toBeDefined();
+ expect(result?.monitorTagId).toBe(tagId);
+});
+
+test("monitor.updateNotifiers rejects monitor from another workspace", async () => {
+ const ctx = createInnerTRPCContext({
+ req: undefined,
+ session: { user: { id: "1" } },
+ // @ts-expect-error - minimal workspace for test
+ workspace: { id: 1 },
+ });
+ const caller = edgeRouter.createCaller(ctx);
+
+ try {
+ await caller.monitor.updateNotifiers({ id: 5, notifiers: [] });
+ throw new Error("Should have thrown");
+ } catch (e) {
+ expect(e).toBeInstanceOf(TRPCError);
+ expect((e as TRPCError).code).toBe("NOT_FOUND");
+ }
+});
+
+test("monitor.updateNotifiers succeeds for own workspace monitor", async () => {
+ const ctx = createInnerTRPCContext({
+ req: undefined,
+ session: { user: { id: "1" } },
+ // @ts-expect-error - minimal workspace for test
+ workspace: { id: 1 },
+ });
+ const caller = edgeRouter.createCaller(ctx);
+
+ await caller.monitor.updateNotifiers({ id: 1, notifiers: [1] });
+});
diff --git a/packages/api/src/router/monitor.ts b/packages/api/src/router/monitor.ts
index f3925205e..f295877c9 100644
--- a/packages/api/src/router/monitor.ts
+++ b/packages/api/src/router/monitor.ts
@@ -488,6 +488,20 @@ export const monitorRouter = createTRPCRouter({
.meta({ track: Events.UpdateMonitor })
.input(z.object({ id: z.number(), tags: z.array(z.number()) }))
.mutation(async ({ ctx, input }) => {
+ const existingMonitor = await ctx.db.query.monitor.findFirst({
+ where: and(
+ eq(monitor.id, input.id),
+ eq(monitor.workspaceId, ctx.workspace.id),
+ ),
+ });
+
+ if (!existingMonitor) {
+ throw new TRPCError({
+ code: "NOT_FOUND",
+ message: "Monitor not found.",
+ });
+ }
+
const allTags = await ctx.db.query.monitorTag.findMany({
where: and(
eq(monitorTag.workspaceId, ctx.workspace.id),
@@ -615,6 +629,20 @@ export const monitorRouter = createTRPCRouter({
.meta({ track: Events.UpdateMonitor })
.input(z.object({ id: z.number(), notifiers: z.array(z.number()) }))
.mutation(async ({ ctx, input }) => {
+ const existingMonitor = await ctx.db.query.monitor.findFirst({
+ where: and(
+ eq(monitor.id, input.id),
+ eq(monitor.workspaceId, ctx.workspace.id),
+ ),
+ });
+
+ if (!existingMonitor) {
+ throw new TRPCError({
+ code: "NOT_FOUND",
+ message: "Monitor not found.",
+ });
+ }
+
const allNotifiers = await ctx.db.query.notification.findMany({
where: and(
eq(notification.workspaceId, ctx.workspace.id),
diff --git a/packages/api/src/router/notification.test.ts b/packages/api/src/router/notification.test.ts
new file mode 100644
index 000000000..ebd962d66
--- /dev/null
+++ b/packages/api/src/router/notification.test.ts
@@ -0,0 +1,74 @@
+import { afterAll, beforeAll, expect, test } from "bun:test";
+import { db, eq } from "@openstatus/db";
+import { notification } from "@openstatus/db/src/schema";
+
+import { edgeRouter } from "../edge";
+import { createInnerTRPCContext } from "../trpc";
+
+let otherWorkspaceNotifId: number;
+
+beforeAll(async () => {
+ const notif = await db
+ .insert(notification)
+ .values({
+ provider: "email",
+ name: "workspace 2 idor test notification",
+ data: '{"email":"[email protected]"}',
+ workspaceId: 2,
+ })
+ .returning()
+ .get();
+ otherWorkspaceNotifId = notif.id;
+});
+
+afterAll(async () => {
+ await db
+ .delete(notification)
+ .where(eq(notification.id, otherWorkspaceNotifId));
+});
+
+test("notification.delete does not delete a notification from another workspace", async () => {
+ const ctx = createInnerTRPCContext({
+ req: undefined,
+ session: { user: { id: "1" } },
+ // @ts-expect-error - minimal workspace for test
+ workspace: { id: 1 },
+ });
+ const caller = edgeRouter.createCaller(ctx);
+
+ await caller.notification.delete({ id: otherWorkspaceNotifId });
+
+ const notifAfter = await db.query.notification.findFirst({
+ where: eq(notification.id, otherWorkspaceNotifId),
+ });
+ expect(notifAfter).toBeDefined();
+ expect(notifAfter?.id).toBe(otherWorkspaceNotifId);
+});
+
+test("notification.delete succeeds for own workspace notification", async () => {
+ const tempNotif = await db
+ .insert(notification)
+ .values({
+ provider: "email",
+ name: "temp test notification",
+ data: '{"email":"[email protected]"}',
+ workspaceId: 1,
+ })
+ .returning()
+ .get();
+
+ const ctx = createInnerTRPCContext({
+ req: undefined,
+ session: { user: { id: "1" } },
+ // @ts-expect-error - minimal workspace for test
+ workspace: { id: 1 },
+ });
+ const caller = edgeRouter.createCaller(ctx);
+
+ await caller.notification.delete({ id: tempNotif.id });
+
+ const notifAfter = await db.query.notification.findFirst({
+ where: eq(notification.id, tempNotif.id),
+ });
+ expect(notifAfter).toBeUndefined();
+});
diff --git a/packages/api/src/router/notification.ts b/packages/api/src/router/notification.ts
index 12f948a91..35ca07c74 100644
--- a/packages/api/src/router/notification.ts
+++ b/packages/api/src/router/notification.ts
@@ -1,7 +1,7 @@
import { TRPCError } from "@trpc/server";
import { z } from "zod";
-import { type SQL, and, count, db, eq, inArray } from "@openstatus/db";
+import { type SQL, and, count, eq, inArray } from "@openstatus/db";
import {
NotificationDataSchema,
googleChatDataSchema,
@@ -191,27 +191,11 @@ export const notificationRouter = createTRPCRouter({
return currentNotification;
}),
- deleteNotification: protectedProcedure
- .meta({ track: Events.DeleteNotification })
- .input(z.object({ id: z.number() }))
- .mutation(async (opts) => {
- await opts.ctx.db
- .delete(notification)
- .where(
- and(
- eq(notification.id, opts.input.id),
- eq(notification.id, opts.input.id),
- ),
- )
- .run();
- }),
-
getNotificationById: protectedProcedure
.input(z.object({ id: z.number() }))
.query(async (opts) => {
const _notification = await opts.ctx.db.query.notification.findFirst({
where: and(
- eq(notification.id, opts.input.id),
eq(notification.id, opts.input.id),
eq(notification.workspaceId, opts.ctx.workspace.id),
),
@@ -302,7 +286,6 @@ export const notificationRouter = createTRPCRouter({
}),
)
.mutation(async (opts) => {
- console.log(opts.input);
const whereCondition: SQL[] = [
eq(notification.id, opts.input.id),
eq(notification.workspaceId, opts.ctx.workspace.id),
@@ -331,7 +314,7 @@ export const notificationRouter = createTRPCRouter({
});
}
- await db.transaction(async (tx) => {
+ await opts.ctx.db.transaction(async (tx) => {
await tx
.update(notification)
.set({
@@ -438,8 +421,8 @@ export const notificationRouter = createTRPCRouter({
});
}
- const _notification = await db.transaction(async (tx) => {
- const _notification = await opts.ctx.db
+ const _notification = await opts.ctx.db.transaction(async (tx) => {
+ const _notification = await tx
.insert(notification)
.values({
name: opts.input.name,
diff --git a/packages/api/src/router/statusReport.test.ts b/packages/api/src/router/statusReport.test.ts
new file mode 100644
index 000000000..8b524ef77
--- /dev/null
+++ b/packages/api/src/router/statusReport.test.ts
@@ -0,0 +1,77 @@
+import { afterAll, beforeAll, expect, test } from "bun:test";
+import { db, eq } from "@openstatus/db";
+import { page, statusReport } from "@openstatus/db/src/schema";
+import { TRPCError } from "@trpc/server";
+
+import { edgeRouter } from "../edge";
+import { createInnerTRPCContext } from "../trpc";
+
+let otherWorkspacePageId: number;
+
+beforeAll(async () => {
+ const p = await db
+ .insert(page)
+ .values({
+ workspaceId: 2,
+ title: "IDOR Test Page",
+ description: "Page for IDOR testing",
+ slug: "idor-test-page",
+ customDomain: "",
+ })
+ .returning()
+ .get();
+ otherWorkspacePageId = p.id;
+});
+
+afterAll(async () => {
+ await db.delete(page).where(eq(page.id, otherWorkspacePageId));
+});
+
+test("statusReport.create rejects pageId from another workspace", async () => {
+ const ctx = createInnerTRPCContext({
+ req: undefined,
+ session: { user: { id: "1" } },
+ // @ts-expect-error - minimal workspace for test
+ workspace: { id: 1 },
+ });
+ const caller = edgeRouter.createCaller(ctx);
+
+ try {
+ await caller.statusReport.create({
+ title: "Test Report",
+ status: "investigating",
+ pageId: otherWorkspacePageId,
+ pageComponents: [],
+ date: new Date(),
+ message: "Testing cross-workspace access",
+ });
+ throw new Error("Should have thrown");
+ } catch (e) {
+ expect(e).toBeInstanceOf(TRPCError);
+ expect((e as TRPCError).code).toBe("NOT_FOUND");
+ }
+});
+
+test("statusReport.create succeeds for own workspace page", async () => {
+ const ctx = createInnerTRPCContext({
+ req: undefined,
+ session: { user: { id: "1" } },
+ // @ts-expect-error - minimal workspace for test
+ workspace: { id: 1 },
+ });
+ const caller = edgeRouter.createCaller(ctx);
+
+ const result = await caller.statusReport.create({
+ title: "Valid Test Report",
+ status: "investigating",
+ pageId: 1,
+ pageComponents: [],
+ date: new Date(),
+ message: "Testing own workspace access",
+ });
+
+ expect(result).toBeDefined();
+
+ // Clean up
+ await db.delete(statusReport).where(eq(statusReport.id, result.id));
+});
diff --git a/packages/api/src/router/statusReport.ts b/packages/api/src/router/statusReport.ts
index 1d22e2a16..840e0918c 100644
--- a/packages/api/src/router/statusReport.ts
+++ b/packages/api/src/router/statusReport.ts
@@ -3,6 +3,7 @@ import { z } from "zod";
import { type SQL, and, asc, desc, eq, gte } from "@openstatus/db";
import {
insertStatusReportUpdateSchema,
+ page,
selectPageComponentSchema,
selectPageSchema,
selectStatusReportSchema,
@@ -196,6 +197,20 @@ export const statusReportRouter = createTRPCRouter({
}),
)
.mutation(async (opts) => {
+ const existingPage = await opts.ctx.db.query.page.findFirst({
+ where: and(
+ eq(page.id, opts.input.pageId),
+ eq(page.workspaceId, opts.ctx.workspace.id),
+ ),
+ });
+
+ if (!existingPage) {
+ throw new TRPCError({
+ code: "NOT_FOUND",
+ message: "Page not found.",
+ });
+ }
+
return opts.ctx.db.transaction(async (tx) => {
const newStatusReport = await tx
.insert(statusReport)