Skip to content

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

share: locked dashboards and wrong share passwords answer FORBIDDEN

Carl-Gerhard Lindesvärddff16a8bd8fd
diff --git a/apps/start/src/routes/share.dashboard.$shareId.tsx b/apps/start/src/routes/share.dashboard.$shareId.tsx
index bb005074f..ba9b21a49 100644
--- a/apps/start/src/routes/share.dashboard.$shareId.tsx
+++ b/apps/start/src/routes/share.dashboard.$shareId.tsx
@@ -85,11 +85,14 @@ function RouteComponent() {
})
);
- const reportsQuery = useQuery(
- trpc.share.dashboardReports.queryOptions({
+ // Not fetched while locked: the server answers FORBIDDEN until the password
+ // cookie is set, and there is nothing to render before then anyway.
+ const reportsQuery = useQuery({
+ ...trpc.share.dashboardReports.queryOptions({
shareId,
- })
- );
+ }),
+ enabled: !shareQuery.data?.requiresPassword,
+ });
const share = shareQuery.data;
diff --git a/packages/core/src/modules/auth/auth.service.ts b/packages/core/src/modules/auth/auth.service.ts
index c261371cb..a59f80616 100644
--- a/packages/core/src/modules/auth/auth.service.ts
+++ b/packages/core/src/modules/auth/auth.service.ts
@@ -298,7 +298,11 @@ import { generateSecureId } from '@openpanel/shared';
import { decrypt, encrypt } from '@openpanel/shared/server';
import { sendEmail } from '../../clients/email';
import type { Logger } from '../../logger';
-import { TRPCAccessError, TRPCNotFoundError } from '../../rpc/errors';
+import {
+ TRPCAccessError,
+ TRPCForbiddenError,
+ TRPCNotFoundError,
+} from '../../rpc/errors';
import { connectUserToOrganization } from '../organization/organization.service';
import { getUserAccount } from '../user/user.service';
import {
@@ -906,7 +910,8 @@ export async function signInToShare(
const validPassword = await verifyPasswordHash(share.password, password);
if (!validPassword) {
- throw new TRPCAccessError('Incorrect password');
+ // FORBIDDEN so the dashboard's 401 handler does not log the viewer out.
+ throw new TRPCForbiddenError('Incorrect password');
}
setCookie(cookieName, '1', { maxAge: 60 * 60 * 24 * 7 });
diff --git a/packages/core/src/modules/share/share.service.test.ts b/packages/core/src/modules/share/share.service.test.ts
index 5e51d96e1..25e815edb 100644
--- a/packages/core/src/modules/share/share.service.test.ts
+++ b/packages/core/src/modules/share/share.service.test.ts
@@ -354,7 +354,7 @@ test('getShareDashboardReports rejects without the unlock cookie when password-p
await expect(
subject.getShareDashboardReports('share_1', cookies())
- ).rejects.toMatchObject({ code: 'UNAUTHORIZED' });
+ ).rejects.toMatchObject({ code: 'FORBIDDEN' });
});
test('getShareDashboardReports returns the dashboard reports once unlocked', async () => {
diff --git a/packages/core/src/modules/share/share.service.ts b/packages/core/src/modules/share/share.service.ts
index 70a7b5a0b..b65fc73e7 100644
--- a/packages/core/src/modules/share/share.service.ts
+++ b/packages/core/src/modules/share/share.service.ts
@@ -13,7 +13,7 @@
// (docs/TECH_DEBT.md §4).
import ShortUniqueId from 'short-unique-id';
-import { TRPCAccessError, TRPCNotFoundError } from '../../rpc/errors';
+import { TRPCForbiddenError, TRPCNotFoundError } from '../../rpc/errors';
import type { ServiceDeps, Services } from '../../services';
import { getProjectAccess } from '../../shared/access-lookups';
import { hashPassword } from '../auth/auth.service';
@@ -483,9 +483,13 @@ export async function getShareDashboardReports(
throw new TRPCNotFoundError('Dashboard share not found');
}
+ // FORBIDDEN, not UNAUTHORIZED: the dashboard treats a 401 as "session
+ // gone" and hard-navigates to /login, which made every password-protected
+ // share unusable. A locked share is an authorization refusal, like the
+ // share guards in overview.rpc.ts and chart.rpc.ts.
const hasAccess = !!cookies.get(`shared-dashboard-${share.id}`);
if (share.password && !hasAccess) {
- throw new TRPCAccessError('Password required');
+ throw new TRPCForbiddenError('Password required');
}
return getReportsByDashboardId(deps, share.dashboardId);