Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 5a9d7ba

Browse files
authored
Remove unused CryptoCallbacks implementations (#12919)
* Remove unused `onSecretRequested` callback This thing is unused with the rust crypto stack (which is lucky, because it uses methods that only work with the legacy stack). * Remove unused `getDehydrationKey` method This callback is no longer used, so there is no need for an implementation. * Remove unused `dehydrationCache` This is no longer written to, so is redundant. * Remove another write to `CryptoCallbacks.getDehydrationKey` As before: this hook is no longer used by the js-sdk, so writing to it is pointless.
1 parent 69da175 commit 5a9d7ba

File tree

3 files changed

+1
-188
lines changed

3 files changed

+1
-188
lines changed

src/MatrixClientPeg.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ import MatrixClientBackedSettingsHandler from "./settings/handlers/MatrixClientB
4141
import * as StorageManager from "./utils/StorageManager";
4242
import IdentityAuthClient from "./IdentityAuthClient";
4343
import { crossSigningCallbacks } from "./SecurityManager";
44-
import { ModuleRunner } from "./modules/ModuleRunner";
4544
import { SlidingSyncManager } from "./SlidingSyncManager";
4645
import { _t, UserFriendlyError } from "./languageHandler";
4746
import { SettingLevel } from "./settings/SettingLevel";
@@ -452,11 +451,6 @@ class MatrixClientPegClass implements IMatrixClientPeg {
452451
},
453452
};
454453

455-
const dehydrationKeyCallback = ModuleRunner.instance.extensions.cryptoSetup.getDehydrationKeyCallback();
456-
if (dehydrationKeyCallback) {
457-
opts.cryptoCallbacks!.getDehydrationKey = dehydrationKeyCallback;
458-
}
459-
460454
this.matrixClient = createMatrixClient(opts);
461455
this.matrixClient.setGuest(Boolean(creds.guest));
462456

src/SecurityManager.ts

Lines changed: 1 addition & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
import { Crypto, ICryptoCallbacks, encodeBase64, SecretStorage } from "matrix-js-sdk/src/matrix";
17+
import { ICryptoCallbacks, SecretStorage } from "matrix-js-sdk/src/matrix";
1818
import { deriveKey } from "matrix-js-sdk/src/crypto/key_passphrase";
1919
import { decodeRecoveryKey } from "matrix-js-sdk/src/crypto/recoverykey";
2020
import { logger } from "matrix-js-sdk/src/logger";
@@ -39,11 +39,6 @@ let secretStorageKeys: Record<string, Uint8Array> = {};
3939
let secretStorageKeyInfo: Record<string, SecretStorage.SecretStorageKeyDescription> = {};
4040
let secretStorageBeingAccessed = false;
4141

