Skip to content

make encryption stream wrapper seekable - #12909

Closed
jknockaert wants to merge 19 commits into
owncloud:masterfrom
jknockaert:master
Closed

jknockaert wants to merge 19 commits into
owncloud:masterfrom
jknockaert:master

Conversation

@jknockaert

Copy link
Copy Markdown
Contributor

Rewrite of #8390.
There's a lot of stuff fixed, such as support for reads that are not exactly 6126 bytes, the encrypted file size pushed to fileinfo, but most importantly it makes encrypted streams seekable (if the storage is seekable). Main purpose is to serve http range requests well. Note that #10422 must be reversed to support http range requests on encrypted streams.
A main change is that stream_read and stream_write operate on an unencrypted cached block, with all file readings/writings handled by flush and readCache, which is where encryption/decryption happens.

@ghost

ghost commented Dec 17, 2014

Copy link
Copy Markdown

Thanks a lot for your contribution! Contributions to the core repo require a signed contributors agreement http://owncloud.org/contribute/agreement/ Alternatively you can add a comment here stating that this contribution is MIT licensed. Some more details about out pull request workflow can be found here: http://owncloud.org/code-reviews-on-github/

@PVince81

Copy link
Copy Markdown
Contributor

CC @schiesbn

@MorrisJobke

Copy link
Copy Markdown
Contributor

@owncloud-bot okay to test

@jknockaert

Copy link
Copy Markdown
Contributor Author

I had a quick look at the inspection report. As far as I can see there's nothing of much concern. The duplication reported is deliberate, the $newPosition bug seems to be a false positive and the $this->plainkey bug is not new.
Other than the fixes I mentioned in my first post I also want to note that the code comment by @icewind1991 in #8390 has been addressed in this PR.
The next step now would be to give the code some testing and review. As for testing I would like to advise that you can (manually) backport the changed parts of the code to 7.0.4, but you will need #11659 as well (and reverse #10422 if you want to test http range requests). The code written for #8390 worked perfectly on OC6, and the code for this PR does a perfect job for me on OC7, but I don't use the full range of functionality of owncloud.

@PVince81

Copy link
Copy Markdown
Contributor

This is a too big change for a backport (we only backport critical bugfixes) so this is more likely to make it either into 8.0 or 8.1.

@jknockaert

Copy link
Copy Markdown
Contributor Author

@PVince81: The backporting suggestion was only meant as an advise to those who want to use/test the PR on 7.0.4.

@DeepDiver1975 DeepDiver1975 added this to the 8.0-current milestone Dec 22, 2014
@schiessle

Copy link
Copy Markdown
Contributor

@jknockaert would it possible to add some unit test for the fseek behaviour? Thanks!

@MorrisJobke

Copy link
Copy Markdown
Contributor

@schiesbn You should maybe help a bit. What to test, where to test, how to test ... add maybe an example. Otherwise it will be hard to do ;)

@schiessle

Copy link
Copy Markdown
Contributor

@jknockaert just let me know if you need any help. Think it would be good to have a unit test which does some seek operation on a encrypted file and check that it end up at the correct position. Also it would be good to perform this test with a encrypted file which contain a header and with a encrypted file which doesn't have a header.

@jknockaert

Copy link
Copy Markdown
Contributor Author

@schiesbn I never before wrote a unit test (but it's definitely something I'd like to learn). I will have a look at it. I agree on the suggestion to test it with and without header (which is something I did already). And on a file shorter than a block and one longer than a block (and maybe also one where the the last block is full). And an empty file (writing and reading), as empty files should be headerless. We should probably also test it for a seekable as well as a nonseekable encrypted file stream, but less sure how to do that.
Running the unit tests is something different entirely; I've tried that before but gave up when I learned that a fair amount of supporting software is needed to run the tests.
I suggest to leave out any http-range-request tests here, that should be done when #10422 is reversed.

@schiessle

Copy link
Copy Markdown
Contributor

@jknockaert did you saw: http://doc.owncloud.org/server/6.0/developer_manual/core/unit-testing.html Installing phpunit should be quite easy on most distributions. If phpunit is installed you can run all tests with:

./autotest.sh <database>

or a specific test with for example:

./autotest.sh <database> ../apps/files_encryption/tests/stream.php

The easiest way is to use "sqlite" as database but the autotest.sh also contains a description how to prepare a mysql or postgresql database for the unit tests. "/apps/files_encryption/tests/stream.php" is also the place where you could place your unit test, you can probably reuse many stuff from the existing tests and just copy&pasting them.

Regarding #10422, this needs to be reverted to make your PR work, right? Don't you want to revert it as part of your PR, so that everything which belongs together is in one PR?

@jknockaert

Copy link
Copy Markdown
Contributor Author

@schiesb Technically #10422 is unrelated. My PR will work without reverting #10422 (but https-range-requests won't be supported). My motivation to keep it apart is to reduce complexity. But it is definitely true that the only motivation for #10422 is gone with my PR.

@schiessle

Copy link
Copy Markdown
Contributor

@jknockaert thanks for clarification

@jknockaert

Copy link
Copy Markdown
Contributor Author

