Skip to main content
An email-processing agent using Cloudflare Email Routing. Demonstrates email parsing, auto-reply with HMAC-signed headers for secure routing, loop prevention, and comprehensive security testing.

What it demonstrates

  • Email Routing - Routes emails to agents based on email addresses (e.g., agent+id@domain.com)
  • Secure Reply Routing - HMAC-signed headers for secure reply flows
  • Email Parsing - Uses PostalMime to parse incoming emails
  • Auto-Reply - Automatically responds to incoming emails with loop prevention
  • State Management - Tracks email count and stores recent emails
  • Security Tests - Comprehensive test suite including attack bypass attempts

Server Implementation

src/index.ts

Email Routing Strategies

Uses HMAC-signed headers to securely route email replies:
Security features:
  • HMAC-SHA256 signatures prevent header forgery
  • Timestamp validation prevents replay attacks
  • Constant-time comparison prevents timing attacks

2. Address-Based Routing

Routes based on email address patterns:
Routing rules:
  • With sub-addresses: localpart+subaddress@domain.com โ†’ agentName: "localpart", agentId: "subaddress"
  • Without sub-addresses: localpart@domain.com โ†’ agentName: defaultAgentName, agentId: "localpart"

3. Catch-All Routing

Routes all emails to a single agent:

Composing Resolvers

Auto-Reply with Loop Prevention

The agent detects auto-replies to prevent infinite loops:

Secure Reply Flow

When sending outbound emails, the agent signs headers:
The reply includes signed headers:
When a reply comes back, the signature is verified before routing:

Testing

Automated Test Suite

Sample output:

Manual Testing

Available scenarios: basic, unicode, long-subject, multiline, special-chars

Running the Example

1

Install dependencies

2

Configure secret

Update wrangler.jsonc with a unique secret:
For production, use Wrangler secrets:
3

Start development server

4

Run tests

Deployment

1

Set production secret

2

Deploy

3

Configure email routing

In Cloudflare Dashboard:
  1. Go to https://dash.cloudflare.com/<account-id>/<domain>/email/routing/routes
  2. Add routing rules to point to your worker
4

Send emails

Send emails to addresses like:
  • support@yourdomain.com โ†’ EmailAgent with ID โ€œsupportโ€
  • EmailAgent+urgent@yourdomain.com โ†’ EmailAgent with ID โ€œurgentโ€

Security Tests

The test suite includes 15 attack scenarios:
  • Forged headers without signature
  • Random/fake signatures
  • Expired signatures
  • Future timestamps
  • Malformed timestamps
  • SQL injection payloads
  • Path traversal attempts
  • Header injection (newlines)
  • Unicode normalization attacks
  • Case manipulation
  • Long payload DoS attempts
  • Null byte injection
Run: npm run test:security

GitHub Webhook

Handle webhooks with signature verification

x402 Payments

HTTP payment gating with automatic payment

Workflows

Multi-step workflows with approval gates

Email Guide

In-depth guide to email routing