42-
let dehydrationCache: {
43-
key?: Uint8Array;
44-
keyInfo?: SecretStorage.SecretStorageKeyDescription;
45-
} = {};
46-
4742
/**
4843
* This can be used by other components to check if secret storage access is in
4944
* progress, so that we can e.g. avoid intermittently showing toasts during
@@ -119,14 +114,6 @@ async function getSecretStorageKey({
119114
return [keyId, secretStorageKeys[keyId]];
120115
}
121116

122-
if (dehydrationCache.key) {
123-
if (await MatrixClientPeg.safeGet().checkSecretStorageKey(dehydrationCache.key, keyInfo)) {
124-
logger.debug("getSecretStorageKey: returning key from dehydration cache");
125-
cacheSecretStorageKey(keyId, keyInfo, dehydrationCache.key);
126-
return [keyId, dehydrationCache.key];
127-
}
128-
}
129-
130117
const keyFromCustomisations = ModuleRunner.instance.extensions.cryptoSetup.getSecretStorageKey();
131118
if (keyFromCustomisations) {
132119
logger.log("getSecretStorageKey: Using secret storage key from CryptoSetupExtension");
@@ -171,56 +158,6 @@ async function getSecretStorageKey({
171158
return [keyId, key];
172159
}
173160

174-
export async function getDehydrationKey(
175-
keyInfo: SecretStorage.SecretStorageKeyDescription,
176-
checkFunc: (data: Uint8Array) => void,
177-
): Promise<Uint8Array> {
178-
const keyFromCustomisations = ModuleRunner.instance.extensions.cryptoSetup.getSecretStorageKey();
179-
if (keyFromCustomisations) {
180-
logger.log("CryptoSetupExtension: Using key from extension (dehydration)");
181-
return keyFromCustomisations;
182-
}
183-
184-
const inputToKey = makeInputToKey(keyInfo);
185-
const { finished } = Modal.createDialog(
186-
AccessSecretStorageDialog,
187-
/* props= */
188-
{
189-
keyInfo,
190-
checkPrivateKey: async (input: KeyParams): Promise<boolean> => {
191-
const key = await inputToKey(input);
192-
try {
193-
checkFunc(key);
194-
return true;
195-
} catch (e) {
196-
return false;
197-
}
198-
},
199-
},
200-
/* className= */ undefined,
201-
/* isPriorityModal= */ false,
202-
/* isStaticModal= */ false,
203-
/* options= */ {
204-
onBeforeClose: async (reason): Promise<boolean> => {
205-
if (reason === "backgroundClick") {
206-
return confirmToDismiss();
207-
}
208-
return true;
209-
},
210-
},
211-
);
212-
const [input] = await finished;
213-
if (!input) {
214-
throw new AccessCancelledError();
215-
}
216-
const key = await inputToKey(input);
217-
218-
// need to copy the key because rehydration (unpickling) will clobber it
219-
dehydrationCache = { key: new Uint8Array(key), keyInfo };
220-
221-
return key;
222-
}
223-
224161
function cacheSecretStorageKey(
225162
keyId: string,
226163
keyInfo: SecretStorage.SecretStorageKeyDescription,
@@ -232,50 +169,9 @@ function cacheSecretStorageKey(
232169
}
233170
}
234171

235-
async function onSecretRequested(
236-
userId: string,
237-
deviceId: string,
238-
requestId: string,
239-
name: string,
240-
deviceTrust: Crypto.DeviceVerificationStatus,
241-
): Promise<string | undefined> {
242-
logger.log("onSecretRequested", userId, deviceId, requestId, name, deviceTrust);
243-
const client = MatrixClientPeg.safeGet();
244-
if (userId !== client.getUserId()) {
245-
return;
246-
}
247-
if (!deviceTrust?.isVerified()) {
248-
logger.log(`Ignoring secret request from untrusted device ${deviceId}`);
249-
return;
250-
}
251-
if (
252-
name === "m.cross_signing.master" ||
253-
name === "m.cross_signing.self_signing" ||
254-
name === "m.cross_signing.user_signing"
255-
) {
256-
const callbacks = client.getCrossSigningCacheCallbacks();
257-
if (!callbacks?.getCrossSigningKeyCache) return;
258-
const keyId = name.replace("m.cross_signing.", "");
259-
const key = await callbacks.getCrossSigningKeyCache(keyId);
260-
if (!key) {
261-
logger.log(`${keyId} requested by ${deviceId}, but not found in cache`);
262-
}
263-
return key ? encodeBase64(key) : undefined;
264-
} else if (name === "m.megolm_backup.v1") {
265-
const key = await client.crypto?.getSessionBackupPrivateKey();
266-
if (!key) {
267-
logger.log(`session backup key requested by ${deviceId}, but not found in cache`);
268-
}
269-
return key ? encodeBase64(key) : undefined;
270-
}
271-
logger.warn("onSecretRequested didn't recognise the secret named ", name);
272-
}
273-
274172
export const crossSigningCallbacks: ICryptoCallbacks = {
275173
getSecretStorageKey,
276174
cacheSecretStorageKey,
277-
onSecretRequested,
278-
getDehydrationKey,
279175
};
280176

