Skip to content

Commit 69647a3

Browse files
authored
Use shield status codes from Rust rather than string matching (#4529)
1 parent 006929a commit 69647a3

File tree

3 files changed

+58
-23
lines changed

3 files changed

+58
-23
lines changed

spec/unit/rust-crypto/rust-crypto.spec.ts

+25-7
Original file line numberDiff line numberDiff line change
@@ -991,23 +991,41 @@ describe("RustCrypto", () => {
991991
});
992992

993993
it.each([
994-
[undefined, null],
995-
["Encrypted by an unverified user.", EventShieldReason.UNVERIFIED_IDENTITY],
996-
["Encrypted by a device not verified by its owner.", EventShieldReason.UNSIGNED_DEVICE],
994+
[undefined, undefined, null],
995+
[
996+
"Encrypted by an unverified user.",
997+
RustSdkCryptoJs.ShieldStateCode.UnverifiedIdentity,
998+
EventShieldReason.UNVERIFIED_IDENTITY,
999+
],
1000+
[
1001+
"Encrypted by a device not verified by its owner.",
1002+
RustSdkCryptoJs.ShieldStateCode.UnsignedDevice,
1003+
EventShieldReason.UNSIGNED_DEVICE,
1004+
],
9971005
[
9981006
"The authenticity of this encrypted message can't be guaranteed on this device.",
1007+
RustSdkCryptoJs.ShieldStateCode.AuthenticityNotGuaranteed,
9991008
EventShieldReason.AUTHENTICITY_NOT_GUARANTEED,
10001009
],
1001-
["Encrypted by an unknown or deleted device.", EventShieldReason.UNKNOWN_DEVICE],
1002-
["bloop", EventShieldReason.UNKNOWN],
1003-
])("gets the right shield reason (%s)", async (rustReason, expectedReason) => {
1010+
[
1011+
"Encrypted by an unknown or deleted device.",
1012+
RustSdkCryptoJs.ShieldStateCode.UnknownDevice,
1013+
EventShieldReason.UNKNOWN_DEVICE,
1014+
],
1015+
["Not encrypted.", RustSdkCryptoJs.ShieldStateCode.SentInClear, EventShieldReason.SENT_IN_CLEAR],
1016+
[
1017+
"Encrypted by a previously-verified user who is no longer verified.",
1018+
RustSdkCryptoJs.ShieldStateCode.PreviouslyVerified,
1019+
EventShieldReason.VERIFICATION_VIOLATION,
1020+
],
1021+
])("gets the right shield reason (%s)", async (rustReason, rustCode, expectedReason) => {
10041022
// suppress the warning from the unknown shield reason
10051023
jest.spyOn(console, "warn").mockImplementation(() => {});
10061024

10071025
const mockEncryptionInfo = {
10081026
shieldState: jest
10091027
.fn()
1010-
.mockReturnValue({ color: RustSdkCryptoJs.ShieldColor.None, message: rustReason }),
1028+
.mockReturnValue({ color: RustSdkCryptoJs.ShieldColor.None, code: rustCode, message: rustReason }),
10111029
} as unknown as RustSdkCryptoJs.EncryptionInfo;
10121030
olmMachine.getRoomEventEncryptionInfo.mockResolvedValue(mockEncryptionInfo);
10131031

src/crypto-api/index.ts

+10
Original file line numberDiff line numberDiff line change
@@ -1157,6 +1157,16 @@ export enum EventShieldReason {
11571157
* decryption keys.
11581158
*/
11591159
MISMATCHED_SENDER_KEY,
1160+
1161+
/**
1162+
* The event was sent unencrypted in an encrypted room.
1163+
*/
1164+
SENT_IN_CLEAR,
1165+
1166+
/**
1167+
* The sender was previously verified but changed their identity.
1168+
*/
1169+
VERIFICATION_VIOLATION,
11601170
}
11611171

11621172
/** The result of a call to {@link CryptoApi.getOwnDeviceKeys} */

src/rust-crypto/rust-crypto.ts

+23-16
Original file line numberDiff line numberDiff line change
@@ -2180,22 +2180,29 @@ function rustEncryptionInfoToJsEncryptionInfo(
21802180
}
21812181

21822182
let shieldReason: EventShieldReason | null;
2183-
if (shieldState.message === undefined) {
2184-
shieldReason = null;
2185-
} else if (shieldState.message === "Encrypted by an unverified user.") {
2186-
// this case isn't actually used with lax shield semantics.
2187-
shieldReason = EventShieldReason.UNVERIFIED_IDENTITY;
2188-
} else if (shieldState.message === "Encrypted by a device not verified by its owner.") {
2189-
shieldReason = EventShieldReason.UNSIGNED_DEVICE;
2190-
} else if (
2191-
shieldState.message === "The authenticity of this encrypted message can't be guaranteed on this device."
2192-
) {
2193-
shieldReason = EventShieldReason.AUTHENTICITY_NOT_GUARANTEED;
2194-
} else if (shieldState.message === "Encrypted by an unknown or deleted device.") {
2195-
shieldReason = EventShieldReason.UNKNOWN_DEVICE;
2196-
} else {
2197-
logger.warn(`Unknown shield state message '${shieldState.message}'`);
2198-
shieldReason = EventShieldReason.UNKNOWN;
2183+
switch (shieldState.code) {
2184+
case undefined:
2185+
case null:
2186+
shieldReason = null;
2187+
break;
2188+
case RustSdkCryptoJs.ShieldStateCode.AuthenticityNotGuaranteed:
2189+
shieldReason = EventShieldReason.AUTHENTICITY_NOT_GUARANTEED;
2190+
break;
2191+
case RustSdkCryptoJs.ShieldStateCode.UnknownDevice:
2192+
shieldReason = EventShieldReason.UNKNOWN_DEVICE;
2193+
break;
2194+
case RustSdkCryptoJs.ShieldStateCode.UnsignedDevice:
2195+
shieldReason = EventShieldReason.UNSIGNED_DEVICE;
2196+
break;
2197+
case RustSdkCryptoJs.ShieldStateCode.UnverifiedIdentity:
2198+
shieldReason = EventShieldReason.UNVERIFIED_IDENTITY;
2199+
break;
2200+
case RustSdkCryptoJs.ShieldStateCode.SentInClear:
2201+
shieldReason = EventShieldReason.SENT_IN_CLEAR;
2202+
break;
2203+
case RustSdkCryptoJs.ShieldStateCode.PreviouslyVerified:
2204+
shieldReason = EventShieldReason.VERIFICATION_VIOLATION;
2205+
break;
21992206
}
22002207

22012208
return { shieldColour, shieldReason };

0 commit comments

Comments
 (0)