Two problems the mail screens exposed, both in the delivery path. The subject, snippet, sender name and message-id columns have existed since the first migration, but nothing ever wrote them: every row went in with those fields NULL. A message list built on the database would have shown blank subjects for every message. Delivery now parses them once per message and stores them, decoding RFC 2047 encoded words so an accented subject is readable rather than raw. Parsing failures are non-fatal: a message with unreadable headers is still delivered, just with less metadata, because refusing it would lose mail over a malformed Subject. Worse, a message the spam filter routed to Junk failed delivery outright when the mailbox had no Junk folder - the sender got 451 and retried forever against a mailbox that would never grow one on its own. Delivery now falls back to INBOX and says so in the log. Found by sending a real message through port 25 against the running stack. The SMTP handler also swallowed the cause of any unexpected failure: the peer saw "temporary error" and the operator got nothing. It is logged now, which is how the missing folder was diagnosed at all. Also adds the JWT verifier the mail API needs. The tokens are minted by the auth service and verified here under the same secret, so the test fixtures are real tokens produced by that service: if the two implementations drift, this fails before the webmail does. Challenge tokens, wrong secrets, tampered payloads, expired tokens and the other surface's audience are all rejected. The dev seed gained the standard folder set, which is what a provisioned mailbox has and what the fallback above should not have to compensate for. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
94 lines
4.1 KiB
Go
94 lines
4.1 KiB
Go
package httppresentation
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// These tokens were minted by the auth service itself (services/auth,
|
|
// session.ts) under the secret below. They are the real wire format: if the
|
|
// two implementations ever drift, every mail request from the webmail starts
|
|
// failing and this test says so first.
|
|
const (
|
|
interopSecret = "shared-secret-for-interop-test"
|
|
interopSession = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJtYi0xIiwiZW1haWwiOiJ1c2VyQGV4YW1wbGUudGVzdCIsInJvbGUiOiJVU0VSIiwiZG9tYWluSWQiOiJkb20tMSIsInN1cmZhY2UiOiJ1c2VyIiwiYXVkIjoibGFtYmRhbWFpbDp1c2VyIiwibWZhU2F0aXNmaWVkIjp0cnVlLCJtZmFTYXRpc2ZpZWRBdCI6MTc4NTY3NDI0MzYzMiwicHVycG9zZSI6InNlc3Npb24iLCJpYXQiOjE3ODU2NzQyNDMsImV4cCI6MTc4NTcwMzA0M30.vrXM2T9HKfHDOThmx0LD_n05jfsr4iaKYpZrKTBa5NM"
|
|
interopChallenge = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJtYi0xIiwiZW1haWwiOiJ1c2VyQGV4YW1wbGUudGVzdCIsInJvbGUiOiJVU0VSIiwiZG9tYWluSWQiOiJkb20tMSIsInN1cmZhY2UiOiJ1c2VyIiwiYXVkIjoibGFtYmRhbWFpbDp1c2VyIiwibWZhU2F0aXNmaWVkIjpmYWxzZSwicHVycG9zZSI6Im1mYV9jaGFsbGVuZ2UiLCJpYXQiOjE3ODU2NzQyNDMsImV4cCI6MTc4NTY3NDU0M30.7OqmI9A1L9Efbt6AX4lqojZZ8IKuWi-6oe_VJNgJAxM"
|
|
// The captured tokens carry a fixed validity window. Verification is
|
|
// pinned to an instant inside it, so these tests assert on the signature
|
|
// and the claims rather than quietly turning red once the tokens age out.
|
|
interopIssuedAt = 1785674243
|
|
)
|
|
|
|
func testVerifier(secret string) *WebSessionVerifier {
|
|
v := NewWebSessionVerifier(secret)
|
|
v.now = func() time.Time { return time.Unix(interopIssuedAt+60, 0) }
|
|
return v
|
|
}
|
|
|
|
func TestWebSessionVerifier_AcceptsTokenIssuedByTheAuthService(t *testing.T) {
|
|
session, err := testVerifier(interopSecret).Verify(interopSession)
|
|
if err != nil {
|
|
t.Fatalf("a token minted by the auth service was rejected: %v", err)
|
|
}
|
|
if session.Email != "user@example.test" {
|
|
t.Errorf("Email = %q, want user@example.test", session.Email)
|
|
}
|
|
if session.Surface != "user" || session.Audience != "lambdamail:user" {
|
|
t.Errorf("surface/aud = %q/%q, want user/lambdamail:user", session.Surface, session.Audience)
|
|
}
|
|
}
|
|
|
|
// The challenge token is issued after the password but before the second
|
|
// factor. Accepting it here would let the mail API be opened without MFA.
|
|
func TestWebSessionVerifier_RejectsChallengeToken(t *testing.T) {
|
|
_, err := testVerifier(interopSecret).Verify(interopChallenge)
|
|
if !errors.Is(err, ErrNotASession) {
|
|
t.Fatalf("error = %v, want ErrNotASession", err)
|
|
}
|
|
}
|
|
|
|
func TestWebSessionVerifier_RejectsWrongSecret(t *testing.T) {
|
|
_, err := testVerifier("not-the-secret").Verify(interopSession)
|
|
if !errors.Is(err, ErrBadSignature) {
|
|
t.Fatalf("error = %v, want ErrBadSignature", err)
|
|
}
|
|
}
|
|
|
|
// A tampered payload must not survive: this is the check that stops a user
|
|
// from promoting their own token to the admin surface.
|
|
func TestWebSessionVerifier_RejectsTamperedPayload(t *testing.T) {
|
|
tampered := interopSession[:len(interopSession)-6] + "AAAAAA"
|
|
if _, err := testVerifier(interopSecret).Verify(tampered); err == nil {
|
|
t.Fatal("a tampered token verified")
|
|
}
|
|
}
|
|
|
|
func TestWebSessionVerifier_RequireSurfaceRejectsOtherSurface(t *testing.T) {
|
|
_, err := testVerifier(interopSecret).RequireSurface(interopSession, "admin")
|
|
if !errors.Is(err, ErrWrongAudience) {
|
|
t.Fatalf("error = %v, want ErrWrongAudience", err)
|
|
}
|
|
if _, err := testVerifier(interopSecret).RequireSurface(interopSession, "user"); err != nil {
|
|
t.Fatalf("the matching surface was rejected: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestWebSessionVerifier_RejectsMalformed(t *testing.T) {
|
|
v := testVerifier(interopSecret)
|
|
for _, token := range []string{"", "a.b", "a.b.c.d", "not-a-token"} {
|
|
if _, err := v.Verify(token); err == nil {
|
|
t.Errorf("token %q verified", token)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Expiry still has to bite: pinning the clock must not disable the check.
|
|
func TestWebSessionVerifier_RejectsExpiredToken(t *testing.T) {
|
|
v := NewWebSessionVerifier(interopSecret)
|
|
v.now = func() time.Time { return time.Unix(interopIssuedAt+9*60*60, 0) }
|
|
if _, err := v.Verify(interopSession); !errors.Is(err, ErrTokenExpired) {
|
|
t.Fatalf("error = %v, want ErrTokenExpired", err)
|
|
}
|
|
}
|