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 the unencrypted file size when closing streams #35649

Merged
merged 2 commits into from
Dec 7, 2022

Conversation

juliusknorr
Copy link
Member

@juliusknorr juliusknorr commented Dec 7, 2022

Summary

This PR fixes two issues related to primary object storage and master key encryption, both related to apps using the PHP Nodes api for file access.

77edd36 makes sure that the proper unencrypted size is written into the filecache when writing to a file through the putContent method. This for example is done by Collabora to save a file. Without updating the unencrypted size any follow up download would cause a BadSignature error.

f6d6898 Addresses a similar issue when a newFile was created with empty content. In this case the encryption stream writes just the header to the file but never updates the isEncrypted flag on it, so when creating a new empty file in the text app the encrypted content is returned to the user instead of the decrypted one.

Checklist

Signed-off-by: Julius Härtl <jus@bitgrid.net>
@juliusknorr juliusknorr requested review from PVince81, a team, ArtificialOwl and icewind1991 and removed request for a team December 7, 2022 15:33
@juliusknorr juliusknorr added bug 3. to review Waiting for reviews labels Dec 7, 2022
@szaimen szaimen added this to the Nextcloud 26 milestone Dec 7, 2022
@juliusknorr
Copy link
Member Author

/backport to stabel25

@juliusknorr
Copy link
Member Author

/backport to stable24

@juliusknorr
Copy link
Member Author

Backporting only to 24/25 as in general this requires #31966 to work

Copy link
Member

@PVince81 PVince81 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 wow, nice catch!

@juliusknorr
Copy link
Member Author

@icewind1991 @PVince81 Any hint on where the best place for a test case of this would be?

To reproduce this I used the following plain code snippets:

<?php
require_once __DIR__ . '/../lib/versioncheck.php';
try {
	require_once __DIR__ . '/../lib/base.php';
	$root = \OC::$server->getRootFolder()->getUserFolder('admin');
	try {
		$root->delete('existingfile');
	} catch (\OCP\Files\InvalidPathException $e) {
	} catch (\OCP\Files\NotFoundException $e) {
	} catch (\OCP\Files\NotPermittedException $e) {
	}

	$file = $root->newFile('existingfile', '');
	$data = [
		'size' => $file->getSize(),
		'unencrypted_size' => $file->getFileInfo()['unencrypted_size'],
		'encrypted' => $file->isEncrypted()
	];
	var_dump($data);

	$file = $root->get('existingfile');
	$file->putContent(str_repeat('A', 64*1024));
	$data = [
		'size' => $file->getSize(),
		'unencrypted_size' => $file->getFileInfo()['unencrypted_size'],
		'encrypted' => $file->isEncrypted()
	];
	var_dump($data);
} catch (Error $ex) {
	echo $ex->getMessage();
}

Before

array(3) { ["size"]=> int(8192) ["unencrypted_size"]=> int(0) ["encrypted"]=> bool(true) } array(3) { ["size"]=> int(74592) ["unencrypted_size"]=> int(0) ["encrypted"]=> bool(true) }

After

array(3) { ["size"]=> int(8192) ["unencrypted_size"]=> int(0) ["encrypted"]=> bool(true) } array(3) { ["size"]=> int(65536) ["unencrypted_size"]=> int(65536) ["encrypted"]=> bool(true) }

@PVince81
Copy link
Member

PVince81 commented Dec 7, 2022

@juliushaertl it seems there's a matching test class that might be useable for a unit test: tests/lib/Files/Stream/EncryptionTest.php

for integration tests, it seems we don't have encryption there.
otherwise we could have added a test that uploads and then propfinds the size

another idea would be to simply add code like your snippet above either the storage tests or stream tests, and rely on https://github.com/nextcloud/server/blob/master/.github/workflows/s3-primary.yml#L59 to have them run on S3.

maybe we should copy that workflow to also have one with encryption enabled ?

…ty content

Signed-off-by: Julius Härtl <jus@bitgrid.net>
@juliusknorr juliusknorr force-pushed the bugfix/object-storage-master-key-enc branch from f6d6898 to 59ae6e9 Compare December 7, 2022 18:22
@juliusknorr
Copy link
Member Author

Apparently the webdav API didn't trigger those issues, but maybe because they interact a bit differently with the file system apis through the view directly.

I adjusted the unit tests accordingly to have some basic coverage so we can get this in, but at some point dedicated integration tests with encryption might be nice to have.

@juliusknorr juliusknorr merged commit 87e638b into master Dec 7, 2022
@juliusknorr juliusknorr deleted the bugfix/object-storage-master-key-enc branch December 7, 2022 21:03
@backportbot-nextcloud
Copy link

The backport to stabel25 failed. Please do this backport manually.

@PVince81
Copy link
Member

PVince81 commented Dec 8, 2022

/backport to stable25

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3. to review Waiting for reviews bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Encryption header visible in newly created files
4 participants