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: validate asset id from deeplinks #5973

Merged
merged 3 commits into from Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/shared/lib/auxiliary/deep-link/errors/index.ts
@@ -1,5 +1,6 @@
export * from './amount-not-an-integer.error'
export * from './invalid-address.error'
export * from './invalid-asset-id.error'
export * from './metadata-length.error'
export * from './no-address-specified.error'
export * from './tag-length.error'
Expand Down
@@ -0,0 +1,14 @@
import { BaseError } from '@core/error'
import { localize } from '@core/i18n'

export class InvalidAssetIdError extends BaseError {
constructor() {
const message = localize('error.send.invalidAssetId')
super({
message,
showNotification: true,
saveToErrorLog: false,
logToConsole: true,
})
}
}
@@ -1,7 +1,7 @@
import { get } from 'svelte/store'

import { networkHrp } from '@core/network'
import { isStringTrue, isValidBech32AddressAndPrefix, getByteLengthOfString } from '@core/utils'
import { isStringTrue, isValidBech32AddressAndPrefix, getByteLengthOfString, validateAssetId } from '@core/utils'
import {
getAssetById,
NewTransactionDetails,
Expand Down Expand Up @@ -60,6 +60,7 @@ function parseSendConfirmationOperation(searchParams: URLSearchParams): NewTrans
const recipient: Subject = { type: 'address', address }

const assetId = searchParams.get(SendOperationParameter.AssetId)
assetId && validateAssetId(assetId)
const baseAsset = get(selectedAccountAssets).baseCoin
const asset = assetId ? getAssetById(assetId) : baseAsset
if (!asset) {
Expand Down
1 change: 1 addition & 0 deletions packages/shared/lib/core/utils/crypto/utils/index.ts
@@ -1,2 +1,3 @@
export * from './validateAssetId'
export * from './validateBech32Address'
export * from './validateEthereumAddress'
11 changes: 11 additions & 0 deletions packages/shared/lib/core/utils/crypto/utils/validateAssetId.ts
@@ -0,0 +1,11 @@
import { InvalidAssetIdError } from '@auxiliary/deep-link'
import { COIN_TYPE } from '@core/network'

export function validateAssetId(id: string): void {
const isHex = id.startsWith('0x')
if (isHex && !/^(0x08)?[0-9a-f]{64}?(?:0[1-9]|[1-5][0-9]|6[0-4])?0{8}$/i.test(id)) {
throw new InvalidAssetIdError()
} else if (!isHex && !Object.values(COIN_TYPE).includes(Number(id))) {
throw new InvalidAssetIdError()
}
}
1 change: 1 addition & 0 deletions packages/shared/locales/en.json
Expand Up @@ -1825,6 +1825,7 @@
"wrongAddressPrefix": "Addresses start with the prefix {prefix}.",
"wrongAddressFormat": "The address is not correctly formatted.",
"invalidAddress": "The address is not valid.",
"invalidAssetId": "The asset id is not valid.",
"unknownAsset": "The asset is not known to this account.",
"insufficientFunds": "This wallet has insufficient funds.",
"insufficientFundsStorageDeposit": "Insufficient funds to cover the storage deposit",
Expand Down