The read_bandwidth per-IP byte throttle is only authoritative when every
`noteguard read` process shares one bucket table: strfry runs one process per
read-serving thread, so an IP's reconnects fan across up to numThreads
processes and a per-process bucket enforces bytes_per_minute × N. That shared
table was opt-in via shared_state_path — so the safe, working behavior was
opt-in and the broken-for-enforcement per-process behavior was the default. We
got bitten by this in prod: the filter was enabled but shared_state_path was
unset, so enforcement silently didn't hold across processes.
Flip the default. `shared_state_path` unset now attaches to the standard tmpfs
path (DEFAULT_SHARED_STATE_PATH = /dev/shm/noteguard-read-bandwidth), so
cross-process enforcement is ON out of the box. An explicit empty string
(shared_state_path = "") is the per-process opt-out for hosts that can't use
tmpfs; a non-empty path overrides the location. Fail-open on an unattachable
file is unchanged.
Keep the tests hermetic: every read_bandwidth test config now sets
shared_state_path = "" so `cargo test` uses a fresh in-process map per test
and never creates or depends on the real /dev/shm file.
Also document the new default-on semantics in noteguard.toml and the README.
Changelog-Changed: read_bandwidth now enforces its per-IP budget across read processes by default (shared /dev/shm bucket); set shared_state_path = "" to opt out to per-process buckets
Headway: headway:noteguard/arrive-female-crane
Agentium: agentium:setup-illegal-peanut
strfry constructs its PluginReadPolicy as a per-thread local, spawning one
`noteguard read` process per read-serving thread (reqWorker + reqMonitor). Each
process kept its own in-memory ByteBucket, so an IP's reconnects — pinned to a
thread by connId % numThreads — fanned across up to N processes, making the
effective per-IP budget bytes_per_minute × N (~12x on the damus relay). The only
exact-enforcement lever was reqWorker=1/reqMonitor=1, which kills read
parallelism.
Back read_bandwidth's per-IP byte buckets with a single mmap'd MAP_SHARED file
(memmap2) shared by every read process on the host: a fixed-size open-addressing
table keyed by client IP (repr(C): IP bytes + tokens f64-bits + last_refill),
FNV-1a home slot + linear probe, one per-slot spinlock guarding each bucket, and
a versioned header with a one-time CAS init handshake (first process creates +
zeroes, others attach + validate). last_refill is CLOCK_MONOTONIC nanoseconds
(std::time::Instant is process-relative and can't live in shared memory); a
future timestamp (stale from a prior boot) resets the slot. Fails open —
delivers — when a slot lock can't be taken, the table is full, the key is
over-long, or the mmap/init fails, matching the ReadFilter contract. No eviction:
the table sizes to max_ips and lives on tmpfs, so it survives strfry restarts and
resets on reboot. New config keys shared_state_path + max_ips (unset path => a
private per-process map, the old behavior). Scope is read_bandwidth only:
max_events_per_conn is keyed by connId, already pinned to one process.
Verified E2E against strfry (req-plugin-simplified, reqWorker=3 reqMonitor=3),
one IP across 8 reconnects, budget 1000 B/min, ~413 B events: with
shared_state_path set the aggregate delivered ONE budget (2 events) and every
later reconnect got CLOSED "budget exhausted" from whichever of the 6 processes
served it; with the path unset (per-process control) the same drive delivered 6
= 3 reqWorker processes × 2 — the sharding blowup, reproduced and then removed.
Changelog-Added: Shared-memory per-IP read-bandwidth buckets for accurate cross-process enforcement without reducing read thread count.
Headway: headway:noteguard/ethics-palm-remember
Agentium: agentium:swim-brother-army
Add an optional, feature-gated Cloudflare integration that escalates any
write-path IP-ban to the edge. Because both the ratelimit and content filters
funnel their bans through the shared `BanTracker::strike`, that one hook is the
single choke point: when a ban trips, the offender's IP is added to a Cloudflare
WAF IP list for the ban's duration, then removed when it expires, so a banned IP
is dropped by Cloudflare before it ever reaches the relay.
A new `cloudflare` module owns a process-global background worker (std thread +
mpsc, blocking `ureq`) shared by every `BanTracker`, armed once at startup from a
top-level `[cloudflare]` config table. The worker is required because (a) HTTP
must not block the note path and (b) a banned IP never reaches noteguard again,
so the local `banned_until` can't drive cleanup — removals are scheduled on the
worker's own expiry timer instead. The Lists API is async and deletes by item
id, so removal resolves the id via `GET ?search=<ip>` then `DELETE`.
The API token comes from the CLOUDFLARE_API_TOKEN env var (never the config
file); the non-secret account_id/list_id live in `[cloudflare]`. The operator
wires the list into a zone WAF custom rule once — noteguard only manages list
membership. Entirely opt-in: with no `[cloudflare]` section, a missing token, or
the feature compiled out, `edge_ban` is a no-op and noteguard behaves as before.
Verified end-to-end against a mock Cloudflare endpoint: two spam notes trip a
content ban, POST adds the IP with the right bearer token and body, and the 2s
expiry timer fires the GET+DELETE. Worker scheduling/dedup/extend logic is
unit-tested via a mock API (30 lib tests pass); default build is unchanged.
Changelog-Added: Optional Cloudflare edge-banning (cloudflare feature): IP-bans from the ratelimit and content filters are pushed to a Cloudflare WAF IP list and removed when they expire
Headway: headway:noteguard/fluid-tonight-siren
Agentium: agentium:neck-case-fog
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Extract the per-IP strike counter + temporary IP-ban that `ratelimit` grew
into a reusable `BanTracker` (src/filters/ban.rs), embedded in a filter with
`#[serde(flatten)]` so its `ban_after` / `ban_duration_secs` / `ban_message`
knobs stay at the filter's own config level. The tracker holds state; the
filter decides what a "strike" is and how to respond while banned.
`ratelimit` keeps its consecutive-streak policy (any accepted post calls
`reset`), and now `content` reuses the same machinery with a cumulative
policy: content violations accrue per IP without a clean note resetting them,
and once they cross `ban_after` every note from that IP is silently
shadow-rejected — matching content's existing stealth stance rather than
revealing a visible reject. Banning stays opt-in; with `ban_after` unset both
filters store zero per-IP state and behave exactly as before.
Changelog-Added: Content filter can now IP-ban repeat offenders after N violations, reusing the ratelimit ban mechanism
Headway: headway:noteguard/depend-jeans-enable
Agentium: agentium:voyage-hamster-exile
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The content filter compared raw bytes with `str::contains`, so airdrop-scam
spam spoofing the phrase with Cyrillic look-alikes ("Dаmus Аirdrop", where
а/А are U+0430/U+0410) slipped straight through a "Damus Airdrop" blocklist
entry.
Fold both the note content and each blocklist term to their UTS #39
confusables skeleton (via unicode-security) and case-fold before matching, so
homoglyph and casing variants collapse to the same form. Case is folded on
both sides of the skeleton because the confusable prototypes are not
case-stable. Normalized terms are cached lazily on first use.
Also list "Damus Airdrop" in noteguard.toml so the running relay catches it.
Changelog-Changed: content filter now catches homoglyph/case spoofs of blocked phrases
Headway: headway:noteguard/copy-resource-merit
Agentium: agentium:rate-guess-visit
Persistent offenders that keep hammering the write path while rate-limited
now escalate into a temporary IP-ban. The ratelimit filter tracks a per-IP
streak of consecutive rate-limit rejections; once it crosses `ban_after`,
every note from that IP is rejected for `ban_duration_secs` (default 3600,
one hour). Any accepted post resets the streak, and an expired ban clears
itself (and the streak) on the next post.
All three knobs are optional; omitting `ban_after` preserves the old
rate-limit-only behavior. Whitelisted IPs bypass banning entirely.
Changelog-Added: Ratelimit filter can now IP-ban persistent offenders that repeatedly hit the rate limit
Headway: headway:noteguard/depend-jeans-enable
Agentium: agentium:voyage-hamster-exile
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Add a worked `[readpolicy]` example to the shipped config, alongside the
write-side filters it already documents. The read loop reads this same file, so
an operator wiring up `readPolicy { plugin = "./noteguard read" }` had nothing
in front of them but the README.
Ships as a no-op: `pipeline = []` keeps the default deliver-all behavior (every
subscription takes strfry's passthrough fast path), and the three filter tables
below it are inert until a name is listed in the pipeline — the engine only
constructs what the pipeline names. The commented-out pipeline line shows how to
turn them on. Config for all three read filters is covered:
max_events_per_conn, read_bandwidth (including the per-thread scoping caveat),
and the feature-gated nscript guest.
Verified against the built binary: the write loop still parses this file
unchanged (it ignores the unknown `readpolicy` key); the read loop loads it and
reports `pipeline: [] (3 filter configs)` with passthrough:true and delivers
everything; with the pipeline uncommented all three tables deserialize and
behave (throttles disable passthrough, a whitelisted IP is exempt, an absent
guest module fails open); and in a build without the `nscript` feature the
nscript entry is logged and skipped rather than failing.
Changelog-None:
Headway: headway:noteguard/knee-derive-worth