openfaas / templates

OpenFaaS Classic templates
https://www.openfaas.com
MIT License
279 stars 227 forks source link

Add env vars to use raw body and set max json size #195

Closed burtonr closed 4 years ago

burtonr commented 4 years ago

Description

This change adds the use of 2 environment variables: RAW_BODY and MAX_JSON_SIZE, allowing users to receive the request body as a buffer (the 'raw' body) as well as the ability to set the maximum JSON body size. The raw body is required when processing Stripe webhooks The max JSON body size is defaulted to a low limit from body-parser which has caused issues with users in the past.

Signed-off-by: Burton Rheutan rheutan7@gmail.com

Motivation and Context

There was some discussion in the issue linked below. I've decided to implement the change in this was so that it was a more overall change rather than adding it to a specific verb (POST) to avoid surprising results when a user sets the RAW_BODY to true, it won't be limited only to post requests.

This also means that PUT, DELETE, and GET requests will also use the raw parser, but I think that would be understood when a user creates a function and explicitly sets the RAW_BODY variable to true expecting that the raw request body will be used.

Which issue(s) this PR fixes

Fixes #191

How Has This Been Tested?

Created sample function to print request body for verifying the type received by the function code.

Using Postman, called the function with application/json request.

For the MAX_JSON_SIZE testing: I've used an example request from HugeJsonViewer example that is 1.5mb

Using Postman, called the function with a large request body:

node-raw.1.nil4m16x2t2q@pop-os    | 2020/02/01 20:45:49 stderr: PayloadTooLargeError: request entity too large
node-raw.1.nil4m16x2t2q@pop-os    | 2020/02/01 20:45:49 stderr:     at IncomingMessage.onData (/home/app/node_modules/raw-body/index.js:246:12)
node-raw.1.nil4m16x2t2q@pop-os    | 2020/02/01 20:45:49 stderr:     at IncomingMessage.emit (events.js:210:5)
node-raw.1.nil4m16x2t2q@pop-os    | 2020/02/01 20:45:49 stderr:     at addChunk (_stream_readable.js:308:12)
node-raw.1.nil4m16x2t2q@pop-os    | 2020/02/01 20:45:49 stderr:     at readableAddChunk (_stream_readable.js:289:11)
node-raw.1.nil4m16x2t2q@pop-os    | 2020/02/01 20:45:49 stderr:     at IncomingMessage.Readable.push (_stream_readable.js:223:10)
node-raw.1.nil4m16x2t2q@pop-os    | 2020/02/01 20:45:49 stderr:     at HTTPParser.parserOnBody (_http_common.js:128:22)
node-raw.1.nil4m16x2t2q@pop-os    | 2020/02/01 20:45:49 POST / - 413 Payload Too Large - ContentLength: 597

Adding the following to the fn1.yml:

    environment:
      MAX_JSON_SIZE: '5mb'

The result was successful:

node-raw.1.dzsb7o50ahso@pop-os    | 2020/02/01 20:49:45 stdout:   ... 237 more items
node-raw.1.dzsb7o50ahso@pop-os    | 2020/02/01 20:49:45 stdout: ]
node-raw.1.dzsb7o50ahso@pop-os    | 2020/02/01 20:49:45 POST / - 200 OK - ContentLength: 5424

Types of changes

Impact to existing users

None as the changes are wrapped in an environment variable. Verified the function works as expected without the env vars defined or used.

Checklist: