部署前审查这个由 AI 生成的 Webhook 投递函数。
只向已配置的合作方端点发送小型 JSON 事件,阻止访问内网和泄露凭据,重新校验重定向,并返回有界的投递结果。
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 };
}
生成代码仅作示例,不代表任何特定模型