From 8bbb9ccb1b7134a1a8d74514f97253cbdb85913e Mon Sep 17 00:00:00 2001 From: Silberengel Date: Sun, 31 May 2026 10:38:32 +0200 Subject: [PATCH] bug-fixes --- src/lib/event-ingest-filter.test.ts | 33 +++++++++++++++++++++++++++++ src/lib/event-ingest-filter.ts | 27 ++++++++++++++++++++++- 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/src/lib/event-ingest-filter.test.ts b/src/lib/event-ingest-filter.test.ts index 91d3db1f..c371abed 100644 --- a/src/lib/event-ingest-filter.test.ts +++ b/src/lib/event-ingest-filter.test.ts @@ -16,6 +16,17 @@ const DRIFT_GITS_SPAM: Event = { ] } +const BASE64_BLOB_SPAM: Event = { + kind: 1, + content: + 'yBH9z+dFkrjXwdWnT43WlzguqTlaMEaeVr2+2A5cJKpbgnSxuU/rstTbQzkb1ormLJOt6ary5iWeBVul1xHFgMzVFlnDeIrUyOGeMIBu18gwTlOyJ4NY4RsmegRYivAoej1Hik+ifi5DmXYQN3dsIiz2xYqMiks+uegscL71yY2QZOA=', + created_at: 1780215178, + id: '6b5451748d2aa66c699b99d343275d161708a0692b3edd95dcc162409bd8e0c6', + pubkey: '3ccf8522563127b37aaf0cafd0545851d9d1f6a62033ce373636b2fb72a2ffdf', + sig: '960d9c5fe890de907e2ebe922d4a3101670add71306d3f55f066b11c301f457b3635e305a5487a3c8bdad3c5405bab3bca26623ccbd56753c13c5e68b400c20e', + tags: [] +} + describe('shouldDropEventOnIngest', () => { it('drops drift.gits.net kind-1 spam', () => { expect(shouldDropEventOnIngest(DRIFT_GITS_SPAM)).toBe(true) @@ -29,6 +40,28 @@ describe('shouldDropEventOnIngest', () => { ).toBe(false) }) + it('drops long base64-like kind-1 blobs ending with =', () => { + expect(shouldDropEventOnIngest(BASE64_BLOB_SPAM)).toBe(true) + }) + + it('allows long base64 blob on explicit note lookup', () => { + expect( + shouldDropEventOnIngest(BASE64_BLOB_SPAM, { + explicitNoteLookupHexId: BASE64_BLOB_SPAM.id + }) + ).toBe(false) + }) + + it('does not drop short kind-1 text ending with =', () => { + expect( + shouldDropEventOnIngest({ + ...BASE64_BLOB_SPAM, + content: 'x=3', + tags: [] + }) + ).toBe(false) + }) + it('does not drop normal kind-1 text', () => { expect( shouldDropEventOnIngest({ diff --git a/src/lib/event-ingest-filter.ts b/src/lib/event-ingest-filter.ts index 391497c3..7e6ea0af 100644 --- a/src/lib/event-ingest-filter.ts +++ b/src/lib/event-ingest-filter.ts @@ -44,6 +44,27 @@ function isKactiBroadcastSpamKind1(event: Pick): boo /** * drift.gits.net kind-1 payloads (`sp_.….drift.gits.net` + `t` tag) — relay index noise, not discussion text. */ +/** Min length for kind-1 opaque blobs (base64-like relay noise). */ +const OPAQUE_BLOB_KIND1_MIN_LEN = 80 +/** Min length when there is no `=` padding but the whole note is still one opaque token. */ +const OPAQUE_BLOB_KIND1_MIN_LEN_NO_PAD = 120 + +/** + * Long single-token payloads (usually base64) with no readable text — relay index spam. + */ +function isLongOpaqueRandomStringKind1(event: Pick): boolean { + if (event.kind !== kinds.ShortTextNote) return false + const raw = typeof event.content === 'string' ? event.content : '' + const compact = raw.trim().replace(/\s+/g, '') + if (compact.length < OPAQUE_BLOB_KIND1_MIN_LEN) return false + if (!/^[A-Za-z0-9+/]+=*$/.test(compact)) return false + const bodyLen = compact.replace(/=+$/, '').length + if (compact.endsWith('=')) { + return bodyLen >= OPAQUE_BLOB_KIND1_MIN_LEN - 4 + } + return compact.length >= OPAQUE_BLOB_KIND1_MIN_LEN_NO_PAD +} + function isDriftGitsNetSpamKind1( event: Pick ): boolean { @@ -78,7 +99,8 @@ const DEPRECATED_NIP71_SHORT_VIDEO_ADDRESSABLE_KIND = 34236 /** * Single gate for subscribe/cache/IDB read paths: drop kind-1 JSON-object spam, Kacti broadcast spam, - * drift.gits.net spam, and malformed relay reviews. Optional {@link ShouldDropEventOnIngestOptions} relaxes + * drift.gits.net spam, long opaque random strings, and malformed relay reviews. Optional + * {@link ShouldDropEventOnIngestOptions} relaxes * kind-1 spam drops for explicit id fetch. */ export function shouldDropEventOnIngest( @@ -95,5 +117,8 @@ export function shouldDropEventOnIngest( if (isDriftGitsNetSpamKind1(event)) { if (!relaxKind1Spam) return true } + if (isLongOpaqueRandomStringKind1(event)) { + if (!relaxKind1Spam) return true + } return false }