Skip to content

Commit

Permalink
fix: check workspace ownership
Browse files Browse the repository at this point in the history
  • Loading branch information
chronark committed Dec 6, 2024
1 parent 0c51ab1 commit b1ed43c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 34 deletions.
25 changes: 8 additions & 17 deletions apps/dashboard/lib/trpc/routers/api/setDefaultBytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,26 @@ export const setDefaultApiBytes = t.procedure
}),
)
.mutation(async ({ ctx, input }) => {
const workspace = await db.query.workspaces
const keyAuth = await db.query.keyAuth
.findFirst({
where: (table, { eq }) => eq(table.tenantId, ctx.tenant.id),
where: (table, { eq, and, isNull }) =>
and(eq(table.id, input.keyAuthId), isNull(table.deletedAt)),
with: {
keySpaces: {
where: (table, { eq }) => eq(table.id, input.keyAuthId),
},
workspace: true,
},
})
.catch((_err) => {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message:
"We were unable to find the KeyAuth. Please try again or contact support@unkey.dev.",
"We were unable to update the key auth. Please try again or contact support@unkey.dev",
});
});
if (!workspace) {
throw new TRPCError({
code: "NOT_FOUND",
message:
"We are unable to find the correct workspace. Please try again or contact support@unkey.dev",
});
}
const keyAuth = workspace.keySpaces.at(0);
if (!keyAuth) {
if (!keyAuth || keyAuth.workspace.tenantId !== ctx.tenant.id) {
throw new TRPCError({
code: "NOT_FOUND",
message:
"We are unable to find the correct keyAuth. Please try again or contact support@unkey.dev",
"We are unable to find the correct key auth. Please try again or contact support@unkey.dev.",
});
}
await db
Expand All @@ -65,7 +56,7 @@ export const setDefaultApiBytes = t.procedure
});
});
await insertAuditLogs(tx, {
workspaceId: workspace.id,
workspaceId: keyAuth.workspace.id,
actor: {
type: "user",
id: ctx.user.id,
Expand Down
25 changes: 8 additions & 17 deletions apps/dashboard/lib/trpc/routers/api/setDefaultPrefix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,26 @@ export const setDefaultApiPrefix = t.procedure
}),
)
.mutation(async ({ ctx, input }) => {
const workspace = await db.query.workspaces
const keyAuth = await db.query.keyAuth
.findFirst({
where: (table, { eq }) => eq(table.tenantId, ctx.tenant.id),
where: (table, { eq, and, isNull }) =>
and(eq(table.id, input.keyAuthId), isNull(table.deletedAt)),
with: {
keySpaces: {
where: (table, { eq }) => eq(table.id, input.keyAuthId),
},
workspace: true,
},
})
.catch((_err) => {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message:
"We were unable to find the KeyAuth. Please try again or contact support@unkey.dev.",
"We were unable to update the key auth. Please try again or contact support@unkey.dev",
});
});
if (!workspace) {
throw new TRPCError({
code: "NOT_FOUND",
message:
"We are unable to find the correct workspace. Please try again or contact support@unkey.dev",
});
}
const keyAuth = workspace.keySpaces.at(0);
if (!keyAuth) {
if (!keyAuth || keyAuth.workspace.tenantId !== ctx.tenant.id) {
throw new TRPCError({
code: "NOT_FOUND",
message:
"We are unable to find the correct keyAuth. Please try again or contact support@unkey.dev",
"We are unable to find the correct key auth. Please try again or contact support@unkey.dev.",
});
}

Expand All @@ -62,7 +53,7 @@ export const setDefaultApiPrefix = t.procedure
});
});
await insertAuditLogs(tx, {
workspaceId: workspace.id,
workspaceId: keyAuth.workspace.id,
actor: {
type: "user",
id: ctx.user.id,
Expand Down

0 comments on commit b1ed43c

Please sign in to comment.