281177
/**

test/MatrixClientPeg-test.ts

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,11 @@ limitations under the License.
1616

1717
import { logger } from "matrix-js-sdk/src/logger";
1818
import fetchMockJest from "fetch-mock-jest";
19-
import {
20-
ProvideCryptoSetupExtensions,
21-
SecretStorageKeyDescription,
22-
} from "@matrix-org/react-sdk-module-api/lib/lifecycles/CryptoSetupExtensions";
2319

2420
import { advanceDateAndTime, stubClient } from "./test-utils";
2521
import { IMatrixClientPeg, MatrixClientPeg as peg } from "../src/MatrixClientPeg";
2622
import SettingsStore from "../src/settings/SettingsStore";
2723
import { SettingLevel } from "../src/settings/SettingLevel";
28-
import { ModuleRunner } from "../src/modules/ModuleRunner";
2924

3025
jest.useFakeTimers();
3126

@@ -78,78 +73,6 @@ describe("MatrixClientPeg", () => {
7873
expect(peg.userRegisteredWithinLastHours(24)).toBe(false);
7974
});
8075

81-
describe(".start extensions", () => {
82-
let testPeg: IMatrixClientPeg;
83-
84-
beforeEach(() => {
85-
// instantiate a MatrixClientPegClass instance, with a new MatrixClient
86-
testPeg = new PegClass();
87-
fetchMockJest.get("http://example.com/_matrix/client/versions", {});
88-
});
89-
90-
describe("cryptoSetup extension", () => {
91-
it("should call default cryptoSetup.getDehydrationKeyCallback", async () => {
92-
const mockCryptoSetup = {
93-
SHOW_ENCRYPTION_SETUP_UI: true,
94-
examineLoginResponse: jest.fn(),
95-
persistCredentials: jest.fn(),
96-
getSecretStorageKey: jest.fn(),
97-
createSecretStorageKey: jest.fn(),
98-
catchAccessSecretStorageError: jest.fn(),
99-
setupEncryptionNeeded: jest.fn(),
100-
getDehydrationKeyCallback: jest.fn().mockReturnValue(null),
101-
} as ProvideCryptoSetupExtensions;
102-
103-
// Ensure we have an instance before we set up spies
104-
const instance = ModuleRunner.instance;
105-
jest.spyOn(instance.extensions, "cryptoSetup", "get").mockReturnValue(mockCryptoSetup);
106-
107-
testPeg.replaceUsingCreds({
108-
accessToken: "SEKRET",
109-
homeserverUrl: "http://example.com",
110-
userId: "@user:example.com",
111-
deviceId: "TEST_DEVICE_ID",
112-
});
113-
114-
expect(mockCryptoSetup.getDehydrationKeyCallback).toHaveBeenCalledTimes(1);
115-
});
116-
117-
it("should call overridden cryptoSetup.getDehydrationKeyCallback", async () => {
118-
const mockDehydrationKeyCallback = () => Uint8Array.from([0x11, 0x22, 0x33]);
119-
120-
const mockCryptoSetup = {
121-
SHOW_ENCRYPTION_SETUP_UI: true,
122-
examineLoginResponse: jest.fn(),
123-
persistCredentials: jest.fn(),
124-
getSecretStorageKey: jest.fn(),
125-
createSecretStorageKey: jest.fn(),
126-
catchAccessSecretStorageError: jest.fn(),
127-
setupEncryptionNeeded: jest.fn(),
128-
getDehydrationKeyCallback: jest.fn().mockReturnValue(mockDehydrationKeyCallback),
129-
} as ProvideCryptoSetupExtensions;
130-
131-
// Ensure we have an instance before we set up spies
132-
const instance = ModuleRunner.instance;
133-
jest.spyOn(instance.extensions, "cryptoSetup", "get").mockReturnValue(mockCryptoSetup);
134-
135-
testPeg.replaceUsingCreds({
136-
accessToken: "SEKRET",
137-
homeserverUrl: "http://example.com",
138-
userId: "@user:example.com",
139-
deviceId: "TEST_DEVICE_ID",
140-
});
141-
expect(mockCryptoSetup.getDehydrationKeyCallback).toHaveBeenCalledTimes(1);
142-
143-
const client = testPeg.get();
144-
const dehydrationKey = await client?.cryptoCallbacks.getDehydrationKey!(
145-
{} as SecretStorageKeyDescription,
146-
(key: Uint8Array) => true,
147-
);
148-
expect(dehydrationKey).toEqual(Uint8Array.from([0x11, 0x22, 0x33]));
149-
});
150-
});
151-
});
152-
15376
describe(".start", () => {
15477
let testPeg: IMatrixClientPeg;
15578

0 commit comments

Comments
 (0)