Retry busy model calls, share shun state, patch CVE-2025-29927, log country #14

Merged
james merged 4 commits from retry-shared-state-geo into main 2026-10-23 17:32:54 +00:00
Owner

Follow-up to the 2026-10-23 scanner burst (34.84.93.203, about 415 requests in two minutes). The burst filled both Laya replicas: 68 model calls got 503 and failed open, and the client was never shunned because its 28 blocks were split 18/10 across the two pods.

One commit per change; each builds and passes tests on its own.

Retry a 503 from the model backend once

  • Laya sheds a call with 503 before doing any work, so one retry after 25 ms is safe. It stays inside the budget and is skipped once the deadline has passed.
  • For laya the retry uses a client with keep-alives off. A Service picks the backend per connection, and the pooled client would reuse the connection that just got the 503, sending the retry to the same busy replica.
  • New counters centinela_model_retries and centinela_model_retries_failed. Only a retry that also fails is logged as model call failed, so the existing Grafana panel still works.

Share shuns and client counts between replicas through Valkey

  • CENTINELA_STATE_URL=valkey://host:6379 keeps shuns, CENTINELA_SHUN_AFTER strike counts and rate-limit counts in Valkey (or Redis 7+), shared by all replicas. One centinela unshun then clears a shun everywhere.
  • If Valkey is down, each replica falls back to its own memory, as today, and tries again every 5 s. Each call gets 50 ms. An unreachable Valkey at startup is not fatal. Without the URL nothing changes.
  • internal/valkey is a small RESP client rather than a new dependency. Tests run against an in-process fake and, with CENTINELA_TEST_VALKEY, a real server; they pass on Valkey 9.

Virtual patch for CVE-2025-29927 (Next.js middleware bypass)

  • The scanner sent x-middleware-subrequest on every request. CRS 920450 scored it only as a restricted header, and the model allowed nearly all of them.
  • Rule 1029927 matches the header with any value, adds +5 and names the CVE, so the model sees what the request is.

Log the client's country with each decision

  • A country field from Cloudflare's CF-IPCountry (CENTINELA_COUNTRY_HEADER), for the dashboard's traffic map. The model never sees it.

Deploying

  • Needs a release. Setting CENTINELA_STATE_URL also needs a Valkey in the centinela namespace (gitops-infra change).
  • The updated Grafana dashboard with the country map is going into gitops-infra, where dashboards now live.
Follow-up to the 2026-10-23 scanner burst (34.84.93.203, about 415 requests in two minutes). The burst filled both Laya replicas: 68 model calls got 503 and failed open, and the client was never shunned because its 28 blocks were split 18/10 across the two pods. One commit per change; each builds and passes tests on its own. **Retry a 503 from the model backend once** - Laya sheds a call with 503 before doing any work, so one retry after 25 ms is safe. It stays inside the budget and is skipped once the deadline has passed. - For `laya` the retry uses a client with keep-alives off. A Service picks the backend per connection, and the pooled client would reuse the connection that just got the 503, sending the retry to the same busy replica. - New counters `centinela_model_retries` and `centinela_model_retries_failed`. Only a retry that also fails is logged as `model call failed`, so the existing Grafana panel still works. **Share shuns and client counts between replicas through Valkey** - `CENTINELA_STATE_URL=valkey://host:6379` keeps shuns, `CENTINELA_SHUN_AFTER` strike counts and rate-limit counts in Valkey (or Redis 7+), shared by all replicas. One `centinela unshun` then clears a shun everywhere. - If Valkey is down, each replica falls back to its own memory, as today, and tries again every 5 s. Each call gets 50 ms. An unreachable Valkey at startup is not fatal. Without the URL nothing changes. - `internal/valkey` is a small RESP client rather than a new dependency. Tests run against an in-process fake and, with `CENTINELA_TEST_VALKEY`, a real server; they pass on Valkey 9. **Virtual patch for CVE-2025-29927 (Next.js middleware bypass)** - The scanner sent `x-middleware-subrequest` on every request. CRS 920450 scored it only as a restricted header, and the model allowed nearly all of them. - Rule 1029927 matches the header with any value, adds +5 and names the CVE, so the model sees what the request is. **Log the client's country with each decision** - A `country` field from Cloudflare's `CF-IPCountry` (`CENTINELA_COUNTRY_HEADER`), for the dashboard's traffic map. The model never sees it. **Deploying** - Needs a release. Setting `CENTINELA_STATE_URL` also needs a Valkey in the `centinela` namespace (gitops-infra change). - The updated Grafana dashboard with the country map is going into gitops-infra, where dashboards now live.
Laya answers 503 "server busy" when its queue is full, in a few
milliseconds and before doing any work, so one retry is safe and cheap.
On 2026-10-23 a scanner burst filled both Laya replicas and 68 model calls
failed open this way.

