Force use of UCS-2 instead of GSM-7 for SMS to China (#297)

This commit is contained in:
Ehren Kret 2020-11-20 14:41:48 -06:00 committed by GitHub
parent 36aca49fc3
commit 00a3e562dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 6 deletions

View File

@ -22,6 +22,7 @@ import org.whispersystems.textsecuregcm.util.ExecutorUtils;
import org.whispersystems.textsecuregcm.util.SystemMapper;
import org.whispersystems.textsecuregcm.util.Util;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.IOException;
import java.net.URI;
@ -113,7 +114,7 @@ public class TwilioSmsSender {
Map<String, String> requestParameters = new HashMap<>();
requestParameters.put("To", destination);
boolean usedSenderId = setOriginationRequestParameter(destination, requestParameters, enableSenderId);
requestParameters.put("Body", String.format(Locale.US, getBodyFormatString(clientType.orElse(null)), verificationCode));
requestParameters.put("Body", String.format(Locale.US, getBodyFormatString(destination, clientType.orElse(null)), verificationCode));
HttpRequest request = HttpRequest.newBuilder()
.uri(smsUri)
@ -129,15 +130,23 @@ public class TwilioSmsSender {
.thenCompose(twilioResponse -> retrySendSmsVerificationIfApplicable(usedSenderId, twilioResponse, destination, clientType, verificationCode));
}
private String getBodyFormatString(@Nullable String clientType) {
private String getBodyFormatString(@Nonnull String destination, @Nullable String clientType) {
final String result;
if ("ios".equals(clientType)) {
return iosVerificationText;
result = iosVerificationText;
} else if ("android-ng".equals(clientType)) {
return androidNgVerificationText;
result = androidNgVerificationText;
} else if ("android-2020-01".equals(clientType)) {
return android202001VerificationText;
result = android202001VerificationText;
} else {
return genericVerificationText;
result = genericVerificationText;
}
if (destination.startsWith("+86")) { // is China
return result + "\u2008";
// Twilio recommends adding this character to the end of strings delivered to China because some carriers in
// China are blocking GSM-7 encoding and this will force Twilio to send using UCS-2 instead.
} else {
return result;
}
}

View File

@ -338,4 +338,18 @@ public class TwilioSmsSenderTest {
.withHeader("Content-Type", equalTo("application/x-www-form-urlencoded"))
.withRequestBody(equalTo("MessagingServiceSid=test_messaging_services_id&To=%2B14153333333&Body=%3C%23%3E+Verify+on+AndroidNg%3A+123-456%0A%0Acharacters")));
}
@Test
public void testSendSmsChina() {
setupSuccessStubForSms();
TwilioConfiguration configuration = createTwilioConfiguration();
TwilioSmsSender sender = new TwilioSmsSender("http://localhost:" + wireMockRule.port(), configuration);
boolean success = sender.deliverSmsVerification("+861065529988", Optional.of("android-ng"), "123-456").join();
assertThat(success).isTrue();
verify(1, postRequestedFor(urlEqualTo("/2010-04-01/Accounts/" + ACCOUNT_ID + "/Messages.json"))
.withHeader("Content-Type", equalTo("application/x-www-form-urlencoded"))
.withRequestBody(equalTo("MessagingServiceSid=test_messaging_services_id&To=%2B861065529988&Body=%3C%23%3E+Verify+on+AndroidNg%3A+123-456%0A%0Acharacters%E2%80%88")));
}
}