Pass original message to BDF validators and hooks.

This commit is contained in:
akwizgran
2016-03-08 17:03:22 +00:00
parent 9e40d0be16
commit 5bb8e95b26
6 changed files with 44 additions and 39 deletions

View File

@@ -29,8 +29,9 @@ public abstract class BdfIncomingMessageHook implements IncomingMessageHook,
this.metadataParser = metadataParser; this.metadataParser = metadataParser;
} }
protected abstract void incomingMessage(Transaction txn, BdfList message, protected abstract void incomingMessage(Transaction txn, Message m,
BdfDictionary meta) throws DbException, FormatException; BdfList body, BdfDictionary meta) throws DbException,
FormatException;
@Override @Override
public void incomingMessage(Transaction txn, Message m, Metadata meta) public void incomingMessage(Transaction txn, Message m, Metadata meta)
@@ -48,10 +49,10 @@ public abstract class BdfIncomingMessageHook implements IncomingMessageHook,
int headerLength) throws DbException { int headerLength) throws DbException {
try { try {
byte[] raw = m.getRaw(); byte[] raw = m.getRaw();
BdfList message = clientHelper.toList(raw, headerLength, BdfList body = clientHelper.toList(raw, headerLength,
raw.length - headerLength); raw.length - headerLength);
BdfDictionary metaDictionary = metadataParser.parse(meta); BdfDictionary metaDictionary = metadataParser.parse(meta);
incomingMessage(txn, message, metaDictionary); incomingMessage(txn, m, body, metaDictionary);
} catch (FormatException e) { } catch (FormatException e) {
throw new DbException(e); throw new DbException(e);
} }

View File

@@ -37,8 +37,8 @@ public abstract class BdfMessageValidator implements MessageValidator,
this.clock = clock; this.clock = clock;
} }
protected abstract BdfDictionary validateMessage(BdfList message, Group g, protected abstract BdfDictionary validateMessage(Message m, Group g,
long timestamp) throws FormatException; BdfList body) throws FormatException;
@Override @Override
public Metadata validateMessage(Message m, Group g) { public Metadata validateMessage(Message m, Group g) {
@@ -63,9 +63,9 @@ public abstract class BdfMessageValidator implements MessageValidator,
return null; return null;
} }
try { try {
BdfList message = clientHelper.toList(raw, headerLength, BdfList body = clientHelper.toList(raw, headerLength,
raw.length - headerLength); raw.length - headerLength);
BdfDictionary meta = validateMessage(message, g, m.getTimestamp()); BdfDictionary meta = validateMessage(m, g, body);
if (meta == null) { if (meta == null) {
LOG.info("Invalid message"); LOG.info("Invalid message");
return null; return null;

View File

@@ -6,6 +6,7 @@ import org.briarproject.api.data.BdfDictionary;
import org.briarproject.api.data.BdfList; import org.briarproject.api.data.BdfList;
import org.briarproject.api.data.MetadataEncoder; import org.briarproject.api.data.MetadataEncoder;
import org.briarproject.api.sync.Group; import org.briarproject.api.sync.Group;
import org.briarproject.api.sync.Message;
import org.briarproject.api.system.Clock; import org.briarproject.api.system.Clock;
import org.briarproject.clients.BdfMessageValidator; import org.briarproject.clients.BdfMessageValidator;
@@ -20,15 +21,15 @@ class ForumListValidator extends BdfMessageValidator {
} }
@Override @Override
protected BdfDictionary validateMessage(BdfList message, Group g, protected BdfDictionary validateMessage(Message m, Group g,
long timestamp) throws FormatException { BdfList body) throws FormatException {
// Version, forum list // Version, forum list
checkSize(message, 2); checkSize(body, 2);
// Version // Version
long version = message.getLong(0); long version = body.getLong(0);
if (version < 0) throw new FormatException(); if (version < 0) throw new FormatException();
// Forum list // Forum list
BdfList forumList = message.getList(1); BdfList forumList = body.getList(1);
for (int i = 0; i < forumList.size(); i++) { for (int i = 0; i < forumList.size(); i++) {
BdfList forum = forumList.getList(i); BdfList forum = forumList.getList(i);
// Name, salt // Name, salt

View File

@@ -13,6 +13,7 @@ import org.briarproject.api.data.MetadataEncoder;
import org.briarproject.api.identity.Author; import org.briarproject.api.identity.Author;
import org.briarproject.api.identity.AuthorFactory; import org.briarproject.api.identity.AuthorFactory;
import org.briarproject.api.sync.Group; import org.briarproject.api.sync.Group;
import org.briarproject.api.sync.Message;
import org.briarproject.api.system.Clock; import org.briarproject.api.system.Clock;
import org.briarproject.clients.BdfMessageValidator; import org.briarproject.clients.BdfMessageValidator;
@@ -38,16 +39,16 @@ class ForumPostValidator extends BdfMessageValidator {
} }
@Override @Override
protected BdfDictionary validateMessage(BdfList message, Group g, protected BdfDictionary validateMessage(Message m, Group g,
long timestamp) throws FormatException { BdfList body) throws FormatException {
// Parent ID, author, content type, forum post body, signature // Parent ID, author, content type, forum post body, signature
checkSize(message, 5); checkSize(body, 5);
// Parent ID is optional // Parent ID is optional
byte[] parent = message.getOptionalRaw(0); byte[] parent = body.getOptionalRaw(0);
checkLength(parent, UniqueId.LENGTH); checkLength(parent, UniqueId.LENGTH);
// Author is optional // Author is optional
Author author = null; Author author = null;
BdfList authorList = message.getOptionalList(1); BdfList authorList = body.getOptionalList(1);
if (authorList != null) { if (authorList != null) {
// Name, public key // Name, public key
checkSize(authorList, 2); checkSize(authorList, 2);
@@ -58,13 +59,13 @@ class ForumPostValidator extends BdfMessageValidator {
author = authorFactory.createAuthor(name, publicKey); author = authorFactory.createAuthor(name, publicKey);
} }
// Content type // Content type
String contentType = message.getString(2); String contentType = body.getString(2);
checkLength(contentType, 0, MAX_CONTENT_TYPE_LENGTH); checkLength(contentType, 0, MAX_CONTENT_TYPE_LENGTH);
// Forum post body // Forum post body
byte[] body = message.getRaw(3); byte[] forumPostBody = body.getRaw(3);
checkLength(body, 0, MAX_FORUM_POST_BODY_LENGTH); checkLength(forumPostBody, 0, MAX_FORUM_POST_BODY_LENGTH);
// Signature is optional // Signature is optional
byte[] sig = message.getOptionalRaw(4); byte[] sig = body.getOptionalRaw(4);
checkLength(sig, 0, MAX_SIGNATURE_LENGTH); checkLength(sig, 0, MAX_SIGNATURE_LENGTH);
// If there's an author there must be a signature and vice versa // If there's an author there must be a signature and vice versa
if (author != null && sig == null) { if (author != null && sig == null) {
@@ -82,7 +83,7 @@ class ForumPostValidator extends BdfMessageValidator {
KeyParser keyParser = crypto.getSignatureKeyParser(); KeyParser keyParser = crypto.getSignatureKeyParser();
PublicKey key = keyParser.parsePublicKey(author.getPublicKey()); PublicKey key = keyParser.parsePublicKey(author.getPublicKey());
// Serialise the data to be signed // 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); authorList, contentType, body);
// Verify the signature // Verify the signature
Signature signature = crypto.getSignature(); Signature signature = crypto.getSignature();
@@ -99,7 +100,7 @@ class ForumPostValidator extends BdfMessageValidator {
} }
// Return the metadata // Return the metadata
BdfDictionary meta = new BdfDictionary(); BdfDictionary meta = new BdfDictionary();
meta.put("timestamp", timestamp); meta.put("timestamp", m.getTimestamp());
if (parent != null) meta.put("parent", parent); if (parent != null) meta.put("parent", parent);
if (author != null) { if (author != null) {
BdfDictionary authorMeta = new BdfDictionary(); BdfDictionary authorMeta = new BdfDictionary();

View File

@@ -7,6 +7,7 @@ import org.briarproject.api.data.BdfDictionary;
import org.briarproject.api.data.BdfList; import org.briarproject.api.data.BdfList;
import org.briarproject.api.data.MetadataEncoder; import org.briarproject.api.data.MetadataEncoder;
import org.briarproject.api.sync.Group; import org.briarproject.api.sync.Group;
import org.briarproject.api.sync.Message;
import org.briarproject.api.system.Clock; import org.briarproject.api.system.Clock;
import org.briarproject.clients.BdfMessageValidator; import org.briarproject.clients.BdfMessageValidator;
@@ -21,22 +22,22 @@ class PrivateMessageValidator extends BdfMessageValidator {
} }
@Override @Override
protected BdfDictionary validateMessage(BdfList message, Group g, protected BdfDictionary validateMessage(Message m, Group g,
long timestamp) throws FormatException { BdfList body) throws FormatException {
// Parent ID, content type, private message body // Parent ID, content type, private message body
checkSize(message, 3); checkSize(body, 3);
// Parent ID is optional // Parent ID is optional
byte[] parentId = message.getOptionalRaw(0); byte[] parentId = body.getOptionalRaw(0);
checkLength(parentId, UniqueId.LENGTH); checkLength(parentId, UniqueId.LENGTH);
// Content type // Content type
String contentType = message.getString(1); String contentType = body.getString(1);
checkLength(contentType, 0, MAX_CONTENT_TYPE_LENGTH); checkLength(contentType, 0, MAX_CONTENT_TYPE_LENGTH);
// Private message body // Private message body
byte[] body = message.getRaw(2); byte[] privateMessageBody = body.getRaw(2);
checkLength(body, 0, MAX_PRIVATE_MESSAGE_BODY_LENGTH); checkLength(privateMessageBody, 0, MAX_PRIVATE_MESSAGE_BODY_LENGTH);
// Return the metadata // Return the metadata
BdfDictionary meta = new BdfDictionary(); BdfDictionary meta = new BdfDictionary();
meta.put("timestamp", timestamp); meta.put("timestamp", m.getTimestamp());
if (parentId != null) meta.put("parent", parentId); if (parentId != null) meta.put("parent", parentId);
meta.put("contentType", contentType); meta.put("contentType", contentType);
meta.put("local", false); meta.put("local", false);

View File

@@ -7,6 +7,7 @@ import org.briarproject.api.data.BdfDictionary;
import org.briarproject.api.data.BdfList; import org.briarproject.api.data.BdfList;
import org.briarproject.api.data.MetadataEncoder; import org.briarproject.api.data.MetadataEncoder;
import org.briarproject.api.sync.Group; import org.briarproject.api.sync.Group;
import org.briarproject.api.sync.Message;
import org.briarproject.api.system.Clock; import org.briarproject.api.system.Clock;
import org.briarproject.clients.BdfMessageValidator; import org.briarproject.clients.BdfMessageValidator;
@@ -22,21 +23,21 @@ class TransportPropertyValidator extends BdfMessageValidator {
} }
@Override @Override
protected BdfDictionary validateMessage(BdfList message, Group g, protected BdfDictionary validateMessage(Message m, Group g,
long timestamp) throws FormatException { BdfList body) throws FormatException {
// Device ID, transport ID, version, properties // Device ID, transport ID, version, properties
checkSize(message, 4); checkSize(body, 4);
// Device ID // Device ID
byte[] deviceId = message.getRaw(0); byte[] deviceId = body.getRaw(0);
checkLength(deviceId, UniqueId.LENGTH); checkLength(deviceId, UniqueId.LENGTH);
// Transport ID // Transport ID
String transportId = message.getString(1); String transportId = body.getString(1);
checkLength(transportId, 1, MAX_TRANSPORT_ID_LENGTH); checkLength(transportId, 1, MAX_TRANSPORT_ID_LENGTH);
// Version // Version
long version = message.getLong(2); long version = body.getLong(2);
if (version < 0) throw new FormatException(); if (version < 0) throw new FormatException();
// Properties // Properties
BdfDictionary dictionary = message.getDictionary(3); BdfDictionary dictionary = body.getDictionary(3);
checkSize(dictionary, 0, MAX_PROPERTIES_PER_TRANSPORT); checkSize(dictionary, 0, MAX_PROPERTIES_PER_TRANSPORT);
for (String key : dictionary.keySet()) { for (String key : dictionary.keySet()) {
checkLength(key, 0, MAX_PROPERTY_LENGTH); checkLength(key, 0, MAX_PROPERTY_LENGTH);