check_client_ip_job_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. package job
  2. import (
  3. "os"
  4. "path/filepath"
  5. "reflect"
  6. "runtime"
  7. "testing"
  8. "time"
  9. )
  10. func TestMergeClientIps_EvictsStaleOldEntries(t *testing.T) {
  11. // #4077: after a ban expires, a single IP that reconnects used to get
  12. // banned again immediately because a long-disconnected IP stayed in the
  13. // DB with an ancient timestamp and kept "protecting" itself against
  14. // eviction. Guard against that regression here.
  15. old := []IPWithTimestamp{
  16. {IP: "1.1.1.1", Timestamp: 100}, // stale — client disconnected long ago
  17. {IP: "2.2.2.2", Timestamp: 1900}, // fresh — still connecting
  18. }
  19. new := []IPWithTimestamp{
  20. {IP: "2.2.2.2", Timestamp: 2000}, // same IP, newer log line
  21. }
  22. got := mergeClientIps(old, new, 1000, false)
  23. want := map[string]int64{"2.2.2.2": 2000}
  24. if !reflect.DeepEqual(got, want) {
  25. t.Fatalf("stale 1.1.1.1 should have been dropped\ngot: %v\nwant: %v", got, want)
  26. }
  27. }
  28. func TestMergeClientIps_KeepsFreshOldEntriesUnchanged(t *testing.T) {
  29. // Backwards-compat: entries that aren't stale are still carried forward,
  30. // so enforcement survives access-log rotation.
  31. old := []IPWithTimestamp{
  32. {IP: "1.1.1.1", Timestamp: 1500},
  33. }
  34. got := mergeClientIps(old, nil, 1000, false)
  35. want := map[string]int64{"1.1.1.1": 1500}
  36. if !reflect.DeepEqual(got, want) {
  37. t.Fatalf("fresh old IP should have been retained\ngot: %v\nwant: %v", got, want)
  38. }
  39. }
  40. func TestMergeClientIps_PrefersLaterTimestampForSameIp(t *testing.T) {
  41. old := []IPWithTimestamp{{IP: "1.1.1.1", Timestamp: 1500}}
  42. new := []IPWithTimestamp{{IP: "1.1.1.1", Timestamp: 1700}}
  43. got := mergeClientIps(old, new, 1000, false)
  44. if got["1.1.1.1"] != 1700 {
  45. t.Fatalf("expected latest timestamp 1700, got %d", got["1.1.1.1"])
  46. }
  47. }
  48. func TestMergeClientIps_DropsStaleNewEntries(t *testing.T) {
  49. // A log line with a clock-skewed old timestamp must not resurrect a
  50. // stale IP past the cutoff.
  51. new := []IPWithTimestamp{{IP: "1.1.1.1", Timestamp: 500}}
  52. got := mergeClientIps(nil, new, 1000, false)
  53. if len(got) != 0 {
  54. t.Fatalf("stale new IP should have been dropped, got %v", got)
  55. }
  56. }
  57. func TestMergeClientIps_NoStaleCutoffStillWorks(t *testing.T) {
  58. // Defensive: a zero cutoff (e.g. during very first run on a fresh
  59. // install) must not over-evict.
  60. old := []IPWithTimestamp{{IP: "1.1.1.1", Timestamp: 100}}
  61. new := []IPWithTimestamp{{IP: "2.2.2.2", Timestamp: 200}}
  62. got := mergeClientIps(old, new, 0, false)
  63. want := map[string]int64{"1.1.1.1": 100, "2.2.2.2": 200}
  64. if !reflect.DeepEqual(got, want) {
  65. t.Fatalf("zero cutoff should keep everything\ngot: %v\nwant: %v", got, want)
  66. }
  67. }
  68. func TestMergeClientIps_LiveObservationsBypassStaleCutoff(t *testing.T) {
  69. // online-API mode: lastSeen is set when the connection was dispatched, so
  70. // a connection held open for hours has an "old" timestamp while being live
  71. // by definition. It must survive the stale cutoff.
  72. new := []IPWithTimestamp{{IP: "1.1.1.1", Timestamp: 500}} // opened long ago, still connected
  73. got := mergeClientIps(nil, new, 1000, true)
  74. want := map[string]int64{"1.1.1.1": 500}
  75. if !reflect.DeepEqual(got, want) {
  76. t.Fatalf("live observation must bypass the stale cutoff\ngot: %v\nwant: %v", got, want)
  77. }
  78. }
  79. func TestMergeClientIps_LiveModeStillEvictsStaleOldEntries(t *testing.T) {
  80. // the bypass applies only to this scan's observations — persisted entries
  81. // from past scans still age out as before.
  82. old := []IPWithTimestamp{{IP: "2.2.2.2", Timestamp: 100}}
  83. new := []IPWithTimestamp{{IP: "1.1.1.1", Timestamp: 2000}}
  84. got := mergeClientIps(old, new, 1000, true)
  85. want := map[string]int64{"1.1.1.1": 2000}
  86. if !reflect.DeepEqual(got, want) {
  87. t.Fatalf("stale db entry must still be evicted in live mode\ngot: %v\nwant: %v", got, want)
  88. }
  89. }
  90. func TestSelectIpsToBan(t *testing.T) {
  91. live := []IPWithTimestamp{ // sorted oldest-first, as partitionLiveIps returns
  92. {IP: "A", Timestamp: 100},
  93. {IP: "B", Timestamp: 200},
  94. {IP: "C", Timestamp: 300},
  95. }
  96. // over the limit: oldest connections are banned, newest keep the slots
  97. kept, banned := selectIpsToBan(live, 1)
  98. if got := collectIps(kept); !reflect.DeepEqual(got, []string{"C"}) {
  99. t.Fatalf("newest ip must keep the slot, got %v", got)
  100. }
  101. if got := collectIps(banned); !reflect.DeepEqual(got, []string{"A", "B"}) {
  102. t.Fatalf("older ips must be banned oldest-first, got %v", got)
  103. }
  104. // at the limit: nothing banned
  105. kept, banned = selectIpsToBan(live, 3)
  106. if len(banned) != 0 || len(kept) != 3 {
  107. t.Fatalf("at-limit set must not ban, kept=%v banned=%v", kept, banned)
  108. }
  109. // under the limit: nothing banned
  110. kept, banned = selectIpsToBan(live[:1], 3)
  111. if len(banned) != 0 || len(kept) != 1 {
  112. t.Fatalf("under-limit set must not ban, kept=%v banned=%v", kept, banned)
  113. }
  114. // defensive: non-positive limit never reaches enforcement, but must not panic
  115. if _, banned := selectIpsToBan(live, 0); banned != nil {
  116. t.Fatalf("zero limit must not ban, got %v", banned)
  117. }
  118. }
  119. func collectIps(entries []IPWithTimestamp) []string {
  120. out := make([]string, 0, len(entries))
  121. for _, e := range entries {
  122. out = append(out, e.IP)
  123. }
  124. return out
  125. }
  126. func TestPartitionLiveIps_SingleLiveNotStarvedByStillFreshHistoricals(t *testing.T) {
  127. // #4091: db holds A, B, C from minutes ago (still in the 30min
  128. // window) but they're not connecting anymore. only D is. old code
  129. // merged all four, sorted ascending, kept [A,B,C] and banned D
  130. // every tick. pin the new rule: only live ips count toward the limit.
  131. ipMap := map[string]int64{
  132. "A": 1000,
  133. "B": 1100,
  134. "C": 1200,
  135. "D": 2000,
  136. }
  137. observed := map[string]bool{"D": true}
  138. live, historical := partitionLiveIps(ipMap, observed)
  139. if got := collectIps(live); !reflect.DeepEqual(got, []string{"D"}) {
  140. t.Fatalf("live set should only contain the ip observed this scan\ngot: %v\nwant: [D]", got)
  141. }
  142. if got := collectIps(historical); !reflect.DeepEqual(got, []string{"A", "B", "C"}) {
  143. t.Fatalf("historical set should contain db-only ips in ascending order\ngot: %v\nwant: [A B C]", got)
  144. }
  145. }
  146. func TestPartitionLiveIps_ConcurrentLiveIpsSortedAscending(t *testing.T) {
  147. // when several ips are really live, partition returns them all in the
  148. // live set sorted ascending by timestamp. updateInboundClientIps then
  149. // keeps the newest and bans the oldest (last-IP-wins, #4699).
  150. ipMap := map[string]int64{
  151. "A": 5000,
  152. "B": 5500,
  153. }
  154. observed := map[string]bool{"A": true, "B": true}
  155. live, historical := partitionLiveIps(ipMap, observed)
  156. if got := collectIps(live); !reflect.DeepEqual(got, []string{"A", "B"}) {
  157. t.Fatalf("both live ips should be in the live set, ascending\ngot: %v\nwant: [A B]", got)
  158. }
  159. if len(historical) != 0 {
  160. t.Fatalf("no historical ips expected, got %v", historical)
  161. }
  162. }
  163. func TestPartitionLiveIps_EmptyScanLeavesDbIntact(t *testing.T) {
  164. // quiet tick: nothing observed => nothing live. everything merged
  165. // is historical. keeps the panel from wiping recent-but-idle ips.
  166. ipMap := map[string]int64{
  167. "A": 1000,
  168. "B": 1100,
  169. }
  170. observed := map[string]bool{}
  171. live, historical := partitionLiveIps(ipMap, observed)
  172. if len(live) != 0 {
  173. t.Fatalf("no live ips expected, got %v", live)
  174. }
  175. if got := collectIps(historical); !reflect.DeepEqual(got, []string{"A", "B"}) {
  176. t.Fatalf("all merged entries should flow to historical\ngot: %v\nwant: [A B]", got)
  177. }
  178. }
  179. func TestPartitionLiveIps_RecentSyncedIpIsLive(t *testing.T) {
  180. // Synced IPs from other nodes within 2 minutes should be counted as live
  181. // even if they weren't observed in the local scan.
  182. now := time.Now().Unix()
  183. ipMap := map[string]int64{
  184. "A": now - 30, // synced 30s ago -> live
  185. "B": now - 150, // synced 2m30s ago -> historical
  186. }
  187. observed := map[string]bool{}
  188. live, historical := partitionLiveIps(ipMap, observed)
  189. if got := collectIps(live); !reflect.DeepEqual(got, []string{"A"}) {
  190. t.Fatalf("recent IP should be live\ngot: %v\nwant: [A]", got)
  191. }
  192. if got := collectIps(historical); !reflect.DeepEqual(got, []string{"B"}) {
  193. t.Fatalf("older IP should be historical\ngot: %v\nwant: [B]", got)
  194. }
  195. }
  196. func TestCheckFail2BanInstalled_DisabledEnvSkipsClientProbe(t *testing.T) {
  197. t.Setenv("XUI_ENABLE_FAIL2BAN", "false")
  198. marker := fakeFail2BanClient(t)
  199. if (&CheckClientIpJob{}).checkFail2BanInstalled() {
  200. t.Fatal("fail2ban should be unavailable when XUI_ENABLE_FAIL2BAN=false")
  201. }
  202. if _, err := os.Stat(marker); !os.IsNotExist(err) {
  203. t.Fatalf("fail2ban-client should not have been executed, stat error: %v", err)
  204. }
  205. }
  206. func TestCheckFail2BanInstalled_EmptyEnvSkipsClientProbe(t *testing.T) {
  207. t.Setenv("XUI_ENABLE_FAIL2BAN", "")
  208. marker := fakeFail2BanClient(t)
  209. if (&CheckClientIpJob{}).checkFail2BanInstalled() {
  210. t.Fatal("fail2ban should be unavailable when XUI_ENABLE_FAIL2BAN is empty")
  211. }
  212. if _, err := os.Stat(marker); !os.IsNotExist(err) {
  213. t.Fatalf("fail2ban-client should not have been executed, stat error: %v", err)
  214. }
  215. }
  216. func TestIsFail2BanEnabled_DefaultsToEnabledWhenUnset(t *testing.T) {
  217. value, ok := os.LookupEnv("XUI_ENABLE_FAIL2BAN")
  218. os.Unsetenv("XUI_ENABLE_FAIL2BAN")
  219. t.Cleanup(func() {
  220. if ok {
  221. os.Setenv("XUI_ENABLE_FAIL2BAN", value)
  222. } else {
  223. os.Unsetenv("XUI_ENABLE_FAIL2BAN")
  224. }
  225. })
  226. if !isFail2BanEnabled() {
  227. t.Fatal("fail2ban should default to enabled when XUI_ENABLE_FAIL2BAN is unset")
  228. }
  229. }
  230. func TestCheckFail2BanInstalled_EnabledEnvProbesClient(t *testing.T) {
  231. t.Setenv("XUI_ENABLE_FAIL2BAN", "true")
  232. marker := fakeFail2BanClient(t)
  233. if !(&CheckClientIpJob{}).checkFail2BanInstalled() {
  234. t.Fatal("fail2ban should be available when the client probe succeeds")
  235. }
  236. if _, err := os.Stat(marker); err != nil {
  237. t.Fatalf("fail2ban-client should have been executed: %v", err)
  238. }
  239. }
  240. func fakeFail2BanClient(t *testing.T) string {
  241. t.Helper()
  242. dir := t.TempDir()
  243. marker := filepath.Join(dir, "probe-called")
  244. fakeClient := filepath.Join(dir, "fail2ban-client")
  245. script := "#!/bin/sh\n: > \"$FAIL2BAN_PROBE_MARKER\"\nexit 0\n"
  246. if runtime.GOOS == "windows" {
  247. fakeClient += ".bat"
  248. script = "@echo off\ntype nul > \"%FAIL2BAN_PROBE_MARKER%\"\nexit /b 0\n"
  249. }
  250. if err := os.WriteFile(fakeClient, []byte(script), 0o755); err != nil {
  251. t.Fatalf("write fake fail2ban-client: %v", err)
  252. }
  253. t.Setenv("FAIL2BAN_PROBE_MARKER", marker)
  254. t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH"))
  255. return marker
  256. }