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

feat: Support move for encrypted files #2609

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

# 1.42.0

## ✨ Features

* Support moving files from/to encrypted folder

## 🐛 Bug Fixes

* Disable sharing on public file viewer
Expand Down
30 changes: 30 additions & 0 deletions jestHelpers/setup.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from 'react'
import Enzyme from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'

Expand All @@ -10,6 +11,35 @@ jest.mock('cozy-ui/transpiled/react/utils/color', () => ({
getCssVariableValue: () => '#fff'
}))

jest.mock('cozy-keys-lib', () => ({
withVaultUnlockContext: BaseComponent => {
const Wrapper = props => {
return <BaseComponent {...props} />
}
Wrapper.displayName = `withVaultUnlockContext(${
BaseComponent.displayName || BaseComponent.name
})`
return Wrapper
},
withVaultClient: BaseComponent => {
const Component = props => (
<>
{({ vaultClient }) => (
<BaseComponent vaultClient={vaultClient} {...props} />
)}
</>
)

Component.displayName = `withVaultClient(${
BaseComponent.displayName || BaseComponent.name
})`

return Component
},
useVaultUnlockContext: jest.fn().mockReturnValue(jest.fn()),
useVaultClient: jest.fn()
}))

global.cozy = {
bar: {
BarLeft: () => null,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
"cozy-scanner": "2.0.2",
"cozy-scripts": "6.3.1",
"cozy-sharing": "4.1.5",
"cozy-stack-client": "^29.0.0",
"cozy-stack-client": "^29.1.0",
"cozy-ui": "^67.0.2",
"date-fns": "1.30.1",
"diacritics": "1.3.0",
Expand Down
102 changes: 91 additions & 11 deletions src/drive/lib/encryption.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ export const createEncryptedDir = async (
export const encryptAndUploadNewFile = async (
client,
vaultClient,
{ file, encryptionKey, fileOptions }
{ binary, encryptionKey, fileOptions }
) => {
const { name, dirID, onUploadProgress } = fileOptions
const encryptedFile = await vaultClient.encryptFile(file, encryptionKey)
const encryptedFile = await vaultClient.encryptFile(binary, encryptionKey)
const resp = await client
.collection(DOCTYPE_FILES)
.createFile(encryptedFile, {
Expand All @@ -75,40 +75,120 @@ export const encryptAndUploadNewFile = async (
return resp.data
}

export const encryptAndUploadExistingFile = async (
client,
vaultClient,
{ file, encryptionKey }
) => {
const encryptedFile = await encryptFile(client, vaultClient, {
file,
encryptionKey
})

const attributes = {
...file,
encrypted: true,
fileId: file._id,
data: encryptedFile
}
const resp = await client.save(attributes)
return resp.data
}

export const decryptAndUploadExistingFile = async (
client,
vaultClient,
{ file, decryptionKey }
) => {
const plaintextFile = await decryptFile(client, vaultClient, {
file,
decryptionKey
})

const attributes = {
...file,
encrypted: false,
fileId: file._id,
data: plaintextFile
}
const resp = await client.save(attributes)
return resp.data
}

export const reencryptAndUploadExistingFile = async (
client,
vaultClient,
{ file, encryptionKey, decryptionKey }
) => {
const plaintextFile = await decryptFile(client, vaultClient, {
file,
decryptionKey
})
const reencryptedFile = await vaultClient.encryptFile(
plaintextFile,
encryptionKey
)
const attributes = {
...file,
encrypted: true,
fileId: file._id,
data: reencryptedFile
}
const resp = await client.save(attributes)
return resp.data
}

const getBinaryFile = async (client, fileId) => {
const resp = await client
.collection(DOCTYPE_FILES)
.fetchFileContentById(fileId)
return resp.arrayBuffer()
}

export const decryptFile = async (
const decryptFile = async (client, vaultClient, { file, decryptionKey }) => {
const cipher = await getBinaryFile(client, file._id)
return vaultClient.decryptFile(cipher, decryptionKey)
}

const encryptFile = async (client, vaultClient, { file, encryptionKey }) => {
const cipher = await getBinaryFile(client, file._id)
return vaultClient.encryptFile(cipher, encryptionKey)
}

export const decryptFileIntoBlob = async (
client,
vaultClient,
{ file, encryptionKey }
{ file, decryptionKey }
) => {
const cipher = await getBinaryFile(client, file._id)
const decryptedFile = await vaultClient.decryptFile(cipher, encryptionKey)
return new Blob([decryptedFile], { type: file.type })
const plaintextFile = decryptFile(client, vaultClient, {
file,
decryptionKey
})
return new Blob([plaintextFile], { type: file.type })
}

export const downloadEncryptedFile = async (
client,
vaultClient,
{ file, encryptionKey }
{ file, decryptionKey }
) => {
const url = await getDecryptedFileURL(client, vaultClient, {
file,
encryptionKey
decryptionKey
})
return client.collection(DOCTYPE_FILES).forceFileDownload(url, file.name)
}

export const getDecryptedFileURL = async (
client,
vaultClient,
{ file, encryptionKey }
{ file, decryptionKey }
) => {
const blob = await decryptFile(client, vaultClient, { file, encryptionKey })
const plaintextFile = await decryptFile(client, vaultClient, {
file,
decryptionKey
})
const blob = new Blob([plaintextFile], { type: file.type })

return URL.createObjectURL(blob)
}
9 changes: 6 additions & 3 deletions src/drive/mobile/modules/offline/duck.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Alerter from 'cozy-ui/transpiled/react/Alerter'

import {
getEncryptionKeyFromDirId,
decryptFile,
decryptFileToBlob,
isEncryptedFile
} from 'drive/lib/encryption'
import { openFileWith } from 'drive/web/modules/actions/utils'
Expand Down Expand Up @@ -73,8 +73,11 @@ export const saveOfflineFileCopy = async (file, client, { vaultClient }) => {
try {
let blob
if (isEncryptedFile(file)) {
const encryptionKey = await getEncryptionKeyFromDirId(client, file.dir_id)
blob = await decryptFile(client, vaultClient, { file, encryptionKey })
const decryptionKey = await getEncryptionKeyFromDirId(client, file.dir_id)
blob = await decryptFileToBlob(client, vaultClient, {
file,
decryptionKey
})
} else {
const response = await client
.collection(DOCTYPE_FILES)
Expand Down
3 changes: 1 addition & 2 deletions src/drive/mobile/modules/upload/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,8 @@ export class DumbUpload extends Component {
>
<div>
<FileList
folder={folder}
files={data}
targets={items}
entries={items}
navigateTo={this.navigateTo}
/>
<LoadMore hasMore={hasMore} fetchMore={fetchMore} />
Expand Down
3 changes: 2 additions & 1 deletion src/drive/mobile/modules/upload/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { shallow } from 'enzyme'
import { DumbUpload, generateForQueue } from './'

jest.mock('cozy-keys-lib', () => ({
withVaultClient: jest.fn().mockReturnValue({})
withVaultClient: jest.fn().mockReturnValue({}),
withVaultUnlockContext: jest.fn().mockReturnValue({})
}))

const tSpy = jest.fn()
Expand Down
4 changes: 4 additions & 0 deletions src/drive/web/modules/actions/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { download } from './index'
import { DOCTYPE_FILES_ENCRYPTION } from 'drive/lib/doctypes'

jest.mock('cozy-keys-lib', () => ({
withVaultUnlockContext: jest.fn().mockReturnValue({})
}))

describe('download', () => {
it('should not display when an encrypted folder is selected', () => {
const files = [
Expand Down
28 changes: 17 additions & 11 deletions src/drive/web/modules/actions/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
getEncryptionKeyFromDirId,
downloadEncryptedFile,
isEncryptedFolder,
decryptFile,
decryptFileToBlob,
isEncryptedFile
} from 'drive/lib/encryption'
import { DOCTYPE_FILES } from 'drive/lib/doctypes'
Expand Down Expand Up @@ -48,16 +48,16 @@ const openFileDownloadError = error => {
* @param {array} files One or more files to download
*/
export const downloadFiles = async (client, files, { vaultClient } = {}) => {
const encryptionKey = await getEncryptionKeyFromDirId(client, files[0].dir_id)
const decryptionKey = await getEncryptionKeyFromDirId(client, files[0].dir_id)

if (files.length === 1 && !isDirectory(files[0])) {
const file = files[0]
try {
const filename = file.name
if (encryptionKey) {
if (decryptionKey) {
return downloadEncryptedFile(client, vaultClient, {
file,
encryptionKey
decryptionKey
})
} else {
return client.collection(DOCTYPE_FILES).download(file, null, filename)
Expand All @@ -66,8 +66,8 @@ export const downloadFiles = async (client, files, { vaultClient } = {}) => {
Alerter.error(downloadFileError(error))
}
} else {
if (encryptionKey) {
// Multiple download is forbidden for encrypted files because we cannot generate client archive for now.
if (decryptionKey) {
// decryptionKey download is forbidden for encrypted files because we cannot generate client archive for now.
return Alerter.error('error.download_file.encryption_many')
}
const hasEncryptedDirs = files.find(
Expand Down Expand Up @@ -136,13 +136,16 @@ export const exportFilesNative = async (
files,
{ vaultClient } = {}
) => {
const encryptionKey = isEncryptedFile(files[0])
const decryptionKey = isEncryptedFile(files[0])
? await getEncryptionKeyFromDirId(client, files[0].dir_id)
: null
const downloadAllFiles = files.map(async file => {
let blob
if (encryptionKey) {
blob = await decryptFile(client, vaultClient, { file, encryptionKey })
if (decryptionKey) {
blob = await decryptFileToBlob(client, vaultClient, {
file,
decryptionKey
})
} else {
const response = await client
.collection(DOCTYPE_FILES)
Expand Down Expand Up @@ -206,11 +209,14 @@ export const openFileWith = async (client, file, { vaultClient } = {}) => {
let originalMime = file.mime
try {
if (isEncryptedFile(file)) {
const encryptionKey = await getEncryptionKeyFromDirId(
const decryptionKey = await getEncryptionKeyFromDirId(
client,
file.dir_id
)
blob = await decryptFile(client, vaultClient, { file, encryptionKey })
blob = await decryptFileToBlob(client, vaultClient, {
file,
decryptionKey
})
originalMime = client
.collection(DOCTYPE_FILES)
.getFileTypeFromName(file.name)
Expand Down
16 changes: 8 additions & 8 deletions src/drive/web/modules/actions/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import {
getEncryptionKeyFromDirId,
downloadEncryptedFile,
decryptFile
decryptFileToBlob
} from 'drive/lib/encryption'
import { DOCTYPE_FILES_ENCRYPTION } from 'drive/lib/doctypes'
import { TRASH_DIR_ID } from 'drive/constants/config'
Expand Down Expand Up @@ -50,7 +50,7 @@ jest.mock('drive/lib/encryption', () => ({
...jest.requireActual('drive/lib/encryption'),
getEncryptionKeyFromDirId: jest.fn(),
downloadEncryptedFile: jest.fn(),
decryptFile: jest.fn()
decryptFileToBlob: jest.fn()
}))

describe('trashFiles', () => {
Expand Down Expand Up @@ -140,7 +140,7 @@ describe('downloadFiles', () => {
expect(downloadEncryptedFile).toHaveBeenCalledWith(
mockClient,
{},
{ file, encryptionKey: 'encryption-key' }
{ file, decryptionKey: 'encryption-key' }
)
})

Expand Down Expand Up @@ -254,14 +254,14 @@ describe('openFileWith', () => {

it('open an encrypted file', async () => {
getEncryptionKeyFromDirId.mockResolvedValueOnce('encryption-key')
decryptFile.mockResolvedValueOnce('fake file blob')
decryptFileToBlob.mockResolvedValueOnce('fake file blob')
await openFileWith(mockClient, encryptedFile, { vaultClient })
expect(decryptFile).toHaveBeenCalledWith(
expect(decryptFileToBlob).toHaveBeenCalledWith(
mockClient,
{},
{
file: encryptedFile,
encryptionKey: 'encryption-key'
decryptionKey: 'encryption-key'
}
)
expect(saveAndOpenWithCordova).toHaveBeenCalledWith('fake file blob', {
Expand Down Expand Up @@ -344,12 +344,12 @@ describe('exportFilesNative', () => {
await exportFilesNative(mockClient, encryptedFiles, { vaultClient })

encryptedFiles.forEach(file =>
expect(decryptFile).toHaveBeenCalledWith(
expect(decryptFileToBlob).toHaveBeenCalledWith(
mockClient,
{},
{
file,
encryptionKey: 'encryption-key'
decryptionKey: 'encryption-key'
}
)
)
Expand Down
Loading