From 4b16308ff06155fbb7db0311e49af50eea507138 Mon Sep 17 00:00:00 2001 From: William Casarin Date: Tue, 8 Sep 2026 21:49:57 -0700 Subject: [PATCH] content: homoglyph-resistant matching via confusables skeleton MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The content filter compared raw bytes with `str::contains`, so airdrop-scam spam spoofing the phrase with Cyrillic look-alikes ("Dаmus Аirdrop", where а/А are U+0430/U+0410) slipped straight through a "Damus Airdrop" blocklist entry. Fold both the note content and each blocklist term to their UTS #39 confusables skeleton (via unicode-security) and case-fold before matching, so homoglyph and casing variants collapse to the same form. Case is folded on both sides of the skeleton because the confusable prototypes are not case-stable. Normalized terms are cached lazily on first use. Also list "Damus Airdrop" in noteguard.toml so the running relay catches it. Changelog-Changed: content filter now catches homoglyph/case spoofs of blocked phrases Headway: headway:noteguard/copy-resource-merit Agentium: agentium:rate-guess-visit --- Cargo.lock | 47 +++++++++++++++++++++++++++++++--- Cargo.toml | 1 + noteguard.toml | 9 ++++++- src/filters/content.rs | 26 +++++++++++++++++-- src/main.rs | 57 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 134 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4bea359..8f3cc97 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -470,7 +470,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -828,6 +828,7 @@ dependencies = [ "tokio", "tokio-tungstenite", "toml", + "unicode-security", "wasmtime", ] @@ -1092,7 +1093,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys 0.12.1", - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -1336,6 +1337,21 @@ dependencies = [ "syn 3.0.5", ] +[[package]] +name = "tinyvec" +version = "1.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cf0ded5c4e56918d8f8a339e1bb67d038d3bc6d144ac407904015ba2e4cde9b" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + [[package]] name = "tokio" version = "1.38.0" @@ -1428,6 +1444,31 @@ version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +[[package]] +name = "unicode-normalization" +version = "0.1.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fd4f6878c9cb28d874b009da9e8d183b5abc80117c40bbd187a1fde336be6e8" +dependencies = [ + "tinyvec", +] + +[[package]] +name = "unicode-script" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "383ad40bb927465ec0ce7720e033cb4ca06912855fc35db31b5755d0de75b1ee" + +[[package]] +name = "unicode-security" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e4ddba1535dd35ed8b61c52166b7155d7f4e4b8847cec6f48e71dc66d8b5e50" +dependencies = [ + "unicode-normalization", + "unicode-script", +] + [[package]] name = "utf-8" version = "0.7.6" @@ -1667,7 +1708,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 0042854..d9c4cdf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ serde = { version = "1.0", features = ["derive"] } serde_json = { version = "1.0", features = ["raw_value"] } toml = "0.5" ipnetwork = "0.20.0" +unicode-security = "0.1.2" # forwarder deps tokio-tungstenite = { version = "0.23.1", optional = true, features = ["native-tls"] } diff --git a/noteguard.toml b/noteguard.toml index 0e3893f..3672cae 100644 --- a/noteguard.toml +++ b/noteguard.toml @@ -12,8 +12,15 @@ ban_after = 10 ban_duration_secs = 3600 # one hour ban_message = "banned: too many rate-limit violations, try again later" +# Substring blocklist. Matching is homoglyph- and case-insensitive: both the +# note content and each term are folded to their UTS #39 confusables skeleton +# and lowercased before comparison, so Cyrillic/look-alike spoofs like +# "Dаmus Аirdrop" (Cyrillic а/А) are caught by the plain "Damus Airdrop" entry. [filters.content] -filters = ["https://cdn.nostr.build/i/some-spammy-or-abusive-image-image.png"] +filters = [ + "https://cdn.nostr.build/i/some-spammy-or-abusive-image-image.png", + "Damus Airdrop", +] [filters.whitelist] pubkeys = ["16c21558762108afc34e4ff19e4ed51d9a48f79e0c34531efc423d21ab435e93"] diff --git a/src/filters/content.rs b/src/filters/content.rs index 2344d18..04d5ba9 100644 --- a/src/filters/content.rs +++ b/src/filters/content.rs @@ -1,15 +1,37 @@ use crate::{Action, InputMessage, NoteFilter, OutputMessage}; use serde::Deserialize; +use unicode_security::skeleton; #[derive(Deserialize, Default)] pub struct Content { filters: Vec, + + /// Confusables-normalized, case-folded copies of `filters`, built lazily on + /// first use. Matching against these catches homoglyph spoofs — e.g. Cyrillic + /// `а`/`А` (U+0430/U+0410) standing in for Latin `a`/`A` in "Damus Airdrop" + /// spam — that a raw byte-wise `contains` check silently misses. + #[serde(skip)] + normalized: Option>, +} + +/// Fold a string to its UTS #39 confusables skeleton, case-folded, so that +/// visually-identical strings built from different codepoints compare equal. +/// Case is folded both before and after skeletonizing: the confusable prototypes +/// are not case-stable (uppercase `А` and lowercase `а` skeletonize differently), +/// so lowercasing up front lines the inputs up before the skeleton is taken. +fn normalize(s: &str) -> String { + skeleton(&s.to_lowercase()).collect::().to_lowercase() } impl NoteFilter for Content { fn filter_note(&mut self, msg: &InputMessage) -> OutputMessage { - for filter in &self.filters { - if msg.event.content.contains(filter) { + let normalized = self + .normalized + .get_or_insert_with(|| self.filters.iter().map(|f| normalize(f)).collect()); + + let content = normalize(&msg.event.content); + for filter in normalized.iter() { + if content.contains(filter) { return OutputMessage::new(msg.event.id.clone(), Action::ShadowReject, None); } } diff --git a/src/main.rs b/src/main.rs index afc7c2a..7b6d96a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -407,6 +407,63 @@ mod tests { } } + // Helper to build a mock "new" message carrying arbitrary note content. + fn create_mock_content_message(content: &str) -> InputMessage { + let mut msg = create_mock_input_message("content_event", "new", "mock_source_info"); + msg.event.content = content.to_string(); + msg + } + + #[test] + fn test_content_filter_homoglyph() { + let mut noteguard = Noteguard::new(); + + let config: Config = toml::from_str( + r#" + pipeline = ["content"] + [filters.content] + filters = ["Damus Airdrop"] + "#, + ) + .expect("Failed to parse config"); + + noteguard + .load_config(&config) + .expect("Failed to load config"); + + // "Dаmus Аirdrop" — the 'а'/'А' are Cyrillic (U+0430/U+0410) homoglyphs, + // the actual bytes seen in airdrop-scam spam. A raw substring check would + // miss this; the confusables skeleton folds it back to "Damus Airdrop". + let spam = "\u{1F680} D\u{0430}mus \u{0410}irdrop is open \u{00B7} \ + Check your claim https://tinyurl.com/damuslive/"; + assert_eq!( + noteguard.run(create_mock_content_message(spam)).action, + Action::ShadowReject + ); + + // Plain ASCII match and a case variant both still get caught. + assert_eq!( + noteguard + .run(create_mock_content_message("free Damus Airdrop here")) + .action, + Action::ShadowReject + ); + assert_eq!( + noteguard + .run(create_mock_content_message("DAMUS AIRDROP!!!")) + .action, + Action::ShadowReject + ); + + // An unrelated note is accepted. + assert_eq!( + noteguard + .run(create_mock_content_message("gm nostr, lovely morning")) + .action, + Action::Accept + ); + } + #[test] fn test_deserialize_input_message() { let input_json = r#"