1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-08-09 10:22:46 +03:00

Use shield status codes from Rust rather than string matching (#4529)

This commit is contained in:
Hubert Chathi
2024-11-26 10:06:57 -05:00
committed by GitHub
parent 006929ac0c
commit 69647a33b6
3 changed files with 58 additions and 23 deletions

View File

@@ -991,23 +991,41 @@ describe("RustCrypto", () => {
}); });
it.each([ it.each([
[undefined, null], [undefined, undefined, null],
["Encrypted by an unverified user.", EventShieldReason.UNVERIFIED_IDENTITY], [
["Encrypted by a device not verified by its owner.", EventShieldReason.UNSIGNED_DEVICE], "Encrypted by an unverified user.",
RustSdkCryptoJs.ShieldStateCode.UnverifiedIdentity,
EventShieldReason.UNVERIFIED_IDENTITY,
],
[
"Encrypted by a device not verified by its owner.",
RustSdkCryptoJs.ShieldStateCode.UnsignedDevice,
EventShieldReason.UNSIGNED_DEVICE,
],
[ [
"The authenticity of this encrypted message can't be guaranteed on this device.", "The authenticity of this encrypted message can't be guaranteed on this device.",
RustSdkCryptoJs.ShieldStateCode.AuthenticityNotGuaranteed,
EventShieldReason.AUTHENTICITY_NOT_GUARANTEED, EventShieldReason.AUTHENTICITY_NOT_GUARANTEED,
], ],
["Encrypted by an unknown or deleted device.", EventShieldReason.UNKNOWN_DEVICE], [
["bloop", EventShieldReason.UNKNOWN], "Encrypted by an unknown or deleted device.",
])("gets the right shield reason (%s)", async (rustReason, expectedReason) => { RustSdkCryptoJs.ShieldStateCode.UnknownDevice,
EventShieldReason.UNKNOWN_DEVICE,
],
["Not encrypted.", RustSdkCryptoJs.ShieldStateCode.SentInClear, EventShieldReason.SENT_IN_CLEAR],
[
"Encrypted by a previously-verified user who is no longer verified.",
RustSdkCryptoJs.ShieldStateCode.PreviouslyVerified,
EventShieldReason.VERIFICATION_VIOLATION,
],
])("gets the right shield reason (%s)", async (rustReason, rustCode, expectedReason) => {
// suppress the warning from the unknown shield reason // suppress the warning from the unknown shield reason
jest.spyOn(console, "warn").mockImplementation(() => {}); jest.spyOn(console, "warn").mockImplementation(() => {});
const mockEncryptionInfo = { const mockEncryptionInfo = {
shieldState: jest shieldState: jest
.fn() .fn()
.mockReturnValue({ color: RustSdkCryptoJs.ShieldColor.None, message: rustReason }), .mockReturnValue({ color: RustSdkCryptoJs.ShieldColor.None, code: rustCode, message: rustReason }),
} as unknown as RustSdkCryptoJs.EncryptionInfo; } as unknown as RustSdkCryptoJs.EncryptionInfo;
olmMachine.getRoomEventEncryptionInfo.mockResolvedValue(mockEncryptionInfo); olmMachine.getRoomEventEncryptionInfo.mockResolvedValue(mockEncryptionInfo);

View File

@@ -1157,6 +1157,16 @@ export enum EventShieldReason {
* decryption keys. * decryption keys.
*/ */
MISMATCHED_SENDER_KEY, MISMATCHED_SENDER_KEY,
/**
* The event was sent unencrypted in an encrypted room.
*/
SENT_IN_CLEAR,
/**
* The sender was previously verified but changed their identity.
*/
VERIFICATION_VIOLATION,
} }
/** The result of a call to {@link CryptoApi.getOwnDeviceKeys} */ /** The result of a call to {@link CryptoApi.getOwnDeviceKeys} */

View File

@@ -2180,22 +2180,29 @@ function rustEncryptionInfoToJsEncryptionInfo(
} }
let shieldReason: EventShieldReason | null; let shieldReason: EventShieldReason | null;
if (shieldState.message === undefined) { switch (shieldState.code) {
shieldReason = null; case undefined:
} else if (shieldState.message === "Encrypted by an unverified user.") { case null:
// this case isn't actually used with lax shield semantics. shieldReason = null;
shieldReason = EventShieldReason.UNVERIFIED_IDENTITY; break;
} else if (shieldState.message === "Encrypted by a device not verified by its owner.") { case RustSdkCryptoJs.ShieldStateCode.AuthenticityNotGuaranteed:
shieldReason = EventShieldReason.UNSIGNED_DEVICE; shieldReason = EventShieldReason.AUTHENTICITY_NOT_GUARANTEED;
} else if ( break;
shieldState.message === "The authenticity of this encrypted message can't be guaranteed on this device." case RustSdkCryptoJs.ShieldStateCode.UnknownDevice:
) { shieldReason = EventShieldReason.UNKNOWN_DEVICE;
shieldReason = EventShieldReason.AUTHENTICITY_NOT_GUARANTEED; break;
} else if (shieldState.message === "Encrypted by an unknown or deleted device.") { case RustSdkCryptoJs.ShieldStateCode.UnsignedDevice:
shieldReason = EventShieldReason.UNKNOWN_DEVICE; shieldReason = EventShieldReason.UNSIGNED_DEVICE;
} else { break;
logger.warn(`Unknown shield state message '${shieldState.message}'`); case RustSdkCryptoJs.ShieldStateCode.UnverifiedIdentity:
shieldReason = EventShieldReason.UNKNOWN; shieldReason = EventShieldReason.UNVERIFIED_IDENTITY;
break;
case RustSdkCryptoJs.ShieldStateCode.SentInClear:
shieldReason = EventShieldReason.SENT_IN_CLEAR;
break;
case RustSdkCryptoJs.ShieldStateCode.PreviouslyVerified:
shieldReason = EventShieldReason.VERIFICATION_VIOLATION;
break;
} }
return { shieldColour, shieldReason }; return { shieldColour, shieldReason };