fix(audit-2026-06-11/H-H1): rate-limit email::send + per-message recipient cap

The Rhai SDK email::send path went straight to the SMTP relay with no
admission control. EmailRateLimiter lived in users_service.rs (gating
verification + password-reset emails) but was unreachable from the SDK
surface. Any anonymous-callable HTTP-route script could loop email::send
to burn the operator's SMTP quota or BCC-bomb thousands of recipients
per request.

Two new caps on EmailServiceImpl, both fire BEFORE message assembly +
SMTP connect:

1. Per-message recipient cap (to+cc+bcc combined). Default 20, override
   with PICLOUD_EMAIL_MAX_RECIPIENTS. New EmailError::TooManyRecipients
   variant. Closes the BCC-bomb amplification path.

2. EmailRateLimiter inlined into EmailServiceImpl:
   * Per-(app, recipient): RECIPIENT_BURST=5 / RECIPIENT_WINDOW=60min.
   * Per-app daily: APP_DAILY_CAP=200 / 24h.
   The structure mirrors users_service's private limiter; defense-in-
   depth redundancy is fine since the gates are identical and never
   conflict. New EmailError::RateLimited(&'static str) variant; the
   string names which bucket tripped so the operator can act.

users_service::map_email_error grows two cases (mapped to
EmailTransport for the script-facing error shape).

Tests:
* too_many_recipients_rejected — 30 recipients, default cap 20, rejected
  with TooManyRecipients.
* per_recipient_burst_caps_repeated_send_to_same_address — 6 sends to
  the same address; 6th returns RateLimited("per-recipient burst").

Audit ref: security_audit/09_external_integrations.md#f-ext-h-001.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
MechaCat02
2026-06-11 20:54:34 +02:00
parent 8b5a02b3ef
commit 34c63f31be
3 changed files with 194 additions and 1 deletions

View File

@@ -74,6 +74,19 @@ pub enum EmailError {
/// The SMTP relay rejected the message or the connection failed.
#[error("email transport error: {0}")]
Transport(String),
/// Audit 2026-06-11 H-H1 — the message recipient count exceeded
/// the per-message cap (default 20, override via
/// `PICLOUD_EMAIL_MAX_RECIPIENTS`).
#[error("too many recipients: {actual} exceeds the per-message cap of {limit}")]
TooManyRecipients { limit: usize, actual: usize },
/// Audit 2026-06-11 H-H1 — per-(app, recipient) or per-app burst
/// limit hit. The string names which bucket so operators can
/// distinguish "this user is being spammed" from "this app is
/// blowing through its SMTP quota."
#[error("email rate limit exceeded ({0})")]
RateLimited(&'static str),
}
/// Stub used by test harnesses that build a `Services` bundle without an