Store & submit spam reporting token from server.

This commit is contained in:
Nicholas
2023-02-02 10:03:56 -05:00
committed by Nicholas Tinsley
parent 6a8e82ef91
commit 72449fd73e
14 changed files with 128 additions and 21 deletions

View File

@@ -93,7 +93,8 @@ public class PushTable extends DatabaseTable {
cursor.getString(cursor.getColumnIndexOrThrow(SERVER_GUID)),
"",
true,
false);
false,
null);
}
} catch (IOException e) {
Log.w(TAG, e);
@@ -175,7 +176,8 @@ public class PushTable extends DatabaseTable {
serverGuid,
"",
true,
false);
false,
null);
} catch (IOException e) {
throw new AssertionError(e);
}

View File

@@ -186,6 +186,7 @@ open class RecipientTable(context: Context, databaseHelper: SignalDatabase) : Da
private const val NEEDS_PNI_SIGNATURE = "needs_pni_signature"
private const val UNREGISTERED_TIMESTAMP = "unregistered_timestamp"
private const val HIDDEN = "hidden"
const val REPORTING_TOKEN = "reporting_token"
@JvmField
val CREATE_TABLE =
@@ -246,7 +247,8 @@ open class RecipientTable(context: Context, databaseHelper: SignalDatabase) : Da
$DISTRIBUTION_LIST_ID INTEGER DEFAULT NULL,
$NEEDS_PNI_SIGNATURE INTEGER DEFAULT 0,
$UNREGISTERED_TIMESTAMP INTEGER DEFAULT 0,
$HIDDEN INTEGER DEFAULT 0
$HIDDEN INTEGER DEFAULT 0,
$REPORTING_TOKEN BLOB DEFAULT NULL
)
""".trimIndent()
@@ -308,7 +310,8 @@ open class RecipientTable(context: Context, databaseHelper: SignalDatabase) : Da
BADGES,
DISTRIBUTION_LIST_ID,
NEEDS_PNI_SIGNATURE,
HIDDEN
HIDDEN,
REPORTING_TOKEN
)
private val ID_PROJECTION = arrayOf(ID)
@@ -1721,6 +1724,31 @@ open class RecipientTable(context: Context, databaseHelper: SignalDatabase) : Da
return updated
}
fun setReportingToken(id: RecipientId, reportingToken: ByteArray) {
val values = ContentValues(1).apply {
put(REPORTING_TOKEN, reportingToken)
}
if (update(id, values)) {
ApplicationDependencies.getDatabaseObserver().notifyRecipientChanged(id)
}
}
fun getReportingToken(id: RecipientId): ByteArray? {
readableDatabase
.select(REPORTING_TOKEN)
.from(TABLE_NAME)
.where(ID_WHERE, id)
.run()
.use { cursor ->
if (cursor.moveToFirst()) {
return cursor.requireBlob(REPORTING_TOKEN)
} else {
return null
}
}
}
fun getSimilarRecipientIds(recipient: Recipient): List<RecipientId> {
val projection = SqlUtil.buildArgs(ID, "COALESCE(NULLIF($SYSTEM_JOINED_NAME, ''), NULLIF($PROFILE_JOINED_NAME, '')) AS checked_name")
val where = "checked_name = ? AND $HIDDEN = ?"

View File

@@ -33,6 +33,7 @@ import org.thoughtcrime.securesms.database.helpers.migration.V174_ReactionForeig
import org.thoughtcrime.securesms.database.helpers.migration.V175_FixFullTextSearchLink
import org.thoughtcrime.securesms.database.helpers.migration.V176_AddScheduledDateToQuoteIndex
import org.thoughtcrime.securesms.database.helpers.migration.V177_MessageSendLogTableCleanupMigration
import org.thoughtcrime.securesms.database.helpers.migration.V178_ReportingTokenColumnMigration
/**
* Contains all of the database migrations for [SignalDatabase]. Broken into a separate file for cleanliness.
@@ -41,7 +42,7 @@ object SignalDatabaseMigrations {
val TAG: String = Log.tag(SignalDatabaseMigrations.javaClass)
const val DATABASE_VERSION = 177
const val DATABASE_VERSION = 178
@JvmStatic
fun migrate(context: Application, db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
@@ -160,6 +161,10 @@ object SignalDatabaseMigrations {
if (oldVersion < 177) {
V177_MessageSendLogTableCleanupMigration.migrate(context, db, oldVersion, newVersion)
}
if (oldVersion < 178) {
V178_ReportingTokenColumnMigration.migrate(context, db, oldVersion, newVersion)
}
}
@JvmStatic

View File

@@ -0,0 +1,13 @@
package org.thoughtcrime.securesms.database.helpers.migration
import android.app.Application
import net.zetetic.database.sqlcipher.SQLiteDatabase
/**
* This adds a column to the Recipients table to store a spam reporting token.
*/
object V178_ReportingTokenColumnMigration : SignalDatabaseMigration {
override fun migrate(context: Application, db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
db.execSQL("ALTER TABLE recipient ADD COLUMN reporting_token BLOB DEFAULT NULL")
}
}

View File

@@ -24,6 +24,8 @@ import org.thoughtcrime.securesms.messages.MessageDecryptionUtil;
import org.thoughtcrime.securesms.messages.MessageDecryptionUtil.DecryptionResult;
import org.thoughtcrime.securesms.notifications.NotificationChannels;
import org.thoughtcrime.securesms.notifications.NotificationIds;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.RecipientId;
import org.thoughtcrime.securesms.transport.RetryLaterException;
import org.thoughtcrime.securesms.util.FeatureFlags;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
@@ -31,6 +33,7 @@ import org.whispersystems.signalservice.api.SignalServiceMessageSender;
import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope;
import org.whispersystems.signalservice.api.messages.SignalServicePniSignatureMessage;
import org.whispersystems.signalservice.api.push.PNI;
import org.whispersystems.signalservice.api.push.ServiceId;
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import java.util.LinkedList;
@@ -111,6 +114,10 @@ public final class PushDecryptMessageJob extends BaseJob {
if (result.getContent().getSenderKeyDistributionMessage().isPresent()) {
handleSenderKeyDistributionMessage(result.getContent().getSender(), result.getContent().getSenderDevice(), result.getContent().getSenderKeyDistributionMessage().get());
}
result.getContent();
if (envelope.hasReportingToken()) {
SignalDatabase.recipients().setReportingToken(RecipientId.from(result.getContent().getSender()), envelope.getReportingToken());
}
if (FeatureFlags.phoneNumberPrivacy() && result.getContent().getPniSignatureMessage().isPresent()) {
handlePniSignatureMessage(result.getContent().getSender(), result.getContent().getSenderDevice(), result.getContent().getPniSignatureMessage().get());

View File

@@ -11,13 +11,17 @@ import org.thoughtcrime.securesms.jobmanager.Job;
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint;
import org.thoughtcrime.securesms.keyvalue.SignalStore;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.RecipientId;
import org.thoughtcrime.securesms.util.Base64;
import org.whispersystems.signalservice.api.SignalServiceAccountManager;
import org.whispersystems.signalservice.api.push.ServiceId;
import org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException;
import org.whispersystems.signalservice.api.push.exceptions.ServerRejectedException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
@@ -68,17 +72,26 @@ public class ReportSpamJob extends BaseJob {
return;
}
int count = 0;
List<ReportSpamData> reportSpamData = SignalDatabase.messages().getReportSpamMessageServerData(threadId, timestamp, MAX_MESSAGE_COUNT);
SignalServiceAccountManager signalServiceAccountManager = ApplicationDependencies.getSignalServiceAccountManager();
int count = 0;
List<ReportSpamData> reportSpamData = SignalDatabase.messages().getReportSpamMessageServerData(threadId, timestamp, MAX_MESSAGE_COUNT);
SignalServiceAccountManager signalServiceAccountManager = ApplicationDependencies.getSignalServiceAccountManager();
for (ReportSpamData data : reportSpamData) {
Optional<ServiceId> serviceId = Recipient.resolved(data.getRecipientId()).getServiceId();
final RecipientId recipientId = data.getRecipientId();
Optional<ServiceId> serviceId = Recipient.resolved(recipientId).getServiceId();
if (serviceId.isPresent() && !serviceId.get().isUnknown()) {
signalServiceAccountManager.reportSpam(serviceId.get(), data.getServerGuid());
String reportingTokenEncoded = "";
byte[] reportingTokenBytes = SignalDatabase.recipients().getReportingToken(recipientId);
if (reportingTokenBytes != null) {
reportingTokenEncoded = Base64.encodeBytes(reportingTokenBytes);
}
signalServiceAccountManager.reportSpam(serviceId.get(), data.getServerGuid(), reportingTokenEncoded);
count++;
} else {
Log.w(TAG, "Unable to report spam without an ACI for " + data.getRecipientId());
Log.w(TAG, "Unable to report spam without an ACI for " + recipientId);
}
}
Log.i(TAG, "Reported " + count + " out of " + reportSpamData.size() + " messages in thread " + threadId + " as spam");