Skip to content

Commit

Permalink
Remove legacy crypto support in sync api
Browse files Browse the repository at this point in the history
  • Loading branch information
florianduros committed Jan 16, 2025
1 parent bdd4d82 commit 030cc03
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 140 deletions.
62 changes: 1 addition & 61 deletions spec/integ/sliding-sync-sdk.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,13 @@ describe("SlidingSyncSdk", () => {
};

// assign client/httpBackend globals
const setupClient = async (testOpts?: Partial<IStoredClientOpts & { withCrypto: boolean }>) => {
const setupClient = async (testOpts?: Partial<IStoredClientOpts>) => {
testOpts = testOpts || {};
const syncOpts: SyncApiOptions = {};
const testClient = new TestClient(selfUserId, "DEVICE", selfAccessToken);
httpBackend = testClient.httpBackend;
client = testClient.client;
mockSlidingSync = mockifySlidingSync(new SlidingSync("", new Map(), {}, client, 0));
if (testOpts.withCrypto) {
httpBackend!.when("GET", "/room_keys/version").respond(404, {});
await client!.initLegacyCrypto();
syncOpts.cryptoCallbacks = syncOpts.crypto = client!.crypto;
}
httpBackend!.when("GET", "/_matrix/client/v3/pushrules").respond(200, {});
sdk = new SlidingSyncSdk(mockSlidingSync, client, testOpts, syncOpts);
};
Expand Down Expand Up @@ -627,61 +622,6 @@ describe("SlidingSyncSdk", () => {
});
});

describe("ExtensionE2EE", () => {
let ext: Extension<any, any>;

beforeAll(async () => {
await setupClient({
withCrypto: true,
});
const hasSynced = sdk!.sync();
await httpBackend!.flushAllExpected();
await hasSynced;
ext = findExtension("e2ee");
});

afterAll(async () => {
// needed else we do some async operations in the background which can cause Jest to whine:
// "Cannot log after tests are done. Did you forget to wait for something async in your test?"
// Attempted to log "Saving device tracking data null"."
client!.crypto!.stop();
});

it("gets enabled on the initial request only", () => {
expect(ext.onRequest(true)).toEqual({
enabled: true,
});
expect(ext.onRequest(false)).toEqual(undefined);
});

it("can update device lists", () => {
client!.crypto!.processDeviceLists = jest.fn();
ext.onResponse({
device_lists: {
changed: ["@alice:localhost"],
left: ["@bob:localhost"],
},
});
expect(client!.crypto!.processDeviceLists).toHaveBeenCalledWith({
changed: ["@alice:localhost"],
left: ["@bob:localhost"],
});
});

it("can update OTK counts and unused fallback keys", () => {
client!.crypto!.processKeyCounts = jest.fn();
ext.onResponse({
device_one_time_keys_count: {
signed_curve25519: 42,
},
device_unused_fallback_key_types: ["signed_curve25519"],
});
expect(client!.crypto!.processKeyCounts).toHaveBeenCalledWith({ signed_curve25519: 42 }, [
"signed_curve25519",
]);
});
});

