Skip to content

Use enums for EventShield fields #188

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

Merged
merged 2 commits into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions internal/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,49 @@ type Event struct {
}

type EventShield struct {
Colour string // "Red" or "Grey"
Code string // "VerificationViolation" or similar
Colour EventShieldColour
Code EventShieldCode
}

type EventShieldColour string

var (
EventShieldColourRed EventShieldColour = "Red"
EventShieldColourGrey EventShieldColour = "Grey"
)

type EventShieldCode string

var (
// "An unknown reason from the crypto library (if you see this, it is a bug in matrix-js-sdk)."
EventShieldCodeUnknown EventShieldCode = "Unknown"

// "Encrypted by an unverified user."
EventShieldCodeUnverifiedIdentity EventShieldCode = "UnverifiedIdentity"

// "Encrypted by a device not verified by its owner."
EventShieldCodeUnsignedDevice EventShieldCode = "UnsignedDevice"

// "Encrypted by an unknown or deleted device."
EventShieldCodeUnknownDevice EventShieldCode = "UnknownDevice"

// "The authenticity of this encrypted message can't be guaranteed on this device."
//
// i.e.: the key has been forwarded, or retrieved from an insecure backup.
EventShieldCodeAuthenticityNotGuaranteed EventShieldCode = "AuthenticityNotGuaranteed"

// "The (deprecated) sender_key field in the event does not match the Ed25519 key of the device that sent us the decryption keys.
//
// No longer used with rust crypto stack, since it doesn't check the sender_key field.
EventShieldCodeMismatchedSenderKey EventShieldCode = "MismatchedSenderKey"

// "The event was sent unencrypted in an encrypted room."
EventShieldCodeSentInClear EventShieldCode = "SentInClear"

// "The sender was previously verified but changed their identity."
EventShieldCodeVerificationViolation EventShieldCode = "VerificationViolation"
)

type Waiter interface {
// Wait for something to happen, up until the timeout s. If nothing happens,
// fail the test with the formatted string provided.
Expand Down
41 changes: 10 additions & 31 deletions internal/api/js/js.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,58 +587,37 @@ func (c *JSClient) GetEventShield(t ct.TestLike, roomID, eventID string) (*api.E
var eventShield api.EventShield
switch encryptionInfo.ShieldColour {
case 1:
eventShield.Colour = "grey"
eventShield.Colour = api.EventShieldColourGrey
case 2:
eventShield.Colour = "red"
eventShield.Colour = api.EventShieldColourRed
default:
return nil, fmt.Errorf("unknown shield colour: %d", encryptionInfo.ShieldColour)
}

switch encryptionInfo.ShieldReason {
case 0:
/** An unknown reason from the crypto library (if you see this, it is a bug in matrix-js-sdk). */
eventShield.Code = "Unknown"
eventShield.Code = api.EventShieldCodeUnknown

case 1:
/** "Encrypted by an unverified user." */
eventShield.Code = "UnverifiedIdentity"
eventShield.Code = api.EventShieldCodeUnverifiedIdentity

case 2:
/** "Encrypted by a device not verified by its owner." */
eventShield.Code = "UnsignedDevice"
eventShield.Code = api.EventShieldCodeUnsignedDevice

case 3:
/** "Encrypted by an unknown or deleted device." */
eventShield.Code = "UnknownDevice"
eventShield.Code = api.EventShieldCodeUnknownDevice

case 4:
/**
* "The authenticity of this encrypted message can't be guaranteed on this device."
*
* i.e.: the key has been forwarded, or retrieved from an insecure backup.
*/
eventShield.Code = "AuthenticityNotGuaranteed"
eventShield.Code = api.EventShieldCodeAuthenticityNotGuaranteed

case 5:
/**
* The (deprecated) sender_key field in the event does not match the Ed25519 key of the device that sent us the
* decryption keys.
*
* No longer used with rust crypto stack, since it doesn't check the sender_key field.
*/
eventShield.Code = "MismatchedSenderKey"
eventShield.Code = api.EventShieldCodeMismatchedSenderKey

case 6:
/**
* The event was sent unencrypted in an encrypted room.
*/
eventShield.Code = "SentInClear"
eventShield.Code = api.EventShieldCodeSentInClear

case 7:
/**
* The sender was previously verified but changed their identity.
*/
eventShield.Code = "VerificationViolation"
eventShield.Code = api.EventShieldCodeVerificationViolation

default:
return nil, fmt.Errorf("unknown shield reason code: %d", encryptionInfo.ShieldReason)
Expand Down
20 changes: 10 additions & 10 deletions internal/api/rust/rust.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,21 +350,21 @@ func (c *RustClient) GetEventShield(t ct.TestLike, roomID, eventID string) (*api
}
shieldState := timelineItem.LazyProvider.GetShields(false)

codeToString := func(code matrix_sdk_common.ShieldStateCode) string {
var result string
codeToString := func(code matrix_sdk_common.ShieldStateCode) api.EventShieldCode {
var result api.EventShieldCode
switch code {
case matrix_sdk_common.ShieldStateCodeAuthenticityNotGuaranteed:
result = "AuthenticityNotGuaranteed"
result = api.EventShieldCodeAuthenticityNotGuaranteed
case matrix_sdk_common.ShieldStateCodeUnknownDevice:
result = "UnknownDevice"
result = api.EventShieldCodeUnknownDevice
case matrix_sdk_common.ShieldStateCodeUnsignedDevice:
result = "UnsignedDevice"
result = api.EventShieldCodeUnsignedDevice
case matrix_sdk_common.ShieldStateCodeUnverifiedIdentity:
result = "UnverifiedIdentity"
result = api.EventShieldCodeUnverifiedIdentity
case matrix_sdk_common.ShieldStateCodeSentInClear:
result = "SentInClear"
result = api.EventShieldCodeSentInClear
case matrix_sdk_common.ShieldStateCodeVerificationViolation:
result = "VerificationViolation"
result = api.EventShieldCodeVerificationViolation
default:
log.Panicf("Unknown shield code %d", code)
}
Expand All @@ -381,13 +381,13 @@ func (c *RustClient) GetEventShield(t ct.TestLike, roomID, eventID string) (*api

case matrix_sdk_ffi.ShieldStateGrey:
eventShield = &api.EventShield{
Colour: "grey",
Colour: api.EventShieldColourGrey,
Code: codeToString(shield.(matrix_sdk_ffi.ShieldStateGrey).Code),
}

case matrix_sdk_ffi.ShieldStateRed:
eventShield = &api.EventShield{
Colour: "red",
Colour: api.EventShieldColourRed,
Code: codeToString(shield.(matrix_sdk_ffi.ShieldStateRed).Code),
}
}
Expand Down
Loading