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(mqtt-proxy): client id can be empty #5816

Merged
merged 1 commit into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions apisix/stream/plugins/mqtt-proxy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,13 @@ local function parse_mqtt(data)
return res
end

res.client_id = str_sub(data, parsed_pos + 1, parsed_pos + client_id_len)
if client_id_len == 0 then
-- A Server MAY allow a Client to supply a ClientID that has a length of zero bytes
res.client_id = ""
else
res.client_id = str_sub(data, parsed_pos + 1, parsed_pos + client_id_len)
end

parsed_pos = parsed_pos + client_id_len

res.expect_len = parsed_pos
Expand All @@ -120,10 +126,10 @@ end


function _M.preread(conf, ctx)
core.log.warn("plugin rewrite phase, conf: ", core.json.encode(conf))
-- core.log.warn(" ctx: ", core.json.encode(ctx, true))
local sock = ngx.req.socket()
local data, err = sock:peek(16)
-- the header format of MQTT CONNECT can be found in
-- https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901033
local data, err = sock:peek(14)
if not data then
core.log.error("failed to read first 16 bytes: ", err)
return 503
Expand Down
18 changes: 18 additions & 0 deletions t/stream-plugin/mqtt-proxy.t
Original file line number Diff line number Diff line change
Expand Up @@ -316,5 +316,23 @@ passed
"\x10\x0f\x00\x04\x4d\x51\x54\x54\x04\x02\x00\x3c\x00\x03\x66\x6f\x6f"
--- stream_response
hello world
--- grep_error_log eval
qr/mqtt client id: \w+/
--- grep_error_log_out
mqtt client id: foo
--- no_error_log
[error]



=== TEST 13: hit route with empty client id
--- stream_enable
--- stream_request eval
"\x10\x0f\x00\x04\x4d\x51\x54\x54\x04\x02\x00\x3c\x00\x00"
--- stream_response
hello world
--- grep_error_log eval
qr/mqtt client id: \w+/
--- grep_error_log_out
--- no_error_log
[error]