| 1 | package sheets |
| 2 | |
| 3 | import ( |
| 4 | "encoding/base64" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestLoadSealerRefusesAMissingDEK(t *testing.T) { |
| 10 | t.Setenv("SHEETS_DEK", "") |
| 11 | _, err := LoadSealer() |
| 12 | if err == nil { |
| 13 | t.Fatal("LoadSealer with no SHEETS_DEK = nil error, want a refusal") |
| 14 | } |
| 15 | if !strings.Contains(err.Error(), "SHEETS_DEK") { |
| 16 | t.Fatalf("error %q does not name the variable an operator has to set", err) |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | func TestLoadSealerRefusesAShortDEK(t *testing.T) { |
| 21 | t.Setenv("SHEETS_DEK", base64.StdEncoding.EncodeToString(make([]byte, 16))) |
| 22 | if _, err := LoadSealer(); err == nil { |
| 23 | t.Fatal("LoadSealer with a 16-byte DEK = nil error, want a refusal") |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | func TestLoadSealerRefusesJunk(t *testing.T) { |
| 28 | t.Setenv("SHEETS_DEK", "not base64 at all!!") |
| 29 | if _, err := LoadSealer(); err == nil { |
| 30 | t.Fatal("LoadSealer with unparseable base64 = nil error, want a refusal") |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | func TestLoadSealerAcceptsA32ByteDEK(t *testing.T) { |
| 35 | t.Setenv("SHEETS_DEK", base64.StdEncoding.EncodeToString(make([]byte, 32))) |
| 36 | s, err := LoadSealer() |
| 37 | if err != nil { |
| 38 | t.Fatalf("LoadSealer: %v", err) |
| 39 | } |
| 40 | if s == nil { |
| 41 | t.Fatal("LoadSealer returned a nil sealer and a nil error") |
| 42 | } |
| 43 | // A sealer that cannot seal is not a sealer. This also pins that |
| 44 | // LoadSealer hands back something wired to the key it read, rather |
| 45 | // than a zero value that happens to be non-nil. |
| 46 | ct, err := s.Seal("cell.input", []byte("42")) |
| 47 | if err != nil { |
| 48 | t.Fatalf("Seal through a LoadSealer sealer: %v", err) |
| 49 | } |
| 50 | if len(ct) == 0 { |
| 51 | t.Fatal("Seal produced no ciphertext") |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // The DEK is a live key. A refusal that echoes it puts it in |
| 56 | // platform-held log storage we cannot grep (AGENTS.md / Mechanical |
| 57 | // rules), which is exactly the leak sealing exists to prevent. |
| 58 | func TestLoadSealerNeverEchoesTheKey(t *testing.T) { |
| 59 | // Valid base64, wrong length — so it reaches the length branch, |
| 60 | // which is the one most likely to be written with %q of the input. |
| 61 | const secret = "c2VjcmV0LXNlY3JldC1zZWNyZXQ=" |
| 62 | t.Setenv("SHEETS_DEK", secret) |
| 63 | _, err := LoadSealer() |
| 64 | if err == nil { |
| 65 | t.Fatal("a short DEK was accepted") |
| 66 | } |
| 67 | if strings.Contains(err.Error(), secret) { |
| 68 | t.Fatalf("the refusal contains the DEK itself: %q", err) |
| 69 | } |
| 70 | } |
| 71 | |