Skip to content

Fix crash in receiveEmail when no R2 bucket binding is found - #148

Merged
G4brym merged 1 commit into
mainfrom
fix/receive-email-missing-bucket-guard
Mar 9, 2026
Merged

Fix crash in receiveEmail when no R2 bucket binding is found#148
G4brym merged 1 commit into
mainfrom
fix/receive-email-missing-bucket-guard

Conversation

@G4brym

@G4brym G4brym commented Mar 8, 2026

Copy link
Copy Markdown
Owner

Summary

  • Adds a missing null check in receiveEmail() to prevent a TypeError crash when no R2 bucket binding is available
  • If neither emailRouting.targetBucket resolves to a valid bucket nor any R2-like binding is found in the environment, the handler now throws a descriptive Error instead of crashing on bucket.put() with an opaque TypeError: Cannot read properties of undefined

What changed

In packages/worker/src/modules/emails/receiveEmail.ts, after the two bucket discovery attempts (explicit config and environment scan), a guard clause was added:

if (!bucket) {
    throw new Error(
        "No R2 bucket binding found for email routing. Configure emailRouting.targetBucket or add an R2 bucket binding.",
    );
}

Why

Without this check, if a worker has email routing enabled but no R2 bucket binding configured, incoming emails would crash the worker with an unhelpful TypeError. The new error message tells the operator exactly what's wrong and how to fix it.

Test plan

  • All 78 worker tests pass (7 skipped — same as baseline)
  • All 110 dashboard tests pass
  • Linter passes with no issues

🤖 Generated with Claude Code

If no R2 bucket is configured via emailRouting.targetBucket and no
bucket binding exists in the environment, the email handler would
crash with a TypeError when calling bucket.put() on undefined.

This adds a guard that throws a descriptive error message instead.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
r2-explorer-docs 705622c Commit Preview URL

Branch Preview URL
Mar 08 2026, 01:08 AM

@G4brym G4brym left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated Code Review — APPROVED ✅

Review Scores: 5/5 reviewers approved

Summary

Reviewed the addition of a null guard for the R2 bucket binding in receiveEmail(). This is a clean, minimal defensive fix that replaces an opaque TypeError crash with a descriptive, actionable error message. The change is correctly placed after both bucket discovery attempts and before any bucket usage.

Review Perspectives

  1. Correctness: ✅ Guard is in the right position, covers the exact gap between discovery and usage
  2. Security: ✅ Static error string, no user input in the message, no exposure risks
  3. Performance: ✅ Single boolean check, zero overhead
  4. Code Quality: ✅ Follows existing patterns, clear error message, proper changeset
  5. Testing: ✅ All 188 tests pass; the guard replaces one crash with a better one

Minor notes (non-blocking)

  • A unit test covering the no-bucket-binding scenario would be a nice addition in the future, but is not required for this defensive fix.

🤖 Automated review by prodboard

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant