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

wip: e2ee #17

Open
wants to merge 4 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
90 changes: 74 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/matrix-crdt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"rimraf": "^3.0.2",
"typescript": "^4.4.4",
"vite": "^3.0.0",
"vitest": "^0.20.3",
"vitest": "^0.23.1",
"@vitest/coverage-c8": "^0.23.1",
"y-protocols": "^1.0.5",
"yjs": "^13.5.16"
},
Expand Down
73 changes: 62 additions & 11 deletions packages/matrix-crdt/src/MatrixProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { beforeAll, expect, it } from "vitest";
import { event } from "vscode-lib";
import * as Y from "yjs";
import { MatrixProvider } from "./MatrixProvider";
import {
PublicRoomPermissionType,
RoomSecuritySetting,
} from "./matrixRoomManagement";
import { createMatrixGuestClient } from "./test-utils/matrixGuestClient";
import {
createRandomMatrixClient,
Expand All @@ -23,9 +27,9 @@ type UnPromisify<T> = T extends Promise<infer U> ? U : T;

async function getRoomAndTwoUsers(opts: {
bobIsGuest: boolean;
roomAccess: "public-read-write" | "public-read";
security: RoomSecuritySetting;
}) {
const setup = await createRandomMatrixClientAndRoom(opts.roomAccess);
const setup = await createRandomMatrixClientAndRoom(opts.security);
const doc = new Y.Doc();
const provider = new MatrixProvider(doc, setup.client, {
type: "alias",
Expand All @@ -41,6 +45,11 @@ async function getRoomAndTwoUsers(opts: {
alias: "#" + setup.roomName + ":" + HOMESERVER_NAME,
});

if (opts.security.permissions === "private") {
// invite user to private room
await setup.client.invite(setup.roomId, client2.credentials.userId!);
}

return {
alice: {
doc,
Expand Down Expand Up @@ -120,6 +129,7 @@ async function validateTwoWaySync(
console.log("Bob sending change", bob.client.credentials.userId);
expect(bob.provider.canWrite).toBe(true);
bob.doc.getMap("test3").set("key", 1);

await bob.provider.waitForFlush();
await event.Event.toPromise(alice.provider.onReceivedEvents);
expect(alice.doc.getMap("test3").get("key")).toBe(1);
Expand All @@ -129,34 +139,73 @@ async function validateTwoWaySync(
bob.provider.dispose();
}

it("syncs public room guest", async () => {
for (let permissions of [
"public-read-write",
"public-read",
] as PublicRoomPermissionType[]) {
it(`Guest is read-only for room set to ${permissions}`, async () => {
const users = await getRoomAndTwoUsers({
bobIsGuest: true,
security: {
permissions: "public-read-write",
encrypted: false,
},
});
await validateOneWaySync(users);
}, 30000);
}

it("User is read-only for room set to public-read", async () => {
const users = await getRoomAndTwoUsers({
bobIsGuest: true,
roomAccess: "public-read-write",
bobIsGuest: false,
security: {
permissions: "public-read",
encrypted: false,
},
});
await validateOneWaySync(users);
}, 30000);

it("syncs write-only access", async () => {
it("User can read and write for room set to public-read-write ", async () => {
const users = await getRoomAndTwoUsers({
bobIsGuest: false,
roomAccess: "public-read",
security: {
permissions: "public-read-write",
encrypted: false,
},
});
await validateOneWaySync(users);
await validateTwoWaySync(users);
}, 30000);

it("syncs two users writing ", async () => {
it("User can read and write for room set to private", async () => {
const users = await getRoomAndTwoUsers({
bobIsGuest: false,
roomAccess: "public-read-write",
security: {
permissions: "private",
encrypted: false,
},
});
await validateTwoWaySync(users);
}, 30000);

it("User can read and write for room set to private, encrypted", async () => {
const users = await getRoomAndTwoUsers({
bobIsGuest: false,
security: {
permissions: "private",
encrypted: true,
},
});
await validateTwoWaySync(users);
}, 30000);

it("syncs with intermediate snapshots ", async () => {
const users = await getRoomAndTwoUsers({
bobIsGuest: false,
roomAccess: "public-read-write",
security: {
permissions: "public-read-write",
encrypted: false,
},
});

const { alice, bob } = users;
Expand All @@ -175,6 +224,8 @@ it("syncs with intermediate snapshots ", async () => {

const val = bob.doc.getMap("test").get("contents") as any;
expect(val.toJSON()).toEqual(text.toJSON());

// validate that the snapshot system has been used
expect(bob.provider.totalEventsReceived).toBeLessThan(20);

alice.provider.dispose();
Expand Down
14 changes: 7 additions & 7 deletions packages/matrix-crdt/src/MatrixProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ import { MatrixClient } from "matrix-js-sdk";
import { event, lifecycle } from "vscode-lib";
import * as awarenessProtocol from "y-protocols/awareness";
import * as Y from "yjs";
import { signObject, verifyObject } from "./util/authUtil";
import {
MatrixCRDTEventTranslator,
MatrixCRDTEventTranslatorOptions,
} from "./MatrixCRDTEventTranslator";
import { MatrixMemberReader } from "./memberReader/MatrixMemberReader";
import { MatrixReader, MatrixReaderOptions } from "./reader/MatrixReader";
import { SignedWebrtcProvider } from "./SignedWebrtcProvider";
import { signObject, verifyObject } from "./util/authUtil";
import { arrayBuffersAreEqual } from "./util/binary";
import { decodeBase64 } from "./util/olmlib";
import {
ThrottledMatrixWriter,
ThrottledMatrixWriterOptions,
} from "./writer/ThrottledMatrixWriter";
import { decodeBase64 } from "./util/olmlib";
import { arrayBuffersAreEqual } from "./util/binary";
import {
MatrixCRDTEventTranslator,
MatrixCRDTEventTranslatorOptions,
} from "./MatrixCRDTEventTranslator";

const DEFAULT_OPTIONS = {
enableExperimentalWebrtcSync: false,
Expand Down
Loading