From 9d6cbce94ecb17e400ef25dbab4b500a06b65483 Mon Sep 17 00:00:00 2001 From: f-r00t Date: Sat, 1 Jun 2024 12:20:42 +0200 Subject: [PATCH] Added rudimentary spam filter --- src/Database.js | 53 ++++++++++++++++++++++++++++++++++++++++--- src/Globals.js | 9 +++++++- src/HuginUtilities.js | 7 ++++-- 3 files changed, 63 insertions(+), 6 deletions(-) diff --git a/src/Database.js b/src/Database.js index 43235d0..886e9f3 100644 --- a/src/Database.js +++ b/src/Database.js @@ -529,7 +529,7 @@ export async function openDB() { await createTables(database); } catch (err) { - Globals.logger.addLogMessage('Failed to open DB: ' + err); + Globals.logger.addLogMessage('Failed to open DB: ' + JSON.stringify(err)); } } @@ -2177,6 +2177,8 @@ export async function blockUser(address, nickname) { ) console.log('Added to block list', address) + Globals.blockList.push(address); + Globals.logger.addLogMessage('Blocked users: ' + Globals.blockList.join(' ,')); removeGroupMessagesFromUser(address); } @@ -2195,7 +2197,7 @@ export async function getBlockList() { for (let i = 0; i < data.rows.length; i++) { const item = data.rows.item(i); - timestamp = item.timestamp; + blockList.push(item.address); } @@ -2212,8 +2214,53 @@ export async function unBlockUsers (address) { `DELETE FROM blocklist WHERE - address = ${address}`); + address = "${address}"`); console.log('Removed from block list', address) +} + + +export async function isSpam (address, message, timestamp) { + + const length = message.length; + const an_hour_ago = Date.now() - (60*60*1000); + + if (length < 10) { + const [data] = await database.executeSql( + ` + SELECT hash + FROM privateboards_messages_db D + WHERE message = "${message}" + AND address = "${address}" + AND timestamp > ${an_hour_ago} + `); + + if (data && data.rows && data.rows.length) { + return true; + } else { + return false; + } + }; + + const partLength = Math.ceil(length / 3); + + const part1 = message.slice(0, partLength); + const part2 = message.slice(partLength, partLength * 2); + const part3 = message.slice(partLength * 2); + + const [data] = await database.executeSql( + ` + SELECT hash + FROM privateboards_messages_db D + WHERE message LIKE "%${part1}%" OR message LIKE "%${part2}%" OR message LIKE "%${part3}%" + AND timestamp > ${an_hour_ago} + `); + + if (data && data.rows && data.rows.length) { + return true; + } else { + return false; + } + } \ No newline at end of file diff --git a/src/Globals.js b/src/Globals.js index a61d013..3cb49ed 100644 --- a/src/Globals.js +++ b/src/Globals.js @@ -15,7 +15,7 @@ import NetInfo from "@react-native-community/netinfo"; import { deleteUserPinCode } from '@haskkor/react-native-pincode'; import { getBlockList, getLastSync, setHaveWallet, openDB, deleteDB, getKnownTransactions, getUnreadMessages, getGroupMessages, saveGroupToDatabase, removeMessages, loadPayeeDataFromDatabase, savePayeeToDatabase, removePayeeFromDatabase, -loadTransactionDetailsFromDatabase, saveTransactionDetailsToDatabase, removeGroupFromDatabase, getMessages, getLatestMessages, getBoardsMessages, getBoardSubscriptions, loadGroupsDataFromDatabase } from './Database'; +loadTransactionDetailsFromDatabase, saveTransactionDetailsToDatabase, removeGroupFromDatabase, getMessages, getLatestMessages, getBoardsMessages, getBoardSubscriptions, loadGroupsDataFromDatabase, unBlockUsers } from './Database'; import Config from './Config'; import { Logger } from './Logger'; import { getCoinPriceFromAPI } from './Currency'; @@ -453,6 +453,13 @@ export async function initGlobals() { Globals.lastMessageTimestamp = lastSync.lastSyncGroup; + await unBlockUsers('SEKReWcYe3X1bVh2Cv3YfY6nosjm1ZDhAG5y6jwMbPofEUUwi5Kgt6dM8z1tJjuHe6AASFqViHxBxM92x1oJ71WgRT4aL4XGbC8'); + await unBlockUsers('SEKReTbT3Kr1YAJdGT7HudX9nG7iq99vpbxgWKhHgg3G4daZpnp5zxfeMpQpigJKAf8Fu4RYybyQpSz2rGj4TPDGePE4oUkP4Sp'); + await unBlockUsers('SEKReXtKuFcEfMuvf9SY4nFYVz6KifongWRt3Kuvo5TfU1yDh5DGFYoCNVAVhE6oUZ83GVhpQg8XJg14w8Tzb5XdRJH5iz4VVdE'); + await unBlockUsers('SEKReUabHDKVWqE7mtYjQeY2mMQYq49U8TQYzEQ2KidDd7fwtmWMhorMhHoXChpikL7c8rYi2AAyTS4SVxWfvkK18ku4zVd8zFv'); + Globals.blockList = await getBlockList(); + Globals.logger.addLogMessage('Blocked users: ' + Globals.blockList.join(' ,')); + } diff --git a/src/HuginUtilities.js b/src/HuginUtilities.js index 21d1638..292242d 100644 --- a/src/HuginUtilities.js +++ b/src/HuginUtilities.js @@ -27,7 +27,7 @@ import * as NaclSealed from 'tweetnacl-sealed-box'; import Identicon from 'identicon.js'; -import {setLastSyncGroup, setLastSyncDM, emptyKnownTXs, updateGroupMessage, updateMessage, savePreferencesToDatabase, getGroupName, saveGroupMessage, groupMessageExists, getGroupKey, getLatestGroupMessage, getHistory, getLatestMessages, saveToDatabase, loadPayeeDataFromDatabase, saveMessage, saveBoardsMessage, savePayeeToDatabase, messageExists, getLatestMessage, saveKnownTransaction } from './Database'; +import {setLastSyncGroup, setLastSyncDM, emptyKnownTXs, updateGroupMessage, updateMessage, savePreferencesToDatabase, getGroupName, saveGroupMessage, groupMessageExists, getGroupKey, getLatestGroupMessage, getHistory, getLatestMessages, saveToDatabase, loadPayeeDataFromDatabase, saveMessage, saveBoardsMessage, savePayeeToDatabase, messageExists, getLatestMessage, saveKnownTransaction, isSpam } from './Database'; /** * Save wallet in background @@ -883,8 +883,11 @@ async function getGroupMessage(tx) { Globals.logger.addLogMessage('[Message sync] New message found: ' + payload_json.m.slice(0,10)); const from = payload_json.k; - if (Globals.blockList.some(a => a === from)) return false; + const spam = await isSpam(from, payload_json.m, tx.t); + if (spam) return false; + + if (Globals.blockList.some(a => a === from)) return false; const from_myself = (from == Globals.wallet.getPrimaryAddress() ? true : false);