Skip to content

feat: pass route path to authorize callback via HandlerInfo#135

Merged
JimMoen merged 2 commits intoemqx:masterfrom
JimMoen:feat/authorize-handler-info
Mar 25, 2026
Merged

feat: pass route path to authorize callback via HandlerInfo#135
JimMoen merged 2 commits intoemqx:masterfrom
JimMoen:feat/authorize-handler-info

Conversation

@JimMoen
Copy link
Copy Markdown
Member

@JimMoen JimMoen commented Mar 25, 2026

Summary

Pass the route template path (e.g. "/clients/:clientid") to the authorize callback via the HandlerInfo map, enabling authorization logic to use the same path that was used for handler dispatch — rather than parsing cowboy_req:path(Req) independently.

Motivation

In emqx/emqx#16942 (API Key scope-based authorization), the authorize callback needs to know which route was matched in order to check permissions. Previously, the only option was to re-parse the raw request path from cowboy_req:path(Req) and match it against route patterns — effectively building a parallel path resolution system. This is fragile and security-sensitive (see review comment).

By passing the route Path from the dispatch state directly, we eliminate the dual-resolution concern entirely.

Changes

src/minirest_handler.erl — 1 file, 4 lines changed:

  1. do_authorize/2do_authorize/3: accepts the Path parameter from handle/2's dispatch state
  2. HandlerInfo map: adds path => Path field (route template string, e.g. "/clients/:clientid")
  3. No record changes — #handler{} is untouched to preserve hot-upgrade compatibility

Backward Compatibility

  • The authorize callback with arity 2 (M:F(Request, HandlerInfo)) receives the extra path key in HandlerInfo — existing callbacks that pattern-match on HandlerInfo without path are unaffected
  • The arity-1 fallback (M:F(Request)) is unchanged
  • No public API changes

JimMoen added 2 commits March 24, 2026 14:42
Add Path parameter to do_authorize/3 so that the authorize callback
receives the route template (e.g., "/clients/:clientid") in
HandlerInfo map. This allows authorization logic to know the exact
matched route without re-implementing path matching.

HandlerInfo now contains: method, module, function, path.
No #handler{} record change — zero hot-upgrade risk.
Add t_route_path_in_auth test case that requests /route_path_in_auth/42
and asserts the authorize callback receives the route template
"/route_path_in_auth/:id" (not the actual request path).
@JimMoen JimMoen merged commit de9a591 into emqx:master Mar 25, 2026
2 checks passed
@JimMoen JimMoen deleted the feat/authorize-handler-info branch March 25, 2026 06:39
JimMoen added a commit to JimMoen/emqx that referenced this pull request Mar 25, 2026
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