Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: safeguard utill/address functions for undefined arguments #7056

Merged
merged 13 commits into from
Sep 12, 2023
Merged
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
22 changes: 13 additions & 9 deletions app/components/Views/EditAccountName/EditAccountName.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,19 @@ const EditAccountName = () => {
navigate('WalletView');

InteractionManager.runAfterInteractions(() => {
const analyticsProperties = async () => {
const accountType = getAddressAccountType(selectedAddress);
const account_type = accountType === 'QR' ? 'hardware' : accountType;
return { account_type, chain_id: chainId };
};
Analytics.trackEventWithParameters(
MetaMetricsEvents.ACCOUNT_RENAMED,
analyticsProperties(),
);
try {
const analyticsProperties = async () => {
const accountType = getAddressAccountType(selectedAddress);
const account_type = accountType === 'QR' ? 'hardware' : accountType;
return { account_type, chain_id: chainId };
};
Analytics.trackEventWithParameters(
MetaMetricsEvents.ACCOUNT_RENAMED,
analyticsProperties(),
);
} catch {
return {};
}
});
};

Expand Down
19 changes: 18 additions & 1 deletion app/core/__mocks__/MockedEngine.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { KeyringTypes } from '@metamask/keyring-controller';

const engineModule = jest.requireActual('../../core/Engine');

const mockedEngine = {
Expand All @@ -12,7 +14,22 @@ const mockedEngine = {
},
],
},
state: {},
state: {
keyrings: [
{
accounts: ['0xd018538C87232FF95acbCe4870629b75640a78E7'],
type: KeyringTypes.simple,
},
{
accounts: ['0xB374Ca013934e498e5baD3409147F34E6c462389'],
type: KeyringTypes.qr,
},
{
accounts: ['0x71C7656EC7ab88b098defB751B7401B5f6d8976F'],
type: KeyringTypes.hd,
},
],
},
},
},
};
Expand Down
11 changes: 9 additions & 2 deletions app/util/address/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ export async function importAccountFromPrivateKey(private_key) {
* @returns {Boolean} - Returns a boolean
*/
export function isQRHardwareAccount(address) {
if (!isValidHexAddress(address)) return false;
cortisiko marked this conversation as resolved.
Show resolved Hide resolved

const { KeyringController } = Engine.context;
const { keyrings } = KeyringController.state;
const qrKeyrings = keyrings.filter(
Expand All @@ -171,6 +173,10 @@ export function isQRHardwareAccount(address) {
* @returns {String} - Returns address's account type
*/
export function getAddressAccountType(address) {
if (!isValidHexAddress(address)) {
throw new Error(`Invalid address: ${address}`);
cortisiko marked this conversation as resolved.
Show resolved Hide resolved
}

const { KeyringController } = Engine.context;
const { keyrings } = KeyringController.state;
const targetKeyring = keyrings.find((keyring) =>
Expand Down Expand Up @@ -223,10 +229,11 @@ export function isENS(name) {
/**
* Determines if a given string looks like a valid Ethereum address
*
* @param {address} string
* @param {string} address The 42 character Ethereum address composed of:
* 2 ('0x': 2 char hex prefix) + 20 (last 20 bytes of public key) * 2 (as each byte is 2 chars in ascii)
*/
export function resemblesAddress(address) {
return address.length === 2 + 20 * 2;
return address && address.length === 2 + 20 * 2;
cortisiko marked this conversation as resolved.
Show resolved Hide resolved
}

export function safeToChecksumAddress(address) {
Expand Down
62 changes: 62 additions & 0 deletions app/util/address/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import {
stripHexPrefix,
getAddress,
shouldShowBlockExplorer,
isQRHardwareAccount,
getAddressAccountType,
resemblesAddress,
} from '.';

describe('isENS', () => {
Expand Down Expand Up @@ -216,3 +219,62 @@ describe('shouldShowBlockExplorer', () => {
expect(result).toBe(undefined);
});
});
describe('isQRHardwareAccount', () => {
it('should return false if argument address is undefined', () => {
expect(isQRHardwareAccount(undefined as any)).toBeFalsy();
});
it('should return false if address does not exist on keyring', () => {
expect(isQRHardwareAccount('address-stub')).toBeFalsy();
});

it('should return false if address is from keyring type simple', () => {
expect(
isQRHardwareAccount('0xd018538C87232FF95acbCe4870629b75640a78E7'),
).toBeFalsy();
});
it('should return false if address is from keyring type hd', () => {
expect(
isQRHardwareAccount('0x71C7656EC7ab88b098defB751B7401B5f6d8976F'),
).toBeFalsy();
});
it('should return true if address is from keyring type qr', () => {
expect(
isQRHardwareAccount('0xB374Ca013934e498e5baD3409147F34E6c462389'),
).toBeTruthy();
});
});
describe('getAddressAccountType', () => {
it('should throw an error if argument address is undefined', () => {
expect(() => getAddressAccountType(undefined as any)).toThrow(
'Invalid address: undefined',
);
});
it('should return QR if address is from a keyring type qr', () => {
expect(
getAddressAccountType('0xB374Ca013934e498e5baD3409147F34E6c462389'),
).toBe('QR');
});
it('should return imported if address is from a keyring type simple', () => {
expect(
getAddressAccountType('0xd018538C87232FF95acbCe4870629b75640a78E7'),
).toBe('Imported');
});
it('should return MetaMask if address is not qr or simple', () => {
expect(
getAddressAccountType('0x71C7656EC7ab88b098defB751B7401B5f6d8976F'),
).toBe('MetaMask');
});
});
describe('resemblesAddress', () => {
it('should return false if argument address is undefined', () => {
expect(resemblesAddress(undefined as any)).toBeFalsy();
});
it('should return false if address does not resemble an eth address', () => {
expect(resemblesAddress('address-stub-1')).toBeFalsy();
});
it('should return true if address resemble an eth address', () => {
expect(
resemblesAddress('0x71C7656EC7ab88b098defB751B7401B5f6d8976F'),
).toBeTruthy();
});
});
Loading