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

fix(grpc-gateway): handle null values in payload #10687

Merged
merged 6 commits into from Apr 18, 2023
Merged

fix(grpc-gateway): handle null values in payload #10687

merged 6 commits into from Apr 18, 2023

Conversation

ghost
Copy link

@ghost ghost commented Apr 17, 2023

Summary

#10658

Problem: When using the grpc-gateway plugin, if there is a null in the payload of the client request, such as:

{
    "step_id": "INT.L3-222",
    "step_parent_id": "INT.L2-64",
    "name": null
}

an error will be thrown: bad argument #2 to 'encode' (string expected for field 'object_type', got userdata), causing Kong to return 500 to the client, which is an error because Kong did not handle this edge case.

Cause: In JSON, null is valid and legal. However, after this payload is decoded by cjson, null actually becomes cjson.null in Kong, and its data type is userdata, but the name is defined as a string type in the proto file.

Solution: Use pcall to wrap the pb.encode function call, record the error log, and convert the error into a common client parameter error.

Checklist

Full changelog

  • [Implement ...]

Issue reference

Fix #[issue number]

@ghost ghost marked this pull request as ready for review April 17, 2023 10:42
Copy link
Contributor

@hanshuebner hanshuebner left a comment

Choose a reason for hiding this comment

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

Please provide a useful summary in your pull request description. What is the problem, what is the cause, what is the solution. It can be short, but it needs to describe what the change is about.

@@ -82,6 +82,8 @@
[#9903](https://github.com/Kong/kong/pull/9903)

### Fixes
- **gRPC gateway**: Fix an issue where handling `null` in JSON payload
[#10687](https://github.com/Kong/kong/pull/10687)
Copy link
Contributor

Choose a reason for hiding this comment

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

Please be specific about what has been fixed. "Fix an issue" is not helpful.

Copy link
Author

Choose a reason for hiding this comment

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

updated, PTAL

@ghost
Copy link
Author

ghost commented Apr 18, 2023

Please provide a useful summary in your pull request description. What is the problem, what is the cause, what is the solution. It can be short, but it needs to describe what the change is about.

updated

@hanshuebner hanshuebner merged commit 8cd65ec into Kong:master Apr 18, 2023
@hbagdi
Copy link
Member

hbagdi commented Apr 18, 2023

@hanshuebner Could you please cherry pick this change into EE?

@arunpk-zageno
Copy link

@sabertobihwy does the fix accepts the JSON payload with null? or it will just throw an error says that "Null in Json"?

@ghost
Copy link
Author

ghost commented Apr 21, 2023

does the fix accepts the JSON payload with null? or it will just throw an error says that "Null in Json"?

it does't convert null in JSON payload to a string value, and will return failed to encode payload.

@arunpk-zageno
Copy link

But that doesn't fix the actual problem right?
As per the suggestion from @attenuation #10658 (comment) and our expectation, the fix should allow null values irrespective of what client throws us.

@ghost
Copy link
Author

ghost commented Apr 24, 2023

the fix should allow null values irrespective of what client throws us.

The error occurred due to a type matching error in lua-protobuf. If you want to pass null to upstream after encoding, there should be other ways, including redefining the properties of the field in the proto file to allow the field to accept both string|null attributes.

@oowl
Copy link
Member

oowl commented Apr 24, 2023

@arunpk-zageno This PR has fit my suggestion. After patched this PR, Kong can not return 5xx error code, returning 400 error. 500 error means is Kong internal error, it will print a traceback in the error log, and this is not expected behavior. The correct behavior is Kong at a very early age finds this mistake and tells customers that are not allowed to input.

@arunpk-zageno
Copy link

Got it @attenuation , And thanks @sabertobihwy for the fix. Which version I could expect this fix?

@oowl
Copy link
Member

oowl commented Apr 24, 2023

I think it will release in 3.3

@arunpk-zageno
Copy link

Sorry for iterative question, when can we expect the release?

@oowl
Copy link
Member

oowl commented Apr 24, 2023

Expected time is May 15th

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants