[SEC-001] gRPC auth failure-limiter partitions on peer incl. ephemeral port, resets per connection #244

Closed
opened 2026-12-02 16:07:33 -05:00 by dohertj2 · 1 comment
Owner

SEC-001 — Failure-limiter partition includes the ephemeral source port

  • Severity: High · Category: Bug (security) · Confidence: Confirmed
  • Location: Security/Authorization/GatewayGrpcAuthorizationInterceptor.cs:174-179; Security/Authorization/ApiKeyFailureLimiter.cs:148-159, 404-409; contrast Security/Authorization/MetricsAuthorization.cs:66-68.

Description. ResolveThrottlePartition passes context.Peer straight into ApiKeyThrottlePartition. In Grpc.AspNetCore that string is ipv4:<address>:<port> (the repo's own test fixtures use ipv4:127.0.0.1:5000), and the port is the client's ephemeral source port. The limiter uses the string verbatim as the partition key and as the _peerKeyIds key. The "address that produced the failures" is therefore really "the TCP connection that produced them". The /metrics handler does it correctly with Connection.RemoteIpAddress (no port), so the two auth surfaces disagree.

Failure scenario. An attacker sends 9 bad secrets for mxgw_operator01_*, closes the channel, opens a new one (new source port) and repeats. No (peer, key id) partition ever reaches ApiKeyFailureLimit (10). The per-address cap of 32 key-id partitions (MaxKeyIdPartitionsPerPeer) is equally per-connection. Junk-shaped tokens land on the peer-only fallback partition, which is also fresh per connection, so junk spraying is never throttled at all (no key id means no aggregate either). Each connection also mints a tracked partition, pushing the map to its cap and forcing the O(n) EvictIfOverCapacity scan on every recorded failure.

Suggested fix. Derive the partition from the remote IP only (context.GetHttpContext().Connection.RemoteIpAddress, or strip the trailing :port from Peer, handling the bracketed ipv6:[..]:port form). Share one helper with MetricsAuthenticationHandler.

Test idea. Interceptor test: 9 failures from ipv4:203.0.113.7:40001, then 9 from ipv4:203.0.113.7:40002; assert the 11th failure overall is ResourceExhausted. Today it is Unauthenticated.


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.

## SEC-001 — Failure-limiter partition includes the ephemeral source port - **Severity:** High · **Category:** Bug (security) · **Confidence:** Confirmed - **Location:** `Security/Authorization/GatewayGrpcAuthorizationInterceptor.cs:174-179`; `Security/Authorization/ApiKeyFailureLimiter.cs:148-159, 404-409`; contrast `Security/Authorization/MetricsAuthorization.cs:66-68`. **Description.** `ResolveThrottlePartition` passes `context.Peer` straight into `ApiKeyThrottlePartition`. In Grpc.AspNetCore that string is `ipv4:<address>:<port>` (the repo's own test fixtures use `ipv4:127.0.0.1:5000`), and the port is the client's ephemeral source port. The limiter uses the string verbatim as the partition key and as the `_peerKeyIds` key. The "address that produced the failures" is therefore really "the TCP connection that produced them". The `/metrics` handler does it correctly with `Connection.RemoteIpAddress` (no port), so the two auth surfaces disagree. **Failure scenario.** An attacker sends 9 bad secrets for `mxgw_operator01_*`, closes the channel, opens a new one (new source port) and repeats. No `(peer, key id)` partition ever reaches `ApiKeyFailureLimit` (10). The per-address cap of 32 key-id partitions (`MaxKeyIdPartitionsPerPeer`) is equally per-connection. Junk-shaped tokens land on the peer-only fallback partition, which is also fresh per connection, so junk spraying is never throttled at all (no key id means no aggregate either). Each connection also mints a tracked partition, pushing the map to its cap and forcing the O(n) `EvictIfOverCapacity` scan on every recorded failure. **Suggested fix.** Derive the partition from the remote IP only (`context.GetHttpContext().Connection.RemoteIpAddress`, or strip the trailing `:port` from `Peer`, handling the bracketed `ipv6:[..]:port` form). Share one helper with `MetricsAuthenticationHandler`. **Test idea.** Interceptor test: 9 failures from `ipv4:203.0.113.7:40001`, then 9 from `ipv4:203.0.113.7:40002`; assert the 11th failure overall is `ResourceExhausted`. Today it is `Unauthenticated`. --- 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:33 -05:00
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.

Limiter keys on client IP without port (gRPC and /metrics).

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. Limiter keys on client IP without port (gRPC and `/metrics`).
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: dohertj2/mxaccessgw#244