mirror of
https://github.com/documenso/documenso.git
synced 2026-07-11 13:35:20 +10:00
6b1b1d0417
Webhook URLs were being fetched without validating whether they resolved to private/loopback addresses, exposing the server to SSRF. Current SSRF is best effort and fail open, you should never host services that you cant risk exposure of. This extracts webhook execution into a shared module that validates URLs against private IP ranges (including DNS resolution), enforces timeouts, and disables redirect following. The resend route now queues through the job system instead of calling fetch inline.
48 lines
1.2 KiB
TypeScript
48 lines
1.2 KiB
TypeScript
import { AppError } from '@documenso/lib/errors/app-error';
|
|
import { prisma } from '@documenso/prisma';
|
|
|
|
import { assertNotPrivateUrl } from '../assert-webhook-url';
|
|
import { validateApiToken } from './validateApiToken';
|
|
|
|
export const subscribeHandler = async (req: Request) => {
|
|
try {
|
|
const authorization = req.headers.get('authorization');
|
|
|
|
if (!authorization) {
|
|
return new Response('Unauthorized', { status: 401 });
|
|
}
|
|
|
|
const { webhookUrl, eventTrigger } = await req.json();
|
|
|
|
await assertNotPrivateUrl(webhookUrl);
|
|
|
|
const result = await validateApiToken({ authorization });
|
|
|
|
const createdWebhook = await prisma.webhook.create({
|
|
data: {
|
|
webhookUrl,
|
|
eventTriggers: [eventTrigger],
|
|
secret: null,
|
|
enabled: true,
|
|
userId: result.userId ?? result.user.id,
|
|
teamId: result.teamId ?? undefined,
|
|
},
|
|
});
|
|
|
|
return Response.json(createdWebhook);
|
|
} catch (err) {
|
|
if (err instanceof AppError) {
|
|
return Response.json({ message: err.message }, { status: 400 });
|
|
}
|
|
|
|
console.error(err);
|
|
|
|
return Response.json(
|
|
{
|
|
message: 'Internal Server Error',
|
|
},
|
|
{ status: 500 },
|
|
);
|
|
}
|
|
};
|