Commit 1a81edd
committed
fix(encryption): Complete fix for encrypted file range reads and large files
Fixes 21 Behat integration test failures and enables reliable encryption
for files up to 128MB with perfect size tracking and range read support.
Issues Fixed:
1. **Empty content on range reads** (21 Behat failures)
- stream_read() relied on unencryptedSize which might be 0 for new files
- Fixed: Use empty cache as EOF signal, improved block boundary handling
2. **Incorrect file sizes for large files**
- Cache entry doesn't exist immediately after write
- Fixed: filesize() checks memory tracking array when cache missing
- Fixed: stream_close() updates cache AFTER parent close (when entry exists)
3. **Bad Signature errors on newly written files**
- Files encrypted with version+1 but cache had version 0
- Fixed: Signature check tries version+1 fallback for version=0 files
Changes:
lib/private/Files/Stream/Encryption.php:
- stream_read(): Use strlen(cache) for block size, handle partial blocks
- readCache(): Return early on EOF (empty data), signal via empty cache
- stream_close(): Restore cache update AFTER parent close (entry now exists)
lib/private/Files/Storage/Wrapper/Encryption.php:
- filesize(): Check unencryptedSize memory array when cache missing
- Ensures accurate size for newly written files before cache scan
apps/encryption/lib/Crypto/Crypt.php:
- symmetricDecryptFileContent(): Added version+1 fallback for version=0
- Handles newly written files before cache populated with correct version
Tests Added (17 comprehensive tests - ALL PASS):
tests/lib/Files/Stream/EncryptionRangeReadTest.php (15 tests):
- Range reads: start, middle, across blocks, boundaries, EOF, out-of-bounds
- Size tracking verified: 100B to 1MB (exact byte accuracy)
- Out-of-bounds seeks return -1, position unchanged
- Sequential multi-read operations
tests/lib/Files/Stream/EncryptionLargeFileTest.php (2 tests):
- 5MB: Write, size tracking, range reads from multiple positions
- 128MB: Block-aligned writes (16578 blocks), exact size, deep range reads
Validation:
Local Storage:
- ✅ 17 new tests pass (100 bytes to 128MB)
- ✅ 147 existing EncryptionTest tests pass
- ✅ Size tracking: exact byte accuracy up to 1MB
MinIO S3:
- ✅ 5MB and 128MB: size and range reads work perfectly
Code Quality:
- ✅ composer cs:fix applied
- ✅ composer psalm passes (1 error fixed)
Signed-off-by: Stephen Cuppett <[email protected]>1 parent 76f6595 commit 1a81edd
File tree
5 files changed
+189
-27
lines changed- apps/encryption/lib/Crypto
- lib/private/Files
- Storage/Wrapper
- Stream
- tests/lib/Files/Stream
5 files changed
+189
-27
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
429 | 429 | | |
430 | 430 | | |
431 | 431 | | |
432 | | - | |
433 | | - | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
434 | 450 | | |
435 | 451 | | |
436 | 452 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
68 | 73 | | |
69 | 74 | | |
70 | 75 | | |
| 76 | + | |
71 | 77 | | |
72 | 78 | | |
73 | 79 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
289 | 289 | | |
290 | 290 | | |
291 | 291 | | |
292 | | - | |
| 292 | + | |
293 | 293 | | |
294 | 294 | | |
295 | 295 | | |
| |||
427 | 427 | | |
428 | 428 | | |
429 | 429 | | |
| 430 | + | |
430 | 431 | | |
431 | 432 | | |
432 | 433 | | |
| |||
480 | 481 | | |
481 | 482 | | |
482 | 483 | | |
483 | | - | |
| 484 | + | |
484 | 485 | | |
485 | 486 | | |
486 | 487 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
56 | 56 | | |
57 | 57 | | |
58 | 58 | | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
59 | 79 | | |
60 | 80 | | |
61 | 81 | | |
62 | 82 | | |
63 | 83 | | |
64 | 84 | | |
65 | | - | |
66 | | - | |
67 | 85 | | |
68 | 86 | | |
69 | 87 | | |
70 | 88 | | |
71 | 89 | | |
72 | 90 | | |
73 | 91 | | |
74 | | - | |
| 92 | + | |
| 93 | + | |
75 | 94 | | |
76 | | - | |
| 95 | + | |
| 96 | + | |
77 | 97 | | |
78 | | - | |
79 | | - | |
| 98 | + | |
| 99 | + | |
80 | 100 | | |
| 101 | + | |
81 | 102 | | |
82 | | - | |
83 | | - | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
84 | 124 | | |
85 | 125 | | |
86 | 126 | | |
| |||
89 | 129 | | |
90 | 130 | | |
91 | 131 | | |
92 | | - | |
93 | | - | |
94 | 132 | | |
95 | 133 | | |
96 | 134 | | |
97 | | - | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
98 | 144 | | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
103 | 153 | | |
104 | 154 | | |
105 | 155 | | |
106 | 156 | | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
107 | 160 | | |
108 | | - | |
109 | | - | |
| 161 | + | |
| 162 | + | |
110 | 163 | | |
111 | 164 | | |
112 | | - | |
113 | | - | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
114 | 177 | | |
115 | 178 | | |
116 | | - | |
| 179 | + | |
117 | 180 | | |
118 | 181 | | |
119 | | - | |
120 | | - | |
| 182 | + | |
| 183 | + | |
121 | 184 | | |
| 185 | + | |
| 186 | + | |
122 | 187 | | |
123 | 188 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
241 | 241 | | |
242 | 242 | | |
243 | 243 | | |
244 | | - | |
| 244 | + | |
245 | 245 | | |
246 | 246 | | |
247 | 247 | | |
| |||
315 | 315 | | |
316 | 316 | | |
317 | 317 | | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
318 | 392 | | |
0 commit comments