oficina / sheets Public

Clone
git clone https://amadan.net/oficina/sheets

Plain git — no account needed to clone.

Download

Download this file

1package xlsx_test
2
3import (
4 "bytes"
5 "context"
6 "os"
7 "path/filepath"
8 "strings"
9 "testing"
10
11 "amadan.net/rastrillo/rastrillo/blobs"
12
13 "amadan.net/oficina/sheets/internal/sheets/xlsx"
14)
15
16// StoreSealed must never let a plaintext spreadsheet reach the
17// underlying blob backend — spec §0 and §2.3, and the task's own
18// constraint that an unsealed generated XLSX file in a bucket defeats
19// the one rule through a side door. This proves it against blobs.Dir
20// (content-addressed files on disk — the same shape blobs.S3 has,
21// minus network credentials this test environment doesn't have), by
22// grepping the raw stored bytes for a distinctive string only a cell in
23// the exported workbook could contain.
24func TestStoreSealedNeverWritesPlaintext(t *testing.T) {
25 const canary = "CANARY-XLSX-BLOB-PLAINTEXT-71ad"
26
27 db := openTestDB(t)
28 h := newTab(t, db, testSealer(t))
29 rowID, err := h.EnsureRow(0)
30 if err != nil {
31 t.Fatalf("EnsureRow: %v", err)
32 }
33 colID, err := h.EnsureCol(0)
34 if err != nil {
35 t.Fatalf("EnsureCol: %v", err)
36 }
37 if err := h.Set(rowID, colID, canary, 1); err != nil {
38 t.Fatalf("Set: %v", err)
39 }
40
41 blobDir := t.TempDir()
42 underlying, err := blobs.Dir(blobDir)
43 if err != nil {
44 t.Fatalf("blobs.Dir: %v", err)
45 }
46 key := make([]byte, 32)
47 for i := range key {
48 key[i] = byte(i)
49 }
50
51 ref, err := xlsx.StoreSealed(context.Background(), underlying, key, h)
52 if err != nil {
53 t.Fatalf("StoreSealed: %v", err)
54 }
55 if ref.ContentType != "application/octet-stream" {
56 t.Fatalf("ContentType = %q, want application/octet-stream", ref.ContentType)
57 }
58
59 entries, err := os.ReadDir(blobDir)
60 if err != nil {
61 t.Fatalf("read blob dir: %v", err)
62 }
63 if len(entries) == 0 {
64 t.Fatal("blobs.Dir wrote nothing")
65 }
66 for _, e := range entries {
67 raw, err := os.ReadFile(filepath.Join(blobDir, e.Name()))
68 if err != nil {
69 t.Fatalf("read %s: %v", e.Name(), err)
70 }
71 if bytes.Contains(raw, []byte(canary)) {
72 t.Fatalf("plaintext %q found in stored blob %s", canary, e.Name())
73 }
74 // Also unsealed: not even a bare, un-decrypted XLSX zip should
75 // be recoverable from the stored bytes (a sealed blob's magic
76 // bytes are AES-GCM ciphertext, never the "PK\x03\x04" zip
77 // signature every XLSX file starts with).
78 if bytes.HasPrefix(raw, []byte("PK\x03\x04")) {
79 t.Fatalf("stored blob %s starts with an unsealed XLSX zip signature", e.Name())
80 }
81 }
82
83 // And the round trip itself must still work: LoadSealed recovers
84 // exactly what was stored.
85 h2 := newTab(t, db, testSealer(t))
86 if err := xlsx.LoadSealed(context.Background(), underlying, key, ref, h2); err != nil {
87 t.Fatalf("LoadSealed: %v", err)
88 }
89 rowID2, colID2, ok := cellAt(h2, 0, 0)
90 if !ok {
91 t.Fatal("LoadSealed produced no cell at (0,0)")
92 }
93 raw2, ok := h2.RawInput(rowID2, colID2)
94 if !ok || !strings.Contains(raw2, "CANARY-XLSX-BLOB-PLAINTEXT") {
95 t.Fatalf("RawInput after LoadSealed = %q, %v, want the canary text back", raw2, ok)
96 }
97}
98