Skip to content

fix(handler): make update_log_meta/1 a no-op outside a request - #140

Merged
zmstone merged 2 commits into
emqx:masterfrom
zmstone:260917-auth-failure-log-meta
Sep 17, 2026
Merged

zmstone merged 2 commits into
emqx:masterfrom
zmstone:260917-auth-failure-log-meta

Conversation

@zmstone

@zmstone zmstone commented Sep 17, 2026 •

Copy link
Copy Markdown
Member

Summary

update_log_meta/1 merged new meta into the meta of the current request. When the calling process did not handle a minirest request, the stored meta was undefined, and the call raised badmap. For example, a unit test that calls an authorization callback directly hits this. Callers had to catch the error.

update_log_meta/1 now does nothing and returns ok in that case. It used to return the previous meta. The emqx callers ignore the return value.

Motivation: emqx/emqx#18879 records the denied caller in the audit log from its authorization callback. With this change it can call update_log_meta/1 without catching badmap.

Tests

  • t_update_log_meta_outside_request: update_log_meta/1 returns ok and writes nothing outside a request.

Release

emqx pins minirest 1.4.12 on dev-60 to dev-62 and 1.5.x on dev-63, dev-70 and master. emqx#18879 targets dev-60, so it needs a 1.4.x tag with this change. The emqx branches that pin 1.5.x need a 1.5.x tag.

An authorization callback can now return
`{with_log_meta, LogMeta, Response}`. minirest merges `LogMeta` into
the log meta in the same way as the meta of a successful
authorization, and sends only `Response` to the client.

`update_log_meta/1` now does nothing and returns `ok` when the calling
process does not handle a minirest request.
@zmstone
zmstone marked this pull request as ready for review September 17, 2026 11:24
Comment thread src/minirest_handler.erl Outdated
%% that is not allowed to use the endpoint. `LogMeta' is merged into the log
%% meta the same way as the meta of a successful authorization. Only
%% `Response' is sent to the client.
take_auth_log_meta({with_log_meta, LogMeta, Response}) when is_map(LogMeta) ->

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

My bad 🫠 I didn't realize that minirest_handler:update_log_meta/1 is already used quite extensively in the EMQX codebase. So the badmap fix should already be enough for the EMQX PR.

That said, maybe this API addition is not really necessary. But if it is, perhaps introduce #response{} record in the public API so that we don't need to invent special wrappers for such things?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

revert to only handle badmap in minirest.

`update_log_meta/1` is already the way emqx adds log meta, and it now
works outside a request. Keep only that change.
@zmstone zmstone changed the title feat(handler): let a failed authorization add log meta fix(handler): make update_log_meta/1 a no-op outside a request Sep 17, 2026
@zmstone
zmstone merged commit 87088e2 into emqx:master Sep 17, 2026
2 checks passed
@zmstone
zmstone deleted the 260917-auth-failure-log-meta branch September 17, 2026 20:50
zmstone added a commit that referenced this pull request Sep 18, 2026
`update_log_meta/1` now does nothing and returns `ok` when the calling
process does not handle a minirest request. Before, it raised `badmap`.

Backport of 87088e2 (#140) to the 1.4 line.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants