8000 Add `rawBody` option to disable body parsing on server · Issue #705 · ts-rest/ts-rest · GitHub
[go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rawBody option to disable body parsing on server #705

Open
nickgarlis opened this issue Oct 9, 2024 · 1 comment
Open

Add rawBody option to disable body parsing on server #705

nickgarlis opened this issue Oct 9, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@nickgarlis
Copy link

Problem

There are use-cases where it would be useful to disable body parsing even if the content-type is application/json. An example would be verifying Stripe's webhook signatures.

From their docs:

Stripe requires the raw body of the request to perform signature verification. If you’re using a framework, make sure it doesn’t manipulate the raw body. Any manipulation to the raw body of the request causes the verification to fail.

Proposal
Adding a rawBody flag on the contract could allow users to disable this automatic behavior when necessary.

Implementation
I was thinking of modifying the following function to something like this.

  const evaluateContent = async (request: TsRestRequest) => {
    if (request.method !== 'GET' && request.method !== 'HEAD') {
      if (request.headers.get('content-type')?.includes('json') && !schema.rawBody) {
        request['content'] = await request.json();
      } else if (request.headers.get('content-type')?.startsWith('text/') || schema.rawBody) {
        request['content'] = await request.text();
      }
    }
  };

If this is of interest then I could open a Pull Request with the proposed changes and some tests.

Additional context
I have only used the serverless Next handler and admittedly I haven't given this too much of a thought but perhaps this is something that the other handlers could benefit from ?

@nickgarlis nickgarlis added the enhancement New feature or request label Oct 9, 2024
@antonio-ivanovski
Copy link
Contributor

Personally I have modified the JSON parsing of Express, not sure if the same approach is applicable for you.

application.use(
    express.json({
        verify: (req, _res, buf) => {
            if (req.url && options.rawBodyEndpoints?.includes(req.url)) {
                Object.assign(req, { rawBody: buf.toString() });
            }
        },
    }),
);

options.rawBodyEndpoints includes list of endpoints that should receive the rawBody.

On the consumer side, I have method for getting the rawBody param

export function requestRawBody(req: Request): string | Buffer | undefined {
    if (!('rawBody' in req)) return undefined;
    if (typeof req.rawBody === 'string' || Buffer.isBuffer(req.rawBody)) return req.rawBody;
    console.warn('rawBody is not a string or Buffer');
    return undefined;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants
0