Skip to content

Commit

Permalink
Fix possible crash in ProfileKeySendJob if given an invalid threadId.
Browse files Browse the repository at this point in the history
  • Loading branch information
greyson-signal committed Jul 5, 2022
1 parent e413ee4 commit be2ed89
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 10 deletions.
Expand Up @@ -1153,7 +1153,6 @@ public long getOrCreateThreadIdFor(@NonNull Recipient recipient, int distributio
SQLiteDatabase db = databaseHelper.getSignalReadableDatabase();

try (Cursor cursor = db.query(TABLE_NAME, RECIPIENT_ID_PROJECTION, ID_WHERE, SqlUtil.buildArgs(threadId), null, null, null)) {

if (cursor != null && cursor.moveToFirst()) {
return RecipientId.from(cursor.getLong(cursor.getColumnIndexOrThrow(RECIPIENT_ID)));
}
Expand Down
Expand Up @@ -3,6 +3,7 @@
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.WorkerThread;

import com.annimon.stream.Stream;
Expand Down Expand Up @@ -45,13 +46,16 @@ public class ProfileKeySendJob extends BaseJob {
*
* @param queueLimits True if you only want one of these to be run per person after decryptions
* are drained, otherwise false.
*
* @return The job that is created, or null if the threadId provided was invalid.
*/
@WorkerThread
public static ProfileKeySendJob create(@NonNull Context context, long threadId, boolean queueLimits) {
public static @Nullable ProfileKeySendJob create(long threadId, boolean queueLimits) {
Recipient conversationRecipient = SignalDatabase.threads().getRecipientForThreadId(threadId);

if (conversationRecipient == null) {
throw new AssertionError("We have a thread but no recipient!");
Log.w(TAG, "Thread no longer valid! Aborting.");
return null;
}

if (conversationRecipient.isPushV2Group()) {
Expand Down
Expand Up @@ -144,7 +144,7 @@ void acceptMessageRequest(@NonNull LiveRecipient liveRecipient,
RecipientDatabase recipientDatabase = SignalDatabase.recipients();
recipientDatabase.setProfileSharing(liveRecipient.getId(), true);

MessageSender.sendProfileKey(context, threadId);
MessageSender.sendProfileKey(threadId);

List<MessageDatabase.MarkedMessageInfo> messageIds = SignalDatabase.threads().setEntireThreadRead(threadId);
ApplicationDependencies.getMessageNotifier().updateNotification(context);
Expand Down
Expand Up @@ -325,10 +325,14 @@ private void handleMessage(@NonNull SignalServiceContent content, long timestamp
.enqueue();
} else if (!threadRecipient.isGroup()) {
Log.i(TAG, "Message was to a 1:1. Ensuring this user has our profile key.");
ApplicationDependencies.getJobManager()
.startChain(new RefreshAttributesJob(false))
.then(ProfileKeySendJob.create(context, SignalDatabase.threads().getOrCreateThreadIdFor(threadRecipient), true))
.enqueue();
ProfileKeySendJob profileSendJob = ProfileKeySendJob.create(SignalDatabase.threads().getOrCreateThreadIdFor(threadRecipient), true);

if (profileSendJob != null) {
ApplicationDependencies.getJobManager()
.startChain(new RefreshAttributesJob(false))
.then(profileSendJob)
.enqueue();
}
}
}
}
Expand Down
Expand Up @@ -102,8 +102,11 @@ public class MessageSender {
* Suitable for a 1:1 conversation or a GV1 group only.
*/
@WorkerThread
public static void sendProfileKey(final Context context, final long threadId) {
ApplicationDependencies.getJobManager().add(ProfileKeySendJob.create(context, threadId, false));
public static void sendProfileKey(final long threadId) {
ProfileKeySendJob job = ProfileKeySendJob.create(threadId, false);
if (job != null) {
ApplicationDependencies.getJobManager().add(job);
}
}

public static long send(final Context context,
Expand Down

0 comments on commit be2ed89

Please sign in to comment.