Skip to content

Commit efc3957

Browse files
committed
Use CaptchaMetrics to measure captcha scores for sending messages and verification
1 parent 87e88dd commit efc3957

3 files changed

Lines changed: 27 additions & 4 deletions

File tree

service/src/main/java/org/whispersystems/textsecuregcm/captcha/AssessmentResult.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ public float getScore() {
9393
return this.actualScore;
9494
}
9595

96+
public int getNormalizedIntScore() {
97+
return normalizedIntScore(this.actualScore);
98+
}
9699

97100
/**
98101
* Map a captcha score in [0.0, 1.0] to a low cardinality discrete space in [0, 100] suitable for use in metrics

service/src/main/java/org/whispersystems/textsecuregcm/controllers/VerificationController.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
import org.whispersystems.textsecuregcm.filters.RemoteAddressFilter;
7878
import org.whispersystems.textsecuregcm.limits.RateLimiters;
7979
import org.whispersystems.textsecuregcm.mappers.RegistrationServiceSenderExceptionMapper;
80+
import org.whispersystems.textsecuregcm.metrics.CaptchaMetrics;
8081
import org.whispersystems.textsecuregcm.metrics.DevicePlatformUtil;
8182
import org.whispersystems.textsecuregcm.metrics.UserAgentTagUtil;
8283
import org.whispersystems.textsecuregcm.push.PushNotification;
@@ -492,6 +493,11 @@ private VerificationSession handleCaptcha(
492493
Tag.of(SCORE_TAG_NAME, assessmentResult.getScoreString())))
493494
.increment();
494495

496+
CaptchaMetrics.measureCaptchaOutcome(assessmentResult.getNormalizedIntScore(),
497+
assessmentResult.isValid(captchaScoreThreshold),
498+
Util.getRegion(registrationServiceSession.number()),
499+
"verification");
500+
495501
} catch (final IOException e) {
496502
logger.error("error assessing captcha during registration verification", e);
497503
throw new ServerErrorException(Response.Status.SERVICE_UNAVAILABLE, e);

service/src/main/java/org/whispersystems/textsecuregcm/limits/RateLimitChallengeManager.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
import java.util.List;
1515
import java.util.Optional;
1616
import org.whispersystems.textsecuregcm.captcha.Action;
17+
import org.whispersystems.textsecuregcm.captcha.AssessmentResult;
1718
import org.whispersystems.textsecuregcm.captcha.CaptchaChecker;
1819
import org.whispersystems.textsecuregcm.controllers.RateLimitExceededException;
20+
import org.whispersystems.textsecuregcm.metrics.CaptchaMetrics;
1921
import org.whispersystems.textsecuregcm.metrics.UserAgentTagUtil;
2022
import org.whispersystems.textsecuregcm.push.NotPushRegisteredException;
2123
import org.whispersystems.textsecuregcm.spam.ChallengeType;
@@ -61,13 +63,18 @@ public void answerPushChallenge(final Account account, final String challenge) t
6163
}
6264
}
6365

64-
public boolean answerCaptchaChallenge(final Account account, final String captcha, final String mostRecentProxyIp,
65-
final String userAgent, final Optional<Float> scoreThreshold)
66-
throws RateLimitExceededException, IOException {
66+
public boolean answerCaptchaChallenge(final Account account,
67+
final String captcha,
68+
final String mostRecentProxyIp,
69+
final String userAgent,
70+
final Optional<Float> scoreThreshold) throws RateLimitExceededException, IOException {
6771

6872
rateLimiters.getCaptchaChallengeAttemptLimiter().validate(account.getUuid());
6973

70-
final boolean challengeSuccess = captchaChecker.verify(Optional.of(account.getUuid()), Action.CHALLENGE, captcha, mostRecentProxyIp, userAgent).isValid(scoreThreshold);
74+
final AssessmentResult assessmentResult =
75+
captchaChecker.verify(Optional.of(account.getUuid()), Action.CHALLENGE, captcha, mostRecentProxyIp, userAgent);
76+
77+
final boolean challengeSuccess = assessmentResult.isValid(scoreThreshold);
7178

7279
final Tags tags = Tags.of(
7380
Tag.of(SOURCE_COUNTRY_TAG_NAME, Util.getCountryCode(account.getNumber())),
@@ -77,6 +84,13 @@ public boolean answerCaptchaChallenge(final Account account, final String captch
7784

7885
Metrics.counter(CAPTCHA_ATTEMPT_COUNTER_NAME, tags).increment();
7986

87+
CaptchaMetrics.measureCaptchaOutcome(assessmentResult.getNormalizedIntScore(),
88+
challengeSuccess,
89+
Util.getRegion(account.getNumber()),
90+
// Note: currently all challenges are for message-sending, but if we add more use cases, we'll need to make the
91+
// accept a context from callers rather than hard-coding it here
92+
"sendMessage");
93+
8094
if (challengeSuccess) {
8195
rateLimiters.getCaptchaChallengeSuccessLimiter().validate(account.getUuid());
8296
resetRateLimits(account, ChallengeType.CAPTCHA);

0 commit comments

Comments
 (0)