Skip to content

Commit 035098d

Browse files
cuppettclaude
andcommitted
fix(encryption): Correctly report size for zero-byte encrypted files
Files with 0 bytes no longer incorrectly report as 8192 bytes. Widens unencryptedSize to ?int, fixes verifyUnencryptedSize to compare against header size instead of 0, and corrects Scanner to populate unencrypted_size on initial upload. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: Stephen Cuppett <steve@cuppett.com>
1 parent d56ad83 commit 035098d

5 files changed

Lines changed: 16 additions & 6 deletions

File tree

lib/private/Files/Cache/CacheEntry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public function __clone() {
140140

141141
#[\Override]
142142
public function getUnencryptedSize(): int {
143-
if ($this->data['encrypted'] && isset($this->data['unencrypted_size']) && $this->data['unencrypted_size'] > 0) {
143+
if ($this->data['encrypted'] && isset($this->data['unencrypted_size'])) {
144144
return $this->data['unencrypted_size'];
145145
} else {
146146
return $this->data['size'] ?? 0;

lib/private/Files/Cache/Scanner.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,12 @@ public function scanFile($file, $reuseExisting = 0, $parentId = -1, $cacheData =
183183
}
184184
}
185185

186-
// we only updated unencrypted_size if it's already set
187-
if (isset($cacheData['unencrypted_size']) && $cacheData['unencrypted_size'] === 0) {
186+
// Only skip updating unencrypted_size if both cached and new values are 0
187+
// This allows updating from incorrect cached 0 to correct non-zero value
188+
// while avoiding unnecessary updates when both are legitimately 0
189+
if (isset($cacheData['unencrypted_size'])
190+
&& $cacheData['unencrypted_size'] === 0
191+
&& (!isset($data['unencrypted_size']) || $data['unencrypted_size'] === 0)) {
188192
unset($data['unencrypted_size']);
189193
}
190194

@@ -202,7 +206,12 @@ public function scanFile($file, $reuseExisting = 0, $parentId = -1, $cacheData =
202206
$data['etag_changed'] = true;
203207
}
204208
} else {
205-
unset($data['unencrypted_size']);
209+
// For new files, preserve unencrypted_size only when the file is encrypted
210+
// and the key is present; otherwise clear it so it won't be written stale.
211+
if (!isset($data['encrypted']) || !$data['encrypted']
212+
|| !isset($data['unencrypted_size'])) {
213+
unset($data['unencrypted_size']);
214+
}
206215
$newData = $data;
207216
$fileId = -1;
208217
}

lib/private/Files/FileInfo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public function getSize($includeMounts = true) {
173173
if ($includeMounts) {
174174
$this->updateEntryFromSubMounts();
175175

176-
if ($this->isEncrypted() && isset($this->data['unencrypted_size']) && $this->data['unencrypted_size'] > 0) {
176+
if ($this->isEncrypted() && isset($this->data['unencrypted_size'])) {
177177
return $this->data['unencrypted_size'];
178178
} else {
179179
return isset($this->data['size']) ? 0 + $this->data['size'] : 0;

lib/private/Files/Storage/Wrapper/Encryption.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ protected function verifyUnencryptedSize(string $path, int $unencryptedSize): in
400400
if ($unencryptedSize < 0
401401
|| ($size > 0 && $unencryptedSize === $size)
402402
|| $unencryptedSize > $size
403+
|| ($unencryptedSize === 0 && $size > $this->util->getHeaderSize())
403404
) {
404405
// check if we already calculate the unencrypted size for the
405406
// given path to avoid recursions

lib/private/Files/Stream/Encryption.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Encryption extends Wrapper {
2828
protected string $cache;
2929
protected ?int $size = null;
3030
protected int $position;
31-
protected ?int $unencryptedSize = null;
31+
protected int|float|null $unencryptedSize = null;
3232
protected int $headerSize;
3333
protected int $unencryptedBlockSize;
3434
protected array $header;

0 commit comments

Comments
 (0)