Reject mentions and text styles outside the message text (#2167)

Signal clients drop a message with a body range that ends after its
text (EnvelopeContentValidator: "Body range with out-of-bounds
start/length!"), while the sender gets a successful send result.
Check the ranges against the message length in UTF-16 code units, as
the clients do, and fail with a user error instead.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ryan SandClaude Opus 5.5 authored and GitHub committed 2026-11-13 18:59:39 +01:00
1 parent a7c769615b
commit 8e808a761e
2 files changed
+72

No files matched your search

@@ -216,6 +216,7 @@ public class SendCommand implements JsonRpcLocalCommand {
final var textStyleStrings = ns.<String>getList("text-style");
final var textStyles = textStyleStrings == null ? List.<TextStyle>of() : parseTextStyles(textStyleStrings);
checkRangesInMessageText(messageText, mentions, textStyles);
final Message.Quote quote;
final var quoteTimestamp = ns.getLong("quote-timestamp");
@@ -378,6 +379,40 @@ public class SendCommand implements JsonRpcLocalCommand {
return Optional.of(timestamp);
}
/**
* Signal clients drop a message with a body range that ends after its text, without any error for the sender.
* Like the clients, the length is counted in UTF-16 code units.
*/
private static void checkRangesInMessageText(
final String messageText,
final List<Message.Mention> mentions,
final List<TextStyle> textStyles
) throws UserErrorException {
final var length = messageText.length();
for (final var mention : mentions) {
if ((long) mention.start() + mention.length() > length) {
throw new UserErrorException("Mention "
+ mention.start()
+ ":"
+ mention.length()
+ " is outside of the message text (length "
+ length
+ ")");
}
}
for (final var textStyle : textStyles) {
if ((long) textStyle.start() + textStyle.length() > length) {
throw new UserErrorException("Text style "
+ textStyle.start()
+ ":"
+ textStyle.length()
+ " is outside of the message text (length "
+ length
+ ")");
}
}
}
private List<Message.Mention> parseMentions(
final String selfNumber,
final List<String> mentionStrings
@@ -8,6 +8,7 @@ import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.api.Message;
import org.asamk.signal.manager.api.SendMessageResults;
import org.asamk.signal.manager.api.TextStyle;
import org.asamk.signal.output.JsonWriter;
import org.asamk.signal.testutil.ManagerMock;
import org.junit.jupiter.api.Test;
@@ -231,6 +232,34 @@ class SendCommandParsingTest {
assertTrue(e.getMessage().contains("--end-session"));
}
@Test
void rejectsTextStylesOutsideTheMessageText() {
// Signal clients drop a message with a body range past the end of its text
for (final var style : List.of("0:6:BOLD", "5:1:BOLD", "6:0:BOLD")) {
assertThrows(UserErrorException.class, () -> send(textStyleNamespace("hello", style)), style);
}
}
@Test
void acceptsTextStylesUpToTheEndOfTheMessageText() throws Exception {
final var message = send(textStyleNamespace("hello", "0:5:BOLD"));
assertEquals(List.of(new TextStyle(TextStyle.Style.BOLD, 0, 5)), message.textStyles());
}
@Test
void textStyleBoundsCountUtf16CodeUnits() throws Exception {
// One code point, but two UTF-16 code units, which is how Signal clients count
final var text = "\uD83D\uDE00a";
send(textStyleNamespace(text, "0:3:BOLD"));
assertThrows(UserErrorException.class, () -> send(textStyleNamespace(text, "0:4:BOLD")));
}
@Test
void rejectsMentionsOutsideTheMessageText() throws Exception {
send(mentionNamespace("hi \uFFFC", "3:1:+15550000000"));
assertThrows(UserErrorException.class, () -> send(mentionNamespace("hi \uFFFC", "3:2:+15550000000")));
}
// parseMessageTimestamp takes the current time as a parameter, so the bounds can be tested deterministically.
@Test
@@ -271,6 +300,14 @@ class SendCommandParsingTest {
}
}
private JsonRpcNamespace textStyleNamespace(String message, String textStyle) {
return new JsonRpcNamespace(Map.of("noteToSelf", true, "message", message, "textStyle", List.of(textStyle)));
}
private JsonRpcNamespace mentionNamespace(String message, String mention) {
return new JsonRpcNamespace(Map.of("noteToSelf", true, "message", message, "mention", List.of(mention)));
}
private JsonRpcNamespace timestampNamespace(Object timestamp) {
return new JsonRpcNamespace(Map.of("noteToSelf", true, "message", "hello", "timestamp", timestamp));
}