From 9fe9ccf343144033d54e9401f10dc6e1b697300c Mon Sep 17 00:00:00 2001 From: Ulisses Ferreira Date: Tue, 17 Nov 2026 00:39:39 +0000 Subject: [PATCH] chore(tron-wallet-snap): type caught error values MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `sanitizeSensitiveError` took `error: any` and read `error?.message` / `error?.stack` off it, and `SnapAssetsAdapter` destructured the `any` reason of a rejected `Promise.allSettled` entry — both made the error handling opaque to the type checker. - `sanitizeSensitiveError` now takes `unknown` and reads the two fields it inspects through a small `readErrorField` helper (`isObject` + `typeof` guard), so `message` / `stack` are `string` instead of `any`. The signature still returns `Error` because `PrepareErrorForTrackingFn` requires it, and non-sensitive values are still returned untouched (a `null` throw stays `null`, as the existing test asserts). - `SnapAssetsAdapter` re-checks `status === 'rejected'` instead of reusing the `isInactiveAccount` boolean, which is what narrows the settled result, and annotates the rejected reason and the `.catch` callback parameter as `unknown`. Clears 13 suppression entries (2 `no-unsafe-assignment` in `SnapAssetsAdapter`, plus the whole `sensitiveErrors` block: 2 assignment, 4 call, 4 member-access, 1 return) — 607 → 594 for `packages/tron-wallet-snap`. Verification: - `yarn oxlint packages/tron-wallet-snap` clean (suppressions pruned) - `tsc --project tsconfig.json --noEmit` clean - `yarn workspace @metamask/tron-wallet-snap run test` — 63 files / 1052 tests - `oxfmt --check` clean --- oxlint-suppressions.json | 19 ----------- .../assets/adapters/SnapAssetsAdapter.ts | 8 +++-- .../src/utils/sensitiveErrors.ts | 33 +++++++++++++++---- 3 files changed, 31 insertions(+), 29 deletions(-) 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; }