From 61718192ee3b9d357f7ae86db5ca085776efd2df Mon Sep 17 00:00:00 2001 From: akwizgran Date: Mon, 23 Nov 2020 16:42:45 +0000 Subject: [PATCH] Factor out method for validating auto-delete timers. --- .../bramble/util/ValidationUtils.java | 11 +++++++ .../introduction/IntroductionValidator.java | 23 ++++++-------- .../messaging/PrivateMessageValidator.java | 15 +++++---- .../invitation/GroupInvitationValidator.java | 23 ++++++-------- .../briar/sharing/SharingValidator.java | 31 ++++++++----------- 5 files changed, 51 insertions(+), 52 deletions(-) diff --git a/bramble-api/src/main/java/org/briarproject/bramble/util/ValidationUtils.java b/bramble-api/src/main/java/org/briarproject/bramble/util/ValidationUtils.java index 1bb56a799..bc44a04d5 100644 --- a/bramble-api/src/main/java/org/briarproject/bramble/util/ValidationUtils.java +++ b/bramble-api/src/main/java/org/briarproject/bramble/util/ValidationUtils.java @@ -7,6 +7,10 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import javax.annotation.Nullable; +import static org.briarproject.bramble.api.autodelete.AutoDeleteConstants.MAX_AUTO_DELETE_TIMER_MS; +import static org.briarproject.bramble.api.autodelete.AutoDeleteConstants.MIN_AUTO_DELETE_TIMER_MS; +import static org.briarproject.bramble.api.autodelete.AutoDeleteConstants.NO_AUTO_DELETE_TIMER; + @NotNullByDefault public class ValidationUtils { @@ -69,4 +73,11 @@ public class ValidationUtils { throws FormatException { if (l != null && (l < min || l > max)) throw new FormatException(); } + + public static long validateAutoDeleteTimer(@Nullable Long timer) + throws FormatException { + if (timer == null) return NO_AUTO_DELETE_TIMER; + checkRange(timer, MIN_AUTO_DELETE_TIMER_MS, MAX_AUTO_DELETE_TIMER_MS); + return timer; + } } diff --git a/briar-core/src/main/java/org/briarproject/briar/introduction/IntroductionValidator.java b/briar-core/src/main/java/org/briarproject/briar/introduction/IntroductionValidator.java index 0112b4a23..a4c460bb5 100644 --- a/briar-core/src/main/java/org/briarproject/briar/introduction/IntroductionValidator.java +++ b/briar-core/src/main/java/org/briarproject/briar/introduction/IntroductionValidator.java @@ -15,19 +15,16 @@ import org.briarproject.bramble.api.sync.MessageId; import org.briarproject.bramble.api.system.Clock; import org.briarproject.briar.api.client.SessionId; -import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; import static java.util.Collections.singletonList; -import static org.briarproject.bramble.api.autodelete.AutoDeleteConstants.MAX_AUTO_DELETE_TIMER_MS; -import static org.briarproject.bramble.api.autodelete.AutoDeleteConstants.MIN_AUTO_DELETE_TIMER_MS; import static org.briarproject.bramble.api.autodelete.AutoDeleteConstants.NO_AUTO_DELETE_TIMER; import static org.briarproject.bramble.api.crypto.CryptoConstants.MAC_BYTES; import static org.briarproject.bramble.api.crypto.CryptoConstants.MAX_SIGNATURE_BYTES; import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH; import static org.briarproject.bramble.util.ValidationUtils.checkLength; -import static org.briarproject.bramble.util.ValidationUtils.checkRange; import static org.briarproject.bramble.util.ValidationUtils.checkSize; +import static org.briarproject.bramble.util.ValidationUtils.validateAutoDeleteTimer; import static org.briarproject.briar.api.introduction.IntroductionConstants.MAX_INTRODUCTION_TEXT_LENGTH; import static org.briarproject.briar.introduction.MessageType.ACCEPT; import static org.briarproject.briar.introduction.MessageType.ACTIVATE; @@ -88,7 +85,9 @@ class IntroductionValidator extends BdfMessageValidator { checkLength(text, 1, MAX_INTRODUCTION_TEXT_LENGTH); long timer = NO_AUTO_DELETE_TIMER; - if (body.size() == 5) timer = validateTimer(body.getOptionalLong(4)); + if (body.size() == 5) { + timer = validateAutoDeleteTimer(body.getOptionalLong(4)); + } BdfDictionary meta = messageEncoder.encodeRequestMetadata(m.getTimestamp(), timer); @@ -128,7 +127,9 @@ class IntroductionValidator extends BdfMessageValidator { .parseAndValidateTransportPropertiesMap(transportProperties); long timer = NO_AUTO_DELETE_TIMER; - if (body.size() == 7) timer = validateTimer(body.getOptionalLong(6)); + if (body.size() == 7) { + timer = validateAutoDeleteTimer(body.getOptionalLong(6)); + } SessionId sessionId = new SessionId(sessionIdBytes); BdfDictionary meta = messageEncoder.encodeMetadata(ACCEPT, sessionId, @@ -156,7 +157,9 @@ class IntroductionValidator extends BdfMessageValidator { checkLength(previousMessageId, UniqueId.LENGTH); long timer = NO_AUTO_DELETE_TIMER; - if (body.size() == 4) timer = validateTimer(body.getOptionalLong(3)); + if (body.size() == 4) { + timer = validateAutoDeleteTimer(body.getOptionalLong(3)); + } SessionId sessionId = new SessionId(sessionIdBytes); BdfDictionary meta = messageEncoder.encodeMetadata(type, sessionId, @@ -236,10 +239,4 @@ class IntroductionValidator extends BdfMessageValidator { return new BdfMessageContext(meta, singletonList(dependency)); } } - - private long validateTimer(@Nullable Long timer) throws FormatException { - if (timer == null) return NO_AUTO_DELETE_TIMER; - checkRange(timer, MIN_AUTO_DELETE_TIMER_MS, MAX_AUTO_DELETE_TIMER_MS); - return timer; - } } diff --git a/briar-core/src/main/java/org/briarproject/briar/messaging/PrivateMessageValidator.java b/briar-core/src/main/java/org/briarproject/briar/messaging/PrivateMessageValidator.java index 761e27842..14581eeb8 100644 --- a/briar-core/src/main/java/org/briarproject/briar/messaging/PrivateMessageValidator.java +++ b/briar-core/src/main/java/org/briarproject/briar/messaging/PrivateMessageValidator.java @@ -24,13 +24,12 @@ import java.io.InputStream; import javax.annotation.concurrent.Immutable; -import static org.briarproject.bramble.api.autodelete.AutoDeleteConstants.MAX_AUTO_DELETE_TIMER_MS; -import static org.briarproject.bramble.api.autodelete.AutoDeleteConstants.MIN_AUTO_DELETE_TIMER_MS; +import static org.briarproject.bramble.api.autodelete.AutoDeleteConstants.NO_AUTO_DELETE_TIMER; import static org.briarproject.bramble.api.sync.SyncConstants.MAX_MESSAGE_BODY_LENGTH; import static org.briarproject.bramble.api.transport.TransportConstants.MAX_CLOCK_DIFFERENCE; import static org.briarproject.bramble.util.ValidationUtils.checkLength; -import static org.briarproject.bramble.util.ValidationUtils.checkRange; import static org.briarproject.bramble.util.ValidationUtils.checkSize; +import static org.briarproject.bramble.util.ValidationUtils.validateAutoDeleteTimer; import static org.briarproject.briar.api.attachment.MediaConstants.MAX_CONTENT_TYPE_BYTES; import static org.briarproject.briar.api.attachment.MediaConstants.MSG_KEY_CONTENT_TYPE; import static org.briarproject.briar.api.attachment.MediaConstants.MSG_KEY_DESCRIPTOR_LENGTH; @@ -137,11 +136,9 @@ class PrivateMessageValidator implements MessageValidator { String contentType = header.getString(1); checkLength(contentType, 1, MAX_CONTENT_TYPE_BYTES); } - Long timer = null; + long timer = NO_AUTO_DELETE_TIMER; if (body.size() == 4) { - timer = body.getOptionalLong(3); - checkRange(timer, MIN_AUTO_DELETE_TIMER_MS, - MAX_AUTO_DELETE_TIMER_MS); + timer = validateAutoDeleteTimer(body.getOptionalLong(3)); } // Return the metadata BdfDictionary meta = new BdfDictionary(); @@ -151,7 +148,9 @@ class PrivateMessageValidator implements MessageValidator { meta.put(MSG_KEY_MSG_TYPE, PRIVATE_MESSAGE); meta.put(MSG_KEY_HAS_TEXT, text != null); meta.put(MSG_KEY_ATTACHMENT_HEADERS, headers); - if (timer != null) meta.put(MSG_KEY_AUTO_DELETE_TIMER, timer); + if (timer != NO_AUTO_DELETE_TIMER) { + meta.put(MSG_KEY_AUTO_DELETE_TIMER, timer); + } return new BdfMessageContext(meta); } diff --git a/briar-core/src/main/java/org/briarproject/briar/privategroup/invitation/GroupInvitationValidator.java b/briar-core/src/main/java/org/briarproject/briar/privategroup/invitation/GroupInvitationValidator.java index 0896ea6ac..7e3a6fb76 100644 --- a/briar-core/src/main/java/org/briarproject/briar/privategroup/invitation/GroupInvitationValidator.java +++ b/briar-core/src/main/java/org/briarproject/briar/privategroup/invitation/GroupInvitationValidator.java @@ -21,16 +21,13 @@ import org.briarproject.briar.api.privategroup.PrivateGroupFactory; import java.security.GeneralSecurityException; import java.util.Collections; -import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; -import static org.briarproject.bramble.api.autodelete.AutoDeleteConstants.MAX_AUTO_DELETE_TIMER_MS; -import static org.briarproject.bramble.api.autodelete.AutoDeleteConstants.MIN_AUTO_DELETE_TIMER_MS; import static org.briarproject.bramble.api.autodelete.AutoDeleteConstants.NO_AUTO_DELETE_TIMER; import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_SIGNATURE_LENGTH; import static org.briarproject.bramble.util.ValidationUtils.checkLength; -import static org.briarproject.bramble.util.ValidationUtils.checkRange; import static org.briarproject.bramble.util.ValidationUtils.checkSize; +import static org.briarproject.bramble.util.ValidationUtils.validateAutoDeleteTimer; import static org.briarproject.briar.api.privategroup.PrivateGroupConstants.GROUP_SALT_LENGTH; import static org.briarproject.briar.api.privategroup.PrivateGroupConstants.MAX_GROUP_INVITATION_TEXT_LENGTH; import static org.briarproject.briar.api.privategroup.PrivateGroupConstants.MAX_GROUP_NAME_LENGTH; @@ -91,7 +88,9 @@ class GroupInvitationValidator extends BdfMessageValidator { byte[] signature = body.getRaw(5); checkLength(signature, 1, MAX_SIGNATURE_LENGTH); long timer = NO_AUTO_DELETE_TIMER; - if (body.size() == 7) timer = validateTimer(body.getOptionalLong(6)); + if (body.size() == 7) { + timer = validateAutoDeleteTimer(body.getOptionalLong(6)); + } // Validate the creator and create the private group Author creator = clientHelper.parseAndValidateAuthor(creatorList); @@ -128,7 +127,9 @@ class GroupInvitationValidator extends BdfMessageValidator { byte[] previousMessageId = body.getOptionalRaw(2); checkLength(previousMessageId, UniqueId.LENGTH); long timer = NO_AUTO_DELETE_TIMER; - if (body.size() == 4) timer = validateTimer(body.getOptionalLong(3)); + if (body.size() == 4) { + timer = validateAutoDeleteTimer(body.getOptionalLong(3)); + } BdfDictionary meta = messageEncoder.encodeMetadata(JOIN, new GroupId(privateGroupId), m.getTimestamp(), false, false, @@ -154,7 +155,9 @@ class GroupInvitationValidator extends BdfMessageValidator { byte[] previousMessageId = body.getOptionalRaw(2); checkLength(previousMessageId, UniqueId.LENGTH); long timer = NO_AUTO_DELETE_TIMER; - if (body.size() == 4) timer = validateTimer(body.getOptionalLong(3)); + if (body.size() == 4) { + timer = validateAutoDeleteTimer(body.getOptionalLong(3)); + } BdfDictionary meta = messageEncoder.encodeMetadata(LEAVE, new GroupId(privateGroupId), m.getTimestamp(), false, false, @@ -178,10 +181,4 @@ class GroupInvitationValidator extends BdfMessageValidator { false, false, false, NO_AUTO_DELETE_TIMER); return new BdfMessageContext(meta); } - - private long validateTimer(@Nullable Long timer) throws FormatException { - if (timer == null) return NO_AUTO_DELETE_TIMER; - checkRange(timer, MIN_AUTO_DELETE_TIMER_MS, MAX_AUTO_DELETE_TIMER_MS); - return timer; - } } diff --git a/briar-core/src/main/java/org/briarproject/briar/sharing/SharingValidator.java b/briar-core/src/main/java/org/briarproject/briar/sharing/SharingValidator.java index f8cd581ea..76dc51118 100644 --- a/briar-core/src/main/java/org/briarproject/briar/sharing/SharingValidator.java +++ b/briar-core/src/main/java/org/briarproject/briar/sharing/SharingValidator.java @@ -15,16 +15,13 @@ import org.briarproject.bramble.api.sync.Message; import org.briarproject.bramble.api.sync.MessageId; import org.briarproject.bramble.api.system.Clock; -import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; import static java.util.Collections.singletonList; -import static org.briarproject.bramble.api.autodelete.AutoDeleteConstants.MAX_AUTO_DELETE_TIMER_MS; -import static org.briarproject.bramble.api.autodelete.AutoDeleteConstants.MIN_AUTO_DELETE_TIMER_MS; import static org.briarproject.bramble.api.autodelete.AutoDeleteConstants.NO_AUTO_DELETE_TIMER; import static org.briarproject.bramble.util.ValidationUtils.checkLength; -import static org.briarproject.bramble.util.ValidationUtils.checkRange; import static org.briarproject.bramble.util.ValidationUtils.checkSize; +import static org.briarproject.bramble.util.ValidationUtils.validateAutoDeleteTimer; import static org.briarproject.briar.api.sharing.SharingConstants.MAX_INVITATION_TEXT_LENGTH; import static org.briarproject.briar.sharing.MessageType.INVITE; @@ -49,10 +46,10 @@ abstract class SharingValidator extends BdfMessageValidator { return validateInviteMessage(m, body); case ACCEPT: case DECLINE: - return validateNonInviteMessageWithOptionalTimer(type, m, body); + return validateAcceptOrDeclineMessage(type, m, body); case LEAVE: case ABORT: - return validateNonInviteMessageWithoutTimer(type, m, body); + return validateLeaveOrAbortMessage(type, m, body); default: throw new FormatException(); } @@ -72,7 +69,9 @@ abstract class SharingValidator extends BdfMessageValidator { String text = body.getOptionalString(3); checkLength(text, 1, MAX_INVITATION_TEXT_LENGTH); long timer = NO_AUTO_DELETE_TIMER; - if (body.size() == 5) timer = validateTimer(body.getOptionalLong(4)); + if (body.size() == 5) { + timer = validateAutoDeleteTimer(body.getOptionalLong(4)); + } BdfDictionary meta = messageEncoder.encodeMetadata(INVITE, shareableId, m.getTimestamp(), false, false, false, false, false, timer); @@ -87,8 +86,8 @@ abstract class SharingValidator extends BdfMessageValidator { protected abstract GroupId validateDescriptor(BdfList descriptor) throws FormatException; - private BdfMessageContext validateNonInviteMessageWithoutTimer( - MessageType type, Message m, BdfList body) throws FormatException { + private BdfMessageContext validateLeaveOrAbortMessage(MessageType type, + Message m, BdfList body) throws FormatException { checkSize(body, 3); byte[] shareableId = body.getRaw(1); checkLength(shareableId, UniqueId.LENGTH); @@ -106,8 +105,8 @@ abstract class SharingValidator extends BdfMessageValidator { } } - private BdfMessageContext validateNonInviteMessageWithOptionalTimer( - MessageType type, Message m, BdfList body) throws FormatException { + private BdfMessageContext validateAcceptOrDeclineMessage(MessageType type, + Message m, BdfList body) throws FormatException { // Client version 0.0: Message type, shareable ID, optional previous // message ID. // Client version 0.1: Message type, shareable ID, optional previous @@ -118,7 +117,9 @@ abstract class SharingValidator extends BdfMessageValidator { byte[] previousMessageId = body.getOptionalRaw(2); checkLength(previousMessageId, UniqueId.LENGTH); long timer = NO_AUTO_DELETE_TIMER; - if (body.size() == 4) timer = validateTimer(body.getOptionalLong(3)); + if (body.size() == 4) { + timer = validateAutoDeleteTimer(body.getOptionalLong(3)); + } BdfDictionary meta = messageEncoder.encodeMetadata(type, new GroupId(shareableId), m.getTimestamp(), false, false, @@ -130,10 +131,4 @@ abstract class SharingValidator extends BdfMessageValidator { return new BdfMessageContext(meta, singletonList(dependency)); } } - - private long validateTimer(@Nullable Long timer) throws FormatException { - if (timer == null) return NO_AUTO_DELETE_TIMER; - checkRange(timer, MIN_AUTO_DELETE_TIMER_MS, MAX_AUTO_DELETE_TIMER_MS); - return timer; - } }