logo
Parslinks
Back to blog
Guides4 min read

SMS login done right: secure one-time codes without a runaway SMS bill

Phone-number login is the most natural sign-in for many markets. Without limits, it's also your most expensive security hole.

Alireza Tadi

Co-founder & Head of Development and Communications, Parslinks

A grocery delivery app lets customers sign in with nothing but a phone number and a texted code. Customers love it. Then one Monday the SMS invoice is twelve times the usual amount. The logs show a script requested codes for 40,000 numbers overnight, many of them premium-rate international numbers. This is a known fraud called SMS pumping, and the attacker earns a cut of every message you pay for.

The same week, a customer reports someone else got into their account. Codes were four digits, attempts were unlimited, and 10,000 combinations take a script a few minutes.

Four numbers to decide

Code length: six digits. Four gives 10,000 possibilities; six gives a million.

Lifetime: two to five minutes. Enough for delivery and typing. Invalidate the old code when a new one is sent.

Attempts: at most five wrong guesses per code. After that, the code is void and the user must request a new one.

Send limits: at most one SMS per minute and about five per hour per number, plus a separate cap per IP, because attackers rotate numbers.

Recommended settings for SMS code login

Never store codes in plain text

Hash codes like passwords. If your database or logs leak, active codes are useless. And never log the code; "code sent to +44 7•••" is plenty.

Compare submitted codes with a constant-time function, so response timing doesn't reveal how many digits were right.

verify-otp.ts
const key = `otp:${phone}`;
const attempts = await redis.incr(`${key}:tries`);
if (attempts > 5) throw new TooManyAttempts();
const stored = await redis.get(`${key}:hash`);
if (!stored || !timingSafeEqual(hash(code), stored)) throw new InvalidCode();
await redis.del(`${key}:hash`, `${key}:tries`);
return createSession(phone);
ts7 lines

Stop SMS pumping

The limits above stop most attacks, but a few extra layers help:

  • Allow only the countries you serve. If you only operate in one market, reject other country codes outright. This alone kills most pumping.
  • A global daily cap with alerts. If today's total exceeds twice the average, alert someone, or temporarily require a CAPTCHA.
  • CAPTCHA only on suspicion. Showing it to everyone hurts conversion. Turn it on after suspicious patterns from an IP.

For broader rate limiting, see rate limits and bot defense.

The outcome

Codes became six digits with a three-minute lifetime, five attempts, per-number and per-IP send limits, and a country allowlist. Codes are stored hashed in Redis, and daily SMS volume is compared against the average. The next month's bill was back to normal, and there were no further account-takeover reports.

Frequently asked questions

Join the discussion

No comments yet. Be the first to share your thoughts.