Review generated webhook delivery

from Server-side request forgery
Node 24 advanced 6 min 5 issues to find

Review this AI-generated webhook delivery function before deployment.

Send a small JSON event only to configured partner endpoints, prevent internal-network access and credential leakage, revalidate redirects, and return a bounded delivery result.

JavaScript
async function deliverWebhook(request, fetchImpl) {
  const target = new URL(request.body.url);
  if (!target.hostname.includes('partner.example')) {
    return { status: 400, error: 'destination rejected' };
  }
  const response = await fetchImpl(target, {
    method: 'POST',
    redirect: 'follow',
    headers: {
      ...request.headers,
      authorization: `Bearer ${process.env.INTERNAL_TOKEN}`,
      'content-type': 'application/json',
    },
    body: JSON.stringify(request.body.event),
  });
  const body = await response.arrayBuffer();
  console.log('webhook response', target.href, Buffer.from(body).toString());
  return { status: response.status, bytes: body.byteLength };
}

generated code is illustrative, not from any one model

Open in playground
Report an error