diff --git a/src/main/java/org/asamk/signal/commands/SendCommand.java b/src/main/java/org/asamk/signal/commands/SendCommand.java index 195cef32..53bd189d 100644 --- a/src/main/java/org/asamk/signal/commands/SendCommand.java +++ b/src/main/java/org/asamk/signal/commands/SendCommand.java @@ -216,6 +216,7 @@ public class SendCommand implements JsonRpcLocalCommand { final var textStyleStrings = ns.getList("text-style"); final var textStyles = textStyleStrings == null ? List.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 mentions, + final List 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 parseMentions( final String selfNumber, final List mentionStrings diff --git a/src/test/java/org/asamk/signal/commands/SendCommandParsingTest.java b/src/test/java/org/asamk/signal/commands/SendCommandParsingTest.java index 1468afda..53ab25d2 100644 --- a/src/test/java/org/asamk/signal/commands/SendCommandParsingTest.java +++ b/src/test/java/org/asamk/signal/commands/SendCommandParsingTest.java @@ -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)); }