Skip to content

fix(api): reject grp_-prefixed personal user IDs at every identity boundary - #335

Merged
loveRhythm1990 merged 1 commit into
matrixorigin:mainfrom
loveRhythm1990:fix/reject-grp-prefix-personal-id
Oct 19, 2026
Merged

loveRhythm1990 merged 1 commit into
matrixorigin:mainfrom
loveRhythm1990:fix/reject-grp-prefix-personal-id

Conversation

@loveRhythm1990

Copy link
Copy Markdown
Collaborator

Summary

Fixes #334.

DbRouter::scope_store() / MemoryService::user_sql_store() dispatch between personal and group storage purely by checking whether the scope string starts with grp_, without regard to the authenticated principal's actual group_id. Only the owner-scoped master-auth path (validate_owner_scoped_master_request) rejected this prefix for personal user IDs; API-key creation and the master-key/open-mode X-User-Id resolution did not. A personal principal whose user_id happened to start with grp_ (via a mis-issued personal API key, or an open-mode client supplying an arbitrary X-User-Id) could therefore be silently dispatched into an unrelated group's store if a same-named active group existed.

This PR takes the narrower of the two fixes the issue proposes: consistently reject the reserved grp_ prefix at every boundary that mints or accepts a personal user_id, rather than threading an explicit scope-type through the whole call chain. That keeps the existing string-prefix dispatch in router.rs/service.rs safe by invariant, without touching those call sites.

Changes

  • memoria-api/src/auth.rs: extract the existing grp_ rejection (previously inlined in validate_owner_scoped_master_request) into a shared is_reserved_group_id() helper, and a new resolve_personal_user_id() helper that resolves + validates the master-key/open-mode X-User-Id/X-Impersonate-User/query-param path the same way.
  • memoria-api/src/routes/auth.rs: create_key's personal-key branch (group_id absent) now rejects a user_id starting with grp_ before persisting the key.

Out of scope (per issue's own acceptance criteria)

Per discussion, this PR intentionally does not cover: already-persisted API keys/cached principals created before this fix, invalidate_scope()'s own prefix heuristic in router.rs, or threading an explicit scope-type through the router/service layers (the issue's alternative, broader fix). Those remain open follow-ups if needed.

Test plan

  • cargo test -p memoria-api --lib auth:: — 20/20 pass, including 5 new unit tests covering the X-User-Id header, X-Impersonate-User header, and query-param paths, plus the ordinary (non-grp_) case.
  • cargo check -p memoria-api / cargo clippy -p memoria-api --lib -- -D warnings — clean.
  • No DB-backed e2e test added for create_key's rejection path (no local MySQL/MatrixOne instance available in this environment to extend memoria-api/tests/*); the unit tests above cover the same validation logic in isolation.

🤖 Generated with Claude Code

…undary

A personal user ID beginning with grp_ was routed to group storage by
DbRouter::scope_store()/MemoryService::user_sql_store(), which infer scope
type from string prefix alone instead of the authenticated principal's
group_id. Only the owner-scoped master auth path rejected this prefix;
API-key creation and master/open-mode X-User-Id resolution did not,
letting a personal request get silently dispatched into an unrelated
group's store if a same-named active group existed.

Reject the grp_ prefix consistently at every boundary that mints or
accepts a personal user_id: personal API-key creation, and master-key /
open-mode X-User-Id / X-Impersonate-User / query-param resolution. The
owner-scoped path already had this check; it's now reused via a shared
helper instead of being duplicated.

Fixes matrixorigin#334

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@loveRhythm1990
loveRhythm1990 merged commit 1fd370e into matrixorigin:main Oct 19, 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.

Bug: personal user IDs starting with grp_ are routed to group storage

1 participant