Merge pull request #4317 from umami-software/opencode/nimble-planet
Fix board share entity authorization
This commit is contained in:
7 files changed
+252
-10
No files matched your search
@@ -0,0 +1,84 @@
|
||||
import { beforeEach, expect, test, vi } from 'vitest';
|
||||
import { BOARD_TYPES } from '@/lib/boards';
|
||||
import { canViewBoardEntities } from './board';
|
||||
import { canViewLink } from './link';
|
||||
import { canViewPixel } from './pixel';
|
||||
import { canViewWebsite } from './website';
|
||||
|
||||
vi.mock('@/queries/prisma', () => ({
|
||||
getBoard: vi.fn(),
|
||||
getTeamUser: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/prisma', () => ({
|
||||
default: {},
|
||||
}));
|
||||
|
||||
vi.mock('./website', () => ({
|
||||
canViewWebsite: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('./pixel', () => ({
|
||||
canViewPixel: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('./link', () => ({
|
||||
canViewLink: vi.fn(),
|
||||
}));
|
||||
|
||||
const auth = {
|
||||
user: {
|
||||
id: 'user-1',
|
||||
username: 'user',
|
||||
role: 'user',
|
||||
isAdmin: false,
|
||||
},
|
||||
shareToken: {
|
||||
websiteIds: ['victim-website-id'],
|
||||
},
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
vi.mocked(canViewWebsite).mockReset();
|
||||
vi.mocked(canViewPixel).mockReset();
|
||||
vi.mocked(canViewLink).mockReset();
|
||||
});
|
||||
|
||||
test('canViewBoardEntities validates board IDs with user auth only', async () => {
|
||||
vi.mocked(canViewWebsite).mockResolvedValue(true);
|
||||
|
||||
await expect(
|
||||
canViewBoardEntities(auth, BOARD_TYPES.website, { websiteId: 'owned-website-id' }),
|
||||
).resolves.toBe(true);
|
||||
|
||||
expect(canViewWebsite).toHaveBeenCalledWith(
|
||||
{
|
||||
user: auth.user,
|
||||
},
|
||||
'owned-website-id',
|
||||
);
|
||||
});
|
||||
|
||||
test('canViewBoardEntities rejects IDs not accessible to the user', async () => {
|
||||
vi.mocked(canViewWebsite).mockResolvedValue(false);
|
||||
|
||||
await expect(
|
||||
canViewBoardEntities(auth, BOARD_TYPES.mixed, {
|
||||
rows: [
|
||||
{
|
||||
id: 'row-1',
|
||||
columns: [
|
||||
{
|
||||
id: 'column-1',
|
||||
component: {
|
||||
type: 'WebsiteChart',
|
||||
entityType: 'website',
|
||||
entityId: 'victim-website-id',
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
}),
|
||||
).resolves.toBe(false);
|
||||
});
|
||||
@@ -1,7 +1,37 @@
|
||||
import { hasPermission } from '@/lib/auth';
|
||||
import { getBoardEntityIds } from '@/lib/boards';
|
||||
import { PERMISSIONS } from '@/lib/constants';
|
||||
import type { Auth } from '@/lib/types';
|
||||
import type { Auth, BoardParameters } from '@/lib/types';
|
||||
import { getBoard, getTeamUser } from '@/queries/prisma';
|
||||
import { canViewLink } from './link';
|
||||
import { canViewPixel } from './pixel';
|
||||
import { canViewWebsite } from './website';
|
||||
|
||||
async function checkBoardEntityAccess(check: Promise<boolean>) {
|
||||
try {
|
||||
return await check;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export async function canViewBoardEntities(
|
||||
auth: Auth,
|
||||
type: string | undefined,
|
||||
parameters: BoardParameters = {},
|
||||
) {
|
||||
const { websiteIds, pixelIds, linkIds } = getBoardEntityIds({ type, parameters });
|
||||
const userOnlyAuth: Auth = { user: auth.user };
|
||||
const checks = [
|
||||
...websiteIds.map(id => checkBoardEntityAccess(canViewWebsite(userOnlyAuth, id))),
|
||||
...pixelIds.map(id => checkBoardEntityAccess(canViewPixel(userOnlyAuth, id))),
|
||||
...linkIds.map(id => checkBoardEntityAccess(canViewLink(userOnlyAuth, id))),
|
||||
];
|
||||
|
||||
const results = await Promise.all(checks);
|
||||
|
||||
return results.every(Boolean);
|
||||
}
|
||||
|
||||
export async function canViewBoard({ user, shareToken }: Auth, boardId: string) {
|
||||
if (user?.isAdmin) {
|
||||
|
||||
@@ -22,6 +22,10 @@ export async function canViewLink({ user, shareToken }: Auth, linkId: string) {
|
||||
|
||||
const link = await getLink(linkId);
|
||||
|
||||
if (!link) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (link.userId) {
|
||||
return user.id === link.userId;
|
||||
}
|
||||
@@ -46,6 +50,10 @@ export async function canUpdateLink({ user }: Auth, linkId: string) {
|
||||
|
||||
const link = await getLink(linkId);
|
||||
|
||||
if (!link) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (link.userId) {
|
||||
return user.id === link.userId;
|
||||
}
|
||||
@@ -70,6 +78,10 @@ export async function canDeleteLink({ user }: Auth, linkId: string) {
|
||||
|
||||
const link = await getLink(linkId);
|
||||
|
||||
if (!link) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (link.userId) {
|
||||
return user.id === link.userId;
|
||||
}
|
||||
|
||||
@@ -22,6 +22,10 @@ export async function canViewPixel({ user, shareToken }: Auth, pixelId: string)
|
||||
|
||||
const pixel = await getPixel(pixelId);
|
||||
|
||||
if (!pixel) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pixel.userId) {
|
||||
return user.id === pixel.userId;
|
||||
}
|
||||
@@ -46,6 +50,10 @@ export async function canUpdatePixel({ user }: Auth, pixelId: string) {
|
||||
|
||||
const pixel = await getPixel(pixelId);
|
||||
|
||||
if (!pixel) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pixel.userId) {
|
||||
return user.id === pixel.userId;
|
||||
}
|
||||
@@ -70,6 +78,10 @@ export async function canDeletePixel({ user }: Auth, pixelId: string) {
|
||||
|
||||
const pixel = await getPixel(pixelId);
|
||||
|
||||
if (!pixel) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pixel.userId) {
|
||||
return user.id === pixel.userId;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user