Peder sheetSheet Note N1 · Sheet 08 TR / EN

Note N1 · Peder

IP PENALTY ENGINE

An IP penalty engine on Cloudflare Workers: score violations, quarantine, don't lock out the campus

Sheet
PEDER
Topic
Cloudflare Workers · Hono · D1
Source
src/lib/ceza.js
Date
2026-11-12
Reading
3 min

Short answer: instead of answering each violation on its own, write every violation to the IP as points. Points add up within a short window; past a threshold the IP is quarantined for a while and its requests get a 403 before reaching any business logic. The quarantine decision is written to D1, and the hot path reads from an in-memory cache. Don't count failed logins towards these points: a campus or dorm network can put hundreds of users behind one IP.

01Context

Peder is a session management system for an institution that trains psychological counsellors. It holds client records, session notes and supervisor feedback: special category personal data under Turkish data protection law (KVKK). The app runs as a single Cloudflare Worker (Hono), with data in D1.

Before reaching business logic, every request passes these layers in order: request logging, IP penalty, rate limit, body size limit, input filter (null bytes, path traversal, SQL injection patterns), country filter and request policy. The penalty engine is the second link in that chain.

02Points

Each violation type has a weight. Exceeding the rate limit, tripping the input filter, trying to reach someone else's record (IDOR) or calling a function the role isn't allowed to (BFLA) write different points; reaching for someone else's data weighs far more than exceeding the rate limit.

Points accumulate per IP within a 15-minute window; if 15 minutes pass since the last violation, the counter starts from zero. There are two thresholds: the first means a short quarantine, the second a long one. A permanent ban is never automatic, only by admin decision.

03Cache and persistence

The penalty check runs on every request, so it has to be fast. Decisions are kept in two places:

  • An in-memory Map: inside the Worker instance; if there is an active penalty it returns without touching the database.
  • D1 (ip_cezalari): so the same decision holds across Cloudflare locations and Worker instances. If it isn't in memory it is read from here and cached.
const onbellek = cezaOnbellegi.get(istemciIp);
if (onbellek && (!onbellek.bitis || onbellek.bitis > simdi)) return onbellek;

const ceza = await bir(c, `SELECT * FROM ip_cezalari
    WHERE ip = ? AND durum = 'aktif' AND (bitis IS NULL OR bitis > ?)
    ORDER BY id DESC LIMIT 1`, istemciIp, simdi);

Every violation, whether or not a penalty follows, is also written to the guvenlik_olaylari table: request id, route, method, user, points and outcome. The security screen in the admin panel reads from this table.

04What a quarantined IP sees

c.header('Retry-After', String(kalanSaniye));
return c.text('Erişiminiz güvenlik politikaları nedeniyle geçici olarak sınırlandırılmıştır.', 403);

The response doesn't say which rule fired, the points or the threshold; an attacker can't measure responses to stay just below it. Retry-After is there so a legitimate client knows when to try again.

05The shared IP problem

In the first design, failed password attempts also wrote points. Most users sign in from the same university network; hundreds of people share one public IP behind NAT. A few students mistyping their passwords could have locked the whole institution out for 30 minutes.

// No points for the IP jail: on a shared IP such as a campus/NAT, one user's
// failed attempts shouldn't lock out the whole institution for 30 minutes.

Failed logins now only hit the login endpoint's own rate limit and attempt counter. An IP penalty is written only for brute-force attempts that arrive after that lock has triggered. The security rule was tuned to the users' real network conditions.

06Admin intervention

When an admin lifts a penalty, the record is not deleted; its status becomes "lifted", with who lifted it and why. The in-memory penalty and point counter are cleared at the same time; otherwise the stale cached decision would keep enforcing a penalty already lifted in the database.

→More notes from this project

—End of notewritten by MUHAMMEDPYZ