Dev.to Security ๐Ÿ” Cybersecurity ๐Ÿ‘ 0 ๐Ÿ“– 2 min read

How I Built a ~5KB Zero-Cookie Ad Server on Cloudflare Workers with HMAC-SHA256 Replay Protection

Most ad scripts on developer blogs are over 150KB, drop third-party tracking cookies, clash with dark mode themes, and slow down page loads. Over the past few weeks, I built AsynchAds(https://asynchads.com) โ€” a lightwei

Most ad scripts on developer blogs are over 150KB, drop third-party tracking cookies, clash with dark mode themes, and slow down page loads.

Over the past few weeks, I built AsynchAds(https://asynchads.com) โ€” a lightweight, privacy-first white-label ad server running on Cloudflare Workers + D1 designed for dev blogs and technical publications that sell their own direct sponsorships.

Here is how the architecture works under the hood.

1. Sub-Millisecond Edge Delivery with Non-Blocking Analytics (ctx.waitUntil)

When a page requests an ad from /v1/cdn/render, the hot path needs to return JSON immediately without waiting on database writes.

  • Single-step D1 lookup: Checks the tenant's active direct sponsor flights first, falling back to either collapsing the slot cleanly (display: none) or rendering the publisher's custom house ad if no sponsor is scheduled.
  • Off-path telemetry: Placement analytics and quota counters run inside ctx.waitUntil(...), keeping response latency at a few milliseconds on the Cloudflare edge.

2. Stopping Replay & Bot Spam Without Cookies (nonce + ts + HMAC-SHA256)

One of the easiest ways to break an ad server is spamming /click or /view endpoints with curl to inflate sponsor metrics or drain a publisher's monthly impression quota.

Instead of storing user cookies or fingerprinting visitors, every /v1/cdn/render response mints short-lived cryptographic tokens for its view and click beacons:

  1. ts (Timestamp): Rejects expired or future-skewed beacons outside a tight time window.
  2. nonce: One-time token checked at the edge so replaying the exact same signed beacon URL twice is ignored.
  3. HMAC-SHA256 Signature: Verifies via Web Crypto (crypto.subtle) that the payload actually originated from our edge Worker.
  4. Edge Bot Filtering: Inspects User-Agent and Cloudflare metadata (request.cf) plus minimum human view-to-click timing so automated crawlers and headless scrapers don't pollute sponsor reports.

3. ~5KB Client Script That Auto-Adapts to Dark/Light Themes

Publishers hate writing custom CSS overrides just to make a sponsor card look native.

Our client script is Open source on (https://github.com/AsynchAds/client-script) stays around ~5KB with zero external dependencies:

  • XSS-Safe DOM: Uses strictly document.createElement and textContent (zero innerHTML) and enforces https:// destination protocols.
  • Automatic Theme & Typography Detection: Reads the host container's computed background color and font family (getComputedStyle) so card and inline placements blend directly into light or dark developer themes out of the box.

Try It or Poke Holes in the Architecture

I opened up a free Sandbox tier (10,000 monthly views, $0, no credit card) on https://://asynchads.com/ad-server so anyone running a dev blog or documentation site can test it live.

If you spot any edge cases in the client script or have ideas to tighten the edge telemetry pipeline further, drop a comment belowโ€”happy to share more details on the D1 schema or Worker setup!

๐Ÿ“ฐ Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes โ€” full credit and traffic to the original publisher.