Files
openmail/internal/core/core.go
T
karti-ai 428040d964 Fix 13 findings from adversarial milestone-1 review
Blockers in the ingest path (all funnel real mail in milestone 2):
- H1: coerce body bytes to valid UTF-8 (ToValidUTF8) — non-UTF-8/mid-rune cuts
  no longer abort the ingest tx and drop the message.
- H2: EnsurePart's recoverable parse error is non-fatal — proceed with the
  guaranteed-usable Part so messy real-world mail is stored, not rejected.
- H3: extractBodies descends into message/rfc822 (Part.Message via
  SetMessageReaderAt) — forwarded/bounce bodies no longer lost.
- H4: GetMessage/GetThread/GetThreadMessages scoped to inbox_id — no cross-inbox
  access; reply no longer a confused deputy.

Hardening:
- M1: /healthz no longer leaks DB error to unauthenticated callers.
- M2: all DB errors funnel through handleErr; malformed UUID -> 404, dup -> 409,
  internal errors no longer echo the driver string.
- M3: index messages(inbox_id, message_id_hdr) for thread resolution.
- M4: pods UNIQUE(name) + ON CONFLICT (name) — no duplicate default pods.
- L1: skip empty-User/Host addresses (no literal "@").
- L2: skip attachment-disposition parts when picking the body.
- L3: case-insensitive, trimmed 'Re:' detection.

Verified e2e vs Postgres 16: latin1 body stored valid UTF-8; rfc822-only body
extracted; cross-inbox 404; malformed UUID 404; dup 409; threading regression OK.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-21 13:22:35 -07:00

59 lines
1.7 KiB
Go

// Package core holds OpenMail's domain services: inboxes, messages, threads,
// drafts, and the inbound ingest path (parse → thread → store). It is the layer
// the API and MCP server call, and it implements mail.InboundSink so any
// MailBackend can deliver into it.
package core
import (
"context"
"errors"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/karti-ai/openmail/internal/store"
)
// ErrNotFound is returned when a requested resource does not exist.
var ErrNotFound = errors.New("core: not found")
type Service struct {
pool *pgxpool.Pool
}
func New(st *store.Store) *Service { return &Service{pool: st.Pool} }
// Ping verifies the database connection (used by the health endpoint).
func (s *Service) Ping(ctx context.Context) error { return s.pool.Ping(ctx) }
// EnsureDefaultPod returns the id of the singleton "default" pod, creating it if
// absent. Until DB-backed api_keys carry a pod_id, the API operates within this
// one tenant. Idempotent.
func (s *Service) EnsureDefaultPod(ctx context.Context) (string, error) {
var id string
err := s.pool.QueryRow(ctx, `SELECT id::text FROM pods WHERE name = 'default'`).Scan(&id)
if err == nil {
return id, nil
}
if !errors.Is(err, pgx.ErrNoRows) {
return "", err
}
err = s.pool.QueryRow(ctx,
`INSERT INTO pods (name) VALUES ('default')
ON CONFLICT (name) DO NOTHING
RETURNING id::text`).Scan(&id)
if errors.Is(err, pgx.ErrNoRows) {
// Lost a race; read the row the other writer created.
err = s.pool.QueryRow(ctx, `SELECT id::text FROM pods WHERE name = 'default'`).Scan(&id)
}
return id, err
}
// nullStr maps an optional string to a value usable as a nullable SQL arg.
func nullStr(s *string) any {
if s == nil {
return nil
}
return *s
}