[LocalDb] Sync endpoint is unauthenticated by default (ApiKey sent but never checked server-side) #36

Closed
opened 2026-11-09 14:39:20 -05:00 by dohertj2 · 1 comment
Owner

From the scadaproj#15 shared-library review (code-reviews/LocalDb/findings.md, LocalDb-001), verified at 91fb2b4. Update the finding's Resolution row when fixed.

Severity Medium
Priority P2
Category Security & secret handling
Status Open
Location ZB.MOM.WW.LocalDb/src/ZB.MOM.WW.LocalDb.Replication/LocalDbSyncService.cs:85, ZB.MOM.WW.LocalDb/src/ZB.MOM.WW.LocalDb.Replication/ReplicationOptions.cs:11, ZB.MOM.WW.LocalDb/src/ZB.MOM.WW.LocalDb.Replication/DependencyInjection/ReplicationServiceCollectionExtensions.cs:77

Description

ReplicationOptions.ApiKey is documented as "API key presented to / expected from the peer; null
disables key auth". Only the first half is true:

  • SyncBackgroundService attaches Authorization: Bearer <key> to the outbound call.
  • LocalDbSyncService.Sync never reads request headers.
  • MapZbLocalDbSync maps the service with no authorization metadata.
  • The library ships no interceptor or validator.

