Skip to content
Draft
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
19 changes: 0 additions & 19 deletions oxlint-suppressions.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
33 changes: 26 additions & 7 deletions packages/tron-wallet-snap/src/utils/sensitiveErrors.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
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.
*
* @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',
Expand All @@ -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),
);

Expand All @@ -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;
}
Loading