NodeJS get如何访问未修改的Fastify请求体

uwopmtnx  于 11个月前  发布在  Node.js
关注(0)|答案(1)|浏览(103)

我正在使用Fastify服务器和Zod验证模式,我的目标是访问Fastify请求的原始,未更改的主体,专门用于验证webhook中的签名。本质上,我想检查请求的初始状态,不受任何中间件的影响。,这是我的代码:

import { z } from "zod";
    import { ZodFastifyInstance } from "@shared/types";
    
    export const checkout-Webhook = (server: ZodFastifyInstance) =>
      server.route({
        method: "POST",
        url: "/api/webhook",
        schema: {
          headers: webhook_Schema,
        },
        async handler(request, reply) {
          const sig = request.headers['stripe-signature'];
          const body = (request.body as string);
      
          await this.checkoutService.tryCrediUser(body, sig);
    
          return reply.code(201).send();
        },
      });
    
    
    const webhook_Schema = z.object({ "stripe-signature": z.string() });

字符串
我得到了一个错误代码:StripeSignatureVerificationError:Webhook payload must be provided as a string or a Buffer(https://nodejs.org/api/buffer.html)instance representing the raw request body.Payload was provided as a parsed JavaScript object instead. Signature verification is impossible without access to the original signed material.
我使用了fastify-row-body,但rawBody是undefined?:

import { z } from "zod";
import { ZodFastifyInstance } from "@shared/types";
import rawbody,{ RawBodyPluginOptions } from 'fastify-raw-body';
import fp from "fastify-plugin";

export default fp<RawBodyPluginOptions>(async (fastify) => {
  fastify.register(rawbody, {
    field: "rawBody", // change the default request.rawBody property name
    global: false, // add the rawBody to every request. **Default true**
    encoding: false, // set it to false to set rawBody as a Buffer **Default utf8**
    runFirst: true, // get the body before any 
preParsing hook change/uncompress it. **Default false**
    routes: ['/api/webhook'], // array of routes, **`global`** will be ignored, wildcard routes not supported
  });
});

export const checkout-webhook = (server: ZodFastifyInstance) =>
  server.route({
    method: "POST",
    url: "/api/webhook",
    config: {
      rawBody: true
    },
    async handler(request, reply) {
      const sig = request.headers['stripe-signature'];
      const body = request.rawBody ;

      await this.checkoutService.tryCrediUser(body, sig);
      return reply.code(201).send();
    },
  })

xqkwcwgp

xqkwcwgp1#

我通过添加preParsing钩子解决了这个问题:

import { z } from "zod";
    import { ZodFastifyInstance } from "@shared/types";
    
    export const checkout-Webhook = (server: ZodFastifyInstance) =>
      server.route({
        method: "POST",
        url: "/api/webhook",
        schema: {
          headers: webhook_Schema,
        },
       preParsing(request, reply, paload, done) {
           request.headers['content-type'] ='text/plain'
           done();
        },
        async handler(request, reply) {
          const sig = request.headers['stripe-signature'];
          const body = (request.body as string);
      
          await this.checkoutService.tryCrediUser(body, sig);
    
          return reply.code(201).send();
        },
      });
    
    
    const webhook_Schema = z.object({ "stripe-signature": z.string() });

字符串

相关问题