Skip to content
This repository was archived by the owner on May 19, 2025. It is now read-only.

Commit d51cf6b

Browse files
authored
feat: Add throwIfNoAccessToApiKey guard function (boxyhq#1366)
1 parent 52545b7 commit d51cf6b

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

lib/guards/team-api-key.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { getApiKeyById } from 'models/apiKey';
2+
import { ApiError } from '../errors';
3+
4+
export const throwIfNoAccessToApiKey = async (
5+
apiKeyId: string,
6+
teamId: string
7+
) => {
8+
const apiKey = await getApiKeyById(apiKeyId);
9+
10+
if (!apiKey) {
11+
throw new ApiError(404, 'API key not found');
12+
}
13+
14+
if (teamId !== apiKey.teamId) {
15+
throw new ApiError(
16+
403,
17+
'You do not have permission to delete this API key'
18+
);
19+
}
20+
};

models/apiKey.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,15 @@ export const getApiKey = async (apiKey: string) => {
6464
},
6565
});
6666
};
67+
68+
export const getApiKeyById = async (id: string) => {
69+
return prisma.apiKey.findUnique({
70+
where: {
71+
id,
72+
},
73+
select: {
74+
id: true,
75+
teamId: true,
76+
},
77+
});
78+
};

pages/api/teams/[slug]/api-keys/[apiKeyId].ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { recordMetric } from '@/lib/metrics';
66
import env from '@/lib/env';
77
import { ApiError } from '@/lib/errors';
88
import { deleteApiKeySchema, validateWithSchema } from '@/lib/zod';
9+
import { throwIfNoAccessToApiKey } from '@/lib/guards/team-api-key';
910

1011
export default async function handler(
1112
req: NextApiRequest,
@@ -44,6 +45,8 @@ const handleDELETE = async (req: NextApiRequest, res: NextApiResponse) => {
4445

4546
const { apiKeyId } = validateWithSchema(deleteApiKeySchema, req.query);
4647

48+
await throwIfNoAccessToApiKey(apiKeyId, user.team.id);
49+
4750
await deleteApiKey(apiKeyId);
4851

4952
recordMetric('apikey.removed');

pages/api/teams/[slug]/webhooks/[endpointId].ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ export default async function handler(
3838
});
3939
}
4040
} catch (err: any) {
41-
const message = err.message || 'Something went wrong';
42-
const status = err.status || 500;
41+
const message = err?.body?.detail || err.message || 'Something went wrong';
42+
const status = err.status || err.code || 500;
4343

4444
res.status(status).json({ error: { message } });
4545
}
@@ -98,6 +98,8 @@ const handlePUT = async (req: NextApiRequest, res: NextApiResponse) => {
9898
if (eventTypes.length > 0) {
9999
data['filterTypes'] = eventTypes;
100100
}
101+
// Checks if the webhook exists or throws an error
102+
await findWebhook(app.id, endpointId);
101103

102104
const webhook = await updateWebhook(app.id, endpointId, data);
103105

0 commit comments

Comments
 (0)