Unverified Commit 5bb8e95b authored by akwizgran's avatar akwizgran
Browse files

Pass original message to BDF validators and hooks.

parent 9e40d0be
......@@ -29,8 +29,9 @@ public abstract class BdfIncomingMessageHook implements IncomingMessageHook,
this.metadataParser = metadataParser;
}
protected abstract void incomingMessage(Transaction txn, BdfList message,
BdfDictionary meta) throws DbException, FormatException;
protected abstract void incomingMessage(Transaction txn, Message m,
BdfList body, BdfDictionary meta) throws DbException,
FormatException;
@Override
public void incomingMessage(Transaction txn, Message m, Metadata meta)
......@@ -48,10 +49,10 @@ public abstract class BdfIncomingMessageHook implements IncomingMessageHook,
int headerLength) throws DbException {
try {
byte[] raw = m.getRaw();
BdfList message = clientHelper.toList(raw, headerLength,
BdfList body = clientHelper.toList(raw, headerLength,
raw.length - headerLength);
BdfDictionary metaDictionary = metadataParser.parse(meta);
incomingMessage(txn, message, metaDictionary);
incomingMessage(txn, m, body, metaDictionary);
} catch (FormatException e) {
throw new DbException(e);
}
......
......@@ -37,8 +37,8 @@ public abstract class BdfMessageValidator implements MessageValidator,
this.clock = clock;
}
protected abstract BdfDictionary validateMessage(BdfList message, Group g,
long timestamp) throws FormatException;
protected abstract BdfDictionary validateMessage(Message m, Group g,
BdfList body) throws FormatException;
@Override
public Metadata validateMessage(Message m, Group g) {
......@@ -63,9 +63,9 @@ public abstract class BdfMessageValidator implements MessageValidator,
return null;
}
try {
BdfList message = clientHelper.toList(raw, headerLength,
BdfList body = clientHelper.toList(raw, headerLength,
raw.length - headerLength);
BdfDictionary meta = validateMessage(message, g, m.getTimestamp());
BdfDictionary meta = validateMessage(m, g, body);
if (meta == null) {
LOG.info("Invalid message");
return null;
......
......@@ -6,6 +6,7 @@ import org.briarproject.api.data.BdfDictionary;
import org.briarproject.api.data.BdfList;
import org.briarproject.api.data.MetadataEncoder;
import org.briarproject.api.sync.Group;
import org.briarproject.api.sync.Message;
import org.briarproject.api.system.Clock;
import org.briarproject.clients.BdfMessageValidator;
......@@ -20,15 +21,15 @@ class ForumListValidator extends BdfMessageValidator {
}
@Override
protected BdfDictionary validateMessage(BdfList message, Group g,
long timestamp) throws FormatException {
protected BdfDictionary validateMessage(Message m, Group g,
BdfList body) throws FormatException {
// Version, forum list
checkSize(message, 2);
checkSize(body, 2);
// Version
long version = message.getLong(0);
long version = body.getLong(0);
if (version < 0) throw new FormatException();
// Forum list
BdfList forumList = message.getList(1);
BdfList forumList = body.getList(1);
for (int i = 0; i < forumList.size(); i++) {
BdfList forum = forumList.getList(i);
// Name, salt
......
......@@ -13,6 +13,7 @@ import org.briarproject.api.data.MetadataEncoder;
import org.briarproject.api.identity.Author;
import org.briarproject.api.identity.AuthorFactory;
import org.briarproject.api.sync.Group;
import org.briarproject.api.sync.Message;
import org.briarproject.api.system.Clock;
import org.briarproject.clients.BdfMessageValidator;
......@@ -38,16 +39,16 @@ class ForumPostValidator extends BdfMessageValidator {
}
@Override
protected BdfDictionary validateMessage(BdfList message, Group g,
long timestamp) throws FormatException {
protected BdfDictionary validateMessage(Message m, Group g,
BdfList body) throws FormatException {
// Parent ID, author, content type, forum post body, signature
checkSize(message, 5);
checkSize(body, 5);
// Parent ID is optional
byte[] parent = message.getOptionalRaw(0);
byte[] parent = body.getOptionalRaw(0);
checkLength(parent, UniqueId.LENGTH);
// Author is optional
Author author = null;
BdfList authorList = message.getOptionalList(1);
BdfList authorList = body.getOptionalList(1);
if (authorList != null) {
// Name, public key
checkSize(authorList, 2);
......@@ -58,13 +59,13 @@ class ForumPostValidator extends BdfMessageValidator {
author = authorFactory.createAuthor(name, publicKey);
}
// Content type
String contentType = message.getString(2);
String contentType = body.getString(2);
checkLength(contentType, 0, MAX_CONTENT_TYPE_LENGTH);
// Forum post body
byte[] body = message.getRaw(3);
checkLength(body, 0, MAX_FORUM_POST_BODY_LENGTH);
byte[] forumPostBody = body.getRaw(3);
checkLength(forumPostBody, 0, MAX_FORUM_POST_BODY_LENGTH);
// Signature is optional
byte[] sig = message.getOptionalRaw(4);
byte[] sig = body.getOptionalRaw(4);
checkLength(sig, 0, MAX_SIGNATURE_LENGTH);
// If there's an author there must be a signature and vice versa
if (author != null && sig == null) {
......@@ -82,7 +83,7 @@ class ForumPostValidator extends BdfMessageValidator {
KeyParser keyParser = crypto.getSignatureKeyParser();
PublicKey key = keyParser.parsePublicKey(author.getPublicKey());
// Serialise the data to be signed
BdfList signed = BdfList.of(g.getId(), timestamp, parent,
BdfList signed = BdfList.of(g.getId(), m.getTimestamp(), parent,
authorList, contentType, body);
// Verify the signature
Signature signature = crypto.getSignature();
......@@ -99,7 +100,7 @@ class ForumPostValidator extends BdfMessageValidator {
}
// Return the metadata
BdfDictionary meta = new BdfDictionary();
meta.put("timestamp", timestamp);
meta.put("timestamp", m.getTimestamp());
if (parent != null) meta.put("parent", parent);
if (author != null) {
BdfDictionary authorMeta = new BdfDictionary();
......
......@@ -7,6 +7,7 @@ import org.briarproject.api.data.BdfDictionary;
import org.briarproject.api.data.BdfList;
import org.briarproject.api.data.MetadataEncoder;
import org.briarproject.api.sync.Group;
import org.briarproject.api.sync.Message;
import org.briarproject.api.system.Clock;
import org.briarproject.clients.BdfMessageValidator;
......@@ -21,22 +22,22 @@ class PrivateMessageValidator extends BdfMessageValidator {
}
@Override
protected BdfDictionary validateMessage(BdfList message, Group g,
long timestamp) throws FormatException {
protected BdfDictionary validateMessage(Message m, Group g,
BdfList body) throws FormatException {
// Parent ID, content type, private message body
checkSize(message, 3);
checkSize(body, 3);
// Parent ID is optional
byte[] parentId = message.getOptionalRaw(0);
byte[] parentId = body.getOptionalRaw(0);
checkLength(parentId, UniqueId.LENGTH);
// Content type
String contentType = message.getString(1);
String contentType = body.getString(1);
checkLength(contentType, 0, MAX_CONTENT_TYPE_LENGTH);
// Private message body
byte[] body = message.getRaw(2);
checkLength(body, 0, MAX_PRIVATE_MESSAGE_BODY_LENGTH);
byte[] privateMessageBody = body.getRaw(2);
checkLength(privateMessageBody, 0, MAX_PRIVATE_MESSAGE_BODY_LENGTH);
// Return the metadata
BdfDictionary meta = new BdfDictionary();
meta.put("timestamp", timestamp);
meta.put("timestamp", m.getTimestamp());
if (parentId != null) meta.put("parent", parentId);
meta.put("contentType", contentType);
meta.put("local", false);
......
......@@ -7,6 +7,7 @@ import org.briarproject.api.data.BdfDictionary;
import org.briarproject.api.data.BdfList;
import org.briarproject.api.data.MetadataEncoder;
import org.briarproject.api.sync.Group;
import org.briarproject.api.sync.Message;
import org.briarproject.api.system.Clock;
import org.briarproject.clients.BdfMessageValidator;
......@@ -22,21 +23,21 @@ class TransportPropertyValidator extends BdfMessageValidator {
}
@Override
protected BdfDictionary validateMessage(BdfList message, Group g,
long timestamp) throws FormatException {
protected BdfDictionary validateMessage(Message m, Group g,
BdfList body) throws FormatException {
// Device ID, transport ID, version, properties
checkSize(message, 4);
checkSize(body, 4);
// Device ID
byte[] deviceId = message.getRaw(0);
byte[] deviceId = body.getRaw(0);
checkLength(deviceId, UniqueId.LENGTH);
// Transport ID
String transportId = message.getString(1);
String transportId = body.getString(1);
checkLength(transportId, 1, MAX_TRANSPORT_ID_LENGTH);
// Version
long version = message.getLong(2);
long version = body.getLong(2);
if (version < 0) throw new FormatException();
// Properties
BdfDictionary dictionary = message.getDictionary(3);
BdfDictionary dictionary = body.getDictionary(3);
checkSize(dictionary, 0, MAX_PROPERTIES_PER_TRANSPORT);
for (String key : dictionary.keySet()) {
checkLength(key, 0, MAX_PROPERTY_LENGTH);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment