Skip to content

Open-source status page, incident response and uptime monitoring

fix: workspace-slug cookie manipulation (#1384)

Maximilian Kaskeedb103b0df1e
diff --git a/packages/api/src/trpc.ts b/packages/api/src/trpc.ts
index 776442cc5..7fa97090b 100644
--- a/packages/api/src/trpc.ts
+++ b/packages/api/src/trpc.ts
@@ -179,11 +179,13 @@ const enforceUserIsAuthed = t.middleware(async (opts) => {
// });
// }
- const activeWorkspace = usersToWorkspaces?.find(({ workspace }) => {
- // If there is a workspace slug in the cookie, use it to find the workspace
- if (workspaceSlug) return workspace.slug === workspaceSlug;
- return true;
- })?.workspace;
+ // NOTE: if no workspace slug fit (cookie manipulation), use the first workspace
+ const activeWorkspace =
+ usersToWorkspaces?.find(({ workspace }) => {
+ // If there is a workspace slug in the cookie, use it to find the workspace
+ if (workspaceSlug) return workspace.slug === workspaceSlug;
+ return true;
+ })?.workspace ?? usersToWorkspaces?.[0]?.workspace;
if (!activeWorkspace) {
throw new TRPCError({
@@ -192,6 +194,11 @@ const enforceUserIsAuthed = t.middleware(async (opts) => {
});
}
+ if (activeWorkspace.slug !== workspaceSlug) {
+ // properly set the workspace slug cookie
+ ctx.req?.cookies.set("workspace-slug", activeWorkspace.slug);
+ }
+
if (!userProps) {
throw new TRPCError({ code: "UNAUTHORIZED", message: "User Not Found" });
}