[LocalDb] Sync endpoint is unauthenticated by default (ApiKey sent but never checked server-side) #36
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
From the scadaproj#15 shared-library review (
code-reviews/LocalDb/findings.md, LocalDb-001), verified at91fb2b4. Update the finding's Resolution row when fixed.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:77Description
ReplicationOptions.ApiKeyis documented as "API key presented to / expected from the peer; nulldisables key auth". Only the first half is true:
SyncBackgroundServiceattachesAuthorization: Bearer <key>to the outbound call.LocalDbSyncService.Syncnever reads request headers.MapZbLocalDbSyncmaps the service with no authorization metadata.So setting
ApiKeyon both nodes, the natural reading of the option, leaves the passive endpointopen. The README's Ops notes say this, but the XML doc does not. The design doc (§Auth: "
ZB.MOM.WW.Auth.ApiKeyskeyId/Bearer onthe channel") promises enforcement on the channel. Both current consumers wrote their own
fail-closed interceptor (ScadaBridge, and OtOpcUa's
LocalDbSyncAuthInterceptor). This is theduplication the shared library exists to remove, and every future consumer has to rediscover it.
The handshake makes an open endpoint easy to exploit.
SyncSession.HandshakeAsyncsends theserver'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 digestsback 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 setsLocalDb:Replication:ApiKeyon both nodes and does not add aninterceptor. Any client that can reach the gRPC port can then:
Sync, read the server's Handshake, and reply with the same digests andlast_applied_remote_seq = 0.DeltaBatchentries with a high HLC that overwrite or tombstone any replicated row. Theywin LWW and are then replicated onward by nothing, because the 2-node topology does not
re-forward.
(
LocalDbSyncService.cs:152).A different
node_idin 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
ApiKeyis set, theservice should verify
Bearerwith a constant-time compare (CryptographicOperations.FixedTimeEquals)and refuse with
Unauthenticatedotherwise. Make the endpoint fail closed when no key isconfigured, unless the host opts out explicitly, for example with
AllowUnauthenticated = true. Fixthe
ApiKeyXML doc either way. Both consumers could then delete their copies.Resolution
Unresolved.
Fixed on main
48813bb(LocalDb-001 → Resolved), LocalDb 0.3.6 unpublished: with ApiKey set, the server requiresAuthorization: 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.