Detect kit for Cloudflare: rules, hunts and the admission matrix
Status: in development. Nothing here is released or deployed.
The Detect kit looks at records and raises a signal when something matters. It also holds the admission matrix, which decides in which zones it is allowed to look at all.
Purpose
Detect keeps three things separate: what is stored, what is looked at, and what may be acted on. It evaluates a record only if the record's zone, or source, is admitted at detect or above. Until you admit a zone, Detect does nothing with its records. The admission matrix guide explains the controls.
What it deploys on Cloudflare
- A Worker that receives batches of records from a queue and runs the rules.
- A Durable Object, Cloudflare's small stateful object, called
RuleWindow, that counts events inside a time window for threshold rules. - A D1 database for the matrix, its history, the stop switch and the signals. Its history tables refuse updates and deletes.
- A Workers KV namespace holding a fast copy of the matrix. It is never the authority.
- The queue
edge-response-records-tap, which Ingest writes and Detect reads. Detect's stack creates it, because Detect is the consumer; Ingest's stack only binds to it by name, so Detect is deployed first. - Optionally a binding to the queue
edge-response-signals, which Detect writes. Detect never creates it: the Enrichment kit's stack does, and Detect looks it up there only whenEDGE_RESPONSE_SIGNALS_QUEUE=1is set at deploy. - Two scheduled jobs: every five minutes one rewrites the fast copy from the recorded matrix, and at seven minutes past each hour one runs the hunts.
The Worker is declared in Alchemy's plain-module form. How kits join says why.
It ships one rule pack for Cloudflare data with three rules: a burst of blocked or challenged firewall events from one address on one zone, a leaked-credential pair, and the creation of an API token. It also ships three hourly hunts, described under Hunts.
Hunts
A hunt is a stored SQL query over the records table that Ingest fills through Basin, run once an hour. It finds what a rule on a single record cannot: a person's first sign-in from a new country, an API token or Access configuration changed, or the same name blocked by DNS security ten times in a day. Every query passes a guard (one read statement, one table, a time window, a row limit) before it is sent. A hunt raises a signal only for a zone or source admitted at detect or above. Hunts stay off until you set a Basin SQL query URL and a read-only token. The hunts are tested against a fake query endpoint only, never a real one.
Capability entrypoints
A capability entrypoint is a named door that another Worker can be bound to. The binding a deployer writes is the grant, and the name is what gets recorded in audit rows.
AdmissionReadlets a peer read the effective level of a key, which is a zone or a source (effective), or of all of them (listEffective). Respond is the usual caller.AdmissionOperatorlets a console change the matrix on a person's behalf:lower,proposeRaise,confirmRaise,stop,proposeResumeandconfirmResume. Each call carries the raw Access assertion, which Detect verifies itself. A person's name is never an argument.SignalReadlets a peer list signals (listSignals) and read one (getSignal).
The default entrypoint has no such methods.
HTTP routes
All behind Cloudflare Access, apart from health. Requests must be JSON of at most 1 KB with strict fields, and a browser request from another site is refused.
GET /health.GET /v1/admissionandGET /v1/admission/{kind}/{id}read the matrix.POST /v1/admission/lower,/v1/admission/raise/proposeand/v1/admission/raise/confirmchange a level.POST /v1/admission/stop,/v1/admission/resume/proposeand/v1/admission/resume/confirmare the stop switch.GET /v1/signalslists signals.
How a person approves things
Detect does not act on your account. A person changes the matrix: anyone admitted as an operator can lower a level or pull the stop switch at once. Raising a level or resuming after a stop is two steps by one administrator, 60 to 600 seconds apart. The admission matrix guide has the detail.
Stop switches
One switch stops all automation in Detect: POST /v1/admission/stop, or stop through AdmissionOperator. A missing or unreadable switch counts as stopped, so a new deployment starts stopped until an administrator resumes it in two steps.
Peers
All optional. Detect receives records from Ingest and, if you switch it on (EDGE_RESPONSE_SIGNALS_QUEUE=1, off by default), sends signals to Enrichment, whose stack must be deployed first. Without Enrichment it records signals in its own database and lists them. It offers the effective level of a zone to Respond through AdmissionRead.