Repository navigation
Description
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.
- Personal API-key authentication resolves
group_id = Noneand setsscope_id = user_id. The API-key path does not reject a personal user ID beginning withgrp_. MemoryService::user_sql_store()andDbRouter::scope_store()interpret any string starting withgrp_as a group ID and callgroup_store().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:
- Owner-scoped user validation rejects the group prefix.
- API-key authentication constructs a personal scope from the resolved user ID when group_id is absent.
- API-key principal loading reads user_id and group_id without equivalent personal-ID prefix validation.
- Personal-key creation authorizes the caller but does not apply the prefix restriction before persisting user_id.
- Service-layer prefix dispatch.
- Router prefix dispatch.
- Group database lookup uses the group registry.
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:
- Create an active synthetic group through the normal test setup and retain its generated
grp_...ID, calledG. - With the fixture's authorized administrator, create a personal API key whose
user_idisGand whosegroup_idis absent. The current creation path accepts this combination. Do not log or include the generated key in test output. - Authenticate a normal memory read/write request with that personal key. Its principal has
group_id = None, but its string scope ID isG. - Verify the selected database/store: prefix dispatch currently selects
group_store(G)rather than the personal store for userG. - 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 = Nonecannot 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
fix(storage): coordinate routed scope initialization and invalidation (…
fix(api): reject grp_-prefixed personal user IDs at every identity bo…