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

Functions event.isbase64Encoded discrepancy in CLI vs Production #3315

Closed
hrishikesh-k opened this issue Sep 9, 2021 · 2 comments · Fixed by #3631
Closed

Functions event.isbase64Encoded discrepancy in CLI vs Production #3315

hrishikesh-k opened this issue Sep 9, 2021 · 2 comments · Fixed by #3631
Assignees
Labels
area: functions type: bug code to address defects in shipped code

Comments

@hrishikesh-k
Copy link

hrishikesh-k commented Sep 9, 2021

Describe the bug:

The production server encodes the function's event.body to base64 except for a few content-types as mentioned here: https://answers.netlify.com/t/changed-behavior-in-function-body-encoding/19000.

When submitting a file using a (non-Netlify) form to a Netlify Function, we have to use multipart/form-data content-type which is encoded according to the above post. Thus, we need to decode it as:

Buffer.from(event.body, 'base64').toString('utf8')

However, when doing the same in CLI, the body is not encoded and available easily as event.body.

It can easily be worked around like:

if (event.isbase64Encoded) {
  Buffer.from(event.body, 'base64').toString('utf8')
} else {
  event.body
}

But, it leads to confusion.

To reproduce:

Steps to reproduce the behavior:

  1. Clone this commit: https://github.com/Hrishikesh-K/formFile/tree/1f7d002c18eff4830614d6d405d84cee4dd106e8.
  2. Navigate to the cloned repository.
  3. Run netlify dev.
  4. Try to submit the form with any random file and log event.body. Then, log the same in production environment.

Configuration:

netlify.toml:

[build]
  command = "echo Hi!"
  publish = "dist"

[functions]
  directory = "functions"
  node_bundler = "esbuild"

System info:

  System:
    OS: macOS 11.5.2
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 746.86 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.9.0 - /usr/local/bin/node
    npm: 7.21.1 - /usr/local/bin/npm

Expected behaviour:

The CLI should encode event.body to base64.

Additional context:

Slack discussion (INTERNAL ONLY): https://netlify.slack.com/archives/C023PC3D08J/p1631194079008400

@hrishikesh-k hrishikesh-k added type: bug code to address defects in shipped code area: functions labels Sep 9, 2021
@bettse
Copy link

bettse commented Nov 4, 2021

I just ran into this myself recently.
I haven't found where the base64 encoding is happening for production, but I believe the discrepancy in the CLI is in this regex: https://github.com/netlify/cli/blob/main/src/lib/functions/utils.js#L6

I suspect adding multipart/form-data would align the experience.

@bettse
Copy link

bettse commented Nov 5, 2021

I tracked down what causes this in production, I'll drop the details in slack (https://netlify.slack.com/archives/C023PC3D08J/p1636131535288700)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: functions type: bug code to address defects in shipped code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants