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

make workspaceKeyBox mandatory #741

Open
wants to merge 2 commits into
base: main
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
2 changes: 1 addition & 1 deletion apps/app/components/page/Page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export default function Page({
snapshot: {
keyDerivationTrace: snapshotKeyData.keyDerivationTrace,
},
workspaceKeyBox: workspace.currentWorkspaceKey.workspaceKeyBox!,
workspaceKeyBox: workspace.currentWorkspaceKey.workspaceKeyBox,
});

return {
Expand Down
12 changes: 6 additions & 6 deletions apps/app/components/sidebarFolder/SidebarFolder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export default function SidebarFolder(props: Props) {
try {
const parentKeyChainData = deriveKeysFromKeyDerivationTrace({
keyDerivationTrace: props.keyDerivationTrace,
workspaceKeyBox: folderWorkspaceKey.workspaceKeyBox!,
workspaceKeyBox: folderWorkspaceKey.workspaceKeyBox,
activeDevice: {
signingPublicKey: activeDevice.signingPublicKey,
signingPrivateKey: activeDevice.signingPrivateKey!,
Expand Down Expand Up @@ -201,7 +201,7 @@ export default function SidebarFolder(props: Props) {
encryptionPublicKeySignature:
activeDevice.encryptionPublicKeySignature!,
},
workspaceKeyBox: workspace.currentWorkspaceKey.workspaceKeyBox!,
workspaceKeyBox: workspace.currentWorkspaceKey.workspaceKeyBox,
});
const parentChainItem =
parentFolderKeyChainData.trace[parentFolderKeyChainData.trace.length - 1];
Expand Down Expand Up @@ -270,7 +270,7 @@ export default function SidebarFolder(props: Props) {
}
const folderKeyTrace = deriveKeysFromKeyDerivationTrace({
keyDerivationTrace: props.keyDerivationTrace,
workspaceKeyBox: workspace.currentWorkspaceKey.workspaceKeyBox!,
workspaceKeyBox: workspace.currentWorkspaceKey.workspaceKeyBox,
activeDevice: {
signingPublicKey: activeDevice.signingPublicKey,
signingPrivateKey: activeDevice.signingPrivateKey!,
Expand Down Expand Up @@ -327,7 +327,7 @@ export default function SidebarFolder(props: Props) {
snapshot: {
keyDerivationTrace: snapshot.publicData.keyDerivationTrace,
},
workspaceKeyBox: workspace.currentWorkspaceKey.workspaceKeyBox!,
workspaceKeyBox: workspace.currentWorkspaceKey.workspaceKeyBox,
});
const result = await runCreateDocumentMutation(
{
Expand Down Expand Up @@ -399,7 +399,7 @@ export default function SidebarFolder(props: Props) {
if (!workspace) {
throw new Error("Workspace not found");
}
if (!workspace.currentWorkspaceKey!.workspaceKeyBox) {
if (!workspace.currentWorkspaceKey?.workspaceKeyBox) {
throw new Error("no workspace key boxes for this workspace");
}
const workspaceKeyData = await deriveWorkspaceKey({
Expand All @@ -418,7 +418,7 @@ export default function SidebarFolder(props: Props) {
encryptionPublicKeySignature:
activeDevice.encryptionPublicKeySignature!,
},
workspaceKeyBox: workspace.currentWorkspaceKey!.workspaceKeyBox!,
workspaceKeyBox: workspace.currentWorkspaceKey.workspaceKeyBox,
});
// ignore the last chain item as it's the key for the old folder name
let parentKey = workspaceKey;
Expand Down
2 changes: 1 addition & 1 deletion apps/app/components/sidebarPage/SidebarPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export default function SidebarPage(props: Props) {
snapshot: {
keyDerivationTrace: snapshot.keyDerivationTrace,
},
workspaceKeyBox: documentWorkspaceKey.workspaceKeyBox!,
workspaceKeyBox: documentWorkspaceKey.workspaceKeyBox,
});
setDocumentTitle(documentTitle);
documentTitleStore.updateDocumentTitle({
Expand Down
10 changes: 5 additions & 5 deletions apps/app/generated/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1300,7 +1300,7 @@ export type WorkspaceKey = {
generation: Scalars['Int'];
id: Scalars['String'];
workspaceId: Scalars['String'];
workspaceKeyBox?: Maybe<WorkspaceKeyBox>;
workspaceKeyBox: WorkspaceKeyBox;
workspaceKeyBoxes?: Maybe<Array<WorkspaceKeyBox>>;
};

Expand Down Expand Up @@ -1387,7 +1387,7 @@ export type AttachDeviceToWorkspacesMutationVariables = Exact<{
}>;


export type AttachDeviceToWorkspacesMutation = { __typename?: 'Mutation', attachDeviceToWorkspaces?: { __typename?: 'AttachDeviceToWorkspacesResult', workspaceKeys: Array<{ __typename?: 'WorkspaceKey', id: string, generation: number, workspaceId: string, workspaceKeyBox?: { __typename?: 'WorkspaceKeyBox', id: string, deviceSigningPublicKey: string, creatorDeviceSigningPublicKey: string, ciphertext: string, nonce: string, creatorDevice: { __typename?: 'CreatorDevice', signingPublicKey: string, encryptionPublicKey: string } } | null }> } | null };
export type AttachDeviceToWorkspacesMutation = { __typename?: 'Mutation', attachDeviceToWorkspaces?: { __typename?: 'AttachDeviceToWorkspacesResult', workspaceKeys: Array<{ __typename?: 'WorkspaceKey', id: string, generation: number, workspaceId: string, workspaceKeyBox: { __typename?: 'WorkspaceKeyBox', id: string, deviceSigningPublicKey: string, creatorDeviceSigningPublicKey: string, ciphertext: string, nonce: string, creatorDevice: { __typename?: 'CreatorDevice', signingPublicKey: string, encryptionPublicKey: string } } }> } | null };

export type AttachDevicesToWorkspacesMutationVariables = Exact<{
input: AttachDevicesToWorkspacesInput;
Expand Down Expand Up @@ -1436,7 +1436,7 @@ export type CreateInitialWorkspaceStructureMutationVariables = Exact<{
}>;


export type CreateInitialWorkspaceStructureMutation = { __typename?: 'Mutation', createInitialWorkspaceStructure?: { __typename?: 'CreateInitialWorkspaceStructureResult', workspace?: { __typename?: 'Workspace', id: string, name?: string | null, members?: Array<{ __typename?: 'WorkspaceMember', userId: string, role: Role }> | null, currentWorkspaceKey?: { __typename?: 'WorkspaceKey', id: string, workspaceId: string, generation: number, workspaceKeyBox?: { __typename?: 'WorkspaceKeyBox', id: string, workspaceKeyId: string, deviceSigningPublicKey: string, ciphertext: string, creatorDevice: { __typename?: 'CreatorDevice', signingPublicKey: string, encryptionPublicKey: string } } | null } | null } | null, folder?: { __typename?: 'Folder', id: string, nameCiphertext: string, nameNonce: string, parentFolderId?: string | null, rootFolderId?: string | null, workspaceId?: string | null, keyDerivationTrace: { __typename?: 'KeyDerivationTrace', workspaceKeyId: string, trace: Array<{ __typename?: 'KeyDerivationTraceEntry', entryId: string, subkeyId: number, parentId?: string | null, context: string }> } } | null, document?: { __typename?: 'Document', id: string } | null, snapshot?: { __typename?: 'Snapshot', id: string, latestVersion: number, data: string, documentId: string, keyDerivationTrace: { __typename?: 'KeyDerivationTrace', workspaceKeyId: string, trace: Array<{ __typename?: 'KeyDerivationTraceEntry', entryId: string, subkeyId: number, parentId?: string | null, context: string }> } } | null } | null };
export type CreateInitialWorkspaceStructureMutation = { __typename?: 'Mutation', createInitialWorkspaceStructure?: { __typename?: 'CreateInitialWorkspaceStructureResult', workspace?: { __typename?: 'Workspace', id: string, name?: string | null, members?: Array<{ __typename?: 'WorkspaceMember', userId: string, role: Role }> | null, currentWorkspaceKey?: { __typename?: 'WorkspaceKey', id: string, workspaceId: string, generation: number, workspaceKeyBox: { __typename?: 'WorkspaceKeyBox', id: string, workspaceKeyId: string, deviceSigningPublicKey: string, ciphertext: string, creatorDevice: { __typename?: 'CreatorDevice', signingPublicKey: string, encryptionPublicKey: string } } } | null } | null, folder?: { __typename?: 'Folder', id: string, nameCiphertext: string, nameNonce: string, parentFolderId?: string | null, rootFolderId?: string | null, workspaceId?: string | null, keyDerivationTrace: { __typename?: 'KeyDerivationTrace', workspaceKeyId: string, trace: Array<{ __typename?: 'KeyDerivationTraceEntry', entryId: string, subkeyId: number, parentId?: string | null, context: string }> } } | null, document?: { __typename?: 'Document', id: string } | null, snapshot?: { __typename?: 'Snapshot', id: string, latestVersion: number, data: string, documentId: string, keyDerivationTrace: { __typename?: 'KeyDerivationTrace', workspaceKeyId: string, trace: Array<{ __typename?: 'KeyDerivationTraceEntry', entryId: string, subkeyId: number, parentId?: string | null, context: string }> } } | null } | null };

export type CreateWorkspaceInvitationMutationVariables = Exact<{
input: CreateWorkspaceInvitationInput;
Expand Down Expand Up @@ -1752,7 +1752,7 @@ export type WorkspaceQueryVariables = Exact<{
}>;


export type WorkspaceQuery = { __typename?: 'Query', workspace?: { __typename?: 'Workspace', id: string, name?: string | null, members?: Array<{ __typename?: 'WorkspaceMember', userId: string, username?: string | null, role: Role, devices?: Array<{ __typename?: 'MinimalDevice', signingPublicKey: string, encryptionPublicKey: string, encryptionPublicKeySignature: string }> | null }> | null, currentWorkspaceKey?: { __typename?: 'WorkspaceKey', id: string, workspaceId: string, workspaceKeyBox?: { __typename?: 'WorkspaceKeyBox', id: string, workspaceKeyId: string, deviceSigningPublicKey: string, ciphertext: string, nonce: string, creatorDevice: { __typename?: 'CreatorDevice', signingPublicKey: string, encryptionPublicKey: string } } | null } | null, workspaceKeys?: Array<{ __typename?: 'WorkspaceKey', id: string, workspaceId: string, generation: number, workspaceKeyBox?: { __typename?: 'WorkspaceKeyBox', id: string, workspaceKeyId: string, deviceSigningPublicKey: string, ciphertext: string, nonce: string, creatorDevice: { __typename?: 'CreatorDevice', signingPublicKey: string, encryptionPublicKey: string } } | null }> | null } | null };
export type WorkspaceQuery = { __typename?: 'Query', workspace?: { __typename?: 'Workspace', id: string, name?: string | null, members?: Array<{ __typename?: 'WorkspaceMember', userId: string, username?: string | null, role: Role, devices?: Array<{ __typename?: 'MinimalDevice', signingPublicKey: string, encryptionPublicKey: string, encryptionPublicKeySignature: string }> | null }> | null, currentWorkspaceKey?: { __typename?: 'WorkspaceKey', id: string, workspaceId: string, workspaceKeyBox: { __typename?: 'WorkspaceKeyBox', id: string, workspaceKeyId: string, deviceSigningPublicKey: string, ciphertext: string, nonce: string, creatorDevice: { __typename?: 'CreatorDevice', signingPublicKey: string, encryptionPublicKey: string } } } | null, workspaceKeys?: Array<{ __typename?: 'WorkspaceKey', id: string, workspaceId: string, generation: number, workspaceKeyBox: { __typename?: 'WorkspaceKeyBox', id: string, workspaceKeyId: string, deviceSigningPublicKey: string, ciphertext: string, nonce: string, creatorDevice: { __typename?: 'CreatorDevice', signingPublicKey: string, encryptionPublicKey: string } } }> | null } | null };

export type WorkspaceDevicesQueryVariables = Exact<{
workspaceId: Scalars['ID'];
Expand Down Expand Up @@ -1780,7 +1780,7 @@ export type WorkspacesQueryVariables = Exact<{
}>;


export type WorkspacesQuery = { __typename?: 'Query', workspaces?: { __typename?: 'WorkspaceConnection', nodes?: Array<{ __typename?: 'Workspace', id: string, name?: string | null, members?: Array<{ __typename?: 'WorkspaceMember', userId: string, role: Role }> | null, currentWorkspaceKey?: { __typename?: 'WorkspaceKey', id: string, workspaceId: string, generation: number, workspaceKeyBox?: { __typename?: 'WorkspaceKeyBox', id: string, workspaceKeyId: string, deviceSigningPublicKey: string, ciphertext: string, nonce: string, creatorDevice: { __typename?: 'CreatorDevice', signingPublicKey: string, encryptionPublicKey: string } } | null } | null, workspaceKeys?: Array<{ __typename?: 'WorkspaceKey', id: string, workspaceId: string, generation: number, workspaceKeyBox?: { __typename?: 'WorkspaceKeyBox', id: string, workspaceKeyId: string, deviceSigningPublicKey: string, ciphertext: string, nonce: string, creatorDevice: { __typename?: 'CreatorDevice', signingPublicKey: string, encryptionPublicKey: string } } | null }> | null } | null> | null } | null };
export type WorkspacesQuery = { __typename?: 'Query', workspaces?: { __typename?: 'WorkspaceConnection', nodes?: Array<{ __typename?: 'Workspace', id: string, name?: string | null, members?: Array<{ __typename?: 'WorkspaceMember', userId: string, role: Role }> | null, currentWorkspaceKey?: { __typename?: 'WorkspaceKey', id: string, workspaceId: string, generation: number, workspaceKeyBox: { __typename?: 'WorkspaceKeyBox', id: string, workspaceKeyId: string, deviceSigningPublicKey: string, ciphertext: string, nonce: string, creatorDevice: { __typename?: 'CreatorDevice', signingPublicKey: string, encryptionPublicKey: string } } } | null, workspaceKeys?: Array<{ __typename?: 'WorkspaceKey', id: string, workspaceId: string, generation: number, workspaceKeyBox: { __typename?: 'WorkspaceKeyBox', id: string, workspaceKeyId: string, deviceSigningPublicKey: string, ciphertext: string, nonce: string, creatorDevice: { __typename?: 'CreatorDevice', signingPublicKey: string, encryptionPublicKey: string } } }> | null } | null> | null } | null };


export const AcceptWorkspaceInvitationDocument = gql`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const createNewSnapshotKey = async ({
encryptionPrivateKey: activeDevice.encryptionPrivateKey!,
encryptionPublicKeySignature: activeDevice.encryptionPublicKeySignature!,
},
workspaceKeyBox: workspace.currentWorkspaceKey.workspaceKeyBox!,
workspaceKeyBox: workspace.currentWorkspaceKey.workspaceKeyBox,
});

const parentFolderChainItem =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const deriveExistingSnapshotKey = async (
encryptionPrivateKey: activeDevice.encryptionPrivateKey!,
encryptionPublicKeySignature: activeDevice.encryptionPublicKeySignature!,
},
workspaceKeyBox: workspace?.currentWorkspaceKey.workspaceKeyBox!,
workspaceKeyBox: workspace.currentWorkspaceKey.workspaceKeyBox,
});
const folderChainItem =
folderKeyChainData.trace[folderKeyChainData.trace.length - 2];
Expand Down
2 changes: 1 addition & 1 deletion apps/app/utils/document/documentPathStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export const useDocumentPathStore = create<DocumentPathState>((set, get) => ({
encryptionPublicKeySignature:
activeDevice.encryptionPublicKeySignature!,
},
workspaceKeyBox: folderWorkspaceKey.workspaceKeyBox!,
workspaceKeyBox: folderWorkspaceKey.workspaceKeyBox,
});
// since decryptFolderName also derives the folder subkey,
// we can pass the parentKeyTrace's parent key to it
Expand Down
1 change: 0 additions & 1 deletion apps/app/utils/document/updateDocumentName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export const updateDocumentName = async ({
const encryptedDocumentTitle = encryptDocumentTitle({
title: name,
activeDevice,
// @ts-expect-error
workspaceKeyBox: workspace.currentWorkspaceKey.workspaceKeyBox,
snapshot: snapshotResult.data.snapshot,
});
Expand Down
2 changes: 1 addition & 1 deletion apps/app/utils/folder/folderKeyStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export const useFolderKeyStore = create<FolderKeyState>((set, get) => ({
encryptionPublicKeySignature:
activeDevice.encryptionPublicKeySignature!,
},
workspaceKeyBox: workspace!.currentWorkspaceKey.workspaceKeyBox!,
workspaceKeyBox: workspace.currentWorkspaceKey.workspaceKeyBox,
});
folderKey =
derivedFolderKeyData.trace[derivedFolderKeyData.trace.length - 1].key;
Expand Down
10 changes: 5 additions & 5 deletions apps/backend/src/database/workspace/rotateWorkspaceKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const rotateWorkspaceKey = async ({
if (!user) {
throw new ForbiddenError("Unauthorized");
}
const verifedUserWorkspace = await prisma.usersToWorkspaces.findMany({
const verifiedUserWorkspace = await prisma.usersToWorkspaces.findMany({
where: {
userId,
role: Role.ADMIN,
Expand All @@ -35,7 +35,7 @@ export const rotateWorkspaceKey = async ({
},
select: { workspaceId: true },
});
if (!verifedUserWorkspace) {
if (!verifiedUserWorkspace) {
throw new ForbiddenError("Unauthorized");
}

Expand Down Expand Up @@ -92,7 +92,7 @@ export const rotateWorkspaceKey = async ({
const createdWorkspaceKeyBoxes = await prisma.workspaceKeyBox.findMany({
where: { workspaceKeyId: newWorkspaceKey.id },
});
const returnedWorskpaceKey: WorkspaceKey = newWorkspaceKey;
returnedWorskpaceKey.workspaceKeyBoxes = createdWorkspaceKeyBoxes;
return returnedWorskpaceKey;
const returnedWorkspaceKey: WorkspaceKey = newWorkspaceKey;
returnedWorkspaceKey.workspaceKeyBoxes = createdWorkspaceKeyBoxes;
return returnedWorkspaceKey;
};
Original file line number Diff line number Diff line change
Expand Up @@ -356,21 +356,35 @@ test("user can rotate key for multiple devices", async () => {
workspaceInvitationResult.invitationSigningPrivateKey,
authorizationHeader: userData2.sessionKey,
});
const user2DeviceKeyBox = encryptWorkspaceKeyForDevice({
receiverDeviceEncryptionPublicKey: userData1.device.signingPublicKey,
creatorDeviceEncryptionPrivateKey: userData1.deviceEncryptionPrivateKey,
workspaceKey,

const workspaceResult2 = await getWorkspace({
graphql,
workspaceId: userData1.workspace.id,
deviceSigningPublicKey: userData1.webDevice.signingPublicKey,
authorizationHeader: userData1.sessionKey,
});

const workspace2 = workspaceResult2.workspace;
const workspaceKeyDevicePairs = workspace2.workspaceKeys.map(
(workspaceKeyData) => {
const user2DeviceKeyBox = encryptWorkspaceKeyForDevice({
receiverDeviceEncryptionPublicKey: userData2.device.encryptionPublicKey,
creatorDeviceEncryptionPrivateKey: userData1.deviceEncryptionPrivateKey,
// TODO this is not correct, we should put the correct workspaceKey her based on the workspaceKeyData
workspaceKey,
});
return {
workspaceKeyId: workspaceKeyData.id,
nonce: user2DeviceKeyBox.nonce,
ciphertext: user2DeviceKeyBox.ciphertext,
};
}
);

const user2DeviceKeyBoxes = [
{
workspaceId: userData1.workspace.id,
workspaceKeyDevicePairs: [
{
workspaceKeyId: userData1.workspace.currentWorkspaceKey.id,
nonce: user2DeviceKeyBox.nonce,
ciphertext: user2DeviceKeyBox.nonce,
},
],
workspaceKeyDevicePairs,
},
];
await attachDeviceToWorkspaces({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ test("key for main workspace, main device", async () => {
expect(workspaceKey.workspaceId).toBe(userData1.workspace.id);
});

test("empty keys on incomplete workspace share", async () => {
test("throws error on incomplete workspace share", async () => {
// create new user
const userData2 = await createUserWithWorkspace({
id: generateId(),
Expand All @@ -100,17 +100,15 @@ test("empty keys on incomplete workspace share", async () => {
inviteeMainDevice: userData2.mainDevice,
authorizationHeader: userData2.sessionKey,
});
const workspaceKeyResult = await getWorkspaceKeyByDocumentId({
graphql,
documentId: userData1.document.id,
deviceSigningPublicKey: userData2.webDevice.signingPublicKey,
authorizationHeader: userData2.sessionKey,
});
const workspaceKey =
workspaceKeyResult.workspaceKeyByDocumentId.nameWorkspaceKey;
expect(workspaceKey.generation).toBe(0);
expect(workspaceKey.workspaceKeyBox).toBe(null);
expect(workspaceKey.workspaceId).toBe(userData1.workspace.id);
await expect(
(async () =>
await getWorkspaceKeyByDocumentId({
graphql,
documentId: userData1.document.id,
deviceSigningPublicKey: userData2.webDevice.signingPublicKey,
authorizationHeader: userData2.sessionKey,
}))()
).rejects.toThrowError(/Internal server error/);
});

test("key for shared workspace", async () => {
Expand Down
2 changes: 1 addition & 1 deletion apps/backend/src/graphql/types/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const WorkspaceKey = objectType({
t.nonNull.string("id");
t.nonNull.string("workspaceId");
t.nonNull.int("generation");
t.field("workspaceKeyBox", {
t.nonNull.field("workspaceKeyBox", {
type: WorkspaceKeyBox,
});
t.list.nonNull.field("workspaceKeyBoxes", {
Expand Down
4 changes: 2 additions & 2 deletions apps/backend/src/types/workspace.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {
CreatorDevice as PrismaCreatorDevice,
Role,
UsersToWorkspaces,
Workspace as PrismaWorkspace,
WorkspaceKey as PrismaWorkspaceKey,
WorkspaceKeyBox as PrismaWorkspaceKeyBox,
Role,
UsersToWorkspaces,
} from "../../prisma/generated/output";
import { CreatorDevice, Device, MinimalDevice } from "./device";

Expand Down