fix(protocol): don't throw on malformed push notification

This commit is contained in:
Marek Polak
2025-12-03 16:50:33 +01:00
committed by Marek Polák
parent 414392e338
commit d382cbfbe0
2 changed files with 12 additions and 15 deletions

View File

@@ -1,9 +1,4 @@
import {
type DecodedTrezorPushNotification,
TrezorPushNotificationMode,
TrezorPushNotificationType,
tpn,
} from '@trezor/protocol';
import { TrezorPushNotificationMode, TrezorPushNotificationType, tpn } from '@trezor/protocol';
import { resolveAfter } from '@trezor/utils';
import { DEVICE } from '../../events/device';
@@ -48,7 +43,12 @@ const setupDeviceMode = async (
};
export const trezorPushNotificationHandler = async ({ device, message }: TpnWorkflowContext) => {
const decoded: DecodedTrezorPushNotification = tpn.decode(message);
const decodedResult = tpn.decode(message);
if (!decodedResult.success) return;
const decoded = decodedResult.payload;
device.lifecycle.emit(DEVICE.TREZOR_PUSH_NOTIFICATION, decoded);
const { type, mode } = decoded;

View File

@@ -30,23 +30,20 @@ export interface DecodedTrezorPushNotification {
mode: TrezorPushNotificationMode;
}
export const decode = (message: number[]): DecodedTrezorPushNotification => {
export const decode = (message: number[]) => {
const [version, type, mode] = message;
if (!version || version !== TPN_VERSION) {
throw new Error(PROTOCOL_MISSMATCH_VERSION);
return { success: false, error: PROTOCOL_MISSMATCH_VERSION } as const;
}
if (
message.length !== MESSAGE_LENGTH ||
message.length < MESSAGE_LENGTH ||
!Object.values(Version).includes(version) ||
!Object.values(TrezorPushNotificationType).includes(type) ||
!Object.values(TrezorPushNotificationMode).includes(mode)
) {
throw new Error(PROTOCOL_MALFORMED);
return { success: false, error: PROTOCOL_MALFORMED } as const;
}
return {
type,
mode,
};
return { success: true, payload: { type, mode } } as const;
};