Factor out method for validating auto-delete timers.

This commit is contained in:
akwizgran
2020-11-23 16:42:45 +00:00
committed by Torsten Grote
parent 2ae1e9631f
commit b34e6ee2a7
5 changed files with 51 additions and 52 deletions

View File

@@ -7,6 +7,10 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import javax.annotation.Nullable; 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 @NotNullByDefault
public class ValidationUtils { public class ValidationUtils {
@@ -69,4 +73,11 @@ public class ValidationUtils {
throws FormatException { throws FormatException {
if (l != null && (l < min || l > max)) throw new 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;
}
} }

View File

@@ -15,19 +15,16 @@ import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.bramble.api.system.Clock; import org.briarproject.bramble.api.system.Clock;
import org.briarproject.briar.api.client.SessionId; import org.briarproject.briar.api.client.SessionId;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable; import javax.annotation.concurrent.Immutable;
import static java.util.Collections.singletonList; 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.autodelete.AutoDeleteConstants.NO_AUTO_DELETE_TIMER;
import static org.briarproject.bramble.api.crypto.CryptoConstants.MAC_BYTES; 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.crypto.CryptoConstants.MAX_SIGNATURE_BYTES;
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH; 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.checkLength;
import static org.briarproject.bramble.util.ValidationUtils.checkRange;
import static org.briarproject.bramble.util.ValidationUtils.checkSize; 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.api.introduction.IntroductionConstants.MAX_INTRODUCTION_TEXT_LENGTH;
import static org.briarproject.briar.introduction.MessageType.ACCEPT; import static org.briarproject.briar.introduction.MessageType.ACCEPT;
import static org.briarproject.briar.introduction.MessageType.ACTIVATE; import static org.briarproject.briar.introduction.MessageType.ACTIVATE;
@@ -88,7 +85,9 @@ class IntroductionValidator extends BdfMessageValidator {
checkLength(text, 1, MAX_INTRODUCTION_TEXT_LENGTH); checkLength(text, 1, MAX_INTRODUCTION_TEXT_LENGTH);
long timer = NO_AUTO_DELETE_TIMER; 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 = BdfDictionary meta =
messageEncoder.encodeRequestMetadata(m.getTimestamp(), timer); messageEncoder.encodeRequestMetadata(m.getTimestamp(), timer);
@@ -128,7 +127,9 @@ class IntroductionValidator extends BdfMessageValidator {
.parseAndValidateTransportPropertiesMap(transportProperties); .parseAndValidateTransportPropertiesMap(transportProperties);
long timer = NO_AUTO_DELETE_TIMER; 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); SessionId sessionId = new SessionId(sessionIdBytes);
BdfDictionary meta = messageEncoder.encodeMetadata(ACCEPT, sessionId, BdfDictionary meta = messageEncoder.encodeMetadata(ACCEPT, sessionId,
@@ -156,7 +157,9 @@ class IntroductionValidator extends BdfMessageValidator {
checkLength(previousMessageId, UniqueId.LENGTH); checkLength(previousMessageId, UniqueId.LENGTH);
long timer = NO_AUTO_DELETE_TIMER; 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); SessionId sessionId = new SessionId(sessionIdBytes);
BdfDictionary meta = messageEncoder.encodeMetadata(type, sessionId, BdfDictionary meta = messageEncoder.encodeMetadata(type, sessionId,
@@ -236,10 +239,4 @@ class IntroductionValidator extends BdfMessageValidator {
return new BdfMessageContext(meta, singletonList(dependency)); 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;
}
} }

View File

@@ -24,13 +24,12 @@ import java.io.InputStream;
import javax.annotation.concurrent.Immutable; 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.NO_AUTO_DELETE_TIMER;
import static org.briarproject.bramble.api.autodelete.AutoDeleteConstants.MIN_AUTO_DELETE_TIMER_MS;
import static org.briarproject.bramble.api.sync.SyncConstants.MAX_MESSAGE_BODY_LENGTH; 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.api.transport.TransportConstants.MAX_CLOCK_DIFFERENCE;
import static org.briarproject.bramble.util.ValidationUtils.checkLength; 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.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.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_CONTENT_TYPE;
import static org.briarproject.briar.api.attachment.MediaConstants.MSG_KEY_DESCRIPTOR_LENGTH; 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); String contentType = header.getString(1);
checkLength(contentType, 1, MAX_CONTENT_TYPE_BYTES); checkLength(contentType, 1, MAX_CONTENT_TYPE_BYTES);
} }
Long timer = null; long timer = NO_AUTO_DELETE_TIMER;
if (body.size() == 4) { if (body.size() == 4) {
timer = body.getOptionalLong(3); timer = validateAutoDeleteTimer(body.getOptionalLong(3));
checkRange(timer, MIN_AUTO_DELETE_TIMER_MS,
MAX_AUTO_DELETE_TIMER_MS);
} }
// Return the metadata // Return the metadata
BdfDictionary meta = new BdfDictionary(); BdfDictionary meta = new BdfDictionary();
@@ -151,7 +148,9 @@ class PrivateMessageValidator implements MessageValidator {
meta.put(MSG_KEY_MSG_TYPE, PRIVATE_MESSAGE); meta.put(MSG_KEY_MSG_TYPE, PRIVATE_MESSAGE);
meta.put(MSG_KEY_HAS_TEXT, text != null); meta.put(MSG_KEY_HAS_TEXT, text != null);
meta.put(MSG_KEY_ATTACHMENT_HEADERS, headers); 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); return new BdfMessageContext(meta);
} }

View File

@@ -21,16 +21,13 @@ import org.briarproject.briar.api.privategroup.PrivateGroupFactory;
import java.security.GeneralSecurityException; import java.security.GeneralSecurityException;
import java.util.Collections; import java.util.Collections;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable; 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.autodelete.AutoDeleteConstants.NO_AUTO_DELETE_TIMER;
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_SIGNATURE_LENGTH; 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.checkLength;
import static org.briarproject.bramble.util.ValidationUtils.checkRange;
import static org.briarproject.bramble.util.ValidationUtils.checkSize; 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.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_INVITATION_TEXT_LENGTH;
import static org.briarproject.briar.api.privategroup.PrivateGroupConstants.MAX_GROUP_NAME_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); byte[] signature = body.getRaw(5);
checkLength(signature, 1, MAX_SIGNATURE_LENGTH); checkLength(signature, 1, MAX_SIGNATURE_LENGTH);
long timer = NO_AUTO_DELETE_TIMER; 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 // Validate the creator and create the private group
Author creator = clientHelper.parseAndValidateAuthor(creatorList); Author creator = clientHelper.parseAndValidateAuthor(creatorList);
@@ -128,7 +127,9 @@ class GroupInvitationValidator extends BdfMessageValidator {
byte[] previousMessageId = body.getOptionalRaw(2); byte[] previousMessageId = body.getOptionalRaw(2);
checkLength(previousMessageId, UniqueId.LENGTH); checkLength(previousMessageId, UniqueId.LENGTH);
long timer = NO_AUTO_DELETE_TIMER; 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, BdfDictionary meta = messageEncoder.encodeMetadata(JOIN,
new GroupId(privateGroupId), m.getTimestamp(), false, false, new GroupId(privateGroupId), m.getTimestamp(), false, false,
@@ -154,7 +155,9 @@ class GroupInvitationValidator extends BdfMessageValidator {
byte[] previousMessageId = body.getOptionalRaw(2); byte[] previousMessageId = body.getOptionalRaw(2);
checkLength(previousMessageId, UniqueId.LENGTH); checkLength(previousMessageId, UniqueId.LENGTH);
long timer = NO_AUTO_DELETE_TIMER; 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, BdfDictionary meta = messageEncoder.encodeMetadata(LEAVE,
new GroupId(privateGroupId), m.getTimestamp(), false, false, new GroupId(privateGroupId), m.getTimestamp(), false, false,
@@ -178,10 +181,4 @@ class GroupInvitationValidator extends BdfMessageValidator {
false, false, false, NO_AUTO_DELETE_TIMER); false, false, false, NO_AUTO_DELETE_TIMER);
return new BdfMessageContext(meta); 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;
}
} }

View File

@@ -15,16 +15,13 @@ import org.briarproject.bramble.api.sync.Message;
import org.briarproject.bramble.api.sync.MessageId; import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.bramble.api.system.Clock; import org.briarproject.bramble.api.system.Clock;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable; import javax.annotation.concurrent.Immutable;
import static java.util.Collections.singletonList; 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.autodelete.AutoDeleteConstants.NO_AUTO_DELETE_TIMER;
import static org.briarproject.bramble.util.ValidationUtils.checkLength; 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.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.api.sharing.SharingConstants.MAX_INVITATION_TEXT_LENGTH;
import static org.briarproject.briar.sharing.MessageType.INVITE; import static org.briarproject.briar.sharing.MessageType.INVITE;
@@ -49,10 +46,10 @@ abstract class SharingValidator extends BdfMessageValidator {
return validateInviteMessage(m, body); return validateInviteMessage(m, body);
case ACCEPT: case ACCEPT:
case DECLINE: case DECLINE:
return validateNonInviteMessageWithOptionalTimer(type, m, body); return validateAcceptOrDeclineMessage(type, m, body);
case LEAVE: case LEAVE:
case ABORT: case ABORT:
return validateNonInviteMessageWithoutTimer(type, m, body); return validateLeaveOrAbortMessage(type, m, body);
default: default:
throw new FormatException(); throw new FormatException();
} }
@@ -72,7 +69,9 @@ abstract class SharingValidator extends BdfMessageValidator {
String text = body.getOptionalString(3); String text = body.getOptionalString(3);
checkLength(text, 1, MAX_INVITATION_TEXT_LENGTH); checkLength(text, 1, MAX_INVITATION_TEXT_LENGTH);
long timer = NO_AUTO_DELETE_TIMER; 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, BdfDictionary meta = messageEncoder.encodeMetadata(INVITE, shareableId,
m.getTimestamp(), false, false, false, false, false, timer); m.getTimestamp(), false, false, false, false, false, timer);
@@ -87,8 +86,8 @@ abstract class SharingValidator extends BdfMessageValidator {
protected abstract GroupId validateDescriptor(BdfList descriptor) protected abstract GroupId validateDescriptor(BdfList descriptor)
throws FormatException; throws FormatException;
private BdfMessageContext validateNonInviteMessageWithoutTimer( private BdfMessageContext validateLeaveOrAbortMessage(MessageType type,
MessageType type, Message m, BdfList body) throws FormatException { Message m, BdfList body) throws FormatException {
checkSize(body, 3); checkSize(body, 3);
byte[] shareableId = body.getRaw(1); byte[] shareableId = body.getRaw(1);
checkLength(shareableId, UniqueId.LENGTH); checkLength(shareableId, UniqueId.LENGTH);
@@ -106,8 +105,8 @@ abstract class SharingValidator extends BdfMessageValidator {
} }
} }
private BdfMessageContext validateNonInviteMessageWithOptionalTimer( private BdfMessageContext validateAcceptOrDeclineMessage(MessageType type,
MessageType type, Message m, BdfList body) throws FormatException { Message m, BdfList body) throws FormatException {
// Client version 0.0: Message type, shareable ID, optional previous // Client version 0.0: Message type, shareable ID, optional previous
// message ID. // message ID.
// Client version 0.1: Message type, shareable ID, optional previous // Client version 0.1: Message type, shareable ID, optional previous
@@ -118,7 +117,9 @@ abstract class SharingValidator extends BdfMessageValidator {
byte[] previousMessageId = body.getOptionalRaw(2); byte[] previousMessageId = body.getOptionalRaw(2);
checkLength(previousMessageId, UniqueId.LENGTH); checkLength(previousMessageId, UniqueId.LENGTH);
long timer = NO_AUTO_DELETE_TIMER; 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, BdfDictionary meta = messageEncoder.encodeMetadata(type,
new GroupId(shareableId), m.getTimestamp(), false, false, new GroupId(shareableId), m.getTimestamp(), false, false,
@@ -130,10 +131,4 @@ abstract class SharingValidator extends BdfMessageValidator {
return new BdfMessageContext(meta, singletonList(dependency)); 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;
}
} }