check_client_ip_job_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package job
  2. import (
  3. "os"
  4. "path/filepath"
  5. "reflect"
  6. "runtime"
  7. "testing"
  8. )
  9. func TestMergeClientIps_EvictsStaleOldEntries(t *testing.T) {
  10. // #4077: after a ban expires, a single IP that reconnects used to get
  11. // banned again immediately because a long-disconnected IP stayed in the
  12. // DB with an ancient timestamp and kept "protecting" itself against
  13. // eviction. Guard against that regression here.
  14. old := []IPWithTimestamp{
  15. {IP: "1.1.1.1", Timestamp: 100}, // stale — client disconnected long ago
  16. {IP: "2.2.2.2", Timestamp: 1900}, // fresh — still connecting
  17. }
  18. new := []IPWithTimestamp{
  19. {IP: "2.2.2.2", Timestamp: 2000}, // same IP, newer log line
  20. }
  21. got := mergeClientIps(old, new, 1000)
  22. want := map[string]int64{"2.2.2.2": 2000}
  23. if !reflect.DeepEqual(got, want) {
  24. t.Fatalf("stale 1.1.1.1 should have been dropped\ngot: %v\nwant: %v", got, want)
  25. }
  26. }
  27. func TestMergeClientIps_KeepsFreshOldEntriesUnchanged(t *testing.T) {
  28. // Backwards-compat: entries that aren't stale are still carried forward,
  29. // so enforcement survives access-log rotation.
  30. old := []IPWithTimestamp{
  31. {IP: "1.1.1.1", Timestamp: 1500},
  32. }
  33. got := mergeClientIps(old, nil, 1000)
  34. want := map[string]int64{"1.1.1.1": 1500}
  35. if !reflect.DeepEqual(got, want) {
  36. t.Fatalf("fresh old IP should have been retained\ngot: %v\nwant: %v", got, want)
  37. }
  38. }
  39. func TestMergeClientIps_PrefersLaterTimestampForSameIp(t *testing.T) {
  40. old := []IPWithTimestamp{{IP: "1.1.1.1", Timestamp: 1500}}
  41. new := []IPWithTimestamp{{IP: "1.1.1.1", Timestamp: 1700}}
  42. got := mergeClientIps(old, new, 1000)
  43. if got["1.1.1.1"] != 1700 {
  44. t.Fatalf("expected latest timestamp 1700, got %d", got["1.1.1.1"])
  45. }
  46. }
  47. func TestMergeClientIps_DropsStaleNewEntries(t *testing.T) {
  48. // A log line with a clock-skewed old timestamp must not resurrect a
  49. // stale IP past the cutoff.
  50. new := []IPWithTimestamp{{IP: "1.1.1.1", Timestamp: 500}}
  51. got := mergeClientIps(nil, new, 1000)
  52. if len(got) != 0 {
  53. t.Fatalf("stale new IP should have been dropped, got %v", got)
  54. }
  55. }
  56. func TestMergeClientIps_NoStaleCutoffStillWorks(t *testing.T) {
  57. // Defensive: a zero cutoff (e.g. during very first run on a fresh
  58. // install) must not over-evict.
  59. old := []IPWithTimestamp{{IP: "1.1.1.1", Timestamp: 100}}
  60. new := []IPWithTimestamp{{IP: "2.2.2.2", Timestamp: 200}}
  61. got := mergeClientIps(old, new, 0)
  62. want := map[string]int64{"1.1.1.1": 100, "2.2.2.2": 200}
  63. if !reflect.DeepEqual(got, want) {
  64. t.Fatalf("zero cutoff should keep everything\ngot: %v\nwant: %v", got, want)
  65. }
  66. }
  67. func collectIps(entries []IPWithTimestamp) []string {
  68. out := make([]string, 0, len(entries))
  69. for _, e := range entries {
  70. out = append(out, e.IP)
  71. }
  72. return out
  73. }
  74. func TestPartitionLiveIps_SingleLiveNotStarvedByStillFreshHistoricals(t *testing.T) {
  75. // #4091: db holds A, B, C from minutes ago (still in the 30min
  76. // window) but they're not connecting anymore. only D is. old code
  77. // merged all four, sorted ascending, kept [A,B,C] and banned D
  78. // every tick. pin the new rule: only live ips count toward the limit.
  79. ipMap := map[string]int64{
  80. "A": 1000,
  81. "B": 1100,
  82. "C": 1200,
  83. "D": 2000,
  84. }
  85. observed := map[string]bool{"D": true}
  86. live, historical := partitionLiveIps(ipMap, observed)
  87. if got := collectIps(live); !reflect.DeepEqual(got, []string{"D"}) {
  88. t.Fatalf("live set should only contain the ip observed this scan\ngot: %v\nwant: [D]", got)
  89. }
  90. if got := collectIps(historical); !reflect.DeepEqual(got, []string{"A", "B", "C"}) {
  91. t.Fatalf("historical set should contain db-only ips in ascending order\ngot: %v\nwant: [A B C]", got)
  92. }
  93. }
  94. func TestPartitionLiveIps_ConcurrentLiveIpsSortedAscending(t *testing.T) {
  95. // when several ips are really live, partition returns them all in the
  96. // live set sorted ascending by timestamp. updateInboundClientIps then
  97. // keeps the newest and bans the oldest (last-IP-wins, #4699).
  98. ipMap := map[string]int64{
  99. "A": 5000,
  100. "B": 5500,
  101. }
  102. observed := map[string]bool{"A": true, "B": true}
  103. live, historical := partitionLiveIps(ipMap, observed)
  104. if got := collectIps(live); !reflect.DeepEqual(got, []string{"A", "B"}) {
  105. t.Fatalf("both live ips should be in the live set, ascending\ngot: %v\nwant: [A B]", got)
  106. }
  107. if len(historical) != 0 {
  108. t.Fatalf("no historical ips expected, got %v", historical)
  109. }
  110. }
  111. func TestPartitionLiveIps_EmptyScanLeavesDbIntact(t *testing.T) {
  112. // quiet tick: nothing observed => nothing live. everything merged
  113. // is historical. keeps the panel from wiping recent-but-idle ips.
  114. ipMap := map[string]int64{
  115. "A": 1000,
  116. "B": 1100,
  117. }
  118. observed := map[string]bool{}
  119. live, historical := partitionLiveIps(ipMap, observed)
  120. if len(live) != 0 {
  121. t.Fatalf("no live ips expected, got %v", live)
  122. }
  123. if got := collectIps(historical); !reflect.DeepEqual(got, []string{"A", "B"}) {
  124. t.Fatalf("all merged entries should flow to historical\ngot: %v\nwant: [A B]", got)
  125. }
  126. }
  127. func TestCheckFail2BanInstalled_DisabledEnvSkipsClientProbe(t *testing.T) {
  128. t.Setenv("XUI_ENABLE_FAIL2BAN", "false")
  129. marker := fakeFail2BanClient(t)
  130. if (&CheckClientIpJob{}).checkFail2BanInstalled() {
  131. t.Fatal("fail2ban should be unavailable when XUI_ENABLE_FAIL2BAN=false")
  132. }
  133. if _, err := os.Stat(marker); !os.IsNotExist(err) {
  134. t.Fatalf("fail2ban-client should not have been executed, stat error: %v", err)
  135. }
  136. }
  137. func TestCheckFail2BanInstalled_EmptyEnvSkipsClientProbe(t *testing.T) {
  138. t.Setenv("XUI_ENABLE_FAIL2BAN", "")
  139. marker := fakeFail2BanClient(t)
  140. if (&CheckClientIpJob{}).checkFail2BanInstalled() {
  141. t.Fatal("fail2ban should be unavailable when XUI_ENABLE_FAIL2BAN is empty")
  142. }
  143. if _, err := os.Stat(marker); !os.IsNotExist(err) {
  144. t.Fatalf("fail2ban-client should not have been executed, stat error: %v", err)
  145. }
  146. }
  147. func TestIsFail2BanEnabled_DefaultsToEnabledWhenUnset(t *testing.T) {
  148. value, ok := os.LookupEnv("XUI_ENABLE_FAIL2BAN")
  149. os.Unsetenv("XUI_ENABLE_FAIL2BAN")
  150. t.Cleanup(func() {
  151. if ok {
  152. os.Setenv("XUI_ENABLE_FAIL2BAN", value)
  153. } else {
  154. os.Unsetenv("XUI_ENABLE_FAIL2BAN")
  155. }
  156. })
  157. if !isFail2BanEnabled() {
  158. t.Fatal("fail2ban should default to enabled when XUI_ENABLE_FAIL2BAN is unset")
  159. }
  160. }
  161. func TestCheckFail2BanInstalled_EnabledEnvProbesClient(t *testing.T) {
  162. t.Setenv("XUI_ENABLE_FAIL2BAN", "true")
  163. marker := fakeFail2BanClient(t)
  164. if !(&CheckClientIpJob{}).checkFail2BanInstalled() {
  165. t.Fatal("fail2ban should be available when the client probe succeeds")
  166. }
  167. if _, err := os.Stat(marker); err != nil {
  168. t.Fatalf("fail2ban-client should have been executed: %v", err)
  169. }
  170. }
  171. func fakeFail2BanClient(t *testing.T) string {
  172. t.Helper()
  173. dir := t.TempDir()
  174. marker := filepath.Join(dir, "probe-called")
  175. fakeClient := filepath.Join(dir, "fail2ban-client")
  176. script := "#!/bin/sh\n: > \"$FAIL2BAN_PROBE_MARKER\"\nexit 0\n"
  177. if runtime.GOOS == "windows" {
  178. fakeClient += ".bat"
  179. script = "@echo off\ntype nul > \"%FAIL2BAN_PROBE_MARKER%\"\nexit /b 0\n"
  180. }
  181. if err := os.WriteFile(fakeClient, []byte(script), 0o755); err != nil {
  182. t.Fatalf("write fake fail2ban-client: %v", err)
  183. }
  184. t.Setenv("FAIL2BAN_PROBE_MARKER", marker)
  185. t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH"))
  186. return marker
  187. }