【问题标题】:GCP: Stripe webhook error: No signatures found matching the expected signature for payloadGCP:Stripe webhook 错误:未找到与有效负载的预期签名匹配的签名
【发布时间】:2021-02-19 07:40:19
【问题描述】:

条带版本:“8.107.0”

每当我在 GCP 上运行我的 webhook 时,我都会收到 Stripe webhook 验证错误。我尝试在签名中使用原始正文,如下面的代码 sn-p 所述,以及其他 StackOverflow 答案提到的传递 req.rawBody 的其他方法。

奇怪的是,这个错误似乎是在我部署到 GCP 时抛出的,而不是当我在本地运行时抛出的。我尝试手动创建签名 (https://stripe.com/docs/webhooks/signatures#verify-manually),结果相同:本地签名匹配,在 GCP 上不匹配。

我们的服务器托管在 GCP GKE 上,我们通过 Nginx 反向代理向我们的服务器提供请求。其他堆栈溢出解决方案提到了 Google Cloud Functions 和 Lambda。据我所知,我们不解析 GCP 上的请求

我确实使用 bodyParser.json(),但这是在此端点之后设置的。这些是我尝试创建/使用 rawBody 的方式:

app.use(express.json({verify: (req,res,buf) => { req.rawBody = buf }}));
bodyParser.json({
     verify: (req: any, res, buf) => {
      req.rawBody = buf.toString();
    },
  }),
event = stripe.webhooks.constructEvent(req.rawBody, sig, webhookSecret);

我的代码基于此处找到的条带示例:https://github.com/stripe/stripe-node/blob/master/examples/webhook-signing/node-express/express.js

// Stripe requires the raw body to construct the event
app.post('/webhook', bodyParser.raw({type: 'application/json'}), (req, res) => {
  const sig = req.headers['stripe-signature'];

  let event;

  try {
    event = stripe.webhooks.constructEvent(req.body, sig, webhookSecret);
  } catch (err) {
    // On error, log and return the error message
    console.log(`❌ Error message: ${err.message}`);
    return res.status(400).send(`Webhook Error: ${err.message}`);
  }

  // Successfully constructed event
  console.log('✅ Success:', event.id);

  // Return a response to acknowledge receipt of the event
  res.json({received: true});
});

任何帮助将不胜感激,谢谢。

【问题讨论】:

    标签: javascript node.js express google-cloud-platform stripe-payments


    【解决方案1】:

    我刚刚解决了一个类似的问题,简而言之,我是这样解决的:

     // main.js
     server.use('*', cors());
    -server.use(express.json()); // default on my boilerplate
    +server.use(bodyParser.raw({ type: 'application/json' })); // use body-parser instead
     server.use(morgan('tiny'));
    

    然后,请确保在处理 webhook 之前不要重写 bodyParser.raw({type: 'application/json'}),也不要在应用中的任何其他地方重写。

    【讨论】:

      【解决方案2】:

      问题在于我们的一个设置文件,其中基本上一个空格或 \n 字符被添加到我们的 webhookSecret

      【讨论】:

      • 有趣!很高兴听到你想通了。不修改原始主体至关重要,因此这是有道理的。知道如何/为什么会这样吗?
      猜你喜欢
      • 2019-11-10
      • 2019-09-04
      • 2019-05-22
      • 2022-12-12
      • 2020-07-07
      • 2021-03-14
      • 2020-04-25
      • 2021-07-26
      • 1970-01-01
      相关资源
      最近更新 更多