@schiesbn OK, I've setup this unit testing, and the stream.php tests work fine. I've also written a first new test for writing a string to a file, reopening it, seeking and reading (with the reading for a size<6126 bytes and across two encryption blocks). Works fine. Before writing more complicated tests, I've two questions. 1. Where do I post new tests? As a commit attached to this PR? 2. In apps/files_encryption/tests/stream.php: what mechanism does actually enable the encryption stream wrapper in this context? Just the namespace declaration at the top of the file?

@DeepDiver1975 DeepDiver1975 modified the milestones: 8.1-next, 8.0-current Jan 7, 2015
@jknockaert

Copy link
Copy Markdown
Contributor Author

So we missed 8.0; that's OK. But could we now pls move forward with this PR? I've been (re)writing and using the code since 6.0.2. In my opinion the legacy implementation of the encryption stream wrapper is a hack, this PR fixes a number of issues. It properly implements the stream metrics (in the legacy code the metrics of the encrypted stream are used in reading/searching/writing on the unencrypted stream), it fixes the dependency on php bug 21641 (which is actually not a bug, and which no longer holds), it implements proper stream searching (which allows for more efficient webdav using http-range requests after reversing #10422 and probably fixes other bugs caused by the bogus search implementation of the legacy wrapper), it correctly sets the encrypted file size in the cache (which was probably not fixed in #11736). We went quite far in rewriting related code in #11659 to avoid recursion (and improve stability) and we debugged (remotely) related issues in #11736.
While my code is probably not perfect, I still think that it is an improvement over the existing wrapper code, and I think that it's stable, even when it would still be a good idea to do some more testing (anyone?). A unit test was written and the PR successfully passes the existing and the new stream wrapper tests, but we may want additional unit tests written which I am willing to do with some assistance (see comment above).

@jknockaert

Copy link
Copy Markdown
Contributor Author

Forgot to mention here that stream_write (in combination with flush()) as implemented in this PR has provision for writing chunks to existing encrypted streams (at any arbitrary position in that stream). That can potentially allow for future optimisations, like the one suggested by #12006.

@schiessle

Copy link
Copy Markdown
Contributor

@jknockaert Thanks a lot for your work. We will release oc8 at the end of the month and directly afterwards we will review and merge your pr.

@schiessle

Copy link
Copy Markdown
Contributor

ownCloud8 is out so let's review and merge this PR as soon as possible.

@th3fallen can you also have a look at it and help reviewing it? Thanks!

@schiessle

Copy link
Copy Markdown
Contributor

@jknockaert can you rebase the branch? Thanks!

Comment thread apps/files_encryption/lib/stream.php Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

6126 is used quite often, I think it would be good to introduce a constant for it. This makes it easier to find out where this number come from if you read the code again in a few years.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not sure a constant is the best way, I would say a property would be better. (imho)

@jknockaert

Copy link
Copy Markdown
Contributor Author

I will update this PR asap. As for the 6126, I've suggested before that it should be defined in the same place where Crypt::BLOCKSIZE is defined. I would prefer to do that after this PR is in (and in a a separate PR). We've already been reviewing far too much other code in the history of this PR, resulting in it being postponed over again.

@jknockaert

Copy link
Copy Markdown
Contributor Author

I finished the upgrade of my production server to 8.0. I will first port my code to that server and test it. After that I will rebase my git development version and update this PR. After the PR is reviewed and merged I will open a separate PR for the 6126 discussion.

@schiessle

Copy link
Copy Markdown
Contributor

@jknockaert This sounds great! At the same time we (mainly @th3fallen and I) working on a improved encryption which is more modular than what we have today. That's why I would love to have your PR merged soon so that we can also integrated it in the re-design. We can then probably also address the "6126-issue" directly in the branch where we do the re-design.

If this was merged and if you are interested I would like to invite you to also have a look at our re-design. Maybe you even want to join the development? The branch is here https://github.com/owncloud/core/tree/enc_ng

@jknockaert

Copy link
Copy Markdown
Contributor Author

@schiesbn I will first focus on finishing this PR. I plan to work on it this Friday, so expect a rebased PR for review early next week.

After that I will have a look at the broader redesign and check if I can contribute.

Bernhard Posselt and others added 8 commits February 18, 2015 15:34
This prevents a misusage of \OC\Files\View by calling it with user-supplied input. In such cases an exception is now thrown.
add documentation issue trackers and use https everywhere
Dont update the cache when working with part files
…ile-view

Disallow path traversals in file view
@jknockaert

Copy link
Copy Markdown
Contributor Author

OK, ported this PR to OC8 and it works. Next will be rebasing my development version. Later today or tomorrow.

MorrisJobke and others added 6 commits February 19, 2015 13:11
If no link text has been set for the navigation show the link nonetheless
Conflicts:
	apps/files_encryption/lib/stream.php
@jknockaert

Copy link
Copy Markdown
Contributor Author

Ok, this rebase is a complete failure.

@jknockaert

Copy link
Copy Markdown
Contributor Author

rebased in #14383

@jknockaert jknockaert closed this Feb 19, 2015
@scrutinizer-notifier

Copy link
Copy Markdown

The inspection completed: No issues found

@lock lock Bot locked as resolved and limited conversation to collaborators Aug 14, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.