Commit 712d7fa
committed
fix(stack): restore runtime null guards in encryption operations
Commit 5b7288b (feat(stack): remove null from Encrypted type) tightened
the public types to disallow null in single-value encrypt/decrypt, and
deleted the matching runtime `if (value === null) return null` guards
across every operation in packages/stack/src/encryption/operations/.
The type narrowing does not survive runtime. Callers that reach an
operation through a cast (e.g. `null as any`), dynamic model field
walking, or JS interop will silently have their null encrypted by
protect-ffi into a real SteVec ciphertext (`{ k: 'sv', v: 2, ... }`),
breaking symmetry with the model-helpers layer where null is treated as
"absent" at the field level. The newly-ported searchable-json `round-
trips null values` test in PR #328 exercises this path directly and
surfaced the regression.
Restored guards mirror the @cipherstash/protect pattern:
- encrypt / EncryptOperationWithLockContext: early return null
- bulkEncrypt / *WithLockContext: filter-null + position-preserving merge
- decrypt / *WithLockContext: early return null
- bulkDecrypt / *WithLockContext: filter-null + position-preserving merge
- encryptQuery / *WithLockContext: return { data: null } for null/undefined
- batchEncryptQuery / *WithLockContext: per-element filter, position-stable
Internal operation field/return types widen to `T | null` so the
restored guards compile. Public bulk types (`BulkEncryptPayload`,
`BulkEncryptedData`, `BulkDecryptPayload`, `BulkDecryptedData`) and
`EncryptedQueryResult` widen to admit null in element positions — these
now honestly reflect runtime behavior and let callers process mixed
nullable arrays without filtering ahead of time. `Encryption.decrypt()`
accepts `Encrypted | null`. `Encryption.encrypt()`'s public signature
stays narrow (`JsPlaintext`); the runtime guard is defense in depth.1 parent a087730 commit 712d7fa
9 files changed
Lines changed: 248 additions & 83 deletions
File tree
- .changeset
- packages/stack/src
- encryption
- operations
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
311 | 311 | | |
312 | 312 | | |
313 | 313 | | |
314 | | - | |
| 314 | + | |
315 | 315 | | |
316 | 316 | | |
317 | 317 | | |
| |||
Lines changed: 49 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| |||
21 | 22 | | |
22 | 23 | | |
23 | 24 | | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
24 | 40 | | |
25 | 41 | | |
26 | 42 | | |
| |||
42 | 58 | | |
43 | 59 | | |
44 | 60 | | |
45 | | - | |
| 61 | + | |
46 | 62 | | |
47 | 63 | | |
48 | 64 | | |
| |||
57 | 73 | | |
58 | 74 | | |
59 | 75 | | |
60 | | - | |
| 76 | + | |
| 77 | + | |
61 | 78 | | |
62 | 79 | | |
63 | | - | |
| 80 | + | |
64 | 81 | | |
| 82 | + | |
65 | 83 | | |
66 | | - | |
67 | | - | |
68 | | - | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
69 | 92 | | |
70 | 93 | | |
71 | 94 | | |
| |||
107 | 130 | | |
108 | 131 | | |
109 | 132 | | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
110 | 140 | | |
111 | 141 | | |
112 | 142 | | |
113 | 143 | | |
114 | 144 | | |
115 | 145 | | |
116 | | - | |
| 146 | + | |
117 | 147 | | |
118 | 148 | | |
119 | 149 | | |
| |||
122 | 152 | | |
123 | 153 | | |
124 | 154 | | |
125 | | - | |
| 155 | + | |
126 | 156 | | |
127 | 157 | | |
128 | 158 | | |
| |||
169 | 199 | | |
170 | 200 | | |
171 | 201 | | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
172 | 211 | | |
173 | 212 | | |
174 | 213 | | |
| |||
183 | 222 | | |
184 | 223 | | |
185 | 224 | | |
186 | | - | |
| 225 | + | |
187 | 226 | | |
188 | 227 | | |
189 | 228 | | |
| |||
193 | 232 | | |
194 | 233 | | |
195 | 234 | | |
196 | | - | |
| 235 | + | |
197 | 236 | | |
198 | 237 | | |
199 | 238 | | |
| |||
Lines changed: 41 additions & 20 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
14 | | - | |
| 15 | + | |
| 16 | + | |
15 | 17 | | |
16 | 18 | | |
17 | 19 | | |
18 | 20 | | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
24 | 28 | | |
25 | 29 | | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
26 | 35 | | |
27 | 36 | | |
28 | 37 | | |
29 | 38 | | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
35 | 50 | | |
| 51 | + | |
36 | 52 | | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
| 53 | + | |
| 54 | + | |
42 | 55 | | |
43 | 56 | | |
44 | 57 | | |
| |||
71 | 84 | | |
72 | 85 | | |
73 | 86 | | |
74 | | - | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
75 | 92 | | |
76 | 93 | | |
77 | 94 | | |
78 | 95 | | |
79 | | - | |
| 96 | + | |
80 | 97 | | |
81 | 98 | | |
82 | 99 | | |
| |||
140 | 157 | | |
141 | 158 | | |
142 | 159 | | |
143 | | - | |
| 160 | + | |
144 | 161 | | |
145 | 162 | | |
146 | 163 | | |
147 | 164 | | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
148 | 169 | | |
149 | 170 | | |
150 | 171 | | |
151 | | - | |
| 172 | + | |
152 | 173 | | |
153 | 174 | | |
154 | 175 | | |
| |||
0 commit comments