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

Commit

Permalink
Cypress: Use Rust crypto for the bot user in verification tests
Browse files Browse the repository at this point in the history
We can already start using the Rust crypto implementation for the "bot" user in
the verification tests!
  • Loading branch information
richvdh committed Jun 30, 2023
1 parent 8738cf5 commit da1f473
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 32 deletions.
7 changes: 6 additions & 1 deletion cypress/e2e/crypto/complete-security.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

import { HomeserverInstance } from "../../plugins/utils/homeserver";
import { logIntoElement } from "./utils";
import { checkDeviceIsCrossSigned, logIntoElement } from "./utils";

describe("Complete security", () => {
let homeserver: HomeserverInstance;
Expand All @@ -41,6 +41,11 @@ describe("Complete security", () => {
cy.registerUser(homeserver, username, password, "Jeff");
logIntoElement(homeserver.baseUrl, username, password);
cy.findByText("Welcome Jeff");

cy.log("done");

// Check that our device is now cross-signed
checkDeviceIsCrossSigned();
});

// see also "Verify device during login with SAS" in `verifiction.spec.ts`.
Expand Down
11 changes: 6 additions & 5 deletions cypress/e2e/crypto/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@ export type EmojiMapping = [emoji: string, name: string];
export function waitForVerificationRequest(cli: MatrixClient): Promise<VerificationRequest> {
return new Promise<VerificationRequest>((resolve) => {
const onVerificationRequestEvent = async (request: VerificationRequest) => {
// @ts-ignore CryptoEvent is not exported to window.matrixcs; using the string value here
cli.off("crypto.verification.request", onVerificationRequestEvent);
await request.accept();
resolve(request);
};
// @ts-ignore
cli.on("crypto.verification.request", onVerificationRequestEvent);
// @ts-ignore CryptoEvent is not exported to window.matrixcs; using the string value here
cli.once("crypto.verificationRequestReceived", onVerificationRequestEvent);
});
}

Expand Down Expand Up @@ -134,7 +132,10 @@ export function doTwoWaySasVerification(verifier: Verifier): void {
cy.wrap(emojiPromise).then((emojis: EmojiMapping[]) => {
cy.get(".mx_VerificationShowSas_emojiSas_block").then((emojiBlocks) => {
emojis.forEach((emoji: EmojiMapping, index: number) => {
expect(emojiBlocks[index].textContent.toLowerCase()).to.eq(emoji[0] + emoji[1]);
// VerificationShowSas munges the case of the emoji descriptions returned by the js-sdk before
// displaying them. Once we drop support for legacy crypto, that code can go away, and so can the
// case-munging here.
expect(emojiBlocks[index].textContent.toLowerCase()).to.eq(emoji[0] + emoji[1].toLowerCase());
});
});
});
Expand Down
6 changes: 3 additions & 3 deletions cypress/e2e/crypto/verification.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe("Device verification", () => {
cy.window({ log: false }).should("have.property", "matrixcs");

// Create a new device for alice
cy.getBot(homeserver, { bootstrapCrossSigning: true }).then((bot) => {
cy.getBot(homeserver, { rustCrypto: true, bootstrapCrossSigning: true }).then((bot) => {
aliceBotClient = bot;
});
});
Expand Down Expand Up @@ -71,9 +71,9 @@ describe("Device verification", () => {

// Handle emoji SAS verification
cy.get(".mx_InfoDialog").within(() => {
cy.get<VerificationRequest>("@verificationRequest").then((request: VerificationRequest) => {
cy.get<VerificationRequest>("@verificationRequest").then(async (request: VerificationRequest) => {
// the bot chooses to do an emoji verification
const verifier = request.beginKeyVerification("m.sas.v1");
const verifier = await request.startVerification("m.sas.v1");

// Handle emoji request and check that emojis are matching
doTwoWaySasVerification(verifier);
Expand Down
56 changes: 34 additions & 22 deletions cypress/support/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ interface CreateBotOpts {
* Whether or not to generate cross-signing keys
*/
bootstrapCrossSigning?: boolean;
/**
* Whether to use the rust crypto impl. Defaults to false (for now!)
*/
rustCrypto?: boolean;
}

const defaultCreateBotOptions = {
Expand Down Expand Up @@ -125,7 +129,7 @@ function setupBotClient(
opts: CreateBotOpts,
): Chainable<MatrixClient> {
opts = Object.assign({}, defaultCreateBotOptions, opts);
return cy.window({ log: false }).then((win) => {
return cy.window({ log: false }).then((win): Chainable<MatrixClient> => {
const keys = {};

const getCrossSigningKey = (type: string) => {
Expand Down Expand Up @@ -160,27 +164,35 @@ function setupBotClient(
}

return cy.wrap(
cli
.initCrypto()
.then(() => cli.setGlobalErrorOnUnknownDevices(false))
.then(() => cli.startClient())
.then(async () => {
if (opts.bootstrapCrossSigning) {
await cli.bootstrapCrossSigning({
authUploadDeviceSigningKeys: async (func) => {
await func({
type: "m.login.password",
identifier: {
type: "m.id.user",
user: credentials.userId,
},
password: credentials.password,
});
},
});
}
})
.then(() => cli),
(async (): Promise<MatrixClient> => {
if (opts.rustCrypto) {
await cli.initRustCrypto({ useIndexedDB: false });
} else {
await cli.initCrypto();
}
cli.setGlobalErrorOnUnknownDevices(false);
await cli.startClient();

if (opts.bootstrapCrossSigning) {
// XXX: workaround https://github.com/matrix-org/matrix-rust-sdk/issues/2193
// wait for out device list to be available, as a proxy for the device keys having been uploaded.
await cli.getCrypto()!.getUserDeviceInfo([credentials.userId]);

await cli.getCrypto()!.bootstrapCrossSigning({
authUploadDeviceSigningKeys: async (func) => {
await func({
type: "m.login.password",
identifier: {
type: "m.id.user",
user: credentials.userId,
},
password: credentials.password,
});
},
});
}
return cli;
})(),
// extra timeout, as this sometimes takes a while
{ timeout: 30_000 },
);
Expand Down
2 changes: 2 additions & 0 deletions src/components/structures/MatrixChat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,8 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
return;
}

console.log(`postLogin: crossSigningIsSetUp: ${crossSigningIsSetUp}`);

if (crossSigningIsSetUp) {
// if the user has previously set up cross-signing, verify this device so we can fetch the
// private keys.
Expand Down
4 changes: 3 additions & 1 deletion src/components/views/right_panel/VerificationPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ interface IProps {
}

interface IState {
qrCodeBytes: Buffer | undefined;

sasEvent: ShowSasCallbacks | null;
emojiButtonClicked?: boolean;
reciprocateButtonClicked?: boolean;
Expand All @@ -70,7 +72,7 @@ export default class VerificationPanel extends React.PureComponent<IProps, IStat

public constructor(props: IProps) {
super(props);
this.state = { sasEvent: null, reciprocateQREvent: null };
this.state = { qrCodeBytes: undefined, sasEvent: null, reciprocateQREvent: null };
this.hasVerifier = false;
}

Expand Down

0 comments on commit da1f473

Please sign in to comment.