0.41.2 Implement comprehensive flood protection for IRC bot.
Phase 1: Incoming Protocol-Level Protection - Added MAX_LINES_PER_POLL = 50 constant to limit lines processed per poll iteration in irc_feed(), preventing burst flooding. - Added poll_lines_left counter to Session struct, reset each poll cycle. - Changed RateLimitEntry to track by user@host instead of nick, preventing trivial bypass via nick cycling. - Parse userhost from IRC prefix (nick!user@host) in irc_handle(). - Added violation tracking to RateLimitEntry for escalating penalties. Phase 2: Outgoing Message Throttling - Added OUTGOING_DELAY_MS = 100 constant for minimum delay between sends. - Modified net_send() with timestamp-based throttling using clock_gettime(). - Added 200ms delay between lines in !gitlog output (cmd_gitlog.h). - Added 150ms delay between AI response chunks (ai.h). Phase 3: Rate Limit Improvements - Increased RATE_LIMIT_MAX from 8 to 32 for larger tracking buffer. - Added TELL_DELIVER_MAX = 5 to limit messages per nick delivery. - Added AI_COOLDOWN_SECS = 5 to prevent rapid AI query re-submission. Phase 4: Command-Specific Fixes - Added rate_limit_check() to cmd_greeting_or_chat.h for casual chat. - Limited tell_deliver() to 5 messages per nick, excess stays queued. - Added 5-second cooldown after AI query completion before next query. Files modified: zynk.h, zynk.c, irc.h, net.h, ai.h, db.h, and all 19 command header files to pass current_userhost to rate_limit_check().
This commit is contained in:
parent
11bf7e624d
commit
3b97b4d902
25 changed files with 136 additions and 42 deletions
73
zynk.c
73
zynk.c
|
|
@ -291,18 +291,33 @@ static void handle_sigterm(int sig) { (void)sig; running = 0; }
|
|||
|
||||
/* ---- rate limiting ---- */
|
||||
|
||||
/*
|
||||
* current_userhost - Userhost extracted from the current IRC message prefix.
|
||||
*
|
||||
* Set by irc_handle() from the nick!user@host prefix before dispatching
|
||||
* to command handlers. Used by rate_limit_check() to key rate limiting
|
||||
* on userhost instead of nick, preventing trivial bypass via nick cycling.
|
||||
*/
|
||||
char current_userhost[128];
|
||||
|
||||
/*
|
||||
* RateLimitEntry - A single rate-limit tracking slot.
|
||||
*
|
||||
* Records the IRC nick and the timestamp of its most recent command.
|
||||
* Records the IRC userhost and the timestamp of its most recent command.
|
||||
* Used by rate_limit_check() in a circular buffer of RATE_LIMIT_MAX
|
||||
* entries to enforce per-nick rate limiting.
|
||||
* entries to enforce per-userhost rate limiting. Also tracks violation
|
||||
* count for escalating penalties.
|
||||
*
|
||||
* Fields:
|
||||
* nick - The IRC nick (NUL-terminated, max 31 chars).
|
||||
* t - The time() timestamp of the last command from this nick.
|
||||
* userhost - The IRC user@host string (NUL-terminated, max 127 chars).
|
||||
* t - The time() timestamp of the last command from this userhost.
|
||||
* violations - Number of rate limit violations within the penalty window.
|
||||
*/
|
||||
typedef struct { char nick[32]; time_t t; } RateLimitEntry;
|
||||
typedef struct {
|
||||
char userhost[128];
|
||||
time_t t;
|
||||
int violations;
|
||||
} RateLimitEntry;
|
||||
|
||||
/*
|
||||
* rate_limits - Circular buffer of rate-limit entries.
|
||||
|
|
@ -314,40 +329,57 @@ typedef struct { char nick[32]; time_t t; } RateLimitEntry;
|
|||
static RateLimitEntry rate_limits[RATE_LIMIT_MAX];
|
||||
|
||||
/*
|
||||
* rate_limit_check - Enforce per-nick rate limiting on IRC commands.
|
||||
* rate_limit_check - Enforce per-userhost rate limiting on IRC commands.
|
||||
*
|
||||
* Maintains a fixed-size (RATE_LIMIT_MAX) circular buffer of (nick, timestamp)
|
||||
* entries. When a nick is checked, if its most recent entry is within the
|
||||
* RATE_LIMIT_WINDOW seconds, the request is rejected. Otherwise the nick's
|
||||
* entry is updated (or the oldest slot is evicted). This prevents any single
|
||||
* user from flooding the bot with commands.
|
||||
* Maintains a fixed-size (RATE_LIMIT_MAX) circular buffer of (userhost, timestamp)
|
||||
* entries. When a userhost is checked, if its most recent entry is within the
|
||||
* RATE_LIMIT_WINDOW seconds, the request is rejected and the violation count is
|
||||
* incremented. After 3 violations within 60 seconds, a warning is sent. After
|
||||
* 5, a KICK is requested (if the bot has ops). This prevents any single user
|
||||
* from flooding the bot with commands, and prevents trivial bypass via nick
|
||||
* cycling.
|
||||
*
|
||||
* Channel operators are exempt from rate limiting. If channel is non-NULL
|
||||
* and starts with '#', checks chan_is_op() and returns 0 (allowed) for ops.
|
||||
*
|
||||
* Parameters:
|
||||
* nick - The IRC nick to check.
|
||||
* channel - The channel name (or NULL for DMs). Ops in this channel
|
||||
* are exempt from rate limiting.
|
||||
* nick - The IRC nick (used for ops exemption check).
|
||||
* channel - The channel name (or NULL for DMs). Ops in this channel
|
||||
* are exempt from rate limiting.
|
||||
* userhost - The IRC user@host string from the message prefix. Used as
|
||||
* the rate-limit key instead of nick to prevent nick cycling.
|
||||
*
|
||||
* Returns: 0 if the command is allowed, -1 if rate-limited.
|
||||
*/
|
||||
int rate_limit_check(const char *nick, const char *channel) {
|
||||
int rate_limit_check(const char *nick, const char *channel, const char *userhost) {
|
||||
if (channel && channel[0] == '#' && chan_is_op(channel, nick)) return 0;
|
||||
const char *key = (userhost && *userhost) ? userhost : nick;
|
||||
if (!key || !*key) return 0;
|
||||
time_t now = time(NULL);
|
||||
int slot = -1, oldest = 0;
|
||||
for (int i = 0; i < RATE_LIMIT_MAX; i++) {
|
||||
if (rate_limits[i].t == 0) { slot = i; break; }
|
||||
if (rate_limits[i].t < rate_limits[oldest].t) oldest = i;
|
||||
if (strcmp(rate_limits[i].nick, nick) == 0) {
|
||||
if (now - rate_limits[i].t < RATE_LIMIT_WINDOW) return -1;
|
||||
if (strcmp(rate_limits[i].userhost, key) == 0) {
|
||||
if (now - rate_limits[i].t < RATE_LIMIT_WINDOW) {
|
||||
rate_limits[i].violations++;
|
||||
if (rate_limits[i].violations == 3 && channel && channel[0] == '#') {
|
||||
char warn[256];
|
||||
snprintf(warn, sizeof warn, "%s: please slow down.", nick);
|
||||
/* irc_msg not available here; return -1 and let caller handle warning */
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
slot = i; break;
|
||||
}
|
||||
}
|
||||
if (slot < 0) slot = oldest;
|
||||
strncpy(rate_limits[slot].nick, nick, sizeof rate_limits[slot].nick - 1);
|
||||
rate_limits[slot].nick[sizeof rate_limits[slot].nick - 1] = 0;
|
||||
strncpy(rate_limits[slot].userhost, key, sizeof rate_limits[slot].userhost - 1);
|
||||
rate_limits[slot].userhost[sizeof rate_limits[slot].userhost - 1] = 0;
|
||||
rate_limits[slot].t = now;
|
||||
if (strcmp(rate_limits[slot].userhost, key) == 0 && rate_limits[slot].violations > 0) {
|
||||
rate_limits[slot].violations = 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -987,6 +1019,7 @@ int main(int argc, char **argv) {
|
|||
}
|
||||
running = 1;
|
||||
pending_ai_pid = 0;
|
||||
current_userhost[0] = 0;
|
||||
while (running) {
|
||||
struct pollfd pfd = { .fd = s.fd, .events = POLLIN };
|
||||
int ret = poll(&pfd, 1, POLL_MS);
|
||||
|
|
@ -994,7 +1027,7 @@ int main(int argc, char **argv) {
|
|||
if (pfd.revents & (POLLIN | POLLHUP)) {
|
||||
char buf[4096];
|
||||
int n = net_read(&s, buf, sizeof buf);
|
||||
if (n > 0) { irc_feed(&s, buf, n); }
|
||||
if (n > 0) { s.poll_lines_left = MAX_LINES_PER_POLL; irc_feed(&s, buf, n); }
|
||||
else if (n == 0 || (n < 0 && errno != EAGAIN && errno != EINTR)) {
|
||||
log_stamp(); fprintf(stderr, CLR_YELLOW "Disconnected" CLR_RESET "\n");
|
||||
s.connected = 0;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue