Skip to content

Commit

Permalink
feat(suite): ignore FW hash check unrecognized-version
Browse files Browse the repository at this point in the history
  • Loading branch information
Lemonexe committed Oct 31, 2024
1 parent 7df020d commit 0b3dc63
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { useSelector } from 'src/hooks/suite';
import {
selectFirmwareHashCheckError,
selectFirmwareRevisionCheckError,
selectIsUnrecognizedFirmwareWithOutdatedSuite,
} from 'src/reducers/suite/suiteReducer';
import { skippedHashCheckErrors } from 'src/constants/suite/firmware';

const revisionCheckMessages: Record<FirmwareRevisionCheckError, TranslationKey> = {
'cannot-perform-check-offline': 'TR_DEVICE_FIRMWARE_REVISION_CHECK_UNABLE_TO_PERFORM',
Expand All @@ -19,34 +19,23 @@ const revisionCheckMessages: Record<FirmwareRevisionCheckError, TranslationKey>
'firmware-version-unknown': 'TR_FIRMWARE_REVISION_CHECK_FAILED',
};

export const skippedHashCheckErrors = [
'check-skipped',
'check-unsupported',
] satisfies FirmwareHashCheckError[];
type SkippedHashCheckMessage = (typeof skippedHashCheckErrors)[number];

const hashCheckMessages: Record<
Exclude<FirmwareHashCheckError, SkippedHashCheckMessage>,
TranslationKey
> = {
'hash-mismatch': 'TR_DEVICE_FIRMWARE_HASH_CHECK_HASH_MISMATCH',
'unknown-release': 'TR_DEVICE_FIRMWARE_HASH_CHECK_UNKNOWN_RELEASE',
'other-error': 'TR_DEVICE_FIRMWARE_HASH_CHECK_OTHER_ERROR',
};

