#9 Encrypted-at-rest R2 storage for scrubbed reports

Implement the storage path: for each accepted report, scrub PII (#8),
encrypt with AES-256-GCM (ADR #5), and write only ciphertext to R2, wired
in as the real ingest Sink replacing NopSink.

- internal/crypto: AES-256-GCM in the exact ADR #5 wire format
  (magic "LMB1" || version || key_id BE16 || nonce(12) || ct || tag(16);
  the 7-byte header is the GCM AAD). Provider-independent framing shared by
  a host crypto/aes+crypto/cipher impl (tests, devserver) and a Wasm
  SubtleCrypto impl (syscall/js, //go:build js && wasm) per the TinyGo
  constraint; both produce byte-identical frames. Versioned keyring with
  key_id rotation; ParseKeyring reads the Secrets Store JSON secret.
- internal/storage: ObjectStore interface with an in-memory fake (tests,
  devserver) and a Wasm R2Store (syumai/workers R2 binding). Sink ties
  scrub -> Seal -> Put under a unique reports/<ts>-<rand> key. WorkerSink
  loads the keyring from Secrets Store (BUGREPORT_ENC_KEYRING), cached for
  the isolate lifetime.
- handler.New now takes an injectable ingest.Sink; the Worker uses the real
  R2/Secrets-Store sink, the devserver a memory + throwaway-key sink.
- wrangler.jsonc: add REPORTS_BUCKET (R2) and BUGREPORT_ENC_KEYRING
  (Secrets Store) bindings.

Tests (host, no TinyGo): encrypt/decrypt roundtrip; ciphertext != plaintext;
wrong key + tamper (ct/tag/nonce/header-AAD) fail; exact wire layout plus a
known-answer vector; key_id rotation with retained keys; full sink path (PII
scrubbed then encrypted, readback requires the key and yields the scrubbed
content). Existing ingest/handler behavior preserved (202 on valid POST).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-02 15:08:21 -05:00
committed by Jason Ross
co-authored by Claude Opus 4.8
parent 3d320a8cea
commit bd7fe21d97
15 changed files with 1520 additions and 9 deletions
+24 -1
View File
@@ -4,7 +4,10 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/JMR-dev/LibreMail-Bug-Report-Ingest/internal/ingest"
)
// doRequest runs a single request through the handler and returns the recorder.
@@ -12,7 +15,7 @@ func doRequest(t *testing.T, method, target string) *httptest.ResponseRecorder {
t.Helper()
req := httptest.NewRequest(method, target, nil)
rec := httptest.NewRecorder()
New().ServeHTTP(rec, req)
New(nil).ServeHTTP(rec, req)
return rec
}
@@ -71,6 +74,26 @@ func TestUnknownPathReturns404(t *testing.T) {
}
}
// TestReportsRoutedToInjectedSink proves New wires the injected sink into
// POST /v1/reports (the #9 storage seam), returning 202 and storing the report.
func TestReportsRoutedToInjectedSink(t *testing.T) {
sink := &ingest.MemorySink{}
h := New(sink)
body := `{"appVersion":"1.0.0","platform":"android","report":"boom"}`
req := httptest.NewRequest(http.MethodPost, "/v1/reports", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusAccepted {
t.Fatalf("POST /v1/reports status = %d, want %d", rec.Code, http.StatusAccepted)
}
if sink.Len() != 1 {
t.Fatalf("injected sink stored %d reports, want 1", sink.Len())
}
}
func TestNonGetReturns405(t *testing.T) {
for _, path := range []string{"/", "/healthz"} {
rec := doRequest(t, http.MethodPost, path)