So setting ApiKey on both nodes, the natural reading of the option, leaves the passive endpoint
open. The README's Ops notes say this, but the XML doc does not. The design doc (§Auth: "ZB.MOM.WW.Auth.ApiKeys keyId/Bearer on
the channel") promises enforcement on the channel. Both current consumers wrote their own
fail-closed interceptor (ScadaBridge, and OtOpcUa's LocalDbSyncAuthInterceptor). This is the
duplication the shared library exists to remove, and every future consumer has to rediscover it.

The handshake makes an open endpoint easy to exploit. SyncSession.HandshakeAsync sends the
server's own Handshake, including every registered table name and its schema digest
(SyncSession.cs:180), before validating the caller's. A caller can therefore echo the digests
back and pass the fail-closed schema check without knowing the schema.

Failure scenario

A third consumer (or a new OtOpcUa/ScadaBridge host profile) calls AddZbLocalDbReplication +
MapZbLocalDbSync. It sets LocalDb:Replication:ApiKey on both nodes and does not add an
interceptor. Any client that can reach the gRPC port can then:

  1. Open Sync, read the server's Handshake, and reply with the same digests and
    last_applied_remote_seq = 0.
  2. Receive a full snapshot of every replicated table, which is complete data exfiltration.
  3. Push DeltaBatch entries with a high HLC that overwrite or tombstone any replicated row. They
    win LWW and are then replicated onward by nothing, because the 2-node topology does not
    re-forward.
  4. Repeatedly displace the legitimate peer's stream, because the guard is newest-wins
    (LocalDbSyncService.cs:152).

A different node_id in step 1 also makes the server reset its inbound watermark
(SyncSession.cs:199).

Recommendation

Move the interceptor both consumers already wrote into the library. When ApiKey is set, the
service should verify Bearer with a constant-time compare (CryptographicOperations.FixedTimeEquals)
and refuse with Unauthenticated otherwise. Make the endpoint fail closed when no key is
configured, unless the host opts out explicitly, for example with AllowUnauthenticated = true. Fix
the ApiKey XML doc either way. Both consumers could then delete their copies.

Resolution

Unresolved.

From the scadaproj#15 shared-library review (`code-reviews/LocalDb/findings.md`, **LocalDb-001**), verified at 91fb2b4. Update the finding's Resolution row when fixed. | | | |--|--| | Severity | Medium | | Priority | P2 | | Category | Security & secret handling | | Status | Open | | Location | `ZB.MOM.WW.LocalDb/src/ZB.MOM.WW.LocalDb.Replication/LocalDbSyncService.cs:85`, `ZB.MOM.WW.LocalDb/src/ZB.MOM.WW.LocalDb.Replication/ReplicationOptions.cs:11`, `ZB.MOM.WW.LocalDb/src/ZB.MOM.WW.LocalDb.Replication/DependencyInjection/ReplicationServiceCollectionExtensions.cs:77` | **Description** `ReplicationOptions.ApiKey` is documented as "API key presented to / expected from the peer; null disables key auth". Only the first half is true: - `SyncBackgroundService` attaches `Authorization: Bearer <key>` to the outbound call. - `LocalDbSyncService.Sync` never reads request headers. - `MapZbLocalDbSync` maps the service with no authorization metadata. - The library ships no interceptor or validator. So setting `ApiKey` on both nodes, the natural reading of the option, leaves the passive endpoint open. The README's Ops notes say this, but the XML doc does not. The design doc (§Auth: "`ZB.MOM.WW.Auth.ApiKeys` keyId/Bearer on the channel") promises enforcement on the channel. Both current consumers wrote their own fail-closed interceptor (ScadaBridge, and OtOpcUa's `LocalDbSyncAuthInterceptor`). This is the duplication the shared library exists to remove, and every future consumer has to rediscover it. The handshake makes an open endpoint easy to exploit. `SyncSession.HandshakeAsync` sends the server's own `Handshake`, including every registered table name and its schema digest (`SyncSession.cs:180`), before validating the caller's. A caller can therefore echo the digests back and pass the fail-closed schema check without knowing the schema. **Failure scenario** A third consumer (or a new OtOpcUa/ScadaBridge host profile) calls `AddZbLocalDbReplication` + `MapZbLocalDbSync`. It sets `LocalDb:Replication:ApiKey` on both nodes and does not add an interceptor. Any client that can reach the gRPC port can then: 1. Open `Sync`, read the server's Handshake, and reply with the same digests and `last_applied_remote_seq = 0`. 2. Receive a full snapshot of every replicated table, which is complete data exfiltration. 3. Push `DeltaBatch` entries with a high HLC that overwrite or tombstone any replicated row. They win LWW and are then replicated onward by nothing, because the 2-node topology does not re-forward. 4. Repeatedly displace the legitimate peer's stream, because the guard is newest-wins (`LocalDbSyncService.cs:152`). A different `node_id` in step 1 also makes the server reset its inbound watermark (`SyncSession.cs:199`). **Recommendation** Move the interceptor both consumers already wrote into the library. When `ApiKey` is set, the service should verify `Bearer` with a constant-time compare (`CryptographicOperations.FixedTimeEquals`) and refuse with `Unauthenticated` otherwise. Make the endpoint fail closed when no key is configured, unless the host opts out explicitly, for example with `AllowUnauthenticated = true`. Fix the `ApiKey` XML doc either way. Both consumers could then delete their copies. **Resolution** _Unresolved._
dohertj2 added the type:review-findingpriority:p2 labels 2026-11-09 14:39:20 -05:00
Author
Owner

Fixed on main 48813bb (LocalDb-001 → Resolved), LocalDb 0.3.6 unpublished: with ApiKey set, the server requires Authorization: Bearer <key> (constant-time compare) before the handshake and before a new stream can replace the current one → Unauthenticated otherwise. No key anywhere → unchanged (plus a startup warning). Wire unchanged (the client has always sent the header); ScadaBridge/OtOpcUa interceptors check the same key. Rollout: set the same key on both nodes — a keyed node now refuses an unkeyed peer. 5 new tests fail without the check (incl. an unauthenticated caller can't displace the real peer). Evidence: LocalDb 236/236; Auth 344 passed / 78 skipped (live SQL/LDAP gated) after rebase; code-reviews --check consistent.

Fixed on main 48813bb (LocalDb-001 → Resolved), LocalDb **0.3.6 unpublished**: with ApiKey set, the server requires `Authorization: Bearer <key>` (constant-time compare) before the handshake and before a new stream can replace the current one → Unauthenticated otherwise. No key anywhere → unchanged (plus a startup warning). Wire unchanged (the client has always sent the header); ScadaBridge/OtOpcUa interceptors check the same key. **Rollout:** set the same key on both nodes — a keyed node now refuses an unkeyed peer. 5 new tests fail without the check (incl. an unauthenticated caller can't displace the real peer). Evidence: LocalDb 236/236; Auth 344 passed / 78 skipped (live SQL/LDAP gated) after rebase; code-reviews --check consistent.
Sign in to join this conversation.