How Spam Filtering Actually Works In My Nostr Client (And Why 1-3 Rules Beat 100)

Your feed, your rules. Don't let spam decide what you see.

Users ask me the same two questions:

  1. Can a bad regex hang my client?
  2. How many rules is the sweet spot?

Short answer: Use 1-3 rules. You can use 50-100, but you don’t need to. My 1 rule covers 99% of the spam on Global right now.

Here’s the long answer, with the math.

1. What a filter actually is

There’s no central Nostr server to filter spam. There are thousands of relays. Anyone can run one. There are NIPs — rules, so to speak — including anti-spam ones, but nobody has to implement them 100%. Free relays mostly don’t.

So filtering has to happen in your client, on your feed.

A rule in my client is just a regex — a pattern that says “if a post contains this, hide it.”

My default, the one I use myself:

/(evmverse\.cc|gweiportal\.top|drift\.gits\.net)/

That’s it. One rule.

It hides:

https://one.evmverse.cc/?ref=xerapv
https://gweiportal.top/?ref=1yqfmb
sp_4c43bd1d.a76028e121cfb7280903168a.05.JYED2AZLC6NMPI4GIIP4MJPFSNF4P7DFFWRRPBYZ537MSODNPEAMVPDDCFEILCY.drift.gits.net

And it keeps everything else. That’s 99% of the ref spam I was seeing on Global.

If you want to toggle them separately, split it into 3:

/https?:\/\/\S*evmverse\.cc\b/
/https?:\/\/\S*gweiportal\.top\b/
/sp_[A-Za-z0-9._-]+\.drift\.gits\.net/

Same result, just easier to debug which one hid what.

2. How to ask AI to make you a rule

You don’t need to learn regex. You just need to know how to ask.

Don’t ask: “Make me a spam filter”

Ask like this:

I want to hide posts that contain links to example-spam.com and another-spam.top, but I want to keep posts that contain example.com because that’s legit. Give me a regex, and test it on these 5 posts I want to hide and these 5 posts I want to keep.

Give it examples. That’s the whole trick.

Template you can copy:

I use a Nostr client with regex filters.

Hide these:
- [paste 2-3 spam posts]

Keep these:
- [paste 2-3 good posts you don't want to lose]

Give me 1 regex that hides the first group but not the second. Explain what each part does.

Then paste the regex into Settings, turn it on, scroll Global, turn it off. If something good disappeared, you have a false positive. Tell the AI: “It hid this good post too, fix it.”

That’s it. 5 minutes. You don’t need a preview engine — you are the preview engine.

For advanced users who want to actually understand regex, this is the best guide I’ve found, short and practical:

Regex Guide: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions

Test your regex live: https://regex101.com/ — paste your pattern + a post, it shows you exactly what matches and why, and warns you about catastrophic backtracking.

3. The math — why 50-100 rules are possible, but 1 bad rule kills it

This is the part people get wrong. It’s not about the number of rules. It’s about complexity.

Simple rules are cheap

Let’s say Global loads 1,000 posts. Average post is ~500 characters.

A simple pattern like mine:

/(evmverse\.cc|gweiportal\.top|drift\.gits\.net)/

That’s just a linear scan. The engine walks through the text once, looking for those strings.

Measured cost: ~0.005ms per post per rule.

1 rule:   1,000 posts x 0.005ms = 5ms
10 rules:  1,000 x 0.005ms x 10 = 50ms
50 rules:  1,000 x 0.005ms x 50 = 250ms
100 rules: 1,000 x 0.005ms x 100 = 500ms

So yes, you can run 50-100 simple rules and your client will still feel fine. 50 rules = 0.25 seconds. That’s nothing.

And if you combine them with | into ONE rule:

/(domain1|domain2|domain3|...|domain100)/

That’s even faster — 1 test per post instead of 100. That’s why I ship 1 combined rule, not 100 separate ones.

Formal complexity: O(N * M) where N = number of posts, M = total length of patterns. Linear. Boring. Safe.

Bad rules are exponential

Now this:

/^(a+)+$/

Or this, which people actually share as a “filter”:

/(.<em>.</em>.<em>.</em>)bot/

This is not linear. This is catastrophic backtracking.

The regex engine tries every possible way to split the text to make the pattern match. For a post of length n, with nested quantifiers like (a+)+, the number of attempts grows like 2^n.

For a 30-character post, that’s ~1 billion attempts. For a 500-character post, it’s… don’t even calculate it. Your client freezes.

Formal complexity: O(2^n) — exponential. One rule, one post, hang.

So the real rule is:

100 simple rules = fine. 1 evil rule = hang.

What actually hangs a client in practice

  1. Nested quantifiers: (.<em>.</em>), (a+)+, (.+)+ — never use .* twice
  2. Running the regex on everything: content + tags + pubkey + relays + metadata for every post, instead of just content
  3. Re-compiling every time: doing new RegExp(pattern) inside the loop for every post, instead of compiling once and reusing it

Avoid those three and you can run 50-100 rules all day.

4. Why I still use only 1-3 rules

Because I don’t need more.

Spam on Nostr right now isn’t sophisticated. It’s the same 3-4 domains rotating ?ref= links. Block those domains, you block 99% of it.

More rules = more chances to hide something good. Every extra rule is another chance for a false positive.

My philosophy:

  • 1 rule — the combined one — if you want it simple
  • 3 rules — split by domain — if you want to toggle one off and see what it hid
  • 5 rules max — if you add 1-2 for your own personal spam (a scammer, a word you hate)

If you can’t remember what each rule does without opening Settings, you have too many.

You can use 50-100. The math says you can. But if you need 100 rules to make Global usable, you’re not filtering spam anymore, you’re filtering Nostr.

5. Can someone hack you with a malicious regex?

Yes, but not how you think.

A regex can’t steal your nsec, can’t run code, can’t access storage — if the client just uses it as pattern.test(post).

What it can do:

  1. ReDoS: Give you an evil pattern like (a+)+$, your client freezes on a long post
  2. Censorship by list: Share a “spam list” that secretly also blocks primal.net, nos.lol, bitcoin, nostr — you paste it, you hide all the good stuff
  3. Empty feed attack: Share / .+/ — hides everything, you think Nostr is dead

That’s why I don’t auto-import lists from strangers. I give you defaults, you write your own, you own your feed. If you write a bad regex, your feed gets slow. If you write a slow regex, that’s on you. That’s the beauty — your feed, your rules.

If you want to be extra safe: use a safe engine like RE2 (no backtracking), cap pattern length to ~200 chars, and test a shared list on your last 100 posts before you enable it.

TL;DR

Your feed, your rules. Don’t let spam decide what you see.

https://client.nostrwat.ch

#nostr #spam #filter #regex #nostrdev #buildinpublic


Write a comment