Skip to content

Commit

Permalink
fix(grpc): empty message cannot be unframed (#10836)
Browse files Browse the repository at this point in the history
We found an issue when testing the fix of #10801. When a service responds with a message with all default value fields, the JSON response would be totally empty.

The reason for this issue is that we assume a message to be at least of length 1 when unframing.

Removing this restriction (a message can be of length 0) fixes this issue.

Fix FTI-5054
Fix #10802
  • Loading branch information
StarlightIbuki authored May 15, 2023
1 parent 656ba9b commit 47ab666
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@

#### Plugins

- **grpc-gateway**: Fixed an issue that empty (all default value) messages cannot be unframed correctly.
[#10836](https://github.com/Kong/kong/pull/10836)

#### PDK

### Fixes
Expand Down
6 changes: 5 additions & 1 deletion kong/tools/grpc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ end

--- wraps a binary payload into a grpc stream frame.
function _M.frame(ftype, msg)
-- byte 0: frame type
-- byte 1-4: frame size in big endian (could be zero)
-- byte 5-: frame content
return bpack("C>I", ftype, #msg) .. msg
end

Expand All @@ -149,7 +152,8 @@ end
--- If heading frame isn't complete, returns `nil, body`,
--- try again with more data.
function _M.unframe(body)
if not body or #body <= 5 then
-- must be at least 5 bytes(frame header)
if not body or #body < 5 then
return nil, body
end

Expand Down
2 changes: 1 addition & 1 deletion spec/03-plugins/28-grpc-gateway/01-proxy_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ for _, strategy in helpers.each_strategy() do
end)

-- Bug found when test FTI-5002's fix. It will be fixed in another PR.
pending("empty message #10802", function()
test("empty message #10802", function()
local req_body = { array = {}, nullable = "" }
local res, _ = proxy_client:post("/v1/echo", {
headers = { ["Content-Type"] = "application/json" },
Expand Down

1 comment on commit 47ab666

@khcp-gha-bot
Copy link

Choose a reason for hiding this comment

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

Bazel Build

Docker image available kong/kong:47ab666e99c3a334e27c9f0e9e0546daaea30ee7
Artifacts available https://github.com/Kong/kong/actions/runs/4976152553

Please sign in to comment.