const useAuthenticityCheckMessage = (): TranslationKey | null => {
const firmwareRevisionError = useSelector(selectFirmwareRevisionCheckError);
const firmwareHashError = useSelector(selectFirmwareHashCheckError);
const isUnrecognizedFwWithOutadedSuite = useSelector(
selectIsUnrecognizedFirmwareWithOutdatedSuite,
);

if (firmwareRevisionError) {
return revisionCheckMessages[firmwareRevisionError];
}
if (isUnrecognizedFwWithOutadedSuite) {
return 'TR_DEVICE_FIRMWARE_UNRECOGNIZED_OUTDATED_SUITE';
}
if (firmwareHashError && !isArrayMember(firmwareHashError, skippedHashCheckErrors)) {
return hashCheckMessages[firmwareHashError];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { spacingsPx } from '@trezor/theme';
import { isTranslationMode } from 'src/utils/suite/l10n';
import { useSelector } from 'src/hooks/suite';
import { MAX_CONTENT_WIDTH } from 'src/constants/suite/layout';
import { skippedHashCheckErrors } from 'src/constants/suite/firmware';
import {
selectFirmwareHashCheckError,
selectFirmwareRevisionCheckError,
Expand All @@ -22,7 +23,7 @@ import { FailedBackup } from './FailedBackupBanner';
import { SafetyChecksBanner } from './SafetyChecksBanner';
import { TranslationMode } from './TranslationModeBanner';
import { FirmwareHashMismatch } from './FirmwareHashMismatchBanner';
import { FirmwareRevisionCheckBanner, skippedHashCheckErrors } from './FirmwareRevisionCheckBanner';
import { FirmwareRevisionCheckBanner } from './FirmwareRevisionCheckBanner';

const Container = styled.div<{ $isVisible?: boolean }>`
width: 100%;
Expand Down
13 changes: 13 additions & 0 deletions packages/suite/src/constants/suite/firmware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { FirmwareHashCheckError, FirmwareRevisionCheckError } from '@trezor/connect';

export const skippedRevisionCheckErrors = [
'cannot-perform-check-offline',
'other-error',
] satisfies FirmwareRevisionCheckError[];

export const skippedHashCheckErrors = [
'check-skipped',
'check-unsupported',
// this could be serious, but it's also caught by revision check, which handles edge-cases better, so it's skipped here
'unknown-release',
] satisfies FirmwareHashCheckError[];
32 changes: 5 additions & 27 deletions packages/suite/src/reducers/suite/suiteReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@ import type { InvityServerEnvironment } from '@suite-common/invity';
import { Feature, selectIsFeatureDisabled } from '@suite-common/message-system';
import { isDeviceAcquired } from '@suite-common/suite-utils';
import { discoveryActions, DeviceRootState, selectDevice } from '@suite-common/wallet-core';
import { versionUtils } from '@trezor/utils';
import { isArrayMember, versionUtils } from '@trezor/utils';
import { isWeb } from '@trezor/env-utils';
import {
TRANSPORT,
TransportInfo,
ConnectSettings,
FirmwareHashCheckError,
FirmwareRevisionCheckError,
} from '@trezor/connect';
import { TRANSPORT, TransportInfo, ConnectSettings } from '@trezor/connect';

import { getIsTorEnabled, getIsTorLoading } from 'src/utils/suite/tor';
import type { OAuthServerEnvironment } from 'src/types/suite/metadata';
Expand All @@ -27,7 +21,7 @@ import { NetworkSymbol } from '@suite-common/wallet-config';
import { SuiteThemeVariant } from '@trezor/suite-desktop-api';
import { AddressDisplayOptions, WalletType } from '@suite-common/wallet-types';
import { SIDEBAR_WIDTH_NUMERIC } from 'src/constants/suite/layout';
import { UpdateState } from './desktopUpdateReducer';
import { skippedHashCheckErrors, skippedRevisionCheckErrors } from 'src/constants/suite/firmware';

export interface SuiteRootState {
suite: SuiteState;
Expand Down Expand Up @@ -461,12 +455,8 @@ export const selectFirmwareRevisionCheckError = (state: AppState) => {
*/
const selectIsFirmwareRevisionCheckEnabledAndFailed = (state: AppState): boolean => {
const error = selectFirmwareRevisionCheckError(state);
const softErrors: FirmwareRevisionCheckError[] = [
'cannot-perform-check-offline',
'other-error',
];

return error !== null ? !softErrors.includes(error) : false;
return error !== null ? !isArrayMember(error, skippedRevisionCheckErrors) : false;
};

/**
Expand All @@ -484,27 +474,15 @@ export const selectFirmwareHashCheckError = (state: AppState) => {
return isCheckEnabled && checkResult?.success === false ? checkResult.error : null;
};

export const selectIsUnrecognizedFirmwareWithOutdatedSuite = (state: AppState): boolean => {
const device = selectDevice(state);
if (!isDeviceAcquired(device) || !device.authenticityChecks?.firmwareHash) return false;
const isUpdateAvailable = state.desktopUpdate.state === UpdateState.Available;
const checkResult = device.authenticityChecks.firmwareHash;

return !checkResult.success && checkResult.error === 'unknown-release' && isUpdateAvailable;
};

/**
* Determine hard failure of firmware hash check - specific error types which are severe.
* If check was skipped, don't consider it failed.
* If check is unsupported by device, a banner is shown but device is accessible.
*/
const selectIsFirmwareHashCheckEnabledAndFailed = (state: AppState): boolean => {
if (selectIsUnrecognizedFirmwareWithOutdatedSuite(state)) return false; // treat this as a soft failure

const error = selectFirmwareHashCheckError(state);
const softErrors: FirmwareHashCheckError[] = ['check-skipped', 'check-unsupported'];

return error !== null ? !softErrors.includes(error) : false;
return error !== null ? !isArrayMember(error, skippedHashCheckErrors) : false;
};

/**
Expand Down
5 changes: 0 additions & 5 deletions packages/suite/src/support/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7061,11 +7061,6 @@ export default defineMessages({
defaultMessage:
"Firmware hash check couldn't be performed. Your Trezor may be counterfeit.",
},
TR_DEVICE_FIRMWARE_UNRECOGNIZED_OUTDATED_SUITE: {
id: 'TR_DEVICE_FIRMWARE_UNRECOGNIZED_OUTDATED_SUITE',
defaultMessage:
'Firmware unrecognized. Please update your Trezor Suite to verify your Trezor.',
},
TR_ONBOARDING_COINS_STEP: {
id: 'TR_ONBOARDING_COINS_STEP',
defaultMessage: 'Activate coins',
Expand Down

0 comments on commit 0b3dc63

Please sign in to comment.