diff --git a/lib/libsignal-service/src/main/java/org/whispersystems/signalservice/api/messages/EnvelopeContentValidator.kt b/lib/libsignal-service/src/main/java/org/whispersystems/signalservice/api/messages/EnvelopeContentValidator.kt index e2b24e0caa..d64f4f0188 100644 --- a/lib/libsignal-service/src/main/java/org/whispersystems/signalservice/api/messages/EnvelopeContentValidator.kt +++ b/lib/libsignal-service/src/main/java/org/whispersystems/signalservice/api/messages/EnvelopeContentValidator.kt @@ -414,7 +414,10 @@ object EnvelopeContentValidator { return Result.Invalid("[StoryMessage] Style body range is missing a start or length!") } - if (storyMessage.bodyRanges.hasInvalidBounds(storyMessage.textAttachment?.text)) { + // Body ranges apply to the text of a text story, or to the caption of a media story. + val storyText: String? = storyMessage.textAttachment?.text ?: storyMessage.fileAttachment?.caption + + if (storyMessage.bodyRanges.hasInvalidBounds(storyText)) { return Result.Invalid("[StoryMessage] Body range with out-of-bounds start/length!") } diff --git a/lib/libsignal-service/src/test/java/org/whispersystems/signalservice/api/messages/EnvelopeContentValidatorTest.kt b/lib/libsignal-service/src/test/java/org/whispersystems/signalservice/api/messages/EnvelopeContentValidatorTest.kt index 7f480dacc2..96c6ad2a92 100644 --- a/lib/libsignal-service/src/test/java/org/whispersystems/signalservice/api/messages/EnvelopeContentValidatorTest.kt +++ b/lib/libsignal-service/src/test/java/org/whispersystems/signalservice/api/messages/EnvelopeContentValidatorTest.kt @@ -779,6 +779,36 @@ class EnvelopeContentValidatorTest { assert(result is EnvelopeContentValidator.Result.Valid) } + @Test + fun `validate - ensure story body range within the bounds of the file attachment caption is marked valid`() { + val content = Content( + storyMessage = StoryMessage( + fileAttachment = AttachmentPointer(cdnKey = "key", caption = "abc"), + bodyRanges = listOf( + BodyRange(start = 0, length = 3, style = BodyRange.Style.ITALIC) + ) + ) + ) + + val result = EnvelopeContentValidator.validate(Envelope(clientTimestamp = 1234), content, SELF_ACI, CiphertextMessage.WHISPER_TYPE) + assert(result is EnvelopeContentValidator.Result.Valid) + } + + @Test + fun `validate - ensure story body range extending past the end of the file attachment caption is marked invalid`() { + val content = Content( + storyMessage = StoryMessage( + fileAttachment = AttachmentPointer(cdnKey = "key", caption = "abc"), + bodyRanges = listOf( + BodyRange(start = 2, length = 10, style = BodyRange.Style.ITALIC) + ) + ) + ) + + val result = EnvelopeContentValidator.validate(Envelope(clientTimestamp = 1234), content, SELF_ACI, CiphertextMessage.WHISPER_TYPE) + assert(result is EnvelopeContentValidator.Result.Invalid) + } + @Test fun `validate - ensure story context with a valid author but missing sentTimestamp is marked invalid`() { val content = Content(