Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Encryption.php to fix issue #34599 "encrypted remote storage" #36179

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions lib/private/Files/Stream/Encryption.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,25 @@ public function stream_read($count) {
return $result;
}

/**
* stream_read wrapper to read a block of the requested size
* There is a problem with the underlying php functions not returning the requested amount of data in any case.
* Thus, this function will call the read mechanism of its parent until it has read the correct amount.
*/
private function stream_read_block($blockSize) {
$remaining = $blockSize;
$data = "";

do {
$chunk = parent::stream_read($remaining);
$chunk_len = \strlen($chunk);
$data .= $chunk;
$remaining -= $chunk_len;
} while (($remaining > 0) && ($chunk_len > 0));

return $data;
}

public function stream_write($data) {
$length = 0;
// loop over $data to fit it in 6126 sized unencrypted blocks
Expand Down Expand Up @@ -445,7 +464,7 @@ protected function readCache() {
// don't try to fill the cache when trying to write at the end of the unencrypted file when it coincides with new block
if ($this->cache === '' && !($this->position === $this->unencryptedSize && ($this->position % $this->unencryptedBlockSize) === 0)) {
// Get the data from the file handle
$data = parent::stream_read($this->util->getBlockSize());
$data = $this->stream_read_block($this->util->getBlockSize());
$position = (int)\floor($this->position/$this->unencryptedBlockSize);
$numberOfChunks = (int)($this->unencryptedSize / $this->unencryptedBlockSize);
if ($numberOfChunks === $position) {
Expand All @@ -470,7 +489,7 @@ protected function writeHeader() {
* read first block to skip the header
*/
protected function skipHeader() {
parent::stream_read($this->headerSize);
$this->stream_read_block($this->headerSize);
}

/**
Expand Down