Skip to content

Commit

Permalink
fix(OutgoingMessage): Throw InvalidMessageException when parcel is …
Browse files Browse the repository at this point in the history
…too big
  • Loading branch information
gnarea committed Oct 27, 2023
1 parent f7643d0 commit 032b330
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package tech.relaycorp.awaladroid.messaging

import tech.relaycorp.awaladroid.AwaladroidException

/**
* Exception thrown when an incoming or outgoing service message is invalid.
*/
public class InvalidMessageException(
message: String,
cause: Throwable,
) : AwaladroidException(message, cause)
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import tech.relaycorp.awaladroid.endpoint.ThirdPartyEndpoint
import tech.relaycorp.relaynet.issueEndpointCertificate
import tech.relaycorp.relaynet.messages.Parcel
import tech.relaycorp.relaynet.messages.payloads.ServiceMessage
import tech.relaycorp.relaynet.ramf.RAMFException
import tech.relaycorp.relaynet.wrappers.x509.Certificate
import java.time.Duration
import java.time.ZonedDateTime
Expand Down Expand Up @@ -81,15 +82,20 @@ private constructor(
recipientEndpoint.nodeId,
senderEndpoint.nodeId,
)
return Parcel(
recipient = recipientEndpoint.recipient,
payload = payload,
senderCertificate = getSenderCertificate(),
messageId = parcelId.value,
creationDate = parcelCreationDate,
ttl = ttl,
senderCertificateChain = getSenderCertificateChain(),
)
val parcel = try {
Parcel(
recipient = recipientEndpoint.recipient,
payload = payload,
senderCertificate = getSenderCertificate(),
messageId = parcelId.value,
creationDate = parcelCreationDate,
ttl = ttl,
senderCertificateChain = getSenderCertificateChain(),
)
} catch (exc: RAMFException) {
throw InvalidMessageException("Failed to create parcel", exc)
}
return parcel
}

private fun getSenderCertificate(): Certificate =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package tech.relaycorp.awaladroid.messaging

import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertArrayEquals
import org.junit.Assert.assertEquals
import org.junit.Assert.assertThrows
import org.junit.Assert.assertTrue
import org.junit.Test
import tech.relaycorp.awaladroid.endpoint.PrivateThirdPartyEndpoint
Expand All @@ -11,6 +13,7 @@ import tech.relaycorp.awaladroid.test.MessageFactory
import tech.relaycorp.awaladroid.test.MockContextTestCase
import tech.relaycorp.awaladroid.test.RecipientAddressType
import tech.relaycorp.awaladroid.test.assertSameDateTime
import tech.relaycorp.relaynet.ramf.RAMFException
import java.time.Duration
import java.time.ZonedDateTime
import kotlin.math.abs
Expand Down Expand Up @@ -60,6 +63,25 @@ internal class OutgoingMessageTest : MockContextTestCase() {
assertTrue(abs(differenceSeconds) < 3)
}

@Test
fun build_bigServiceMessage() = runTest {
val (senderEndpoint, recipientEndpoint) = createEndpointChannel(RecipientAddressType.PUBLIC)

val exception = assertThrows(InvalidMessageException::class.java) {
runBlocking {
OutgoingMessage.build(
"the type",
ByteArray(8388608 + 1),
senderEndpoint,
recipientEndpoint,
)
}
}

assertEquals("Failed to create parcel", exception.message)
assertTrue(exception.cause is RAMFException)
}

// Public Recipient

@Test
Expand Down

0 comments on commit 032b330

Please sign in to comment.