mirror of
https://github.com/zhigang1992/wallet.git
synced 2026-03-23 17:44:10 +08:00
fix: filter out urls and spam words from token names, closes #4017
This commit is contained in:
26
src/app/common/utils/spam-filter.spec.ts
Normal file
26
src/app/common/utils/spam-filter.spec.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { spamFilter, spamReplacement } from '@app/common/utils/spam-filter';
|
||||
|
||||
describe('Spam filter', () => {
|
||||
it('should allow valid tokens', () => {
|
||||
expect(spamFilter('This token name is OK')).not.toEqual(spamReplacement);
|
||||
});
|
||||
it('should detect spam urls in strings and replace content', () => {
|
||||
expect(spamFilter('www.fake')).toEqual(spamReplacement);
|
||||
expect(spamFilter('https://www.fake.com')).toEqual(spamReplacement);
|
||||
expect(spamFilter('fake.com')).toEqual(spamReplacement);
|
||||
expect(spamFilter('https://www.fake')).toEqual(spamReplacement);
|
||||
expect(spamFilter('http://www.fake')).toEqual(spamReplacement);
|
||||
expect(spamFilter('ftp://fake.com')).toEqual(spamReplacement);
|
||||
expect(spamFilter('https://fake.com')).toEqual(spamReplacement);
|
||||
expect(spamFilter('http://fake.com')).toEqual(spamReplacement);
|
||||
expect(spamFilter('https://fake')).toEqual(spamReplacement);
|
||||
expect(spamFilter('http://fake')).toEqual(spamReplacement);
|
||||
});
|
||||
it('should detect spam words in strings and replace content', () => {
|
||||
expect(spamFilter('You won some stx')).toEqual(spamReplacement);
|
||||
expect(spamFilter('You Win some stx')).toEqual(spamReplacement);
|
||||
expect(spamFilter('You Won some stx')).toEqual(spamReplacement);
|
||||
expect(spamFilter('click here for some stx')).toEqual(spamReplacement);
|
||||
expect(spamFilter('Click here for some stx')).toEqual(spamReplacement);
|
||||
});
|
||||
});
|
||||
24
src/app/common/utils/spam-filter.ts
Normal file
24
src/app/common/utils/spam-filter.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
const urlRegex =
|
||||
/(http|https|ftp)|(((http|ftp|https):\/\/)?(((http|ftp|https):\/\/)?(([\w.-]*)\.([\w]*))))/g;
|
||||
const spamWords = ['won', 'win', 'click'];
|
||||
export const spamReplacement = 'Unknown token';
|
||||
|
||||
function spamUrlFilter(input: string) {
|
||||
return input.match(urlRegex);
|
||||
}
|
||||
|
||||
function spamWordFilter(input: string): boolean {
|
||||
const containsSpam = (element: string) => input.toLowerCase().includes(element);
|
||||
return spamWords.some(containsSpam);
|
||||
}
|
||||
|
||||
export function spamFilter(input: string): string {
|
||||
const urlFound = spamUrlFilter(input);
|
||||
const spamWordsFound = spamWordFilter(input);
|
||||
|
||||
if (urlFound || spamWordsFound) {
|
||||
return spamReplacement;
|
||||
}
|
||||
|
||||
return input;
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import { forwardRefWithAs } from '@stacks/ui-core';
|
||||
import type { AllCryptoCurrencyAssetBalances } from '@shared/models/crypto-asset-balance.model';
|
||||
|
||||
import { useAnalytics } from '@app/common/hooks/analytics/use-analytics';
|
||||
import { spamFilter } from '@app/common/utils/spam-filter';
|
||||
|
||||
import { AssetItemCopyIcon } from './asset-copy-icon';
|
||||
import { CryptoCurrencyAssetItemLayout } from './crypto-currency-asset-item.layout';
|
||||
@@ -70,7 +71,7 @@ export const CryptoCurrencyAssetItem = forwardRefWithAs(
|
||||
copyIcon={canCopy ? <AssetItemCopyIcon hasCopied={hasCopied} /> : undefined}
|
||||
isPressable={isPressable}
|
||||
ref={ref}
|
||||
title={asset.name}
|
||||
title={spamFilter(asset.name)}
|
||||
isHovered={isHovered}
|
||||
address={address}
|
||||
usdBalance={usdBalance}
|
||||
|
||||
@@ -111,7 +111,6 @@ export function useGenerateFtTokenTransferUnsignedTx(
|
||||
return useCallback(
|
||||
async (values?: StacksSendFormValues | StacksTransactionFormValues) => {
|
||||
if (!assetTransferState || !account) return;
|
||||
|
||||
const {
|
||||
assetBalance,
|
||||
network,
|
||||
|
||||
Reference in New Issue
Block a user