2021-07-19 11:27:43 +02:00
|
|
|
import Note from '../../models/Note';
|
2023-12-03 12:35:46 +02:00
|
|
|
import { createFolderTree, encryptionService, loadEncryptionMasterKey, msleep, resourceService, setupDatabaseAndSynchronizer, simulateReadOnlyShareEnv, supportDir, switchClient, synchronizerStart } from '../../testing/test-utils';
|
2021-07-19 11:27:43 +02:00
|
|
|
import ShareService from './ShareService';
|
2023-12-03 12:35:46 +02:00
|
|
|
import { NoteEntity, ResourceEntity } from '../database/types';
|
2021-11-03 18:24:40 +02:00
|
|
|
import Folder from '../../models/Folder';
|
|
|
|
import { setEncryptionEnabled, setPpk } from '../synchronizer/syncInfoUtils';
|
|
|
|
import { generateKeyPair } from '../e2ee/ppk';
|
|
|
|
import MasterKey from '../../models/MasterKey';
|
|
|
|
import { MasterKeyEntity } from '../e2ee/types';
|
2022-02-09 20:04:27 +02:00
|
|
|
import { loadMasterKeysFromSettings, setupAndEnableEncryption, updateMasterPassword } from '../e2ee/utils';
|
2023-07-27 17:05:56 +02:00
|
|
|
import Logger, { LogLevel } from '@joplin/utils/Logger';
|
2022-02-09 20:04:27 +02:00
|
|
|
import shim from '../../shim';
|
|
|
|
import Resource from '../../models/Resource';
|
|
|
|
import { readFile } from 'fs-extra';
|
|
|
|
import BaseItem from '../../models/BaseItem';
|
2023-07-23 16:57:55 +02:00
|
|
|
import ResourceService from '../ResourceService';
|
|
|
|
import Setting from '../../models/Setting';
|
|
|
|
import { ModelType } from '../../BaseModel';
|
2023-12-03 12:35:46 +02:00
|
|
|
import { remoteNotesFoldersResources } from '../../testing/test-utils-synchronizer';
|
2024-04-15 19:17:34 +02:00
|
|
|
import mockShareService from '../../testing/share/mockShareService';
|
2022-02-09 20:04:27 +02:00
|
|
|
|
|
|
|
interface TestShareFolderServiceOptions {
|
|
|
|
master_key_id?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const testImagePath = `${supportDir}/photo.jpg`;
|
2021-12-20 16:47:50 +02:00
|
|
|
|
2021-07-19 11:27:43 +02:00
|
|
|
|
2023-12-03 12:35:46 +02:00
|
|
|
const mockServiceForNoteSharing = () => {
|
2024-04-15 19:17:34 +02:00
|
|
|
return mockShareService({
|
|
|
|
getShares: async () => {
|
|
|
|
return { items: [] };
|
2023-12-03 12:35:46 +02:00
|
|
|
},
|
2024-04-15 19:17:34 +02:00
|
|
|
postShares: async () => null,
|
|
|
|
getShareInvitations: async () => null,
|
2023-12-03 12:35:46 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-02-20 17:02:29 +02:00
|
|
|
describe('ShareService', () => {
|
2021-07-19 11:27:43 +02:00
|
|
|
|
2022-11-15 12:23:50 +02:00
|
|
|
beforeEach(async () => {
|
2021-07-19 11:27:43 +02:00
|
|
|
await setupDatabaseAndSynchronizer(1);
|
|
|
|
await switchClient(1);
|
|
|
|
});
|
|
|
|
|
2021-11-03 18:24:40 +02:00
|
|
|
it('should not change the note user timestamps when sharing or unsharing', async () => {
|
2021-07-19 11:27:43 +02:00
|
|
|
let note = await Note.save({});
|
2023-12-03 12:35:46 +02:00
|
|
|
const service = mockServiceForNoteSharing();
|
2021-07-19 11:27:43 +02:00
|
|
|
await msleep(1);
|
2022-04-03 20:19:24 +02:00
|
|
|
await service.shareNote(note.id, false);
|
2021-07-19 11:27:43 +02:00
|
|
|
|
|
|
|
function checkTimestamps(previousNote: NoteEntity, newNote: NoteEntity) {
|
|
|
|
// After sharing or unsharing, only the updated_time property should
|
|
|
|
// be updated, for sync purposes. All other timestamps shouldn't
|
|
|
|
// change.
|
|
|
|
expect(previousNote.user_created_time).toBe(newNote.user_created_time);
|
|
|
|
expect(previousNote.user_updated_time).toBe(newNote.user_updated_time);
|
|
|
|
expect(previousNote.updated_time < newNote.updated_time).toBe(true);
|
|
|
|
expect(previousNote.created_time).toBe(newNote.created_time);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const noteReloaded = await Note.load(note.id);
|
|
|
|
checkTimestamps(note, noteReloaded);
|
|
|
|
note = noteReloaded;
|
|
|
|
}
|
|
|
|
|
|
|
|
await msleep(1);
|
|
|
|
await service.unshareNote(note.id);
|
|
|
|
|
|
|
|
{
|
|
|
|
const noteReloaded = await Note.load(note.id);
|
|
|
|
checkTimestamps(note, noteReloaded);
|
|
|
|
}
|
2021-11-03 18:24:40 +02:00
|
|
|
});
|
|
|
|
|
2023-12-03 12:35:46 +02:00
|
|
|
it('should not encrypt items that are shared', async () => {
|
|
|
|
const folder = await Folder.save({});
|
|
|
|
const note = await Note.save({ parent_id: folder.id });
|
|
|
|
await shim.attachFileToNote(note, testImagePath);
|
|
|
|
|
|
|
|
const service = mockServiceForNoteSharing();
|
|
|
|
|
|
|
|
setEncryptionEnabled(true);
|
|
|
|
await loadEncryptionMasterKey();
|
|
|
|
|
|
|
|
await synchronizerStart();
|
|
|
|
|
|
|
|
let previousBlobUpdatedTime = Infinity;
|
|
|
|
{
|
|
|
|
const allItems = await remoteNotesFoldersResources();
|
|
|
|
expect(allItems.map(it => it.encryption_applied)).toEqual([1, 1, 1]);
|
|
|
|
previousBlobUpdatedTime = allItems.find(it => it.type_ === ModelType.Resource).blob_updated_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
await service.shareNote(note.id, false);
|
|
|
|
await msleep(1);
|
|
|
|
await Folder.updateAllShareIds(resourceService());
|
|
|
|
|
|
|
|
await synchronizerStart();
|
|
|
|
|
|
|
|
{
|
|
|
|
const allItems = await remoteNotesFoldersResources();
|
|
|
|
expect(allItems.find(it => it.type_ === ModelType.Note).encryption_applied).toBe(0);
|
|
|
|
expect(allItems.find(it => it.type_ === ModelType.Folder).encryption_applied).toBe(1);
|
|
|
|
|
|
|
|
const resource: ResourceEntity = allItems.find(it => it.type_ === ModelType.Resource);
|
|
|
|
expect(resource.encryption_applied).toBe(0);
|
|
|
|
|
|
|
|
// Indicates that both the metadata and blob have been decrypted on
|
|
|
|
// the sync target.
|
|
|
|
expect(resource.blob_updated_time).toBe(resource.updated_time);
|
|
|
|
expect(resource.blob_updated_time).toBeGreaterThan(previousBlobUpdatedTime);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2022-02-09 20:04:27 +02:00
|
|
|
function testShareFolderService(extraExecHandlers: Record<string, Function> = {}, options: TestShareFolderServiceOptions = {}) {
|
2024-04-15 19:17:34 +02:00
|
|
|
return mockShareService({
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2024-04-15 19:17:34 +02:00
|
|
|
onExec: async (method: string, path: string, query: Record<string, any>, body: any) => {
|
2021-11-03 18:24:40 +02:00
|
|
|
if (extraExecHandlers[`${method} ${path}`]) return extraExecHandlers[`${method} ${path}`](query, body);
|
|
|
|
|
2022-02-09 20:04:27 +02:00
|
|
|
if (method === 'GET' && path === 'api/shares') {
|
|
|
|
return {
|
|
|
|
items: [
|
|
|
|
{
|
|
|
|
id: 'share_1',
|
|
|
|
master_key_id: options.master_key_id,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-11-03 18:24:40 +02:00
|
|
|
if (method === 'POST' && path === 'api/shares') {
|
|
|
|
return {
|
|
|
|
id: 'share_1',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(`Unhandled: ${method} ${path}`);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-12-03 12:04:24 +02:00
|
|
|
const prepareNoteFolderResource = async () => {
|
2021-11-03 18:24:40 +02:00
|
|
|
const folder = await Folder.save({});
|
2022-02-09 20:04:27 +02:00
|
|
|
let note = await Note.save({ parent_id: folder.id });
|
|
|
|
note = await shim.attachFileToNote(note, testImagePath);
|
|
|
|
const resourceId = (await Note.linkedResourceIds(note.body))[0];
|
|
|
|
const resource = await Resource.load(resourceId);
|
|
|
|
|
|
|
|
await resourceService().indexNoteResources();
|
2021-11-03 18:24:40 +02:00
|
|
|
|
2023-12-03 12:04:24 +02:00
|
|
|
return { folder, note, resource };
|
|
|
|
};
|
|
|
|
|
|
|
|
async function testShareFolder(service: ShareService) {
|
|
|
|
const { folder, note, resource } = await prepareNoteFolderResource();
|
2021-11-03 18:24:40 +02:00
|
|
|
const share = await service.shareFolder(folder.id);
|
|
|
|
expect(share.id).toBe('share_1');
|
|
|
|
expect((await Folder.load(folder.id)).share_id).toBe('share_1');
|
|
|
|
expect((await Note.load(note.id)).share_id).toBe('share_1');
|
2022-02-09 20:04:27 +02:00
|
|
|
expect((await Resource.load(resource.id)).share_id).toBe('share_1');
|
2021-11-03 18:24:40 +02:00
|
|
|
|
2022-02-09 20:04:27 +02:00
|
|
|
return { share, folder, note, resource };
|
2021-11-03 18:24:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
it('should share a folder', async () => {
|
|
|
|
await testShareFolder(testShareFolderService());
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should share a folder - E2EE', async () => {
|
2022-02-09 20:04:27 +02:00
|
|
|
const masterKey = await loadEncryptionMasterKey();
|
|
|
|
await setupAndEnableEncryption(encryptionService(), masterKey, '111111');
|
2021-11-03 18:24:40 +02:00
|
|
|
const ppk = await generateKeyPair(encryptionService(), '111111');
|
|
|
|
setPpk(ppk);
|
|
|
|
|
2022-02-09 20:04:27 +02:00
|
|
|
let shareService = testShareFolderService();
|
2021-11-03 18:24:40 +02:00
|
|
|
|
2022-02-09 20:04:27 +02:00
|
|
|
expect(await MasterKey.count()).toBe(1);
|
2021-11-03 18:24:40 +02:00
|
|
|
|
2023-12-03 12:04:24 +02:00
|
|
|
let { folder, note, resource } = await prepareNoteFolderResource();
|
|
|
|
|
|
|
|
BaseItem.shareService_ = shareService;
|
|
|
|
Resource.shareService_ = shareService;
|
|
|
|
|
|
|
|
await shareService.shareFolder(folder.id);
|
|
|
|
|
2022-07-12 12:28:48 +02:00
|
|
|
await Folder.updateAllShareIds(resourceService());
|
2022-02-09 20:04:27 +02:00
|
|
|
|
|
|
|
// The share service should automatically create a new encryption key
|
|
|
|
// specifically for that shared folder
|
|
|
|
expect(await MasterKey.count()).toBe(2);
|
|
|
|
|
|
|
|
folder = await Folder.load(folder.id);
|
|
|
|
note = await Note.load(note.id);
|
|
|
|
resource = await Resource.load(resource.id);
|
|
|
|
|
|
|
|
// The key that is not the master key is the folder key
|
|
|
|
const folderKey = (await MasterKey.all()).find(mk => mk.id !== masterKey.id);
|
|
|
|
|
|
|
|
// Double-check that it's going to encrypt the folder using the shared
|
|
|
|
// key (and not the user's own master key)
|
|
|
|
expect(folderKey.id).not.toBe(masterKey.id);
|
|
|
|
expect(folder.master_key_id).toBe(folderKey.id);
|
|
|
|
|
|
|
|
await loadMasterKeysFromSettings(encryptionService());
|
|
|
|
|
|
|
|
// Reload the service so that the mocked calls use the newly created key
|
|
|
|
shareService = testShareFolderService({}, { master_key_id: folderKey.id });
|
|
|
|
|
|
|
|
BaseItem.shareService_ = shareService;
|
|
|
|
Resource.shareService_ = shareService;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const serializedNote = await Note.serializeForSync(note);
|
|
|
|
expect(serializedNote).toContain(folderKey.id);
|
|
|
|
|
|
|
|
// The resource should be encrypted using the above key (if it is,
|
|
|
|
// the key ID will be in the header).
|
|
|
|
const result = await Resource.fullPathForSyncUpload(resource);
|
|
|
|
const content = await readFile(result.path, 'utf8');
|
|
|
|
expect(content).toContain(folderKey.id);
|
2023-12-03 12:04:24 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
await synchronizerStart();
|
|
|
|
const remoteItems = await remoteNotesFoldersResources();
|
|
|
|
expect(remoteItems.map(it => it.encryption_applied)).toEqual([1, 1, 1]);
|
|
|
|
}
|
2022-02-09 20:04:27 +02:00
|
|
|
} finally {
|
|
|
|
BaseItem.shareService_ = shareService;
|
|
|
|
Resource.shareService_ = null;
|
|
|
|
}
|
2021-11-03 18:24:40 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should add a recipient', async () => {
|
|
|
|
setEncryptionEnabled(true);
|
|
|
|
await updateMasterPassword('', '111111');
|
|
|
|
const ppk = await generateKeyPair(encryptionService(), '111111');
|
|
|
|
setPpk(ppk);
|
|
|
|
const recipientPpk = await generateKeyPair(encryptionService(), '222222');
|
|
|
|
expect(ppk.id).not.toBe(recipientPpk.id);
|
|
|
|
|
2023-02-05 13:32:28 +02:00
|
|
|
let uploadedEmail = '';
|
2021-11-03 18:24:40 +02:00
|
|
|
let uploadedMasterKey: MasterKeyEntity = null;
|
|
|
|
|
|
|
|
const service = testShareFolderService({
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2021-11-03 18:24:40 +02:00
|
|
|
'POST api/shares': (_query: Record<string, any>, body: any) => {
|
|
|
|
return {
|
|
|
|
id: 'share_1',
|
|
|
|
master_key_id: body.master_key_id,
|
|
|
|
};
|
|
|
|
},
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2021-11-03 18:24:40 +02:00
|
|
|
'GET api/users/toto%40example.com/public_key': async (_query: Record<string, any>, _body: any) => {
|
|
|
|
return recipientPpk;
|
|
|
|
},
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2021-11-03 18:24:40 +02:00
|
|
|
'POST api/shares/share_1/users': async (_query: Record<string, any>, body: any) => {
|
|
|
|
uploadedEmail = body.email;
|
|
|
|
uploadedMasterKey = JSON.parse(body.master_key);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-02-09 20:04:27 +02:00
|
|
|
const { share } = await testShareFolder(service);
|
2021-11-03 18:24:40 +02:00
|
|
|
|
2023-07-16 18:42:42 +02:00
|
|
|
await service.addShareRecipient(share.id, share.master_key_id, 'toto@example.com', { can_read: 1, can_write: 1 });
|
2021-11-03 18:24:40 +02:00
|
|
|
|
|
|
|
expect(uploadedEmail).toBe('toto@example.com');
|
|
|
|
|
|
|
|
const content = JSON.parse(uploadedMasterKey.content);
|
|
|
|
expect(content.ppkId).toBe(recipientPpk.id);
|
|
|
|
});
|
2021-07-19 11:27:43 +02:00
|
|
|
|
2021-12-20 16:47:50 +02:00
|
|
|
it('should leave folders that are no longer with the user', async () => {
|
|
|
|
// `checkShareConsistency` will emit a warning so we need to silent it
|
|
|
|
// in tests.
|
|
|
|
const previousLogLevel = Logger.globalLogger.setLevel(LogLevel.Error);
|
|
|
|
|
|
|
|
const service = testShareFolderService({
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2021-12-20 16:47:50 +02:00
|
|
|
'GET api/shares': async (_query: Record<string, any>, _body: any): Promise<any> => {
|
|
|
|
return {
|
|
|
|
items: [],
|
|
|
|
has_more: false,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const folder = await Folder.save({ share_id: 'nolongershared' });
|
|
|
|
await service.checkShareConsistency();
|
|
|
|
expect(await Folder.load(folder.id)).toBeFalsy();
|
|
|
|
|
|
|
|
Logger.globalLogger.setLevel(previousLogLevel);
|
|
|
|
});
|
|
|
|
|
2023-07-23 16:57:55 +02:00
|
|
|
it('should leave a shared folder', async () => {
|
|
|
|
const folder1 = await createFolderTree('', [
|
|
|
|
{
|
|
|
|
title: 'folder 1',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
title: 'note 1',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'note 2',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
|
|
|
|
const resourceService = new ResourceService();
|
|
|
|
await Folder.save({ id: folder1.id, share_id: '123456789' });
|
|
|
|
await Folder.updateAllShareIds(resourceService);
|
|
|
|
|
|
|
|
const cleanup = simulateReadOnlyShareEnv('123456789');
|
|
|
|
|
|
|
|
const shareService = testShareFolderService();
|
|
|
|
await shareService.leaveSharedFolder(folder1.id, 'somethingrandom');
|
|
|
|
|
|
|
|
expect(await Folder.count()).toBe(0);
|
|
|
|
expect(await Note.count()).toBe(0);
|
|
|
|
|
|
|
|
const deletedItems = await BaseItem.deletedItems(Setting.value('sync.target'));
|
|
|
|
|
|
|
|
expect(deletedItems.length).toBe(1);
|
|
|
|
expect(deletedItems[0].item_type).toBe(ModelType.Folder);
|
|
|
|
expect(deletedItems[0].item_id).toBe(folder1.id);
|
|
|
|
|
|
|
|
cleanup();
|
|
|
|
});
|
2021-12-20 16:47:50 +02:00
|
|
|
|
2021-07-19 11:27:43 +02:00
|
|
|
});
|