describe("ExtensionAccountData", () => {
let ext: Extension<any, any>;

Expand Down
1 change: 0 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1554,7 +1554,6 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
*/
protected buildSyncApiOptions(): SyncApiOptions {
return {
crypto: this.crypto,
cryptoCallbacks: this.cryptoBackend,
canResetEntireTimeline: (roomId: string): boolean => {
if (!this.canResetTimelineCallback) {
Expand Down
52 changes: 0 additions & 52 deletions src/sliding-sync-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
SetPresence,
} from "./sync.ts";
import { MatrixEvent } from "./models/event.ts";
import { Crypto } from "./crypto/index.ts";
import { IMinimalEvent, IRoomEvent, IStateEvent, IStrippedState, ISyncResponse } from "./sync-accumulator.ts";
import { MatrixError } from "./http-api/index.ts";
import {
Expand All @@ -53,54 +52,6 @@ import { KnownMembership } from "./@types/membership.ts";
// keepAlive is successful but the server /sync fails.
const FAILED_SYNC_ERROR_THRESHOLD = 3;

type ExtensionE2EERequest = {
enabled: boolean;
};

type ExtensionE2EEResponse = Pick<
ISyncResponse,
| "device_lists"
| "device_one_time_keys_count"
| "device_unused_fallback_key_types"
| "org.matrix.msc2732.device_unused_fallback_key_types"
>;

class ExtensionE2EE implements Extension<ExtensionE2EERequest, ExtensionE2EEResponse> {
public constructor(private readonly crypto: Crypto) {}

public name(): string {
return "e2ee";
}

public when(): ExtensionState {
return ExtensionState.PreProcess;
}

public onRequest(isInitial: boolean): ExtensionE2EERequest | undefined {
if (!isInitial) {
return undefined;
}
return {
enabled: true, // this is sticky so only send it on the initial request
};
}

public async onResponse(data: ExtensionE2EEResponse): Promise<void> {
// Handle device list updates
if (data.device_lists) {
await this.crypto.processDeviceLists(data.device_lists);
}

// Handle one_time_keys_count and unused_fallback_key_types
await this.crypto.processKeyCounts(
data.device_one_time_keys_count,
data["device_unused_fallback_key_types"] || data["org.matrix.msc2732.device_unused_fallback_key_types"],
);

this.crypto.onSyncCompleted({});
}
}

type ExtensionToDeviceRequest = {
since?: string;
limit?: number;
Expand Down Expand Up @@ -373,9 +324,6 @@ export class SlidingSyncSdk {
new ExtensionTyping(this.client),
new ExtensionReceipts(this.client),
];
if (this.syncOpts.crypto) {
extensions.push(new ExtensionE2EE(this.syncOpts.crypto));
}
extensions.forEach((ext) => {
this.slidingSync.registerExtension(ext);
});
Expand Down
26 changes: 0 additions & 26 deletions src/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ import { BeaconEvent } from "./models/beacon.ts";
import { IEventsResponse } from "./@types/requests.ts";
import { UNREAD_THREAD_NOTIFICATIONS } from "./@types/sync.ts";
import { Feature, ServerSupport } from "./feature.ts";
import { Crypto } from "./crypto/index.ts";
import { KnownMembership } from "./@types/membership.ts";

const DEBUG = true;
Expand Down Expand Up @@ -116,13 +115,6 @@ function debuglog(...params: any[]): void {
* Options passed into the constructor of SyncApi by MatrixClient
*/
export interface SyncApiOptions {
/**
* Crypto manager
*
* @deprecated in favour of cryptoCallbacks
*/
crypto?: Crypto;

/**
* If crypto is enabled on our client, callbacks into the crypto module
*/
Expand Down Expand Up @@ -642,9 +634,6 @@ export class SyncApi {
}
this.opts.filter.setLazyLoadMembers(true);
}
if (this.opts.lazyLoadMembers) {
this.syncOpts.crypto?.enableLazyLoading();
}
};

private storeClientOptions = async (): Promise<void> => {
Expand Down Expand Up @@ -880,12 +869,6 @@ export class SyncApi {
catchingUp: this.catchingUp,
};

if (this.syncOpts.crypto) {
// tell the crypto module we're about to process a sync
// response
await this.syncOpts.crypto.onSyncWillProcess(syncEventData);
}

try {
await this.processSyncResponse(syncEventData, data);
} catch (e) {
Expand Down Expand Up @@ -920,15 +903,6 @@ export class SyncApi {
this.updateSyncState(SyncState.Syncing, syncEventData);

if (this.client.store.wantsSave()) {
// We always save the device list (if it's dirty) before saving the sync data:
// this means we know the saved device list data is at least as fresh as the
// stored sync data which means we don't have to worry that we may have missed
// device changes. We can also skip the delay since we're not calling this very
// frequently (and we don't really want to delay the sync for it).
if (this.syncOpts.crypto) {
await this.syncOpts.crypto.saveDeviceList(0);
}

// tell databases that everything is now in a consistent state and can be saved.
await this.client.store.save();
}
Expand Down

0 comments on commit 030cc03

Please sign in to comment.