From 892943ddf957f310377b24d3d8a296eebc4ab42f Mon Sep 17 00:00:00 2001 From: ameba23 Date: Tue, 11 May 2021 10:59:05 +0200 Subject: [PATCH] Ignore old wipe messages --- .../briar/remotewipe/RemoteWipeConstants.java | 1 + .../remotewipe/RemoteWipeManagerImpl.java | 49 ++++++++++++------- .../remotewipe/RemoteWipeIntegrationTest.java | 7 +-- 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/briar-core/src/main/java/org/briarproject/briar/remotewipe/RemoteWipeConstants.java b/briar-core/src/main/java/org/briarproject/briar/remotewipe/RemoteWipeConstants.java index 2d4a5388c..b759387d9 100644 --- a/briar-core/src/main/java/org/briarproject/briar/remotewipe/RemoteWipeConstants.java +++ b/briar-core/src/main/java/org/briarproject/briar/remotewipe/RemoteWipeConstants.java @@ -3,6 +3,7 @@ package org.briarproject.briar.remotewipe; public interface RemoteWipeConstants { int THRESHOLD = 2; + long MAX_MESSAGE_AGE = 24 * 60 * 60 * 1000; // Group metadata keys String GROUP_KEY_CONTACT_ID = "contactId"; diff --git a/briar-core/src/main/java/org/briarproject/briar/remotewipe/RemoteWipeManagerImpl.java b/briar-core/src/main/java/org/briarproject/briar/remotewipe/RemoteWipeManagerImpl.java index 239a1d839..fb1e8544d 100644 --- a/briar-core/src/main/java/org/briarproject/briar/remotewipe/RemoteWipeManagerImpl.java +++ b/briar-core/src/main/java/org/briarproject/briar/remotewipe/RemoteWipeManagerImpl.java @@ -35,6 +35,7 @@ import org.briarproject.briar.client.ConversationClientImpl; import java.util.ArrayList; import java.util.Collection; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; @@ -49,6 +50,7 @@ import static org.briarproject.briar.remotewipe.RemoteWipeConstants.GROUP_KEY_CO import static org.briarproject.briar.remotewipe.RemoteWipeConstants.GROUP_KEY_RECEIVED_WIPE; import static org.briarproject.briar.remotewipe.RemoteWipeConstants.GROUP_KEY_WIPERS; +import static org.briarproject.briar.remotewipe.RemoteWipeConstants.MAX_MESSAGE_AGE; import static org.briarproject.briar.remotewipe.RemoteWipeConstants.MSG_KEY_LOCAL; import static org.briarproject.briar.remotewipe.RemoteWipeConstants.MSG_KEY_MESSAGE_TYPE; import static org.briarproject.briar.remotewipe.RemoteWipeConstants.MSG_KEY_TIMESTAMP; @@ -117,7 +119,7 @@ public class RemoteWipeManagerImpl extends ConversationClientImpl @Override protected boolean incomingMessage(Transaction txn, Message m, BdfList body, BdfDictionary meta) throws DbException, FormatException { - System.out.println("Incoming message called"); + System.out.println("Incoming remote wipe message"); MessageType type = MessageType.fromValue(body.getLong(0).intValue()); if (type == SETUP) { messageTracker.trackIncomingMessage(txn, m); @@ -136,19 +138,21 @@ public class RemoteWipeManagerImpl extends ConversationClientImpl if (receivedWipeMessages == null) receivedWipeMessages = new BdfList(); - for (int i = 0; i < receivedWipeMessages.size(); i++) { + // Traverse the list backwards to avoid problems when removing items + for (int i = receivedWipeMessages.size() -1; i >= 0; --i) { BdfList receivedWipeMessage = receivedWipeMessages.getList(i); - // If we already have one from this contact, ignore - if (receivedWipeMessage.getLong(0).intValue() == contactId.getInt()) { + + long timestamp = receivedWipeMessage.getLong(1); + System.out.println("Message age: " + (clock.currentTimeMillis() - timestamp)); + // Filter the messages for old ones + if (clock.currentTimeMillis() - timestamp > MAX_MESSAGE_AGE) { + System.out.println("Removing outdated wipe message"); + receivedWipeMessages.remove(i); + } else if (receivedWipeMessage.getLong(0).intValue() == contactId.getInt()) { + // If we already have one from this contact, ignore System.out.println("Duplicate wipe message received - ignoring"); return false; } - - // TODO filter the messages for old ones -// long timestamp = receivedWipeMessage.getLong(1); -// if (clock.currentTimeMillis() - timestamp > MAX_MESSAGE_AGE) { -// receivedWipeMessages.remove(i); -// } } if (receivedWipeMessages.size() + 1 == THRESHOLD) { @@ -161,7 +165,8 @@ public class RemoteWipeManagerImpl extends ConversationClientImpl } else { BdfList newReceivedWipeMessage = new BdfList(); newReceivedWipeMessage.add(contactId.getInt()); - newReceivedWipeMessage.add(1); // TODO this should be the timestamp + long timestamp = m.getTimestamp(); + newReceivedWipeMessage.add(timestamp); receivedWipeMessages.add(newReceivedWipeMessage); BdfDictionary newMeta = new BdfDictionary(); newMeta.put(GROUP_KEY_RECEIVED_WIPE, receivedWipeMessages); @@ -175,7 +180,7 @@ public class RemoteWipeManagerImpl extends ConversationClientImpl public void setup(Transaction txn, List wipers) throws DbException, FormatException { // If we already have a set of wipers do nothing - // TODO revoke existing wipers + // TODO revoke existing wipers who are not present in the new list if (remoteWipeIsSetup(txn)) throw new FormatException(); if (wipers.size() < 2) throw new FormatException(); @@ -213,7 +218,6 @@ public class RemoteWipeManagerImpl extends ConversationClientImpl byte[] body = messageEncoder.encodeSetupMessage(); Message m = clientHelper.createMessage(g, timestamp, body); - // TODO remote-wipe versions of MESSAGE_KEY BdfDictionary meta = BdfDictionary.of( new BdfEntry(MSG_KEY_MESSAGE_TYPE, SETUP.getValue()), new BdfEntry(MSG_KEY_LOCAL, true), @@ -238,11 +242,10 @@ public class RemoteWipeManagerImpl extends ConversationClientImpl byte[] body = messageEncoder.encodeWipeMessage(); Message m = clientHelper.createMessage(g, timestamp, body); - // TODO remote-wipe versions of MESSAGE_KEY BdfDictionary meta = BdfDictionary.of( - new BdfEntry(MSG_KEY_MESSAGE_TYPE, SETUP.getValue()), - new BdfEntry(MSG_KEY_LOCAL, true), - new BdfEntry(MSG_KEY_TIMESTAMP, timestamp) + new BdfEntry(MSG_KEY_TIMESTAMP, timestamp), + new BdfEntry(MSG_KEY_MESSAGE_TYPE, WIPE.getValue()), + new BdfEntry(MSG_KEY_LOCAL, true) ); clientHelper.addLocalMessage(txn, m, meta, true, false); messageTracker.trackOutgoingMessage(txn, m); @@ -285,7 +288,15 @@ public class RemoteWipeManagerImpl extends ConversationClientImpl MessageStatus status = db.getMessageStatus(txn, contactId, messageEntry.getKey()); headers.add( - createSetupMessageHeader(message, meta, status)); + createMessageHeader(message, meta, status)); + } else if (meta.getLong(MSG_KEY_MESSAGE_TYPE).intValue() == + WIPE.getValue()) { + Message message = clientHelper + .getMessage(txn, messageEntry.getKey()); + MessageStatus status = db.getMessageStatus(txn, contactId, + messageEntry.getKey()); + headers.add( + createMessageHeader(message, meta, status)); } } return headers; @@ -294,7 +305,7 @@ public class RemoteWipeManagerImpl extends ConversationClientImpl } } - private RemoteWipeMessageHeader createSetupMessageHeader( + private RemoteWipeMessageHeader createMessageHeader( Message message, BdfDictionary meta, MessageStatus status ) throws FormatException { diff --git a/briar-core/src/test/java/org/briarproject/briar/remotewipe/RemoteWipeIntegrationTest.java b/briar-core/src/test/java/org/briarproject/briar/remotewipe/RemoteWipeIntegrationTest.java index 02ea263da..3cc207413 100644 --- a/briar-core/src/test/java/org/briarproject/briar/remotewipe/RemoteWipeIntegrationTest.java +++ b/briar-core/src/test/java/org/briarproject/briar/remotewipe/RemoteWipeIntegrationTest.java @@ -121,11 +121,11 @@ public class RemoteWipeIntegrationTest extends BriarIntegrationTest messages1At0 = getMessages1At0(); - assertEquals(1, messages1At0.size()); + assertEquals(2, messages1At0.size()); Collection messages2At0 = getMessages2At0(); - assertEquals(1, messages2At0.size()); + assertEquals(2, messages2At0.size()); assertTrue(panicCalled); } @@ -178,7 +178,7 @@ public class RemoteWipeIntegrationTest extends BriarIntegrationTest messages1At0 = getMessages1At0(); - assertEquals(1, messages1At0.size()); + assertEquals(3, messages1At0.size()); Collection messages2At0 = getMessages2At0(); @@ -186,6 +186,7 @@ public class RemoteWipeIntegrationTest extends BriarIntegrationTest getMessages1At0() throws DbException { return db0.transactionWithResult(true, txn -> remoteWipeManager0