Skip to content

fix(email): authorize private replies with recipient-bound Message-IDs (WI-2365) - #567

Open
stefan-ernst wants to merge 4 commits into
mainfrom
fix/wi-2365-recipient-bound-reply-keys
Open

stefan-ernst wants to merge 4 commits into
mainfrom
fix/wi-2365-recipient-bound-reply-keys

Conversation

@stefan-ernst

@stefan-ernst stefan-ernst commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes WI-2365. Supersedes the strict sender-authentication hold from WI-2362 / #566: intake no longer requires an SPF/DKIM/DMARC pass, and the claimed From is never treated as an identity. Whether a message may update a private conversation is decided by a recipient-bound outbound Message-ID (a reply key).

The branch is stacked on the WI-2362 branch, so the diff includes that foundation; the WI-2362 hold itself is removed here.

What changed

Reply keys

  • Every replyable outbound delivery gets a cryptographically random, per-recipient Message-ID (newReplyMessageID, 128 bits from crypto/rand). It is generated once and persisted on the email_reply_outbox row, so a delivery retry reuses the same key. The existing mailbox address is kept (no plus-addressing).
  • A key is honored for 30 days; the outbox row is retained for 90 days before pruning, so an expired key never authorizes and the row stays available for audit.

Authorization (internal/email/processor.go)

  • A reply appends to an existing private request only when:
    1. it references a Windshift-issued outbound Message-ID (lookupReplyKey → email_reply_outbox), and
    2. the claimed sender is that key's intended recipient, and
    3. the sender is still the item's creator or a current external participant.
  • A customer-generated inbound ID, a legacy thread reference, a missing header, or a prior unverified sender never authorizes. Those messages start a separate staff-visible request.
  • Pre-upgrade outbound IDs (recorded before WI-2365, so with no recipient-bound outbox row) are tolerated only on the ticket that recorded them; a ticket with a recipient-bound key, and every new ticket, requires an exact reply key.
  • The "prior sender on the thread" clause is removed.
  • Authorization runs before any customer/access side effect. A new request records a contact address without granting portal/thread access; an authorized reply (proven by the key) restores channel access.

Recipient-specific threading (internal/services/email_reply_service.go)

  • loadItemThread + threadHeadersForRecipient build In-Reply-To/References per recipient: the conversation root, inbound messages, and only that recipient's own prior reply keys. Another recipient's reply key is never copied into the headers.
  • A reply is still enqueued and sent when the original mail had no Message-ID (empty threading headers must not drop a staff reply).
  • Automation and participant notices persist their reply key before crossing the SMTP boundary, so a sent notice can always be replied to.
  • All outbound paths intended to accept replies are covered: comment replies, intake confirmations, and notices.

Strict sender-authentication controls removed

  • Removed the mailbox email_sender_authentication and intake sender_authentication fields, the frontend selectors and locale strings, the scheduler hold handling, and the policy helpers.
  • The SPF/DKIM/DMARC verdict is still recorded on the tracking row for the admin diagnostics view, but intake never gates on it.

Acceptance criteria

  • New email creates a staff-visible request with a contact address; the claimed From grants no portal/thread access.
  • Random, per-recipient outbound Message-ID persisted once and reused on retry; used as the reply key via In-Reply-To/References; mailbox address unchanged.
  • Append only when the reply references a Windshift-issued outbound ID for that request and the sender matches its intended recipient, who is still authorized.
  • Threading headers are recipient-specific; a reply key is scoped to its conversation.
  • Unmatched/legacy/missing-header replies become separate staff-visible requests. No SPF/DKIM/DMARC pass required for intake.
  • Covered by tests: valid reply without auth headers, recipient isolation, removed participants, legacy/inbound/missing IDs, stable IDs on retry, new-request identity boundary, key expiry, no-Message-ID replies, and notice key persistence.

Testing

  • go build ./..., go vet, gofmt, golangci-lint on touched packages: clean.
  • ./overlay.sh ../core -- -tags=test -count=1 ./internal/services/... passes.
  • ./internal/handlers/..., ./internal/database/... pass.
  • Frontend npm run check and npm run typecheck: clean.
  • Companion test PR lives in the private core-tests repository.

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
- 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.
…s (WI-2365)

Supersede the strict sender-authentication hold (WI-2362): intake no longer
requires an SPF/DKIM/DMARC pass, and the claimed From is never an identity.

