mirror of
https://github.com/AsamK/signal-cli.git
synced 2026-11-29 08:55:48 +00:00
Reject poll questions that Signal clients would drop (#2174)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
fdc61f09fc
commit
07432820cc
2 files changed
+74
No files matched your search
@@ -26,6 +26,7 @@ public class SendPollCreateCommand implements JsonRpcLocalCommand {
|
||||
private static final Logger logger = LoggerFactory.getLogger(SendPollCreateCommand.class);
|
||||
private static final int MAX_POLL_OPTIONS = 10;
|
||||
private static final int MAX_POLL_OPTION_LENGTH = 100;
|
||||
private static final int MAX_POLL_QUESTION_LENGTH = 200;
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
@@ -69,6 +70,14 @@ public class SendPollCreateCommand implements JsonRpcLocalCommand {
|
||||
usernameStrings);
|
||||
|
||||
final var question = ns.getString("question");
|
||||
if (question == null || question.isBlank()) {
|
||||
throw new UserErrorException("Poll question must not be empty");
|
||||
}
|
||||
if (question.length() > MAX_POLL_QUESTION_LENGTH) {
|
||||
throw new UserErrorException("Poll question exceeds the maximum length of "
|
||||
+ MAX_POLL_QUESTION_LENGTH
|
||||
+ " characters");
|
||||
}
|
||||
final var noMulti = Boolean.TRUE.equals(ns.getBoolean("no-multi"));
|
||||
final var options = ns.<String>getList("option");
|
||||
if (options.size() < 2) {
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package org.asamk.signal.commands;
|
||||
|
||||
import org.asamk.signal.commands.exceptions.UserErrorException;
|
||||
import org.asamk.signal.manager.Manager;
|
||||
import org.asamk.signal.manager.api.SendMessageResults;
|
||||
import org.asamk.signal.output.JsonWriter;
|
||||
import org.asamk.signal.testutil.ManagerMock;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
/**
|
||||
* Signal clients drop a poll whose question is blank or longer than 200 characters, so such a
|
||||
* poll must be rejected before it is sent instead of being reported as sent.
|
||||
*/
|
||||
class SendPollCreateCommandTest {
|
||||
|
||||
private final AtomicInteger sentPolls = new AtomicInteger();
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(ints = {0, 201})
|
||||
void questionWithInvalidLengthIsRejected(final int length) {
|
||||
assertThrows(UserErrorException.class, () -> sendPoll("q".repeat(length)));
|
||||
assertEquals(0, sentPolls.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void blankQuestionIsRejected() {
|
||||
assertThrows(UserErrorException.class, () -> sendPoll(" "));
|
||||
assertEquals(0, sentPolls.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void questionWithMaximumLengthIsSent() throws Exception {
|
||||
sendPoll("q".repeat(200));
|
||||
assertEquals(1, sentPolls.get());
|
||||
}
|
||||
|
||||
private void sendPoll(final String question) throws Exception {
|
||||
final var delegate = ManagerMock.create("+15551234567");
|
||||
final var manager = (Manager) Proxy.newProxyInstance(Manager.class.getClassLoader(),
|
||||
new Class<?>[]{Manager.class},
|
||||
(proxy, method, args) -> {
|
||||
if (method.getName().equals("sendPollCreateMessage")) {
|
||||
sentPolls.incrementAndGet();
|
||||
return new SendMessageResults(1L, Map.of());
|
||||
}
|
||||
return method.invoke(delegate, args);
|
||||
});
|
||||
new SendPollCreateCommand().handleCommand(new JsonRpcNamespace(Map.of("noteToSelf",
|
||||
true,
|
||||
"question",
|
||||
question,
|
||||
"option",
|
||||
List.of("Approve", "Hold"))), manager, (JsonWriter) ignored -> {});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user