Renaming encrypted file with Sharing enabled fails - share key not renamed
Steps to reproduce
- Enable encryption
- Enable sharing
- Upload document (.docx in my tests)
- Click rename on the document
- Type a new name and hit enter
- View error popup

- Note that this is not a shared document
- Refresh page
- Note the name actually changed
- Attempt to download document by clicking it

- See failure to decrypt
Expected behaviour
Files should be able to be renamed and then opened.
Actual behaviour
Files are successfully renamed but cannot be opened because the sharekey file for the cooresponding document is not renamed.
Server configuration
Operating system: Windows Server 2008R2 Web server: IIS 7.5 Database: MySql PHP version: 5.5.12 ownCloud version: (see ownCloud admin page) ownCloud 6.0.3 (stable) Updated from an older ownCloud or fresh install: Fresh install List of activated apps: Activity Deleted Files Documents Encryption First Run Wizard Full Text Search PDF Viewer Share Files Versions Video Viewer The content of config/config.php:
'removed', 'passwordsalt' => 'removed', 'trusted_domains' => array ( 0 => 'localhost', 1 => 'myDomainIp', 2 => 'myDomain', ), 'datadirectory' => 'E:\datadirector', 'logdateformat' => 'F d, Y H:i:s', 'log_authfailip' => true, 'logtimezone' => 'America/New_York', 'dbtype' => 'mysql', 'version' => '6.0.3.1', 'dbname' => 'ownclouddbname', 'dbhost' => 'localhost', 'dbtableprefix' => 'oc_', 'dbuser' => 'aUser', 'dbpassword' => 'removed', 'installed' => true, 'mail_smtpdebug' => false, 'mail_domain' => 'mail_domain', 'mail_smtpmode' => 'smtp', 'mail_smtphost' => 'ip', 'mail_smtpport' => port, 'mail_smtptimeout' => 50, 'mail_smtpsecure' => 'tls', 'mail_smtpauth' => true, 'mail_smtpauthtype' => 'LOGIN', 'mail_smtpname' => 'no-reply', 'knowledgebaseenabled' => true, '‘enable_avatars’' => true, 'maintenance' => false, 'mail_smtppassword' => 'aPassword', 'forcessl' => true, ); **Are you using external storage, if yes which one:** local only **Are you using encryption:** Yes ### Client configuration Chrome Win 7 ### Logs #### Web server error log Nothing of interest #### ownCloud log (data/owncloud.log) Nothing of interest #### Browser log NA
It's worth noting that if I manually rename the corresponding sharekey file in {data-dir}{user}\files-encryption\share-keys\ I can download the file again. I'm going review my logs again as nothing was being written out previously when I tested.
@schiesbn
I've reproduced again and there is nothing written to the OwnCloud or PHP log with this event.
This seems to be a Windows/IIS bug only. I can't reproduce it on a GNU/Linux server. I don't have such a setup to test it, but I have a idea what could be the problem and will try to prepare a fix
Ok, I will test it out when you have it.
@JasonEades can you try this patch https://github.com/owncloud/core/pull/8988/files? At the beginning I had a different fix in mind, but maybe this will already fixes your issue too. You only need to apply the changes for apps/files_encryption/hooks/hooks.php
Ok, I've tested this patch and I still have the same result. The primary keyfile here: {data-dir}{user}\files-encryption\keyfiles\ is successfully renamed as well as the document. The share keyfile here: {data-dir}{user}\files-encryption\share-keys\ still has the old name after attempting to rename. In this scenario we end up in a state with a file that cannot be downloaded unless it is renamed back.
I will add some logging to the postRename function in files_encryption/hooks/hooks.php and see what I can come up with.
I will add some logging to the postRename function in files_encryption/hooks/hooks.php and see what I can come up with.
This would be great! Looking forward to your log
Ok, inside postRename when we glob() the escaped path for *.shareKeys, it is not finding any matches so subsequently, no sharekeys are renamed.
It seems to boil down to this: glob() is called with this param which is an escapeGlobPattern() path: C:\Applications\WebApplications\ownCloudData/jeades/files-encryption/share-keys/100supervisorsinvestigationreporttest.pdf*.shareKey
There does exist a sharekey file here: C:\Applications\WebApplications\ownCloudData\jeades\files-encryption\share-keys\100supervisorsinvestigationreporttest.pdf.jeades.sharekey
But it is not found. Unfortunately, I don't know much about the glob patterns but it's either that or the escaping that is causing this file not to be found. Hopefully this helps?
Log
{"app":"Encryption library logging","message":"checking isset. $ownerOld = jeades","level":2,"time":"June 12, 2014 14:04:46"}
{"app":"Encryption library logging","message":"checking isset. $pathOld = \/100SupervisorsInvestigationReportTest.pdf","level":2,"time":"June 12, 2014 14:04:46"}
{"app":"Encryption library logging","message":"isSystemWideMountPoint == false for $pathOld. oldKeyfilePath = jeades\/files_encryption\/keyfiles\/\/100SupervisorsInvestigationReportTest.pdf","level":2,"time":"June 12, 2014 14:04:46"}
{"app":"Encryption library logging","message":"isSystemWideMountPoint == false for $pathOld. oldShareKeyPath = jeades\/files_encryption\/share-keys\/\/100SupervisorsInvestigationReportTest.pdf","level":2,"time":"June 12, 2014 14:04:46"}
{"app":"Encryption library logging","message":"isSystemWideMountPoint == false for $pathNew. newKeyfilePath = jeades\/files_encryption\/keyfiles\/\/100SupervisorsInvestigationReportTestAgain.pdf","level":2,"time":"June 12, 2014 14:04:46"}
{"app":"Encryption library logging","message":"isSystemWideMountPoint == false for $pathNew. newShareKeyPath = jeades\/files_encryption\/share-keys\/\/100SupervisorsInvestigationReportTestAgain.pdf","level":2,"time":"June 12, 2014 14:04:46"}
{"app":"Encryption library logging","message":"oldKeyfilePath is not a dir adding .key to path","level":2,"time":"June 12, 2014 14:04:46"}
{"app":"Encryption library logging","message":"localKeyPath = C:\\Applications\\WebApplications\\ownCloudData\/jeades\/files-encryption\/share-keys\/100supervisorsinvestigationreporttest.pdf","level":2,"time":"June 12, 2014 14:04:46"}
{"app":"Encryption library logging","message":"escaped localKeyPath (escapedPath) = C:\\Applications\\WebApplications\\ownCloudData\/jeades\/files-encryption\/share-keys\/100supervisorsinvestigationreporttest.pdf","level":2,"time":"June 12, 2014 14:04:46"}
{"app":"Encryption library logging","message":"calling glob on $escapedPath + *.shareKey = C:\\Applications\\WebApplications\\ownCloudData\/jeades\/files-encryption\/share-keys\/100supervisorsinvestigationreporttest.pdf*.shareKey","level":2,"time":"June 12, 2014 14:04:46"}
{"app":"Encryption library logging","message":"number of glob matches = 0","level":2,"time":"June 12, 2014 14:04:46"}
Ok, I've got past that problem. Apparently glob is case sensitive? The search pattern was using .*shareKey with a capital "K" in key https://github.com/owncloud/core/blob/master/apps/files_encryption/hooks/hooks.php#L476. I modified this to use lowercase and it appears to find the file now. Unfortunately it is blowing up on the next block now:
{"app":"Encryption library logging","message":"calling glob on $escapedPath + *.sharekey = C:\\Applications\\WebApplications\\ownCloudData\/jeades\/files-encryption\/share-keys\/100supervisorsinvestigationreporttest.pdf*.sharekey","level":2,"time":"June 12, 2014 14:54:34"}
{"app":"Encryption library logging","message":"number of glob matches = 1","level":2,"time":"June 12, 2014 14:54:34"}
{"app":"Encryption library logging","message":"sharekey match src C:\\Applications\\WebApplications\\ownCloudData\/jeades\/files-encryption\/share-keys\/100supervisorsinvestigationreporttest.pdf.jeades.sharekey","level":2,"time":"June 12, 2014 14:54:34"}
{"app":"Encryption library logging","message":"dst after normalizePath\/C:\/Applications\/WebApplications\/ownCloudData\/jeades\/files-encryption\/share-keys\/100supervisorsinvestigationreporttest.pdf.jeades.sharekey","level":2,"time":"June 12, 2014 14:54:34"}
{"app":"PHP","message":"mkdir(): Invalid argument at C:\\inetpub\\wwwroot\\OwnCloud\\apps\\files_encryption\\hooks\\hooks.php#605","level":3,"time":"June 12, 2014 14:54:34"}
{"app":"Encryption library logging","message":"about to call rename on $src= C:\\Applications\\WebApplications\\ownCloudData\/jeades\/files-encryption\/share-keys\/100supervisorsinvestigationreporttest.pdf.jeades.sharekey$dst = \/C:\/Applications\/WebApplications\/ownCloudData\/jeades\/files-encryption\/share-keys\/100supervisorsinvestigationreporttest.pdf.jeades.sharekey","level":2,"time":"June 12, 2014 14:54:34"}
{"app":"PHP","message":"rename(C:\\Applications\\WebApplications\\ownCloudData\/jeades\/files-encryption\/share-keys\/100supervisorsinvestigationreporttest.pdf.jeades.sharekey,\/C:\/Applications\/WebApplications\/ownCloudData\/jeades\/files-encryption\/share-keys\/100supervisorsinvestigationreporttest.pdf.jeades.sharekey): The filename, directory name, or volume label syntax is incorrect. (code: 123) at C:\\inetpub\\wwwroot\\OwnCloud\\apps\\files_encryption\\hooks\\hooks.php#610","level":3,"time":"June 12, 2014 14:54:34"}
{"app":"rename","message":"insider view.php rename. $path1 = jeades\/files_encryption\/keyfiles\/\/100SupervisorsInvestigationReportTest.pdf.key $path2 = jeades\/files_encryption\/keyfiles\/\/100SupervisorsInvestigationReportTestTwo.pdf.key","level":2,"time":"June 12, 2014 14:54:34"}
I'll take a look at that next.
@schiesbn Yeah, the other problem was related to normalizePath adding a forward slash before the drive letter which makes it think the path doesn't exist. See this line in the log above, notice the forward slash before the drive letter:
{"app":"Encryption library logging","message":"dst after normalizePath\/C:\/Applications\/WebApplications\/ownCloudData\/jeades\/files-encryption\/share-keys\/100supervisorsinvestigationreporttest.pdf.jeades.sharekey","level":2,"time":"June 12, 2014 14:54:34"}
I've added an embarrassingly dirty hack to get around this and it started working but on the second rename it was blowing up again. This time it was related to case sensitivity of the keyfile names. By default, on initial creation of all of my file encryption keys, they are all lowercase? During rename, it began naming them using the actual case of the file name and again glob couldn't find them with mixed case. To remedy this, I simply strtolowered the destination on share rename.
Someone familiar with this code base and PHP in general may be able to put something better together.
The problem seems to be that on Windows "shareKey" gets renamed to "sharekey".
@DeepDiver1975 Do you remember why we changes all paths to lower case (https://github.com/owncloud/core/blob/master/lib/private/files/mapper.php#L235) on Windows servers. Wouldn't it make sense to be case preserving at this point? Would it be possible to change it?
Do you remember why we changes all paths to lower case (https://github.com/owncloud/core/blob/master/lib/private/files/mapper.php#L235) for Windows servers.
Because the file system is not case sensitive we have to ensure that 'shareKey' and 'sharekey' don't collide.
Wouldn't it make sense to be case preserving at this point?
I see no need from a user perspective because the file name on the file system has no relevance.
Would it be possible to change it?
I see no reason to do that as well - we most likely will deprecate Windows support ...
Because the file system is not case sensitive we have to ensure that 'shareKey' and 'sharekey' don't collide.
But it is case preserving so we could be case preserving too. Although I agree that it would make it harder to detect collisions on a higher level.
I see no need from a user perspective because the file name on the file system has no relevance.
From the use point of view it doesn't make a big difference. Beside that windows users might also expect that "MyFile" stays "MyFile" and doesn't get converted to "myfile" But from the developer point of view it becomes more problematic. Like in this case, where we first create ".shareKey" and then don't find it with glob() because it was changed to ".sharekey".
I see no reason to do that as well - we most likely will deprecate Windows support ...
yes, not sure if it worth the efforts if we want to deprecate Windows support anyway. I'm also not sure if it would create any side effects if we remove the strtolower().
But from the developer point of view it becomes more problematic. Like in this case, where we first create ".shareKey" and then don't find it with glob() because it was changed to ".sharekey".
DISCLAIMER: I had no look at the code :wink:
But why is it not found? all file system operations go through the View and the mapper in the storage implementation for windows filesystem still nows about ".shareKey"
But why is it not found? all file system operations go through the View and the mapper in the storage implementation for windows filesystem still nows about ".shareKey".
In this case we doesn't go through the view but use the php method glob() directly. glob() allows you to match all files in a directory with a given regular expression (in this case we search for <filename>.*.shareKey) This should be faster then reading the whole directory content and do string operations on every file.
I could migrate away from glob(), I did this already in other areas. But generally I would prefer to keep the solution with the better performance.
At the moment I would tend to either "wontfix" since windows server is not officially supported in the community edition and we might want to drop it for the enterprise edition too. Or fix it at the root and make our file system mappper (like the filesystem) case preserving.
In this case we doesn't go through the view but use the php method glob() directly. glob() allows you to match all files in a directory with a given regular expression (in this case we search for
.*.shareKey) This should be faster then reading the whole directory content and do string operations on every file.
Thinking about OC7 and the objectstore implementation this will fully fail @butonic
the share keys are outside of the files folder. They are located in the users data folder /data/userId/files_encryption/... Will this be stored at the objectstore too? Until now I thought that only everything in data/userId/files/ will go to the objectstore.
@karlitschek @craigpg I was told the objectstore implementation should not store any files on the machine oc is running on. Which is why I implemented it to store all files in the data and home folders in the object store. Encryption is not working with objectstore and in general will totally screw the performance if we put the keys in the objectstore as well because we have to make three instead of one trip to the objectstore. Gallery and search_lucene have a similar problem.
@butonic This is fine. But than we should add a warning to the object store config/files_encryption description. @schiesbn What do you think?