Skip to content

fix(backend): authenticate the inbound From before trusting it (WI-2362) - #566

Closed
stefan-ernst wants to merge 2 commits into
mainfrom
fix/wi-2362-inbound-sender-authentication
Closed

stefan-ernst wants to merge 2 commits into
mainfrom
fix/wi-2362-inbound-sender-authentication

Conversation

@stefan-ernst

Copy link
Copy Markdown
Contributor

Summary

Fixes WI-2362. Inbound email attributed every message to a portal customer and admitted replies to a private thread based on the attacker-controlled From header. Neither findOrCreatePortalCustomer nor the senderIsThreadParticipant guard consulted Authentication-Results, Received-SPF, DKIM, or DMARC, so anyone who could deliver a spoofed From could:

  • post a reply into a private thread as a participant and gain persistent membership, and
  • create a request and send notifications under a customer's identity.

What changed

  • Evaluate sender authentication (internal/email/sender_auth.go). Parse the receiving MTA's Authentication-Results (falling back to Received-SPF) and require an aligned spf=pass, dkim=pass, or dmarc=pass for the From domain. Relaxed DMARC alignment (parent/child domains) is accepted; a DMARC pass is checked against header.from when present.
  • Trust only the receiving MTA's headers. The parser now records header order; only Authentication-Results / Received-SPF values above the first Received header are considered, so a sender-injected result below the MTA's Received header is ignored.
  • Strict is the default. A mailbox (email_sender_authentication) and per-intake (sender_authentication) policy can be strict or off; an empty value resolves to strict. In strict mode, unauthenticated mail is held: no item or comment is created, no portal customer is matched or created under the claimed identity, and the outcome (sender_auth) is recorded for the admin diagnostics view and channel email log.
  • Schema. email_message_tracking.sender_auth and intakes.sender_authentication (migration 20261204_email_sender_authentication). No backfill: existing intakes stay strict.
  • UI. Mailbox and per-intake sender-authentication controls, plus a label for unauthenticated holds in the email-intake diagnostics view.

Migration note

Strict is the default for new and existing intakes. If a mailbox provider does not add Authentication-Results / Received-SPF headers, operators must set Sender authentication to Off on the mailbox or the affected intake, or legitimate mail will be held. Held mail is visible under Admin → Diagnostics → Email intake and in the channel email log.

Testing

  • ./overlay.sh ../core -- -tags=test -count=1 ./internal/email/...
  • ./overlay.sh ../core -- -tags=test -count=1 ./internal/scheduler/...
  • ./overlay.sh ../core -- -tags=test -count=1 ./internal/database/...
  • ./overlay.sh ../core -- -tags=test -count=1 ./internal/repository/... ./internal/handlers/...
  • ./overlay.sh ../core -- -tags=test -count=1 ./internal/services/...
  • ./overlay.sh ../core -- -tags=test -count=1 -run 'Channel|Diagnostic|Email|Intake' ./tests/...
  • golangci-lint run on the changed packages
  • npm run check and npm run typecheck in frontend

New tests cover SPF/DKIM/DMARC pass, fail, and absent-header cases, aligned and misaligned domains, on both the new-request and reply paths, plus the forged-header ordering guard and the strict/off policies.

Companion PR

Tests live in the private repo: Windshiftapp/core-tests-internal#74

Inbound email attributed every message to a portal customer and admitted
replies to a private thread based on the attacker-controlled From header.
Neither the customer match nor the thread-participant guard consulted
Authentication-Results, Received-SPF, DKIM, or DMARC, so anyone able to
deliver a spoofed From could post into a private thread, become a
persistent participant, or create a request as a customer.

Evaluate the receiving MTA's Authentication-Results (falling back to
Received-SPF) against the From domain and require an aligned SPF, DKIM, or
DMARC pass. Only headers above the first Received header are trusted, so a
sender-injected Authentication-Results below the receiving MTA's Received
header is ignored. Under the default strict policy, mail whose sender
cannot be authenticated is held: no item or comment is created, no portal
customer is matched or created under the claimed identity, and the outcome
is recorded on the tracking row for the admin diagnostics view and channel
email log. Mailbox and intake policies can be set to strict or off for
providers that do not add authentication headers.

- internal/email/sender_auth.go: parser, alignment, and policy resolution
- parser records header order so injected results can be rejected
- email_message_tracking.sender_auth and intakes.sender_authentication
- mailbox and per-intake sender-authentication controls in the UI
@stefan-ernst stefan-ernst changed the title fix(email): authenticate the inbound From before trusting it (WI-2362) fix(backend): authenticate the inbound From before trusting it (WI-2362) Nov 24, 2026
@github-actions

Copy link
Copy Markdown

Size Change: 0 B 🆕

Total Size: 0 B

compressed-size-action

@stefan-ernst

Copy link
Copy Markdown
Contributor Author

Request changes. Strict mode can trust the wrong authentication result, and it can permanently delete legitimate mail that the UI describes as “held.”

