release gate / check (push) Failing after 6m56s
Transactional mail now goes out as the Superbrain HTML design
(internal/mailer/template.html, embedded verbatim) with a plain-text
alternative: multipart/alternative (plain first per RFC 2046), random
boundary, CRLF normalization and SMTP dot-stuffing. Render() fills the
{{placeholders}} with HTML-escaped values and removes optional sections
(CTA + fallback link, note block, footer links, address) when empty;
anchor drift fails the unit tests and degrades to plain text at send
time, never blocking delivery. Password reset gets the headline,
first-name greeting, "Choose a new password" button (Outlook VML
fallback) and expiry/single-use note; notification and agent emails
render title-as-headline with body paragraphs.
The template's footer links are now real pages:
- /help: public help center (10 topic sections + FAQ accordion), linked
from the login page.
- /unsubscribe?token=...: public confirmation page; the HMAC-signed user
id (notify.UnsubscribeToken, domain-separated, stable, no stored row)
IS the credential. POST /api/notifications/unsubscribe accepts the
RFC 8058 one-click shape (token in query, form body ignored) and the
browser JSON shape; it disables the user's enabled email channels and
pauses enabled agent email tasks addressed to their own email
(idempotent, re-enableable, per-client rate limited, audit logged).
- Email preferences deep-links to /settings?tab=notifications; Settings
now honors ?tab=<section> (unknown values fall back to profile).
notify.FooterLinks is the single eligibility point: preferences + help
whenever NEXTAUTH_URL is set; unsubscribe ONLY when the recipient is the
owner's own account email — third-party channel addresses and agent
recipients never receive a link that changes someone else's settings,
and transactional mail (password reset) never carries unsubscribe.
Owner-addressed mail also carries List-Unsubscribe +
List-Unsubscribe-Post headers.
Verified: Go build/vet/unit green; new TestUnsubscribeFlow integration
test pins footer + headers, blast radius (slack channel and third-party
task survive), one-click + JSON paths, idempotence, tampered tokens and
the third-party/transactional exclusions; notifications, phase-2,
password-reset and agent suites green; web lint/SSR-lint/build 0 errors;
full browser regressions pass; live smoke of /help, /unsubscribe states,
the settings deep-link auth gate and bad-token 400 through the proxy.
77 lines
2.9 KiB
Go
77 lines
2.9 KiB
Go
package notify
|
|
|
|
// Email footer links and the unsubscribe token. The token is a stable,
|
|
// HMAC-signed user id (no stored row, no expiry): every notification mail
|
|
// carries the same link, and rotating AUTH_SECRET invalidates outstanding
|
|
// links exactly like sessions. Unsubscribe is a NOTIFICATION preference —
|
|
// transactional mail (password reset) is never gated by it.
|
|
|
|
import (
|
|
"context"
|
|
"crypto/hmac"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"strings"
|
|
|
|
"github.com/andrew/superbrain/internal/config"
|
|
"github.com/andrew/superbrain/internal/db"
|
|
"github.com/andrew/superbrain/internal/mailer"
|
|
)
|
|
|
|
// unsubscribeDomain separates these MACs from every other use of the
|
|
// signing secret (assertions, forwarded-IP attestation, ...).
|
|
const unsubscribeDomain = "superbrain-unsubscribe:"
|
|
|
|
// UnsubscribeToken returns the signed unsubscribe token for a user. IDs
|
|
// are 32-char hex (db.NewID), so the single "." separator is unambiguous.
|
|
func UnsubscribeToken(userID string) string {
|
|
mac := hmac.New(sha256.New, []byte(config.Load().Secret()))
|
|
mac.Write([]byte(unsubscribeDomain + userID))
|
|
return userID + "." + hex.EncodeToString(mac.Sum(nil))
|
|
}
|
|
|
|
// ParseUnsubscribeToken validates a token and returns the user id it
|
|
// carries. The comparison is constant-time; a tampered or foreign-secret
|
|
// token never resolves an identity.
|
|
func ParseUnsubscribeToken(token string) (string, bool) {
|
|
userID, sig, ok := strings.Cut(token, ".")
|
|
if !ok || userID == "" || sig == "" {
|
|
return "", false
|
|
}
|
|
mac := hmac.New(sha256.New, []byte(config.Load().Secret()))
|
|
mac.Write([]byte(unsubscribeDomain + userID))
|
|
want := hex.EncodeToString(mac.Sum(nil))
|
|
if !hmac.Equal([]byte(want), []byte(sig)) {
|
|
return "", false
|
|
}
|
|
return userID, true
|
|
}
|
|
|
|
// FooterLinks builds the branded email footer for a message to recipient.
|
|
// Preferences and Help are offered whenever the deployment has a public
|
|
// origin (NEXTAUTH_URL). Unsubscribe is offered ONLY when recipient is the
|
|
// owner's own account email: a third-party recipient (a channel address or
|
|
// an agent task's To) must never receive a link that changes the owner's
|
|
// settings. With no origin configured every link stays empty and the
|
|
// renderer omits the footer sections.
|
|
func FooterLinks(ctx context.Context, d *db.DB, ownerID, recipient string) mailer.Content {
|
|
var c mailer.Content
|
|
base := strings.TrimRight(strings.TrimSpace(config.Load().NextAuthURL), "/")
|
|
if base == "" {
|
|
return c
|
|
}
|
|
c.PreferencesURL = base + "/settings?tab=notifications"
|
|
c.HelpURL = base + "/help"
|
|
var email *string
|
|
if err := d.QueryRowContext(ctx, `SELECT email FROM "User" WHERE id=$1`, ownerID).Scan(&email); err != nil {
|
|
return c
|
|
}
|
|
if email == nil || *email == "" || !strings.EqualFold(strings.TrimSpace(recipient), *email) {
|
|
return c
|
|
}
|
|
token := UnsubscribeToken(ownerID)
|
|
c.UnsubscribeURL = base + "/unsubscribe?token=" + token
|
|
c.UnsubscribePostURL = base + "/api/notifications/unsubscribe?token=" + token
|
|
return c
|
|
}
|