Validate story body ranges against media story captions.

This commit is contained in:
Greyson Parrelli
2026-09-18 23:28:28 -04:00
parent 7b67f2b3ee
commit c5bb6e70ca
2 changed files with 34 additions and 1 deletions
@@ -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!")
}
@@ -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(