Skip to content

Commit

Permalink
fix(dht): check for empty body contents in initial msg validation (#5123
Browse files Browse the repository at this point in the history
)

Description
---
Discards messages with empty body contents in initial message validation

Motivation and Context
---
[RFC-172 Stabalisation PR](tari-project/rfcs#88) adds a rule that routing nodes should discard a message with empty body contents.

How Has This Been Tested?
---
New unit test
  • Loading branch information
sdbondi authored Jan 18, 2023
1 parent d5db596 commit 48bf2d9
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions comms/dht/src/inbound/decryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ enum DecryptionError {
EncryptedMessageNoDestination,
#[error("Decryption failed: {0}")]
DecryptionFailedMalformedCipher(#[from] DhtEncryptError),
#[error("Encrypted message must have a non-empty body")]
EncryptedMessageEmptyBody,
}

/// This layer is responsible for attempting to decrypt inbound messages.
Expand Down Expand Up @@ -346,6 +348,10 @@ where S: Service<DecryptedDhtMessage, Response = (), Error = PipelineError>
/// Performs message validation that should be performed by all nodes. If an error is encountered, the message is
/// invalid and should never have been sent.
fn initial_validation(message: DhtInboundMessage) -> Result<ValidatedDhtInboundMessage, DecryptionError> {
if message.body.is_empty() {
return Err(DecryptionError::EncryptedMessageEmptyBody);
}

if message.dht_header.flags.is_encrypted() {
// Check if there is no destination specified and discard
if message.dht_header.destination.is_unknown() {
Expand Down Expand Up @@ -572,6 +578,33 @@ mod test {
assert_eq!(decrypted.decryption_result.unwrap_err(), inbound_msg.body);
}

#[test]
fn decrypt_inbound_fail_empty_contents() {
let service = service_fn(
move |_msg: DecryptedDhtMessage| -> future::Ready<Result<(), PipelineError>> {
panic!("Should not be called")
},
);
let node_identity = make_node_identity();
let (connectivity, _) = create_connectivity_mock();
let mut service = DecryptionService::new(Default::default(), node_identity, connectivity, service);

let some_other_node_identity = make_node_identity();
let mut inbound_msg = make_dht_inbound_message(
&some_other_node_identity,
&Vec::new(),
DhtMessageFlags::ENCRYPTED,
true,
true,
)
.unwrap();
inbound_msg.body = Vec::new();

let err = block_on(service.call(inbound_msg.clone())).unwrap_err();
let err = err.downcast::<DecryptionError>().unwrap();
unpack_enum!(DecryptionError::EncryptedMessageEmptyBody = err);
}

#[runtime::test]
async fn decrypt_inbound_fail_destination() {
let (connectivity, mock) = create_connectivity_mock();
Expand Down

0 comments on commit 48bf2d9

Please sign in to comment.