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 env vars to use raw body and set max json size #195

Merged
merged 1 commit into from
Feb 3, 2020
Merged

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

merged 1 commit into from
Feb 3, 2020

Conversation

burtonr
Copy link
Contributor

@burtonr burtonr commented Feb 1, 2020

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 [email protected]

Motivation and Context

  • I have raised an issue to propose this change (required)

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.

  • No changes - { some: 'thing' }
  • env var RAW_BODY: true - <Buffer 7b 0a 09 22 73 6f 6d 65 22 3a 20 22 74 68 69 6e 67 22 0a 7d>

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

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Version change (see: Impact to existing users)

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:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I've read the CONTRIBUTION guide
  • I have signed-off my commits with git commit -s
  • I have added tests to cover my changes.
  • All new and existing tests passed.

This change adds the use of 2 environment variables 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 <[email protected]>
app.use(bodyParser.json());
app.use(bodyParser.raw());
app.use(bodyParser.text({ type : "text/*" }));
if (process.env.RAW_BODY === 'true') {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @burtonr

I'd suggest to keep support of json and text parsers even when RAW_BODY is true.

To keep completely old behavior I suggest inverting the variable name something like NORAW_BODY and enable raw parser by default.

jsonLimit can be also set only if MAX_JSON_SIZE is specified

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest to keep support of json and text parsers even when RAW_BODY is true.

I don't see the point in that change, that's the behaviour we have today?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand this correctly RAW_BODY should just enable/disable raw parser, is not it?

If it is set to true the app will have no JSON and TEXT parsers

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use-case was that users need to disable JSON parsing and only access a RAW body. The docs PR I hope helps with this, but I'd like @burtonr to explain the use-case better there. https://docs.openfaas.com/cli/templates/#nodejs-12-node12-of-watchdog-template

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RAW_BODY confuses me since it was TLDR. maybe ONLY_RAW name is better?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right, but it's important to understand that the bodyParser.raw() only enables the raw body for the content type that you pass in. So, this change makes it so that when a user sets the RAW_BODY variable to true, the raw body will be available for all content types.
The idea is that a user would define a very specific function that requires the raw body, where other functions will not have this enabled and have the JSON body available as normal.

Copy link

@sergeyt sergeyt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@burtonr please see my comments inlined

Copy link
Member

@alexellis alexellis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@alexellis alexellis merged commit 465644c into openfaas:master Feb 3, 2020
@burtonr burtonr deleted the node12-raw-body branch February 3, 2020 14:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support request for node12 template and Stripe middleware
3 participants