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.
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.
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.
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.
Related posts
- Guides
Environment variables and secrets: from a .env on your laptop to production
Why does a key you marked "hidden" in your dashboard show up in your site's JavaScript? The difference between build-time and runtime variables, separate values for each environment, and what to do in the first minutes after a key leaks.
- Guides
Automatic SSL: issuing, renewing, and the TLS errors that still scare users away
Let's Encrypt certificates are free and valid for 90 days, which means four chances a year to forget. This post explains how automatic issuing and renewal work, what common browser errors actually mean, and how to find and fix each one fast.
- Guides
Monitoring and logs for a small team: four numbers that are enough to start
The first person to notice your site is down shouldn't be a customer. This post explains the four basic signals (latency, traffic, error rate and saturation), suggests sensible alert thresholds for each, and shows how to write logs that help on incident day.
