You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vercel Edge部署Probot出现签名不匹配问题求助

解决Vercel Edge上Probot签名不匹配问题

针对你遇到的[@octokit/webhooks] signature does not match event payload and secret错误,结合Vercel Edge Runtime和Probot的特性,给出以下排查和修复方向:

1. 确保Edge函数获取原始请求体

Vercel Edge Runtime默认会自动解析请求体,但Probot的签名验证依赖原始未解析的请求体字符串。如果直接使用解析后的JSON对象,会导致签名计算不匹配。修改你的Edge处理函数,手动获取原始文本:

export const config = {
  runtime: "edge",
};

export default async function handler(req: Request) {
  // 获取原始请求体文本
  const rawBody = await req.text();
  const headers = Object.fromEntries(req.headers);

  try {
    await probot.webhooks.verifyAndReceive({
      id: headers["x-github-delivery"] as string,
      name: headers["x-github-event"] as string,
      signature: headers["x-hub-signature-256"] as string,
      payload: rawBody,
    });
    return new Response("Webhook processed", { status: 200 });
  } catch (error) {
    console.error("Webhook verification failed:", error);
    return new Response("Invalid signature", { status: 403 });
  }
}

2. 修正ESM环境下的变量读取与私钥处理

从CJS转ESM后,环境变量读取方式变为import.meta.env,同时私钥在Vercel环境变量中可能被转义(换行符变为\\n),需要还原:

import { Probot } from "probot";

const probot = new Probot({
  appId: Number(import.meta.env.APP_ID),
  // 还原私钥中的换行符
  privateKey: import.meta.env.PRIVATE_KEY.replace(/\\n/g, "\n"),
  secret: import.meta.env.WEBHOOK_SECRET,
});

注意:粘贴私钥到Vercel环境变量时,不要手动添加多余的换行或空格,直接复制GitHub生成的完整私钥文本。

3. 验证Webhook配置的一致性

  • 确认GitHub App的Webhook URL完全匹配Vercel部署的地址(包含完整路径,比如https://your-vercel-app.vercel.app/api/webhook)
  • 检查GitHub的Webhook Secret与Vercel环境变量中的WEBHOOK_SECRET完全一致,无大小写错误、空格或截断

4. 排查Probot与Edge Runtime的兼容性

部分Probot依赖可能默认使用Node.js特定API(如crypto模块的某些方法),而Vercel Edge Runtime使用Web Crypto API。确保你使用的Probot版本已适配Edge环境,若版本较旧,尝试升级到最新稳定版。

5. 本地模拟Edge环境调试

使用Vercel CLI的vercel dev命令在本地模拟Edge Runtime,结合内网穿透工具将本地服务暴露给GitHub,对比本地计算的签名与GitHub发送的签名是否一致,排查请求体在传输或处理过程中是否被修改。

内容的提问来源于stack exchange,提问作者ihaveaquestion

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 23:17:43