Skip to content

Commit 4de79e4

Browse files
committed
fix(auth): offset TOTP delta histogram to avoid negative StatsD values
Because: * Telegraf's statsd input plugin only accepts non-negative values for histograms * The delta from otplib's checkDelta can be negative (e.g. -1 when the user enters a code from the previous time window) * The previous guard `if (type && delta)` skipped recording when delta === 0, silently dropping the most common exact-match case This commit: * Offsets delta by the configured window size so values are always non-negative (with window=1: -1→0, 0→1, 1→2) * Fixes the falsy check to use `delta !== undefined && delta !== null` so delta=0 is recorded * Guards against undefined otpOptions with optional chaining Fixes #13356
1 parent 3b34b9c commit 4de79e4

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

  • packages/fxa-auth-server/lib/routes/utils

packages/fxa-auth-server/lib/routes/utils/otp.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,12 @@ export class OtpUtils {
5656
const valid = otpAuthenticator.check(code, secret);
5757
const delta = otpAuthenticator.checkDelta(code, secret);
5858

59-
if (type && delta) {
60-
this.statsd.histogram(`${type}.totp.delta_histogram`, delta);
59+
if (type && delta !== undefined && delta !== null) {
60+
// Offset delta by window so the value is always non-negative.
61+
// With window=1: delta -1 → 0, delta 0 → 1, delta 1 → 2.
62+
// Telegraf's statsd plugin only accepts non-negative histogram values.
63+
const window = otpOptions?.window ?? 1;
64+
this.statsd.histogram(`${type}.totp.delta_histogram`, delta + window);
6165
}
6266
// Return delta for logging
6367
return { valid, delta };

0 commit comments

Comments
 (0)