ratelimit: exempt premium pubkeys via exempt_source
Phase 6 of PLAN.md. New optional `exempt_source` names a [sources.<name>] pubkey list. Active members are accepted right after the IP whitelist and before the IP-ban check, without spending or refilling the IP's tokens or touching its strike streak, so a member on a shared IP with a banned spammer can still post while the ban holds for everyone else. Expired members and a never-loaded list exempt nobody. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
0e29aab85a
commit
a9e4c044bf
@@ -111,6 +111,8 @@ Settings:
|
||||
|
||||
- `whitelist` *optional*: a list of IP4 or IP6 addresses that are allowed to bypass the ratelimit.
|
||||
|
||||
- `exempt_source` *optional*: the name of a `[sources.<name>]` pubkey list (see [Sources](#sources)), typically premium members. Active members skip the rate limit and the IP-ban check, and their posts neither spend the IP's budget nor reset its strike streak.
|
||||
|
||||
- `message` *optional*: the error message to return when connection is rate-limited. default is: `rate-limited: you are noting too much`
|
||||
|
||||
- `ban_after` *optional*: IP-ban persistent offenders. After this many *consecutive* rate-limit rejections from the same IP (a streak that any accepted post resets), every note from that IP is rejected until the ban expires. Omit to disable banning — the filter then only ever rate-limits.
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use crate::filters::ban::BanTracker;
|
||||
use crate::sources::PubkeyRef;
|
||||
use crate::{Action, InputMessage, NoteFilter, OutputMessage};
|
||||
use serde::Deserialize;
|
||||
use std::collections::HashMap;
|
||||
@@ -15,6 +16,10 @@ pub struct RateLimit {
|
||||
pub whitelist: Option<Vec<String>>,
|
||||
pub message: Option<String>,
|
||||
|
||||
/// A `[sources.<name>]` pubkey list (e.g. premium members) whose active
|
||||
/// members skip rate limiting and the IP-ban check entirely.
|
||||
pub exempt_source: Option<PubkeyRef>,
|
||||
|
||||
/// IP-ban persistent offenders. `ban_after` *consecutive* rate-limit
|
||||
/// rejections (a streak any accepted post resets) trips a ban; see
|
||||
/// [`BanTracker`]. Flattened so `ban_after` etc. sit at this filter's level.
|
||||
@@ -37,6 +42,15 @@ impl NoteFilter for RateLimit {
|
||||
}
|
||||
}
|
||||
|
||||
// Exempt pubkeys (premium) are checked before the IP-ban, so a member on
|
||||
// a shared IP with a banned spammer can still post. They neither spend
|
||||
// nor refill the IP's tokens, nor touch its strike streak.
|
||||
if let Some(exempt) = &self.exempt_source {
|
||||
if exempt.is_active(&msg.event.pubkey) {
|
||||
return OutputMessage::new(msg.event.id.clone(), Action::Accept, None);
|
||||
}
|
||||
}
|
||||
|
||||
let now = Instant::now();
|
||||
|
||||
// Enforce an active IP-ban before touching the token bucket, so a banned
|
||||
@@ -113,14 +127,19 @@ impl NoteFilter for RateLimit {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::sources::testing;
|
||||
use crate::Note;
|
||||
|
||||
fn msg(id: &str, ip: &str) -> InputMessage {
|
||||
msg_from(id, ip, "pk")
|
||||
}
|
||||
|
||||
fn msg_from(id: &str, ip: &str, pubkey: &str) -> InputMessage {
|
||||
InputMessage {
|
||||
message_type: "new".to_string(),
|
||||
event: Note {
|
||||
id: id.to_string(),
|
||||
pubkey: "pk".to_string(),
|
||||
pubkey: pubkey.to_string(),
|
||||
created_at: 0,
|
||||
kind: 1,
|
||||
tags: vec![],
|
||||
@@ -203,4 +222,78 @@ mod tests {
|
||||
assert_eq!(r.action, Action::Reject);
|
||||
assert!(!r.msg.unwrap().contains("banned"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn premium_never_rate_limited() {
|
||||
testing::pubkeys("rl_premium", Some(&[("member", None)]));
|
||||
let mut rl = ratelimit("posts_per_minute = 1\nexempt_source = \"rl_premium\"\n");
|
||||
for i in 0..10 {
|
||||
let r = rl.filter_note(&msg_from(&format!("e{i}"), "1.2.3.4", "member"));
|
||||
assert_eq!(r.action, Action::Accept);
|
||||
}
|
||||
// Premium posts spent none of the IP's budget: a non-member from the
|
||||
// same IP still gets its first post through.
|
||||
assert_eq!(rl.filter_note(&msg("x", "1.2.3.4")).action, Action::Accept);
|
||||
assert_eq!(rl.filter_note(&msg("y", "1.2.3.4")).action, Action::Reject);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn premium_accepted_from_banned_ip() {
|
||||
testing::pubkeys("rl_banned_ip", Some(&[("member", None)]));
|
||||
let mut rl = ratelimit(
|
||||
"posts_per_minute = 1\nban_after = 1\nexempt_source = \"rl_banned_ip\"\n",
|
||||
);
|
||||
rl.filter_note(&msg("e1", "6.6.6.6"));
|
||||
let banned = rl.filter_note(&msg("e2", "6.6.6.6"));
|
||||
assert!(banned.msg.unwrap().contains("banned"));
|
||||
|
||||
let r = rl.filter_note(&msg_from("e3", "6.6.6.6", "member"));
|
||||
assert_eq!(r.action, Action::Accept);
|
||||
// The ban still holds for everyone else on that IP.
|
||||
assert!(rl.filter_note(&msg("e4", "6.6.6.6")).msg.unwrap().contains("banned"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn premium_does_not_reset_ip_streak() {
|
||||
testing::pubkeys("rl_streak", Some(&[("member", None)]));
|
||||
let mut rl = ratelimit(
|
||||
"posts_per_minute = 1\nban_after = 2\nexempt_source = \"rl_streak\"\n",
|
||||
);
|
||||
rl.filter_note(&msg("e1", "7.7.7.7"));
|
||||
// Strike 1, then a premium post, then strike 2 must still ban.
|
||||
assert_eq!(rl.filter_note(&msg("e2", "7.7.7.7")).action, Action::Reject);
|
||||
assert_eq!(
|
||||
rl.filter_note(&msg_from("e3", "7.7.7.7", "member")).action,
|
||||
Action::Accept
|
||||
);
|
||||
assert!(rl.filter_note(&msg("e4", "7.7.7.7")).msg.unwrap().contains("banned"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn expired_premium_is_rate_limited() {
|
||||
testing::pubkeys("rl_expired", Some(&[("lapsed", Some(1))]));
|
||||
let mut rl = ratelimit("posts_per_minute = 1\nexempt_source = \"rl_expired\"\n");
|
||||
assert_eq!(
|
||||
rl.filter_note(&msg_from("e1", "8.8.4.4", "lapsed")).action,
|
||||
Action::Accept
|
||||
);
|
||||
assert_eq!(
|
||||
rl.filter_note(&msg_from("e2", "8.8.4.4", "lapsed")).action,
|
||||
Action::Reject
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn never_loaded_source_exempts_nobody() {
|
||||
testing::pubkeys("rl_unloaded", None);
|
||||
let mut rl = ratelimit("posts_per_minute = 1\nexempt_source = \"rl_unloaded\"\n");
|
||||
assert_eq!(
|
||||
rl.filter_note(&msg_from("e1", "4.4.4.4", "member")).action,
|
||||
Action::Accept
|
||||
);
|
||||
assert_eq!(
|
||||
rl.filter_note(&msg_from("e2", "4.4.4.4", "member")).action,
|
||||
Action::Reject
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user