Skip to content

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

Closed

Description

@loveRhythm1990

Summary

Personal user IDs beginning with grp_ can be interpreted as group IDs by the service/storage routing layer, even when authentication explicitly resolves the request as personal (group_id = None).

This is an existing dispatch-layer defect verified by code inspection at main 89268fc. It is separate from the store-cache key collision and initialization/invalidation races tracked in #330 / #331; using typed cache keys inside the router does not fix an incorrectly selected routing branch.

Architecture and failure path

In multi-database mode, personal stores resolve through mem_user_registry, while group stores resolve through mem_groups. Authentication retains both user_id and an optional group_id, but downstream service methods receive a string scope ID and infer the scope type from its prefix.

  1. Personal API-key authentication resolves group_id = None and sets scope_id = user_id. The API-key path does not reject a personal user ID beginning with grp_.
  2. MemoryService::user_sql_store() and DbRouter::scope_store() interpret any string starting with grp_ as a group ID and call group_store().
  3. group_store() resolves the string against the group registry. If no matching active group exists, personal access fails with a group-not-found error. If a matching active group exists, the routing layer selects that group's physical store instead of the personal store.

The owner-scoped authentication scheme already rejects this prefix for user IDs, but the rule is not consistently enforced across other authentication/identity-entry paths. Personal-key creation accepts the supplied user_id without the corresponding prefix validation; ordinary master/open-mode user selection also lacks that validation.

Code evidence

All links below are pinned to the inspected main revision:

PR #331 additionally introduces invalidate_scope() with the same string-prefix dispatch, so its behavior must remain consistent with the eventual fix.

Synthetic reproduction recipe

Use an isolated local multi-database test fixture and synthetic records only:

  1. Create an active synthetic group through the normal test setup and retain its generated grp_... ID, called G.
  2. With the fixture's authorized administrator, create a personal API key whose user_id is G and whose group_id is absent. The current creation path accepts this combination. Do not log or include the generated key in test output.
  3. Authenticate a normal memory read/write request with that personal key. Its principal has group_id = None, but its string scope ID is G.
  4. Verify the selected database/store: prefix dispatch currently selects group_store(G) rather than the personal store for user G.
  5. Repeat with a synthetic grp_... personal user ID for which no group exists; dispatch attempts a group lookup instead of personal routing.

This recipe follows the verified code path; an end-to-end execution and regression tests are still needed. No production data access or leakage has been tested or asserted. Creating a personal API key currently requires administrator authorization; this report does not claim an unauthenticated exploit.

Expected behavior

Routing must preserve the authenticated scope type. A personal principal must never become a group principal merely because its user ID resembles a group ID.

Either support such personal user IDs using explicit scope typing, or consistently reject the reserved prefix at every supported personal-identity boundary. Existing persisted keys and cached principals must fail closed under a reservation policy, rather than being silently reinterpreted as group access.

Proposed fix

Prefer carrying an explicit personal/group scope through authentication, service calls, router access, invalidation, and routed background writers. Derive it from the authenticated principal's group_id, not from user-controlled string contents.

If a smaller compatibility guard reserves grp_ for groups, centralize and apply personal-ID validation consistently to key creation/rotation, existing API-key principals including cache hits, master/open-mode identity selection, and other supported user-entry paths. Define behavior for pre-existing conflicting IDs. Checking only the owner-scoped scheme or only one router method is insufficient because the service layer duplicates the heuristic.

Acceptance criteria

  • A personal user ID beginning with grp_ is either routed as personal or rejected explicitly according to a documented policy.
  • A personal principal with group_id = None cannot select a matching group's database/store, including when both identities use the same string.
  • Existing persisted API keys and warmed principal-cache entries receive the same protection as newly created keys.
  • Master/open-mode and owner-scoped identity paths enforce the chosen policy consistently.
  • Genuine group-scoped keys continue to route to group storage and retain their group authorization checks.
  • Service/router dispatch, scope invalidation, and routed background writers agree on scope identity.
  • Synthetic API/service regression tests cover both matching-group and missing-group cases, plus cold and warm caches.

Scope

This issue tracks Memoria's own identity-to-storage dispatch. It does not require changes to external adapters, deployment configuration, or database-engine behavior.

Activity

  1. added a commit that references this issue on Oct 19, 2026
    1fd370e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions