mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 12:49:55 +01:00
Merge branch '1240-no-empty-messages' into 'master'
Don't allow empty message bodies See merge request briar/briar!949
This commit is contained in:
@@ -48,12 +48,8 @@ public abstract class BdfMessageValidator implements MessageValidator {
|
|||||||
throw new InvalidMessageException(
|
throw new InvalidMessageException(
|
||||||
"Timestamp is too far in the future");
|
"Timestamp is too far in the future");
|
||||||
}
|
}
|
||||||
byte[] body = m.getBody();
|
|
||||||
if (body.length == 0) {
|
|
||||||
throw new InvalidMessageException("Message is too short");
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
BdfList bodyList = clientHelper.toList(body);
|
BdfList bodyList = clientHelper.toList(m.getBody());
|
||||||
BdfMessageContext result = validateMessage(m, g, bodyList);
|
BdfMessageContext result = validateMessage(m, g, bodyList);
|
||||||
Metadata meta = metadataEncoder.encode(result.getDictionary());
|
Metadata meta = metadataEncoder.encode(result.getDictionary());
|
||||||
return new MessageContext(meta, result.getDependencies());
|
return new MessageContext(meta, result.getDependencies());
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ public class Message {
|
|||||||
private final byte[] body;
|
private final byte[] body;
|
||||||
|
|
||||||
public Message(MessageId id, GroupId groupId, long timestamp, byte[] body) {
|
public Message(MessageId id, GroupId groupId, long timestamp, byte[] body) {
|
||||||
|
if (body.length == 0) throw new IllegalArgumentException();
|
||||||
if (body.length > MAX_MESSAGE_BODY_LENGTH)
|
if (body.length > MAX_MESSAGE_BODY_LENGTH)
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
this.id = id;
|
this.id = id;
|
||||||
|
|||||||
@@ -1508,7 +1508,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
rs.close();
|
rs.close();
|
||||||
ps.close();
|
ps.close();
|
||||||
if (raw == null) throw new MessageDeletedException();
|
if (raw == null) throw new MessageDeletedException();
|
||||||
if (raw.length < MESSAGE_HEADER_LENGTH) throw new AssertionError();
|
if (raw.length <= MESSAGE_HEADER_LENGTH) throw new AssertionError();
|
||||||
byte[] body = new byte[raw.length - MESSAGE_HEADER_LENGTH];
|
byte[] body = new byte[raw.length - MESSAGE_HEADER_LENGTH];
|
||||||
System.arraycopy(raw, MESSAGE_HEADER_LENGTH, body, 0, body.length);
|
System.arraycopy(raw, MESSAGE_HEADER_LENGTH, body, 0, body.length);
|
||||||
return new Message(m, g, timestamp, body);
|
return new Message(m, g, timestamp, body);
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ class MessageFactoryImpl implements MessageFactory {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Message createMessage(GroupId g, long timestamp, byte[] body) {
|
public Message createMessage(GroupId g, long timestamp, byte[] body) {
|
||||||
|
if (body.length == 0) throw new IllegalArgumentException();
|
||||||
if (body.length > MAX_MESSAGE_BODY_LENGTH)
|
if (body.length > MAX_MESSAGE_BODY_LENGTH)
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
MessageId id = getMessageId(g, timestamp, body);
|
MessageId id = getMessageId(g, timestamp, body);
|
||||||
@@ -54,7 +55,7 @@ class MessageFactoryImpl implements MessageFactory {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Message createMessage(byte[] raw) {
|
public Message createMessage(byte[] raw) {
|
||||||
if (raw.length < MESSAGE_HEADER_LENGTH)
|
if (raw.length <= MESSAGE_HEADER_LENGTH)
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
if (raw.length > MAX_MESSAGE_LENGTH)
|
if (raw.length > MAX_MESSAGE_LENGTH)
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
|
|||||||
@@ -124,7 +124,8 @@ class SyncRecordReaderImpl implements SyncRecordReader {
|
|||||||
if (!hasMessage()) throw new FormatException();
|
if (!hasMessage()) throw new FormatException();
|
||||||
if (nextRecord == null) throw new AssertionError();
|
if (nextRecord == null) throw new AssertionError();
|
||||||
byte[] payload = nextRecord.getPayload();
|
byte[] payload = nextRecord.getPayload();
|
||||||
if (payload.length < MESSAGE_HEADER_LENGTH) throw new FormatException();
|
if (payload.length <= MESSAGE_HEADER_LENGTH)
|
||||||
|
throw new FormatException();
|
||||||
// Validate timestamp
|
// Validate timestamp
|
||||||
long timestamp = ByteUtils.readUint64(payload, UniqueId.LENGTH);
|
long timestamp = ByteUtils.readUint64(payload, UniqueId.LENGTH);
|
||||||
if (timestamp < 0) throw new FormatException();
|
if (timestamp < 0) throw new FormatException();
|
||||||
|
|||||||
@@ -79,18 +79,6 @@ public class BdfMessageValidatorTest extends ValidatorTestCase {
|
|||||||
assertSame(meta, messageContext.getMetadata());
|
assertSame(meta, messageContext.getMetadata());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = InvalidMessageException.class)
|
|
||||||
public void testRejectsTooShortMessage() throws Exception {
|
|
||||||
Message invalidMessage = getMessage(groupId, 0);
|
|
||||||
|
|
||||||
context.checking(new Expectations() {{
|
|
||||||
oneOf(clock).currentTimeMillis();
|
|
||||||
will(returnValue(timestamp));
|
|
||||||
}});
|
|
||||||
|
|
||||||
failIfSubclassIsCalled.validateMessage(invalidMessage, group);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAcceptsMinLengthMessage() throws Exception {
|
public void testAcceptsMinLengthMessage() throws Exception {
|
||||||
Message shortMessage = getMessage(groupId, 1);
|
Message shortMessage = getMessage(groupId, 1);
|
||||||
|
|||||||
Reference in New Issue
Block a user