Merge branch 'dev' of https://github.com/umami-software/umami into dev
This commit is contained in:
commit
527ef9d769
18 files changed
+348
-130
No files matched your search
@@ -14,7 +14,9 @@ export function SessionInfo({ data }) {
|
||||
return (
|
||||
<Grid columns="repeat(auto-fit, minmax(200px, 1fr)" gap>
|
||||
<Info label={t(labels.distinctId)} icon={<KeyRound />}>
|
||||
{data?.distinctId}
|
||||
<span style={{ overflowWrap: 'anywhere' }}>
|
||||
{data?.distinctId}
|
||||
</span>
|
||||
</Info>
|
||||
|
||||
<Info label={t(labels.lastSeen)} icon={<Calendar />}>
|
||||
|
||||
@@ -11,9 +11,7 @@ export async function POST(request: Request) {
|
||||
}
|
||||
|
||||
if (!redis.enabled) {
|
||||
return serverError({
|
||||
message: 'Redis is disabled',
|
||||
});
|
||||
return serverError('Redis is disabled');
|
||||
}
|
||||
|
||||
const token = await saveAuth({ userId: auth.user.id }, 86400);
|
||||
|
||||
@@ -244,11 +244,6 @@ export async function POST(request: Request) {
|
||||
|
||||
return json({ ok: true });
|
||||
} catch (e) {
|
||||
const error = serializeError(e);
|
||||
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(error);
|
||||
|
||||
return serverError({ errorObject: error });
|
||||
return serverError(e);
|
||||
}
|
||||
}
|
||||
+15
-24
@@ -1,6 +1,5 @@
|
||||
import { startOfHour } from 'date-fns';
|
||||
import { isbot } from 'isbot';
|
||||
import { serializeError } from 'serialize-error';
|
||||
import { z } from 'zod';
|
||||
import clickhouse from '@/lib/clickhouse';
|
||||
import { CACHE_TOKEN_TYPE, COLLECTION_TYPE, EVENT_TYPE } from '@/lib/constants';
|
||||
@@ -24,13 +23,10 @@ interface Cache {
|
||||
// Reject strings whose first character is a spreadsheet formula trigger to
|
||||
// prevent CSV formula injection in analytics exports (defense-in-depth).
|
||||
const FORMULA_TRIGGER_RE = /^[=+\-@\t\r]/;
|
||||
const safeStringParam = (maxLen: number) =>
|
||||
z
|
||||
.string()
|
||||
.max(maxLen)
|
||||
.refine(val => !FORMULA_TRIGGER_RE.test(val), {
|
||||
message: 'Value must not start with =, +, -, @, tab, or carriage return',
|
||||
});
|
||||
const safeStringParam = () =>
|
||||
z.string().refine(val => !FORMULA_TRIGGER_RE.test(val), {
|
||||
message: 'Value must not start with =, +, -, @, tab, or carriage return',
|
||||
});
|
||||
|
||||
const schema = z.object({
|
||||
type: z.enum(['event', 'identify', 'performance']),
|
||||
@@ -40,21 +36,21 @@ const schema = z.object({
|
||||
link: z.uuid().optional(),
|
||||
pixel: z.uuid().optional(),
|
||||
data: anyObjectParam.optional(),
|
||||
hostname: z.string().max(100).optional(),
|
||||
language: z.string().max(35).optional(),
|
||||
hostname: z.string().optional(),
|
||||
language: z.string().optional(),
|
||||
referrer: urlOrPathParam.optional(),
|
||||
screen: z.string().max(11).optional(),
|
||||
title: z.string().max(500).optional(),
|
||||
screen: z.string().optional(),
|
||||
title: z.string().optional(),
|
||||
url: urlOrPathParam.optional(),
|
||||
name: safeStringParam(50).optional(),
|
||||
tag: safeStringParam(50).optional(),
|
||||
name: safeStringParam().optional(),
|
||||
tag: safeStringParam().optional(),
|
||||
ip: z.string().optional(),
|
||||
userAgent: z.string().optional(),
|
||||
timestamp: z.coerce.number().int().optional(),
|
||||
id: z.string().max(50).optional(),
|
||||
browser: z.string().max(20).optional(),
|
||||
os: z.string().max(20).optional(),
|
||||
device: z.string().max(20).optional(),
|
||||
id: z.string().optional(),
|
||||
browser: z.string().optional(),
|
||||
os: z.string().optional(),
|
||||
device: z.string().optional(),
|
||||
lcp: z.number().nonnegative().max(60000).optional(),
|
||||
inp: z.number().nonnegative().max(60000).optional(),
|
||||
cls: z.number().nonnegative().max(100).optional(),
|
||||
@@ -326,11 +322,6 @@ export async function POST(request: Request) {
|
||||
|
||||
return json({ cache: token, sessionId, visitId });
|
||||
} catch (e) {
|
||||
const error = serializeError(e);
|
||||
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(error);
|
||||
|
||||
return serverError({ errorObject: error });
|
||||
return serverError(e);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
import { beforeEach, expect, test, vi } from 'vitest';
|
||||
import { ROLES } from '@/lib/constants';
|
||||
import { parseRequest } from '@/lib/request';
|
||||
import { canDeleteTeamUser, canUpdateTeam } from '@/permissions';
|
||||
import { deleteTeamUser, getTeamUser, updateTeamUser } from '@/queries/prisma';
|
||||
import { DELETE, POST } from './route';
|
||||
|
||||
vi.mock('@/lib/request', () => ({
|
||||
parseRequest: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/permissions', () => ({
|
||||
canDeleteTeamUser: vi.fn(),
|
||||
canUpdateTeam: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/queries/prisma', () => ({
|
||||
deleteTeamUser: vi.fn(),
|
||||
getTeamUser: vi.fn(),
|
||||
updateTeamUser: vi.fn(),
|
||||
}));
|
||||
|
||||
const parseRequestMock = vi.mocked(parseRequest);
|
||||
const canUpdateTeamMock = vi.mocked(canUpdateTeam);
|
||||
const canDeleteTeamUserMock = vi.mocked(canDeleteTeamUser);
|
||||
const getTeamUserMock = vi.mocked(getTeamUser);
|
||||
const updateTeamUserMock = vi.mocked(updateTeamUser);
|
||||
const deleteTeamUserMock = vi.mocked(deleteTeamUser);
|
||||
|
||||
beforeEach(() => {
|
||||
parseRequestMock.mockReset();
|
||||
canUpdateTeamMock.mockReset();
|
||||
canDeleteTeamUserMock.mockReset();
|
||||
getTeamUserMock.mockReset();
|
||||
updateTeamUserMock.mockReset();
|
||||
deleteTeamUserMock.mockReset();
|
||||
});
|
||||
|
||||
test('POST rejects a manager modifying the team owner role', async () => {
|
||||
parseRequestMock.mockResolvedValue({
|
||||
auth: {
|
||||
user: {
|
||||
id: 'manager-id',
|
||||
isAdmin: false,
|
||||
},
|
||||
},
|
||||
body: {
|
||||
role: ROLES.teamViewOnly,
|
||||
},
|
||||
error: undefined,
|
||||
});
|
||||
canUpdateTeamMock.mockResolvedValue(true);
|
||||
getTeamUserMock
|
||||
.mockResolvedValueOnce({
|
||||
id: 'target-team-user',
|
||||
role: ROLES.teamOwner,
|
||||
} as any)
|
||||
.mockResolvedValueOnce({
|
||||
id: 'actor-team-user',
|
||||
role: ROLES.teamManager,
|
||||
} as any);
|
||||
|
||||
const response = await POST(new Request('http://localhost/api/teams/team-1/users/owner-id', { method: 'POST' }), {
|
||||
params: Promise.resolve({ teamId: 'team-1', userId: 'owner-id' }),
|
||||
});
|
||||
|
||||
expect(response.status).toBe(401);
|
||||
expect(updateTeamUserMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('DELETE rejects a manager removing the team owner', async () => {
|
||||
parseRequestMock.mockResolvedValue({
|
||||
auth: {
|
||||
user: {
|
||||
id: 'manager-id',
|
||||
isAdmin: false,
|
||||
},
|
||||
},
|
||||
error: undefined,
|
||||
});
|
||||
canDeleteTeamUserMock.mockResolvedValue(true);
|
||||
getTeamUserMock.mockResolvedValue({
|
||||
id: 'owner-team-user',
|
||||
role: ROLES.teamOwner,
|
||||
} as any);
|
||||
|
||||
const response = await DELETE(
|
||||
new Request('http://localhost/api/teams/team-1/users/owner-id', { method: 'DELETE' }),
|
||||
{
|
||||
params: Promise.resolve({ teamId: 'team-1', userId: 'owner-id' }),
|
||||
},
|
||||
);
|
||||
|
||||
expect(response.status).toBe(401);
|
||||
expect(deleteTeamUserMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('DELETE rejects owner self-removal to prevent orphaning the team', async () => {
|
||||
parseRequestMock.mockResolvedValue({
|
||||
auth: {
|
||||
user: {
|
||||
id: 'owner-id',
|
||||
isAdmin: false,
|
||||
},
|
||||
},
|
||||
error: undefined,
|
||||
});
|
||||
canDeleteTeamUserMock.mockResolvedValue(true);
|
||||
getTeamUserMock.mockResolvedValue({
|
||||
id: 'owner-team-user',
|
||||
role: ROLES.teamOwner,
|
||||
} as any);
|
||||
|
||||
const response = await DELETE(
|
||||
new Request('http://localhost/api/teams/team-1/users/owner-id', { method: 'DELETE' }),
|
||||
{
|
||||
params: Promise.resolve({ teamId: 'team-1', userId: 'owner-id' }),
|
||||
},
|
||||
);
|
||||
|
||||
expect(response.status).toBe(401);
|
||||
expect(deleteTeamUserMock).not.toHaveBeenCalled();
|
||||
});
|
||||
@@ -1,5 +1,5 @@
|
||||
import { z } from 'zod';
|
||||
import { TEAM_ROLE_RANK } from '@/lib/constants';
|
||||
import { ROLES, TEAM_ROLE_RANK } from '@/lib/constants';
|
||||
import { parseRequest } from '@/lib/request';
|
||||
import { badRequest, json, ok, unauthorized } from '@/lib/response';
|
||||
import { teamRoleParam } from '@/lib/schema';
|
||||
@@ -91,6 +91,10 @@ export async function DELETE(
|
||||
return badRequest({ message: 'The User does not exists on this team.' });
|
||||
}
|
||||
|
||||
if (!auth.user.isAdmin && teamUser.role === ROLES.teamOwner) {
|
||||
return unauthorized({ message: 'You do not have permission to remove this user.' });
|
||||
}
|
||||
|
||||
// Server-side rank check: actor must outrank target to remove them.
|
||||
if (!auth.user.isAdmin && userId !== auth.user.id) {
|
||||
const actorTeamUser = await getTeamUser(teamId, auth.user.id);
|
||||
|
||||
Reference in new issue
Block a user