Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

fix: improve access rights

Carl-Gerhard Lindesvärd0a51b6805eed
diff --git a/packages/trpc/src/routers/notification.ts b/packages/trpc/src/routers/notification.ts
index 144d50043..ec74d2de5 100644
--- a/packages/trpc/src/routers/notification.ts
+++ b/packages/trpc/src/routers/notification.ts
@@ -76,11 +76,28 @@ export const notificationRouter = createTRPCRouter({
}),
createOrUpdateRule: protectedProcedure
.input(zCreateNotificationRule)
- .mutation(async ({ input }) => {
+ .mutation(async ({ input, ctx }) => {
// Clear the cache for the project
await getNotificationRulesByProjectId.clear(input.projectId);
if (input.id) {
+ const existing = await db.notificationRule.findUniqueOrThrow({
+ where: {
+ id: input.id,
+ },
+ });
+
+ const access = await getProjectAccess({
+ userId: ctx.session.userId,
+ projectId: existing.projectId,
+ });
+
+ if (!access) {
+ throw new TRPCForbiddenError(
+ 'You do not have access to this project',
+ );
+ }
+
return db.notificationRule.update({
where: {
id: input.id,
diff --git a/packages/trpc/src/routers/report.ts b/packages/trpc/src/routers/report.ts
index 8db706b60..5cdf5d144 100644
--- a/packages/trpc/src/routers/report.ts
+++ b/packages/trpc/src/routers/report.ts
@@ -1,10 +1,15 @@
import { z } from 'zod';
-import { db, getReportById, getReportsByDashboardId } from '@openpanel/db';
+import {
+ db,
+ getDashboardById,
+ getReportById,
+ getReportsByDashboardId,
+} from '@openpanel/db';
import { zReport } from '@openpanel/validation';
import { getProjectAccess } from '../access';
-import { TRPCForbiddenError } from '../errors';
+import { TRPCForbiddenError, TRPCNotFoundError } from '../errors';
import { createTRPCRouter, protectedProcedure } from '../trpc';
export const reportRouter = createTRPCRouter({
@@ -15,7 +20,11 @@ export const reportRouter = createTRPCRouter({
projectId: z.string(),
}),
)
- .query(async ({ input: { dashboardId, projectId }, ctx }) => {
+ .query(async ({ input: { dashboardId, projectId } }) => {
+ const dashboard = await getDashboardById(dashboardId, projectId);
+ if (!dashboard) {
+ throw new TRPCNotFoundError('Dashboard not found');
+ }
return getReportsByDashboardId(dashboardId);
}),
create: protectedProcedure
@@ -188,7 +197,18 @@ export const reportRouter = createTRPCRouter({
}),
)
.query(async ({ input: { reportId }, ctx }) => {
- return getReportById(reportId);
+ const report = await getReportById(reportId);
+ if (!report) {
+ throw new TRPCNotFoundError('Report not found');
+ }
+ const access = await getProjectAccess({
+ userId: ctx.session.userId,
+ projectId: report.projectId,
+ });
+ if (!access) {
+ throw new TRPCForbiddenError('You do not have access to this project');
+ }
+ return report;
}),
updateLayout: protectedProcedure
.input(
@@ -225,10 +245,10 @@ export const reportRouter = createTRPCRouter({
// Upsert the layout (create if doesn't exist, update if it does)
return db.reportLayout.upsert({
where: {
- reportId: reportId,
+ reportId,
},
create: {
- reportId: reportId,
+ reportId,
x: layout.x,
y: layout.y,
w: layout.w,
diff --git a/packages/trpc/src/routers/share.ts b/packages/trpc/src/routers/share.ts
index 58ee3f630..0e595111d 100644
--- a/packages/trpc/src/routers/share.ts
+++ b/packages/trpc/src/routers/share.ts
@@ -2,6 +2,7 @@ import ShortUniqueId from 'short-unique-id';
import {
db,
+ getDashboardById,
getReportById,
getReportsByDashboardId,
getShareDashboardById,
@@ -169,6 +170,14 @@ export const shareRouter = createTRPCRouter({
throw new TRPCForbiddenError('You do not have access to this project');
}
+ const dashboard = await getDashboardById(
+ input.dashboardId,
+ input.projectId,
+ );
+ if (!dashboard) {
+ throw new TRPCNotFoundError('Dashboard not found');
+ }
+
const passwordHash = input.password
? await hashPassword(input.password)
: null;
@@ -278,6 +287,11 @@ export const shareRouter = createTRPCRouter({
throw new TRPCForbiddenError('You do not have access to this project');
}
+ const report = await getReportById(input.reportId);
+ if (!report || report.projectId !== input.projectId) {
+ throw new TRPCNotFoundError('Report not found');
+ }
+
const passwordHash = input.password
? await hashPassword(input.password)
: null;