harden report references in boards
This commit is contained in:
1 parent
a4a0167515
commit
7bdc2f215e
8 files changed
+346
-10
No files matched your search
@@ -1,12 +1,18 @@
|
||||
import { beforeEach, expect, test, vi } from 'vitest';
|
||||
import { BOARD_TYPES } from '@/lib/boards';
|
||||
import { canViewBoardEntities } from './board';
|
||||
import { getReport } from '@/queries/prisma';
|
||||
import {
|
||||
canViewBoardEntities,
|
||||
hasValidBoardReports,
|
||||
stripInvalidBoardReports,
|
||||
} from './board';
|
||||
import { canViewLink } from './link';
|
||||
import { canViewPixel } from './pixel';
|
||||
import { canViewWebsite } from './website';
|
||||
|
||||
vi.mock('@/queries/prisma', () => ({
|
||||
getBoard: vi.fn(),
|
||||
getReport: vi.fn(),
|
||||
getTeamUser: vi.fn(),
|
||||
}));
|
||||
|
||||
@@ -42,6 +48,7 @@ beforeEach(() => {
|
||||
vi.mocked(canViewWebsite).mockReset();
|
||||
vi.mocked(canViewPixel).mockReset();
|
||||
vi.mocked(canViewLink).mockReset();
|
||||
vi.mocked(getReport).mockReset();
|
||||
});
|
||||
|
||||
test('canViewBoardEntities validates board IDs with user auth only', async () => {
|
||||
@@ -82,3 +89,170 @@ test('canViewBoardEntities rejects IDs not accessible to the user', async () =>
|
||||
}),
|
||||
).resolves.toBe(false);
|
||||
});
|
||||
|
||||
test('hasValidBoardReports allows saved reports for the board website', async () => {
|
||||
vi.mocked(getReport).mockResolvedValue({
|
||||
id: 'report-1',
|
||||
websiteId: 'website-1',
|
||||
type: 'goal',
|
||||
} as any);
|
||||
|
||||
await expect(
|
||||
hasValidBoardReports(BOARD_TYPES.website, {
|
||||
websiteId: 'website-1',
|
||||
rows: [
|
||||
{
|
||||
id: 'row-1',
|
||||
columns: [
|
||||
{
|
||||
id: 'column-1',
|
||||
component: {
|
||||
type: 'Goal',
|
||||
props: {
|
||||
reportId: 'report-1',
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
}),
|
||||
).resolves.toBe(true);
|
||||
});
|
||||
|
||||
test('hasValidBoardReports rejects saved reports from a different website', async () => {
|
||||
vi.mocked(getReport).mockResolvedValue({
|
||||
id: 'report-1',
|
||||
websiteId: 'website-2',
|
||||
type: 'goal',
|
||||
} as any);
|
||||
|
||||
await expect(
|
||||
hasValidBoardReports(BOARD_TYPES.website, {
|
||||
websiteId: 'website-1',
|
||||
rows: [
|
||||
{
|
||||
id: 'row-1',
|
||||
columns: [
|
||||
{
|
||||
id: 'column-1',
|
||||
component: {
|
||||
type: 'Goal',
|
||||
props: {
|
||||
reportId: 'report-1',
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
}),
|
||||
).resolves.toBe(false);
|
||||
});
|
||||
|
||||
test('hasValidBoardReports rejects saved reports with the wrong type', async () => {
|
||||
vi.mocked(getReport).mockResolvedValue({
|
||||
id: 'report-1',
|
||||
websiteId: 'website-1',
|
||||
type: 'funnel',
|
||||
} as any);
|
||||
|
||||
await expect(
|
||||
hasValidBoardReports(BOARD_TYPES.website, {
|
||||
websiteId: 'website-1',
|
||||
rows: [
|
||||
{
|
||||
id: 'row-1',
|
||||
columns: [
|
||||
{
|
||||
id: 'column-1',
|
||||
component: {
|
||||
type: 'Goal',
|
||||
props: {
|
||||
reportId: 'report-1',
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
}),
|
||||
).resolves.toBe(false);
|
||||
});
|
||||
|
||||
test('hasValidBoardReports rejects report widgets without a website context', async () => {
|
||||
await expect(
|
||||
hasValidBoardReports(BOARD_TYPES.pixel, {
|
||||
pixelId: 'pixel-1',
|
||||
rows: [
|
||||
{
|
||||
id: 'row-1',
|
||||
columns: [
|
||||
{
|
||||
id: 'column-1',
|
||||
component: {
|
||||
type: 'Goal',
|
||||
props: {
|
||||
reportId: 'report-1',
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
}),
|
||||
).resolves.toBe(false);
|
||||
});
|
||||
|
||||
test('stripInvalidBoardReports removes only invalid report IDs', async () => {
|
||||
vi.mocked(getReport).mockResolvedValue({
|
||||
id: 'report-1',
|
||||
websiteId: 'website-2',
|
||||
type: 'goal',
|
||||
} as any);
|
||||
|
||||
await expect(
|
||||
stripInvalidBoardReports(BOARD_TYPES.website, {
|
||||
websiteId: 'website-1',
|
||||
rows: [
|
||||
{
|
||||
id: 'row-1',
|
||||
columns: [
|
||||
{
|
||||
id: 'column-1',
|
||||
component: {
|
||||
type: 'Goal',
|
||||
title: 'Saved signup goal',
|
||||
description: 'Should stay on the board',
|
||||
props: {
|
||||
reportId: 'report-1',
|
||||
customValue: 'keep-me',
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
}),
|
||||
).resolves.toEqual({
|
||||
websiteId: 'website-1',
|
||||
rows: [
|
||||
{
|
||||
id: 'row-1',
|
||||
columns: [
|
||||
{
|
||||
id: 'column-1',
|
||||
component: {
|
||||
type: 'Goal',
|
||||
title: 'Saved signup goal',
|
||||
description: 'Should stay on the board',
|
||||
props: {
|
||||
customValue: 'keep-me',
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
+121
-3
@@ -1,12 +1,58 @@
|
||||
import { hasPermission } from '@/lib/auth';
|
||||
import { getBoardEntityIds } from '@/lib/boards';
|
||||
import { BOARD_ENTITY_TYPES, getBoardEntityIds, getResolvedComponentEntity } from '@/lib/boards';
|
||||
import { PERMISSIONS } from '@/lib/constants';
|
||||
import type { Auth, BoardParameters } from '@/lib/types';
|
||||
import { getBoard, getTeamUser } from '@/queries/prisma';
|
||||
import type { Auth, BoardComponentConfig, BoardParameters } from '@/lib/types';
|
||||
import { getBoard, getReport, getTeamUser } from '@/queries/prisma';
|
||||
import { canViewLink } from './link';
|
||||
import { canViewPixel } from './pixel';
|
||||
import { canViewWebsite } from './website';
|
||||
|
||||
const BOARD_COMPONENT_REPORT_TYPES = {
|
||||
Funnel: 'funnel',
|
||||
Goal: 'goal',
|
||||
} as const;
|
||||
|
||||
function getExpectedBoardReportType(componentType?: string) {
|
||||
return componentType && componentType in BOARD_COMPONENT_REPORT_TYPES
|
||||
? BOARD_COMPONENT_REPORT_TYPES[componentType as keyof typeof BOARD_COMPONENT_REPORT_TYPES]
|
||||
: null;
|
||||
}
|
||||
|
||||
async function isValidBoardReportReference(
|
||||
board: { type?: string; parameters?: BoardParameters },
|
||||
component: BoardComponentConfig,
|
||||
reportCache: Map<string, ReturnType<typeof getReport>>,
|
||||
) {
|
||||
const expectedReportType = getExpectedBoardReportType(component.type);
|
||||
|
||||
if (!expectedReportType) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const reportId = component.props?.reportId;
|
||||
|
||||
if (reportId == null || reportId === '') {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (typeof reportId !== 'string') {
|
||||
return false;
|
||||
}
|
||||
|
||||
const entity = getResolvedComponentEntity(board, component);
|
||||
|
||||
if (entity.entityType !== BOARD_ENTITY_TYPES.website || !entity.entityId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const reportPromise = reportCache.get(reportId) ?? getReport(reportId);
|
||||
reportCache.set(reportId, reportPromise);
|
||||
|
||||
const report = await reportPromise;
|
||||
|
||||
return !!report && report.type === expectedReportType && report.websiteId === entity.entityId;
|
||||
}
|
||||
|
||||
async function checkBoardEntityAccess(check: Promise<boolean>) {
|
||||
try {
|
||||
return await check;
|
||||
@@ -33,6 +79,78 @@ export async function canViewBoardEntities(
|
||||
return results.every(Boolean);
|
||||
}
|
||||
|
||||
export async function hasValidBoardReports(
|
||||
type: string | undefined,
|
||||
parameters: BoardParameters = {},
|
||||
) {
|
||||
const board = { type, parameters };
|
||||
const reportCache = new Map<string, ReturnType<typeof getReport>>();
|
||||
|
||||
for (const row of parameters.rows ?? []) {
|
||||
for (const column of row.columns ?? []) {
|
||||
const component = column.component;
|
||||
if (!component) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!(await isValidBoardReportReference(board, component, reportCache))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
export async function stripInvalidBoardReports(
|
||||
type: string | undefined,
|
||||
parameters: BoardParameters = {},
|
||||
) {
|
||||
const board = { type, parameters };
|
||||
const reportCache = new Map<string, ReturnType<typeof getReport>>();
|
||||
let hasChanges = false;
|
||||
|
||||
const rows = await Promise.all(
|
||||
(parameters.rows ?? []).map(async row => {
|
||||
const columns = await Promise.all(
|
||||
row.columns.map(async column => {
|
||||
const component = column.component;
|
||||
|
||||
if (!component || (await isValidBoardReportReference(board, component, reportCache))) {
|
||||
return column;
|
||||
}
|
||||
|
||||
hasChanges = true;
|
||||
|
||||
const { reportId: _reportId, ...nextProps } = component.props ?? {};
|
||||
|
||||
return {
|
||||
...column,
|
||||
component: {
|
||||
...component,
|
||||
props: Object.keys(nextProps).length > 0 ? nextProps : undefined,
|
||||
},
|
||||
};
|
||||
}),
|
||||
);
|
||||
|
||||
return {
|
||||
...row,
|
||||
columns,
|
||||
};
|
||||
}),
|
||||
);
|
||||
|
||||
if (!hasChanges) {
|
||||
return parameters;
|
||||
}
|
||||
|
||||
return {
|
||||
...parameters,
|
||||
rows,
|
||||
};
|
||||
}
|
||||
|
||||
export async function canViewBoard({ user, shareToken }: Auth, boardId: string) {
|
||||
if (user?.isAdmin) {
|
||||
return true;
|
||||
|
||||
Reference in new issue
Block a user