- Mint a cryptographically random, per-recipient Message-ID for every
  replyable outbound delivery. The reply outbox persists item, recipient and
  Message-ID together, so an outbox retry reuses the same reply key.
- Authorize an email reply onto an existing private request only when it
  references a Windshift-issued outbound Message-ID whose intended recipient
  is the claimed sender, and that sender is still the creator or an active
  participant. Inbound/legacy/missing IDs and removed or unverified senders
  start a separate staff-visible request instead.
- Build In-Reply-To/References per recipient so another participant's reply
  key is never exposed.
- Authorize before creating a portal customer or granting access; a new
  request records a contact address without granting portal access.
@github-actions

Copy link
Copy Markdown

Size Change: 0 B 🆕

Total Size: 0 B

compressed-size-action

- Reply keys expire after 30 days but outbox rows are retained for 90 days
  before pruning, so an expired key can never authorize and the row stays
  available for audit.
- A staff reply is no longer dropped when the original mail had no
  Message-ID: empty threading headers still enqueue and send the reply.
- Automation and participant notices persist their reply key before crossing
  the SMTP boundary, so a sent notice can always be replied to.
- Pre-upgrade outbound IDs (no recipient-bound outbox row) are tolerated only
  on the ticket that recorded them; a ticket with a recipient-bound key, and
  every new ticket, requires an exact reply key.
- Remove the strict sender-authentication controls: the mailbox and intake
  policy fields, the frontend selectors and locale strings, the scheduler
  hold handling, and the policy helpers. The SPF/DKIM/DMARC verdict is still
  recorded for diagnostics, but intake never gates on it.
@stefan-ernst
stefan-ernst force-pushed the fix/wi-2365-recipient-bound-reply-keys branch from e5e84ce to 7b884c0 Compare November 24, 2026 16:40
@stefan-ernst

Copy link
Copy Markdown
Contributor Author

Request changes. The random per-recipient IDs are a good foundation, but this PR still treats some historical IDs as credentials and some unverified senders as request owners. Findings below are against 7b884c08.

  1. P1 — Expired keys become valid again after pruning. The legacy lookup treats an outbound tracking row as legacy whenever its matching outbox row is absent. At 90 days the outbox row is deleted, but the tracking row survives. A key rejected at day 31 can then authorize again, without its original recipient check. The same query accepts an old ID on a ticket that already has newer keys. Remove the fallback or use an explicit migration marker that survives pruning.

  2. P1 — Pre-upgrade outbox IDs are accepted as new reply keys. lookupReplyKey checks the row’s age and recipient, but not which generation of code issued it. Before this PR, comment Message-IDs were deterministic and copied into other participants’ References. A retained row does not prove its ID was private to the recipient. Only IDs explicitly issued under the new scheme should authorize a reply.

  3. P1 — A new request still acquires a portal owner from the unverified From. The intake path resolves the address to a customer, then writes that customer as creator and event actor. For a customer who already has portal access, the request-list query exposes the item as theirs and can include it in automatic organisation sharing. Skipping a new channel grant does not prevent that. Store the unverified contact separately from the authenticated owner.

  4. P1 — A notice is sent even when its key was not saved. sendItemNotice logs an insert error and proceeds to SMTP. If the later tracking insert succeeds, the notice is eligible for recipient-unbound legacy matching; if tracking also fails, its ID cannot route the reply. Return before SMTP on persistence failure, or deliver notices through the durable outbox.

  5. P2 — Staff replies can point to the wrong parent. loadItemThread appends all outbound rows after all inbound rows. When a customer answers a staff message, the next staff reply therefore names the older staff message as In-Reply-To, not the customer’s answer. Pending and discarded rows are also included. Merge inbound messages and delivered outbound messages in chronological order before choosing the parent.

  6. P2 — Reply matching scans the retained outbox once per referenced ID. SQLite EXPLAIN QUERY PLAN confirmed full table scans for both the key lookup and item thread lookup. On a busy installation, an ordinary long thread repeatedly scans 90 days of deliveries. Add indexes on message_id and on the item/thread ordering columns in both schemas and the upgrade migration.

Verification: ./overlay.sh ../core -- -tags=test -count=1 -run 'TestEmailReplyService' ./internal/services/... passed. ./overlay.sh ../core -- -tags=test -count=1 ./internal/email/... could not compile with the available test checkout: it still references the removed EmailSenderAuthentication field. I could not verify the WI-2365 companion coverage. GitHub’s only failing check at review time was the PR title: fix(backend): is an accepted scope; fix(email): is not.

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