[SES-002] Invoke/CloseSession not owner-scoped; Invoke(DrainEvents) bypasses StreamEvents owner check #241

Closed
opened 2026-12-02 16:07:31 -05:00 by dohertj2 · 2 comments
Owner

SES-002: Invoke and CloseSession are not owner-scoped; DrainEvents bypasses the stream owner check

  • Severity: High
  • Category: Bug (authorization)
  • Confidence: Confirmed
  • Locations:
    • Grpc/EventStreamService.cs:58 (the only use of OwnerKeyId in the server)
    • Grpc/MxAccessGatewayService.cs:79-101 (CloseSession), :104-174 (Invoke), :326-336 (ResolveSession)
    • Security/Authorization/GatewayGrpcScopeResolver.cs:52 (DrainEvents needs only events:read)
    • src/ZB.MOM.WW.MxGateway.Worker/Ipc/WorkerPipeSession.cs:615, :997-1037 (DrainEvents dequeues from the same queue the event pump drains)
    • docs/DesignDecisions.md:88-97, docs/Sessions.md:277

Description

StreamEvents rejects a caller whose key id differs from GatewaySession.OwnerKeyId. Invoke and CloseSession resolve the session by id and never compare the owner. The design decision says an events:read key that learns a session id must not receive another key's events. Invoke(DrainEvents) does exactly that: it requires events:read, returns the session's queued events in the reply, and removes them from the worker queue.

The same gap lets a foreign key with the matching scope:

  • close another key's session (session:close);
  • add, advise and write through it (invoke:read / invoke:write), with events delivered to the owner's stream;
  • read worker and session state.

Session ids are random, but they appear on the dashboard Sessions pages (visible to Viewers, and to anonymous localhost by default) and in logs.

Failure scenario

Key A opens session-…abc and streams. Key B (scope events:read, different tenant) calls Invoke with DRAIN_EVENTS on that id in a loop. B receives A's data-change events, including tags outside B's read constraints. A's stream silently misses those events: they never reach the gateway pump, so the replay ring has no record and no ReplayGap is raised.

Suggested fix

Apply the same ordinal owner comparison in Invoke and CloseSession (one helper next to ResolveSession), returning PermissionDenied. Decide explicitly whether an admin-scoped key may override. Internal callers (GatewayAlarmMonitor, dashboard) go through ISessionManager directly and are unaffected.

Test idea

Open a session as key A. As key B, call Invoke(DrainEvents), Invoke(Write) and CloseSession on A's session id. Assert PermissionDenied for each and that A's session is still Ready.


Source: code-reviews/2026-12-02-perf-bugs/README.md and code-reviews/2026-12-02-perf-bugs/PLAN.md (commit f200bda6 on local branch docs/review-2026-12-02-perf-bugs, not yet pushed). Reviewed at 54367e74.

## SES-002: `Invoke` and `CloseSession` are not owner-scoped; `DrainEvents` bypasses the stream owner check - **Severity:** High - **Category:** Bug (authorization) - **Confidence:** Confirmed - **Locations:** - `Grpc/EventStreamService.cs:58` (the only use of `OwnerKeyId` in the server) - `Grpc/MxAccessGatewayService.cs:79-101` (`CloseSession`), `:104-174` (`Invoke`), `:326-336` (`ResolveSession`) - `Security/Authorization/GatewayGrpcScopeResolver.cs:52` (`DrainEvents` needs only `events:read`) - `src/ZB.MOM.WW.MxGateway.Worker/Ipc/WorkerPipeSession.cs:615`, `:997-1037` (`DrainEvents` dequeues from the same queue the event pump drains) - `docs/DesignDecisions.md:88-97`, `docs/Sessions.md:277` ### Description `StreamEvents` rejects a caller whose key id differs from `GatewaySession.OwnerKeyId`. `Invoke` and `CloseSession` resolve the session by id and never compare the owner. The design decision says an `events:read` key that learns a session id must not receive another key's events. `Invoke(DrainEvents)` does exactly that: it requires `events:read`, returns the session's queued events in the reply, and removes them from the worker queue. The same gap lets a foreign key with the matching scope: - close another key's session (`session:close`); - add, advise and write through it (`invoke:read` / `invoke:write`), with events delivered to the owner's stream; - read worker and session state. Session ids are random, but they appear on the dashboard Sessions pages (visible to Viewers, and to anonymous localhost by default) and in logs. ### Failure scenario Key A opens `session-…abc` and streams. Key B (scope `events:read`, different tenant) calls `Invoke` with `DRAIN_EVENTS` on that id in a loop. B receives A's data-change events, including tags outside B's read constraints. A's stream silently misses those events: they never reach the gateway pump, so the replay ring has no record and no `ReplayGap` is raised. ### Suggested fix Apply the same ordinal owner comparison in `Invoke` and `CloseSession` (one helper next to `ResolveSession`), returning `PermissionDenied`. Decide explicitly whether an `admin`-scoped key may override. Internal callers (`GatewayAlarmMonitor`, dashboard) go through `ISessionManager` directly and are unaffected. ### Test idea Open a session as key A. As key B, call `Invoke(DrainEvents)`, `Invoke(Write)` and `CloseSession` on A's session id. Assert `PermissionDenied` for each and that A's session is still `Ready`. --- Source: `code-reviews/2026-12-02-perf-bugs/README.md` and `code-reviews/2026-12-02-perf-bugs/PLAN.md` (commit `f200bda6` on local branch `docs/review-2026-12-02-perf-bugs`, not yet pushed). Reviewed at `54367e74`.
dohertj2 added the area:gatewayarea:authpriority:p0type:bugtype:review-finding labels 2026-12-02 16:07:31 -05:00
Author
Owner

Decision (2026-12-03, recorded as D2 in code-reviews/2026-12-02-perf-bugs/PLAN.md): no admin override. Invoke and CloseSession use the same strict ordinal owner compare as StreamEvents. Operators close or kill any session through the dashboard (direct ISessionManager path). The test must assert PermissionDenied even when the foreign key holds admin.

Decision (2026-12-03, recorded as D2 in `code-reviews/2026-12-02-perf-bugs/PLAN.md`): no `admin` override. `Invoke` and `CloseSession` use the same strict ordinal owner compare as `StreamEvents`. Operators close or kill any session through the dashboard (direct `ISessionManager` path). The test must assert `PermissionDenied` even when the foreign key holds `admin`.
Author
Owner

Merged to main at 68043411 (pushed 2026-12-03). Gateway suite 1610 passed / 0 failed (macOS); windev worker CI 836 passed / 0 failed on that SHA.

No admin override (D2). Docs: docs/Authorization.md, docs/DesignDecisions.md, CLAUDE.md.

Merged to `main` at `68043411` (pushed 2026-12-03). Gateway suite 1610 passed / 0 failed (macOS); windev worker CI 836 passed / 0 failed on that SHA. No admin override (D2). Docs: `docs/Authorization.md`, `docs/DesignDecisions.md`, CLAUDE.md.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: dohertj2/mxaccessgw#241