diff --git a/oxlint-suppressions.json b/oxlint-suppressions.json index a3013c865..ff454de87 100644 --- a/oxlint-suppressions.json +++ b/oxlint-suppressions.json @@ -2131,11 +2131,6 @@ "count": 1 } }, - "packages/tron-wallet-snap/src/services/assets/adapters/SnapAssetsAdapter.ts": { - "typescript/no-unsafe-assignment": { - "count": 2 - } - }, "packages/tron-wallet-snap/src/services/confirmation/ConfirmationHandler.test.ts": { "typescript/no-unnecessary-type-assertion": { "count": 2 @@ -2412,20 +2407,6 @@ "count": 1 } }, - "packages/tron-wallet-snap/src/utils/sensitiveErrors.ts": { - "typescript/no-unsafe-assignment": { - "count": 2 - }, - "typescript/no-unsafe-call": { - "count": 4 - }, - "typescript/no-unsafe-member-access": { - "count": 4 - }, - "typescript/no-unsafe-return": { - "count": 1 - } - }, "packages/tron-wallet-snap/src/utils/transactionStatus.test.ts": { "typescript/no-unnecessary-type-assertion": { "count": 3 diff --git a/packages/tron-wallet-snap/src/services/assets/adapters/SnapAssetsAdapter.ts b/packages/tron-wallet-snap/src/services/assets/adapters/SnapAssetsAdapter.ts index 28222cfa1..46a0554cb 100644 --- a/packages/tron-wallet-snap/src/services/assets/adapters/SnapAssetsAdapter.ts +++ b/packages/tron-wallet-snap/src/services/assets/adapters/SnapAssetsAdapter.ts @@ -198,8 +198,10 @@ export class SnapAssetsAdapter { ]); const isInactiveAccount = tronAccountInfoRequest.status === 'rejected'; - if (isInactiveAccount) { - const { reason } = tronAccountInfoRequest; + if (tronAccountInfoRequest.status === 'rejected') { + // `PromiseRejectedResult['reason']` is `any`; the snap only needs to know + // whether it is the account-not-found error. + const reason: unknown = tronAccountInfoRequest.reason; if (!(reason instanceof TrongridAccountNotFoundError)) { throw new AssetFetchError('Failed to fetch account info', { cause: reason, @@ -215,7 +217,7 @@ export class SnapAssetsAdapter { const trc20BalancesFallback = isInactiveAccount ? await this.#trongridApiClient .getTrc20BalancesByAddress(scope, account.address) - .catch(async (error) => { + .catch(async (error: unknown) => { await this.#snapClient.trackError( new Error('Failed to fetch TRC20 balances for inactive account', { cause: error, diff --git a/packages/tron-wallet-snap/src/utils/sensitiveErrors.ts b/packages/tron-wallet-snap/src/utils/sensitiveErrors.ts index 939dbaf9b..8922582e1 100644 --- a/packages/tron-wallet-snap/src/utils/sensitiveErrors.ts +++ b/packages/tron-wallet-snap/src/utils/sensitiveErrors.ts @@ -1,7 +1,27 @@ import { isSnapRpcError } from '@metamask/snap-networks-utils'; +import { isObject } from '@metamask/utils'; export { isSnapRpcError }; +/** + * Reads a string field off a caught value without trusting its shape. + * + * @param value - The caught value. + * @param property - The field to read. + * @returns The field value, or `undefined` when it is absent or not a string. + */ +function readErrorField( + value: unknown, + property: 'message' | 'stack', +): string | undefined { + if (!isObject(value)) { + return undefined; + } + + const field = value[property]; + return typeof field === 'string' ? field : undefined; +} + /** * Sanitizes error messages that may contain sensitive cryptographic information. * This prevents leaking details about private keys, entropy, or derivation paths. @@ -9,11 +29,9 @@ export { isSnapRpcError }; * @param error - The error to sanitize. * @returns A sanitized error with generic message if sensitive info detected. */ -// TODO: Replace `any` with type -// eslint-disable-next-line @typescript-eslint/no-explicit-any -export function sanitizeSensitiveError(error: any): Error { - const message = error?.message?.toLowerCase() ?? ''; - const stack = error?.stack?.toLowerCase() ?? ''; +export function sanitizeSensitiveError(error: unknown): Error { + const message = readErrorField(error, 'message')?.toLowerCase() ?? ''; + const stack = readErrorField(error, 'stack')?.toLowerCase() ?? ''; const sensitiveKeywords = [ 'private', @@ -28,7 +46,6 @@ export function sanitizeSensitiveError(error: any): Error { ]; const containsSensitiveInfo = sensitiveKeywords.some( - // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing (keyword) => message.includes(keyword) || stack.includes(keyword), ); @@ -43,5 +60,7 @@ export function sanitizeSensitiveError(error: any): Error { return sanitizedError; } - return error; + // Values without sensitive keywords are rethrown untouched, including + // non-`Error` throwables. + return error as Error; }