Spam is the first problem almost every app with user content meets, usually within days of getting any traffic. The attackers are rarely clever. They are scripts posting the same link, or low-paid humans pasting promotions into every comment box they find. You do not need an arms race to stop them. You need a few cheap layers that cost real users nothing and cost spammers a lot.
Think in layers, not in one perfect filter
No single check stops everything, and every check has a false-positive rate. Stack several weak signals instead: friction at sign-up, limits on what a new account can do, content checks, and a human look at what slips through. Each layer should fail soft, which means it slows or queues an account rather than banning it outright. Reserve hard bans for accounts that have earned them.
Sign-up friction that does not hurt real users
Every extra step at sign-up costs you real users, so spend friction only where it buys something. The cheapest approach is to let people in and restrict what they can do until they have shown they are real, rather than interrogating everyone at the door.
- Verify the email, but do not block on it. Let the person browse and react straight away. Require a confirmed address before the first post, the first link or the first message to a stranger.
- Offer sign-in providers when your audience has them. Accounts created through Apple, Google or similar providers are more expensive to mass-produce than throwaway email addresses. Check the current store rules on offering them, since they change.
- Use platform attestation on sensitive endpoints. Apple's App Attest and Google's Play Integrity API let your backend ask whether a request comes from your genuine app on a real device. They will not stop a determined human, but they remove scripts that call your API directly.
- Add a challenge only when something looks off. A CAPTCHA shown to everyone drives people away. Show one when many sign-ups arrive from one network in a few minutes, or when the same device keeps creating accounts.
Device and network identifiers can count as personal data in many countries. Keep what you store to a minimum, say so in your privacy policy, and check with a lawyer about what applies where you operate.
Rate limiting: per account, per action, per network
Rate limits are the highest-value defense on this list because they cap the damage of everything you did not catch. Limit by account for each kind of action separately, and add a coarser per-network limit for sign-ups and logins. A user posting five comments in a minute is plausible. A user posting fifty is not.
Use two windows per action: a short one that catches bursts and a longer one that catches steady drips.
LIMITS = {
"comment": [(5, 60), (40, 3600)], # (max actions, window in seconds)
"signup": [(3, 600)], # keyed by network address
"report": [(10, 3600)],
}
def allowed(action, key, now, history):
for max_count, window in LIMITS[action]:
recent = [t for t in history[(action, key)] if now - t < window]
if len(recent) >= max_count:
return False
return True
In production, keep the counters in a shared store such as Redis, not in the memory of one process. In-memory counters reset on every restart and split across multiple workers, so the limit your code enforces is not the limit you wrote. When you refuse a request, return 429 with a Retry-After header and show a calm message in the app. Keep network limits generous: a university or mobile carrier can put thousands of real people behind one address.
New-account limits: trust is earned
Spammers want to post links on day one. Make the early days boring. A simple tier table is enough.
| Account state | Can do | Cannot do yet |
|---|---|---|
| Just signed up | Read, react, follow, a few short comments | Links, images, direct messages to non-contacts |
| Email confirmed, 24 hours old | Normal posts, images, a handful of links per day | Bulk actions, invitations |
| Some history without removals | Full limits | Nothing special |
The exact numbers are yours to tune. What matters is that the restriction lifts by itself as the account behaves, and that the app says why when it blocks something. "Links unlock after your first day" costs a real user a shrug, while a silent failure costs you a support email.
Link and duplicate detection
Most spam exists to deliver a link, and most spam is repeated. Both properties are cheap to check before you publish anything.
Links
- Extract every URL before publishing, and cap links per post. Allow none from the newest accounts.
- Resolve shorteners before judging a link, and keep a small blocklist of domains you have already seen in spam.
- Flag posts where the same domain appears from several unrelated accounts in a day. That pattern is stronger than any single post.
Duplicates
Spammers vary a word or two, so compare a normalized fingerprint, not the raw text:
import hashlib, re
def fingerprint(text: str) -> str:
t = text.lower()
t = re.sub(r"https?://\S+", "<url>", t) # same pitch, different link
t = re.sub(r"[^a-z0-9]+", " ", t).strip()
return hashlib.sha256(t.encode()).hexdigest()
Store the fingerprint with the account and a timestamp. If the same fingerprint appears three times from one account, or from three accounts within an hour, hold the new item for review. This will not catch heavily reworded text, but it catches the lazy majority.
Reputation signals
Instead of asking "is this post spam", ask "how much do we trust this account right now". Combine a few signals you already have, and use the result to decide which limit tier applies, not to ban anyone.
- Positive: confirmed email, account age, posts that were never reported, other users following or replying to them.
- Negative: content removed by a moderator, reports from several different users, blocks by other users, a duplicate hit, a post held in the queue.
Weight reports by who sends them. Ten reports from ten established accounts mean something. Ten reports from accounts created this morning may be an attack on a rival. Let restrictions expire after a clean week.
Honeypots
A honeypot is a trap only a bot would touch. On a web form, it is an extra field hidden from sighted users that bots fill in because they fill in everything. If it arrives non-empty, drop the request quietly and return a normal-looking success so the bot learns nothing.
<input type="text" name="website" tabindex="-1" autocomplete="off"
aria-hidden="true" style="position:absolute;left:-9999px">
Name the field something tempting, and make sure it is also hidden from screen readers and password managers, or you will catch real users. For a mobile app, the same idea works at the API level. Scripts that call your backend directly will often send fields your app never sets, or hit endpoints your app never calls. Treat such requests as a strong signal. Another cheap trap is timing: a sign-up form submitted 400 milliseconds after it was created was not filled in by a person.
Honeypots do nothing against humans, and capable bots learn to avoid them, so treat them as a free first filter.
Reviewing what gets through
Something will slip through, so build the loop that finds it. Route anything borderline to a queue instead of deciding in code: a post with a link from a day-old account, a fingerprint that matched once, an account that tripped two weak signals. A queue turns uncertain cases into one-click decisions and gives you labeled examples for tuning.
- Let users report spam from every piece of content, with "spam" as an explicit reason. Reports are your best view of what your filters miss.
- Sample your own approvals. Read ten random published items a week from new accounts. Otherwise you only ever see what was blocked, never what was missed.
- Ban accounts, not just posts. When a moderator confirms spam, remove the account's other content and look for siblings that share its fingerprints, domains or network.
- Track false positives: how often a restricted account turned out to be a real user.
One honest caveat about general moderation APIs: many do not score spam at all, so the rule-based layers above are your real defense. TrustShip's documentation says the same about its OpenAI-backed provider, whose spam rule never fires today and is on the roadmap. What it does give you is the surrounding loop: a review queue with approve, remove and ban actions, user reports, and project-wide bans that block a banned user's later content before any provider call.
A starting checklist
- Add per-account, per-action rate limits with two windows each, shared across workers, returning
429withRetry-After. - Restrict links and images for accounts under a day old or without a confirmed email.
- Fingerprint posts and hold repeats for review.
- Add a honeypot field and a minimum-time check to web sign-up.
- Add a spam reason to your report sheet, and review a weekly sample of new accounts' posts.
Keep the review loop in one place
Your spam rules catch the obvious cases; a queue, user reports and bans handle the rest. TrustShip gives you those pieces in one drop-in backend.
Start free