The retry waits 25 ms, stays inside the caller's budget, and is skipped if
the deadline has passed. For laya it goes out on a client with keep-alives
off: a Service picks the backend per connection, and the pooled client
would hand the retry the connection that just got the 503, which reaches
the same busy replica.

centinela_model_retries counts retries and centinela_model_retries_failed
those that failed too. Only a failed retry is logged as "model call failed".
Each replica kept its own shun list, strike counts and rate counts, so with
two pods a client needed CENTINELA_SHUN_AFTER blocks on one of them to be
shunned, could make the rate limit on each, and an unshun had to be run on
every pod. In the 2026-10-23 scanner burst the client had 28 blocks against
a threshold of 20, split 18 and 10, and was never shunned.

CENTINELA_STATE_URL=valkey://host:6379 keeps all three in Valkey (or Redis
7+), shared by every replica pointed at it. Counts are INCR with
PEXPIRE NX in one MULTI/EXEC, so the window's expiry is set once and a key
never lives without one. Exactly one request sees the count reach
ShunAfter, so a shun is made and logged once.

Each call gets 50 ms. If Valkey fails, the replica uses its own memory, as
if no URL were set, and tries again every 5 s, logging the outage once and
the recovery once (centinela_shared_state_errors counts failures). A shun
made during an outage is kept locally for its TTL. An unreachable Valkey at
startup is logged, not fatal. Without the URL nothing changes.

internal/valkey is a small RESP client with a connection pool, rather than
a new dependency. The engine's client state moves behind one interface with
an in-memory and a Valkey version. Tests run against an in-process fake and,
with CENTINELA_TEST_VALKEY set, a real server; they pass on Valkey 9.
Next.js sets x-middleware-subrequest on its own internal subrequests and,
before 15.2.3, 14.2.25, 13.5.9 and 12.3.5, skipped middleware for any
request carrying it. A client never sends it, so its presence is the
exploit, whatever the value.

The 2026-10-23 scanner sent it on every request under rotating bot user
agents. CRS 920450 scored it +5 as a restricted header, and the model saw
only that and allowed nearly all of them. Rule 1029927 adds another +5 and
names the CVE, with the header's value in the match data.
Log the client's country with each decision
All checks were successful
security-scan / security-scan (pull_request) Successful in 45s
test / go (pull_request) Successful in 2m22s
f78ca7e07f
Cloudflare sends the client's country code in CF-IPCountry. Each decision
log line now carries it as "country" when the request has one, for a map of
where traffic comes from. CENTINELA_COUNTRY_HEADER names the header
(default CF-IPCountry; empty turns it off). Only two-character codes are
kept; Cloudflare uses XX for unknown and T1 for Tor.

The country stays out of the model's input, which the Laya checkpoint was
trained on exactly.

🔎 ojo scan results

No findings.

<!-- ojo-scan-summary --> ### 🔎 ojo scan results No findings.
james merged commit 10168116b8 into main 2026-10-23 17:32:54 +00:00
james deleted branch retry-shared-state-geo 2026-10-23 17:32:54 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
ColibriSec/centinela!14
No description provided.