Priority Finding
P1 Header position does not establish who authenticated the message. trustedHeaderValues accepts results when there is no Received boundary, but discards genuine results when a provider adds an internal delivery hop above them. Strict mode can therefore both accept unverified mail and reject authenticated mail. Establish a mailbox-specific trusted receiver boundary, fail closed when it cannot be established, and cover a real multi-hop provider layout.
P1 The parser can read quoted text as an authentication verdict. parseAuthenticationResults splits on semicolons and whitespace without respecting quoted strings, then accepts a method token anywhere in a clause. Even in a genuine receiver header, a quoted diagnostic or property is data, not a separate verdict. Use an RFC 8601-aware parser and accept methods only at clause boundaries.
P1 Received-SPF can authenticate the wrong identity. evaluateReceivedSPF treats a pass as proof of envelope-from without checking which identity the receiver evaluated. A HELO pass authenticates the sending host, not the envelope sender. Reject explicit non-mailfrom identities before using the envelope domain for alignment.
P1 A hold can delete the only copy of the message. The processor returns ActionSkipped, and the scheduler exempts only rate-limited mail from deletion. With delete configured, a legitimate message held on upgrade is expunged; diagnostics retains metadata, not the body. Give authentication holds a distinct disposition that preserves the original, including on a deduplicated retry.
P1 Editing an intake silently removes an explicit strict override. ChannelIntakesSection.svelte:104–108 changes every non-off value to '', which means inherit, not strict. On an off mailbox, saving an unrelated edit disables authentication for that intake. Preserve strict when loading the form.
P2 Relaxed alignment rejects legitimate sibling domains. domainsAligned checks parent/child suffixes rather than organizational domains. For example, billing.example.com and mail.example.com align under relaxed DMARC, but this code holds the message when SPF or DKIM is the only reported pass. Use organizational-domain comparison for those methods; compare a DMARC result’s reported header.from to the actual From domain directly.
P2 The channel email log does not identify holds. GetEmailLog returns sender_auth but not skipped_reason, and EmailLogModal renders - for these rows. Return and display the hold reason explicitly: an unaligned result can still have a raw summary of spf=pass.
P2 Recording header order allocates for the entire message body. parseHeaderOrder calls bytes.Split on the full raw message before breaking at the header boundary. Newline-heavy bodies can allocate far beyond the IMAP message-size limit just to read a few headers. Iterate lines lazily and stop at the blank line.

I ran the companion PR’s internal/email and internal/scheduler tests against 61cb816; both passed. The failed ARM64 build and OpenAPI checks both stopped on Go module proxy download errors.

- Trust only results above a mailbox-specific receiver boundary: the first
  Received hop whose host matches the mailbox's trusted authserv-ids
  (explicit config, provider defaults, or the mail host). A result from an
  untrusted authserv-id is rejected, and a message with no trusted hop fails
  closed instead of trusting an arbitrary header. An internal delivery hop
  above a genuine result no longer discards it.
- Parse Authentication-Results per RFC 8601: clauses split outside quoted
  strings and comments, and a method is accepted only as the first token of
  a clause, so a quoted diagnostic can never be read as a verdict.
- Reject Received-SPF passes without an envelope-from; a HELO pass
  authenticates the sending host, not the sender.
- Use organizational-domain comparison for SPF and DKIM alignment, and
  compare a DMARC header.from to the actual From domain directly, so
  sibling subdomains under one organizational domain align.
- Give authentication holds a distinct action: the scheduler leaves the
  original in the mailbox (never delete or mark read) and reports the hold,
  including on a deduplicated retry.
- Preserve an explicit per-intake strict override when editing an intake;
  it is no longer collapsed into inherit.
- Return and display skipped_reason in the channel email log so a held
  message is identified instead of showing a raw spf=pass summary.
- Read header order line by line instead of splitting the whole body.
@stefan-ernst

Copy link
Copy Markdown
Contributor Author

All eight findings are addressed in 57b9b7a, with companion tests in Windshiftapp/core-tests-internal#74.

P1 — trusted receiver boundary. EvaluateSenderAuthentication now takes a mailbox-specific trusted receiver list from ResolveTrustedAuthServIDs: an explicit email_trusted_authserv_ids, else derived from the OAuth provider, the IMAP host, and the monitored address domain. A result is trusted only when it sits above the first Received hop from a trusted host and its authserv-id matches that list. No trusted hop fails closed. A provider internal hop above a genuine result no longer discards it, and a sender-injected result below the provider's Received is ignored. Tests cover untrusted authserv-id, no boundary, injected-below-Received, and internal-hop.

P1 — RFC 8601 parser. Authentication-Results is split into clauses outside quoted strings and comments, and a method is accepted only as the first token of a clause. Added a test where a quoted reason="dmarc=pass ..." must not verify.

P1 — Received-SPF identity. A pass without envelope-from is rejected as a HELO result; only envelope-from is used for alignment. Covered.

P1 — hold disposition. New ActionHeldForAuth. The scheduler leaves held mail in the mailbox (no delete, no mark-read, no expunge) and reports held_for_auth; the processor returns the same action on a deduplicated retry by inspecting skipped_reason. New scheduler test asserts preservation under the delete disposition.

P1 — intake override. Removed the normalization that collapsed every non-off value to ''; an explicit strict is preserved on edit.

P2 — alignment. SPF/DKIM now use organizational-domain (public suffix list) alignment; DMARC compares the reported header.from to the actual From domain. Sibling-subdomain test added.

P2 — email log. GetEmailLog now returns skipped_reason, and the modal shows a "Held: unauthenticated sender" badge.

P2 — header order allocation. parseHeaderOrder walks the raw bytes line by line with IndexByte and stops at the header boundary instead of bytes.Split-ing the whole message.

Re-ran the email, scheduler, database, repository, handlers, and services suites plus the email/intake integration tests, golangci-lint, npm run check, and npm run typecheck — all green.

@stefan-ernst

Copy link
Copy Markdown
Contributor Author

Closing without merge. WI-2365 supersedes WI-2362: we will authorize private replies using a Windshift-issued, recipient-bound outbound Message-ID and keep ordinary email intake compatible with mailboxes that provide no authentication verdict. Mail filtering stays with the receiving provider. The underlying authorization fix remains open in WI-2365.

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