[PR #6590] [MERGED] fix(ip-limit): append fail2ban lines only after the scan commits #6364

Closed
opened 2026-11-02 04:08:14 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6590
Author: @n0ctal
Created: 9/17/2026
Status: ✅ Merged
Merged: 9/26/2026
Merged by: @MHSanaei

Base: main ← Head: upstream-ban-publish-after-commit


📝 Commits (1)

  • ccaccdd fix(ip-limit): append fail2ban lines only after the scan commits

📊 Changes

4 files changed (+339 additions, -88 deletions)

View changed files

➕ internal/web/job/check_client_ip_ban_commit_test.go (+212 -0)
📝 internal/web/job/check_client_ip_frozen_ban_test.go (+3 -5)
📝 internal/web/job/check_client_ip_job.go (+101 -68)
📝 internal/web/job/check_client_ip_job_integration_test.go (+23 -15)

📄 Description

Summary

The IP-limit scan wrote its fail2ban lines from inside the database
transaction, so a scan that later failed to commit still banned the addresses
it had decided on.

Why

updateInboundClientIps opened GetIPLimitLogPath() and wrote the
[LIMIT_IP] … lines while the caller's transaction was still open. If
tx.Commit() then returned an error, the IP-tracking rows rolled back but the
fail2ban trigger file had already been appended to, and fail2ban acts on the
file, not on the database. The ban is not reversible from the panel.

The same path advanced bannedSeen before the commit. On the next scan the
addresses looked "already banned", so the retry that should have followed the
rollback was suppressed: the client was banned on state that never existed,
and the panel had no record of it.

Scope

  • updateInboundClientIps returns ban candidates instead of publishing them;
    it no longer opens the log file or touches bannedSeen.
  • New publishBans runs after tx.Commit() succeeds, opens the log once for
    the whole batch, and records bannedSeen only after a line is written.
  • writeBanLines takes an io.Writer and checks the fmt.Fprintf error, so a
    short write leaves the ban retryable instead of silently marking it done.
  • selectAdvancedSinceLastBan is now a pure filter; the prune of stale
    bannedSeen entries runs for every enforced client, so a client that drops
    back under its limit is forgotten and can be banned again if it returns.
  • The disconnect loop iterates the published bans rather than every candidate.

The log wording is unchanged, byte for byte, including the
2006/01/02 15:04:05 prefix that log.LstdFlags produced: x-ui.sh
create_iplimit_jails builds filter.d/3x-ipl.conf failregex from it.

Validation

  • go test ./internal/web/job/ passes on this branch, rebased onto 1c0ce80e.

  • TestProcessObserved_CommitFailureDoesNotPublishBan is the guard. Moving
    publishBans back above tx.Commit() — the behaviour before this change —
    fails it on both assertions:

    the rollback still touched the fail2ban trigger file
    the rollback advanced bannedSeen and would suppress the retry
    
  • Four further tests cover the publish-failure retry, the bannedSeen prune
    when a client returns under its limit, the writeBanLines write error, and
    the committed happy path.

  • go build ./... and go vet ./internal/web/job/ are clean.

Risk

Low. The change is confined to when the lines are written, not what they say;
a failed publish now defers the ban to the next scan rather than losing it.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/6590 **Author:** [@n0ctal](https://github.com/n0ctal) **Created:** 9/17/2026 **Status:** ✅ Merged **Merged:** 9/26/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `upstream-ban-publish-after-commit` --- ### 📝 Commits (1) - [`ccaccdd`](https://github.com/MHSanaei/3x-ui/commit/ccaccdd74bf39badc42bb37dcd35669723e4cca1) fix(ip-limit): append fail2ban lines only after the scan commits ### 📊 Changes **4 files changed** (+339 additions, -88 deletions) <details> <summary>View changed files</summary> ➕ `internal/web/job/check_client_ip_ban_commit_test.go` (+212 -0) 📝 `internal/web/job/check_client_ip_frozen_ban_test.go` (+3 -5) 📝 `internal/web/job/check_client_ip_job.go` (+101 -68) 📝 `internal/web/job/check_client_ip_job_integration_test.go` (+23 -15) </details> ### 📄 Description ## Summary The IP-limit scan wrote its fail2ban lines from inside the database transaction, so a scan that later failed to commit still banned the addresses it had decided on. ## Why `updateInboundClientIps` opened `GetIPLimitLogPath()` and wrote the `[LIMIT_IP] …` lines while the caller's transaction was still open. If `tx.Commit()` then returned an error, the IP-tracking rows rolled back but the fail2ban trigger file had already been appended to, and fail2ban acts on the file, not on the database. The ban is not reversible from the panel. The same path advanced `bannedSeen` before the commit. On the next scan the addresses looked "already banned", so the retry that should have followed the rollback was suppressed: the client was banned on state that never existed, and the panel had no record of it. ## Scope - `updateInboundClientIps` returns ban candidates instead of publishing them; it no longer opens the log file or touches `bannedSeen`. - New `publishBans` runs after `tx.Commit()` succeeds, opens the log once for the whole batch, and records `bannedSeen` only after a line is written. - `writeBanLines` takes an `io.Writer` and checks the `fmt.Fprintf` error, so a short write leaves the ban retryable instead of silently marking it done. - `selectAdvancedSinceLastBan` is now a pure filter; the prune of stale `bannedSeen` entries runs for every enforced client, so a client that drops back under its limit is forgotten and can be banned again if it returns. - The disconnect loop iterates the published bans rather than every candidate. The log wording is unchanged, byte for byte, including the `2006/01/02 15:04:05` prefix that `log.LstdFlags` produced: `x-ui.sh` `create_iplimit_jails` builds `filter.d/3x-ipl.conf` `failregex` from it. ## Validation - `go test ./internal/web/job/` passes on this branch, rebased onto `1c0ce80e`. - `TestProcessObserved_CommitFailureDoesNotPublishBan` is the guard. Moving `publishBans` back above `tx.Commit()` — the behaviour before this change — fails it on both assertions: ``` the rollback still touched the fail2ban trigger file the rollback advanced bannedSeen and would suppress the retry ``` - Four further tests cover the publish-failure retry, the `bannedSeen` prune when a client returns under its limit, the `writeBanLines` write error, and the committed happy path. - `go build ./...` and `go vet ./internal/web/job/` are clean. ## Risk Low. The change is confined to when the lines are written, not what they say; a failed publish now defers the ban to the next scan rather than losing it. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:08:14 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#6364
No description provided.