-
-
Notifications
You must be signed in to change notification settings - Fork 5
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
Add invisible decryption config flag and show invisible crypto decryption errors #50
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
Copyright 2024 New Vector Ltd. | ||
|
||
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only | ||
Please see LICENSE files in the repository root for full details. | ||
*/ | ||
|
||
import { expect, test } from "../../element-web-test"; | ||
import { autoJoin, createSharedRoomWithUser, verify } from "./utils"; | ||
import { Bot } from "../../pages/bot"; | ||
|
||
test.describe("Invisible cryptography", () => { | ||
test.use({ | ||
displayName: "Alice", | ||
botCreateOpts: { displayName: "Bob", autoAcceptInvites: true }, | ||
labsFlags: ["feature_invisible_crypto"], | ||
}); | ||
|
||
test("Messages fail to decrypt when sender is previously verified", async ({ | ||
page, | ||
bot: bob, | ||
user: aliceCredentials, | ||
app, | ||
homeserver, | ||
}) => { | ||
await app.client.bootstrapCrossSigning(aliceCredentials); | ||
await autoJoin(bob); | ||
|
||
// create an encrypted room | ||
const testRoomId = await createSharedRoomWithUser(app, bob.credentials.userId, { | ||
name: "TestRoom", | ||
initial_state: [ | ||
{ | ||
type: "m.room.encryption", | ||
state_key: "", | ||
content: { | ||
algorithm: "m.megolm.v1.aes-sha2", | ||
}, | ||
}, | ||
], | ||
}); | ||
|
||
// Verify Bob | ||
await verify(app, bob); | ||
|
||
// Bob logs in a new device and resets cross-signing | ||
const bobSecondDevice = new Bot(page, homeserver, { | ||
bootstrapSecretStorage: true, | ||
bootstrapCrossSigning: true, | ||
setupNewCrossSigning: true, | ||
}); | ||
bobSecondDevice.setCredentials(await homeserver.loginUser(bob.credentials.userId, bob.credentials.password)); | ||
await bobSecondDevice.prepareClient(); | ||
|
||
/* should show an error for a message from a previously verified device */ | ||
await bobSecondDevice.sendMessage(testRoomId, "test encrypted from previously verified"); | ||
const lastTile = page.locator(".mx_EventTile_last"); | ||
await expect(lastTile).toContainText("Verified identity has changed"); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,15 +6,17 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only | |
Please see LICENSE files in the repository root for full details. | ||
*/ | ||
|
||
import classNames from "classnames"; | ||
import React, { forwardRef, ForwardRefExoticComponent, useContext } from "react"; | ||
import { MatrixEvent } from "matrix-js-sdk/src/matrix"; | ||
import { DecryptionFailureCode } from "matrix-js-sdk/src/crypto-api"; | ||
|
||
import { _t } from "../../../languageHandler"; | ||
import { IBodyProps } from "./IBodyProps"; | ||
import { LocalDeviceVerificationStateContext } from "../../../contexts/LocalDeviceVerificationStateContext"; | ||
import { Icon as WarningBadgeIcon } from "../../../../res/img/compound/error-16px.svg"; | ||
|
||
function getErrorMessage(mxEvent: MatrixEvent, isVerified: boolean | undefined): string { | ||
function getErrorMessage(mxEvent: MatrixEvent, isVerified: boolean | undefined): string | React.JSX.Element { | ||
switch (mxEvent.decryptionFailureReason) { | ||
case DecryptionFailureCode.MEGOLM_KEY_WITHHELD_FOR_UNVERIFIED_DEVICE: | ||
return _t("timeline|decryption_failure|blocked"); | ||
|
@@ -33,15 +35,39 @@ function getErrorMessage(mxEvent: MatrixEvent, isVerified: boolean | undefined): | |
|
||
case DecryptionFailureCode.HISTORICAL_MESSAGE_USER_NOT_JOINED: | ||
return _t("timeline|decryption_failure|historical_event_user_not_joined"); | ||
|
||
case DecryptionFailureCode.SENDER_IDENTITY_PREVIOUSLY_VERIFIED: | ||
return ( | ||
<span> | ||
<WarningBadgeIcon className="mx_Icon mx_Icon_16" /> | ||
{_t("timeline|decryption_failure|sender_identity_previously_verified")} | ||
</span> | ||
); | ||
|
||
case DecryptionFailureCode.UNSIGNED_SENDER_DEVICE: | ||
// TODO: event should be hidden instead of showing this error (only | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this needs to be done by checking the decryption failure code in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably, but I'd like us to kick this down the road for a bit. Let's display the error for now and think about hiding them later. (it's the same as HISTORICAL_MESSAGE_USER_NOT_JOINED) |
||
// happens when invisible crypto is enabled) | ||
return _t("encryption|event_shield_reason_unsigned_device"); | ||
} | ||
return _t("timeline|decryption_failure|unable_to_decrypt"); | ||
} | ||
|
||
function getErrorExtraClass(mxEvent: MatrixEvent): Record<string, boolean> { | ||
switch (mxEvent.decryptionFailureReason) { | ||
case DecryptionFailureCode.SENDER_IDENTITY_PREVIOUSLY_VERIFIED: | ||
return { mx_DecryptionFailureVerifiedIdentityChanged: true }; | ||
|
||
default: | ||
return {}; | ||
} | ||
} | ||
|
||
// A placeholder element for messages that could not be decrypted | ||
export const DecryptionFailureBody = forwardRef<HTMLDivElement, IBodyProps>(({ mxEvent }, ref): React.JSX.Element => { | ||
const verificationState = useContext(LocalDeviceVerificationStateContext); | ||
const classes = classNames("mx_DecryptionFailureBody", "mx_EventTile_content", getErrorExtraClass(mxEvent)); | ||
return ( | ||
<div className="mx_DecryptionFailureBody mx_EventTile_content" ref={ref}> | ||
<div className={classes} ref={ref}> | ||
{getErrorMessage(mxEvent, verificationState)} | ||
</div> | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ Please see LICENSE files in the repository root for full details. | |
import React, { ReactNode } from "react"; | ||
|
||
import { _t, _td, TranslationKey } from "../languageHandler"; | ||
import InvisibleCryptoController from "./controllers/InvisibleCryptoController"; | ||
import { | ||
NotificationBodyEnabledController, | ||
NotificationsEnabledController, | ||
|
@@ -316,6 +317,16 @@ export const SETTINGS: { [setting: string]: ISetting } = { | |
supportedLevelsAreOrdered: true, | ||
default: false, | ||
}, | ||
"feature_invisible_crypto": { | ||
isFeature: true, | ||
labsGroup: LabGroup.Encryption, | ||
controller: new InvisibleCryptoController(), | ||
displayName: _td("labs|invisible_crypto"), | ||
description: _td("labs|invisible_crypto_description"), | ||
supportedLevels: LEVELS_DEVICE_ONLY_SETTINGS_WITH_CONFIG_PRIORITISED, | ||
supportedLevelsAreOrdered: true, | ||
default: false, | ||
}, | ||
Comment on lines
+320
to
+329
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we probably need two settings -- one for "invisible crypto", and one for "transition mode" ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I wasn't sure how best to represent this. Having two separate toggles for three levels seems wrong (e.g. what do we do if both "invisible" and "transition" are enabled). I think that ideally, this would be a dropdown or something that would allow us to select one of the three levels, but I don't how to do that in settings, or if it's even possible with feature flags. I think that if it isn't possible for feature flags, my second preference would be for the feature flag to be to enable "new cryptography modes" or some such, which would enable a selector somewhere else to select the mode. But, basically, I didn't have time to sufficiently investigate. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I suspect probably a custom labs flag that's actually a drop down 9and maybe not actually a labs flag but manually put into the labs section?) That or a labs flag to enable another setting in the crypto section, although that risks people not realising they've turned the labs flag on. Either way, would you want this PR is as it is with no option for transition mode or do we want to wait? |
||
"useOnlyCurrentProfiles": { | ||
supportedLevels: LEVELS_ACCOUNT_SETTINGS, | ||
displayName: _td("settings|disable_historical_profile"), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
Copyright 2024 New Vector Ltd. | ||
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only | ||
Please see LICENSE files in the repository root for full details. | ||
*/ | ||
|
||
import { CryptoMode } from "matrix-js-sdk/src/crypto-api"; | ||
|
||
import SettingController from "./SettingController"; | ||
import { MatrixClientPeg } from "../../MatrixClientPeg"; | ||
import { SettingLevel } from "../SettingLevel"; | ||
|
||
export default class InvisibleCryptoController extends SettingController { | ||
public onChange(level: SettingLevel, roomId: string, newValue: any): void { | ||
const crypto = MatrixClientPeg.safeGet().getCrypto(); | ||
if (crypto) { | ||
crypto.setCryptoMode(newValue ? CryptoMode.Invisible : CryptoMode.Legacy); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
Copyright 2024 New Vector Ltd. | ||
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only | ||
Please see LICENSE files in the repository root for full details. | ||
*/ | ||
|
||
import { CryptoApi, CryptoMode } from "matrix-js-sdk/src/crypto-api"; | ||
|
||
import { MatrixClientPeg } from "../../../src/MatrixClientPeg"; | ||
import { stubClient } from "../../test-utils"; | ||
import InvisibleCryptoController from "../../../src/settings/controllers/InvisibleCryptoController"; | ||
import { SettingLevel } from "../../../src/settings/SettingLevel"; | ||
|
||
describe("InvisibleCryptoController", () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("tracks enabling and disabling", () => { | ||
const cli = stubClient(); | ||
const setCryptoModeMock = jest.fn(); | ||
cli.getCrypto = jest.fn(() => { | ||
return { | ||
setCryptoMode: setCryptoModeMock, | ||
} as unknown as CryptoApi; | ||
}); | ||
jest.spyOn(MatrixClientPeg, "safeGet").mockReturnValue(cli); | ||
|
||
const controller = new InvisibleCryptoController(); | ||
|
||
controller.onChange(SettingLevel.DEVICE, "", true); | ||
expect(setCryptoModeMock).toHaveBeenCalledWith(CryptoMode.Invisible); | ||
|
||
controller.onChange(SettingLevel.DEVICE, "", false); | ||
expect(setCryptoModeMock).toHaveBeenCalledWith(CryptoMode.Legacy); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could we maybe move the changes to DecryptionFailureBody into a separate PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, one PR for the config setting, and one PR for the display changes? Possibly, but I suspect that the config setting PR will have a lack of code worth testing, which would cause SonarCloud to complain. I'll rewrite this PR so that it has two commits - one for the config setting and one for the display changes, so that it can be split into two PRs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I discovered some tests for settings, and added one for invisible crypto. So maybe SonarCloud would be happy with a PR for just the setting. But I've run out of time to split it into a separate PR. But it is now in two commits.