Skip to content

Commit

Permalink
fix(mqtt-proxy): client id can be empty (#5816)
Browse files Browse the repository at this point in the history
  • Loading branch information
spacewander authored Dec 16, 2021
1 parent 219847e commit de65fc4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
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]

0 comments on commit de65fc4

Please sign in to comment.