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

Invalid request payload JSON format #4461

Closed
mgmorcos opened this issue Sep 29, 2023 · 2 comments
Closed

Invalid request payload JSON format #4461

mgmorcos opened this issue Sep 29, 2023 · 2 comments
Labels
non issue Issue is not a problem or requires changes support Questions, discussions, and general support

Comments

@mgmorcos
Copy link

Support plan

  • is this issue currently blocking your project? (yes/no): no
  • is this issue affecting a production system? (yes/no): no

Context

  • node version: 20.6.1
  • module version with issue: 21.3.2
  • last module version without issue: ?
  • environment (e.g. node, browser, native): node
  • used with (e.g. hapi application, another framework, standalone, ...): hapi application
  • any other relevant information:

What are you trying to achieve or the steps to reproduce?

I have a valid, large JSON example that complies with a Joi spec, which I verified as follows:

// array of large json documents
import inputExamples from '../data/input-data.json' assert {type: 'json'}
// joi schema
import inputSpec from '../specs/input.js'
// logs value and no error
console.log(inputSpec.validate(inputExamples[0]))

There is no error importing or validating the JSON example.

What was the result you got?

Sending the exact same validated JSON example to a Hapi endpoint results in a rapidly returned:

{
    "statusCode": 400,
    "error": "Bad Request",
    "message": "Invalid request payload JSON format"
}

I have over-allocated maxBytes for payloads to ~100MB server-wide (the request body is ~1MB). I have enabled debug mode request logging (*). I have added logging to the route handler. I have added failAction handlers with logging to the route's payload and response validation configurations. The large request seems not to reach any of these handlers. Sending a much smaller JSON example to the endpoint works.

What result did you expect?

I expect the large request to reach the same handlers that the small request does, since they are both valid and under size limit.

@mgmorcos mgmorcos added the support Questions, discussions, and general support label Sep 29, 2023
@mgmorcos
Copy link
Author

I did some analysis with Bourne.parse and JSON.parse. The issue is unrelated to request size and is only related to JSON string formatting. Consider the following examples:

try {
  JSON.parse(`{"foo":"bar\""}`)
} catch (e) {
  console.error(e)
}

SyntaxError: Expected ',' or '}' after property value in JSON at position 12

try {
  JSON.parse(String.raw`{"foo":"bar\""}`)
} catch (e) {
  console.error(e)
}

No error

Is there any way to configure Hapi/Bourne to parse the JSON string as a raw string as in the second example? This would fix the issue.

@mgmorcos
Copy link
Author

After more analysis the issue originates in the API client (Yaade), not Hapi, so I have opened an issue there.

@Nargonath Nargonath added the non issue Issue is not a problem or requires changes label Oct 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
non issue Issue is not a problem or requires changes support Questions, discussions, and general support
Projects
None yet
Development

No branches or pull requests

2 participants