Skip to content

Commit 7dfd539

Browse files
committed
WIP: Add integration test
1 parent 20e18ed commit 7dfd539

3 files changed

Lines changed: 123 additions & 0 deletions

File tree

banjax_base_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const fixtureConfigTestShaInv = "./fixtures/banjax-config-test-sha-inv.yaml"
2424
const fixtureConfigTestRegexBanner = "./fixtures/banjax-config-test-regex-banner.yaml"
2525
const fixtureConfigTestReloadCIDR = "./fixtures/banjax-config-test-reload-cidr.yaml"
2626
const fixtureConfigTestPersiteFail = "./fixtures/banjax-config-test-persite-fail.yaml"
27+
const fixtureConfigTestUA = "./fixtures/banjax-config-test-ua.yaml"
2728

2829
var tmpDir string
2930
var configFile string
@@ -194,6 +195,17 @@ func ClientIP(ip string) http.Header {
194195
return http.Header{"X-Client-IP": {ip}}
195196
}
196197

198+
func ClientUserAgent(ua string) http.Header {
199+
return http.Header{"X-Client-User-Agent": {ua}}
200+
}
201+
202+
func ClientIPAndUserAgent(ip string, ua string) http.Header {
203+
return http.Header{
204+
"X-Client-IP": {ip},
205+
"X-Client-User-Agent": {ua},
206+
}
207+
}
208+
197209
func randomIP() string {
198210
octets := []string{}
199211
for i := 0; i < 4; i++ {

banjax_integration_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,3 +405,59 @@ func TestRegexesWithRatesAllowList(t *testing.T) {
405405
{"GET", prefix + "/blockme/", 200, ClientIP("20.20.20.20"), nil},
406406
})
407407
}
408+
409+
func TestGlobalUserAgentDecisionLists(t *testing.T) {
410+
defer reloadConfig(fixtureConfigTest, 1, t)
411+
412+
reloadConfig(fixtureConfigTestUA, 1, t)
413+
414+
/*
415+
global_user_agent_decision_lists:
416+
nginx_block:
417+
- "AhrefsBot"
418+
- "SemrushBot"
419+
challenge:
420+
- "Macintosh.*Firefox/\\d+"
421+
*/
422+
prefix := "/auth_request?path="
423+
httpTester(t, []TestResource{
424+
{"GET", "/info", 200, nil, []string{"2025-01-01"}},
425+
// AhrefsBot is globally nginx_blocked (403)
426+
{"GET", prefix + "/ua_ahref", 403, ClientUserAgent("Mozilla/5.0 (compatible; AhrefsBot/7.0; +http://ahrefs.com/robot/)"), nil},
427+
// SemrushBot is globally nginx_blocked (403)
428+
{"GET", prefix + "/ua_semrush", 403, ClientUserAgent("Mozilla/5.0 (compatible; SemrushBot/7.0; +http://www.semrush.com/bot.html)"), nil},
429+
// Firefox on Mac is globally challenged (429)
430+
{"GET", prefix + "/ua_firefox_mac", 429, ClientUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:149.0) Gecko/20100101 Firefox/149.0"), nil},
431+
// Firefox on Windows does not match the Macintosh pattern — allowed (200)
432+
{"GET", prefix + "/ua_firefox_win", 200, ClientUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:149.0) Gecko/20100101 Firefox/149.0"), nil},
433+
// Googlebot has no UA rule — allowed (200)
434+
{"GET", prefix + "/ua_googlebot", 200, ClientUserAgent("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"), nil},
435+
})
436+
}
437+
438+
func TestPerSiteUserAgentDecisionLists(t *testing.T) {
439+
defer reloadConfig(fixtureConfigTest, 1, t)
440+
441+
reloadConfig(fixtureConfigTestUA, 1, t)
442+
443+
/*
444+
per_site_user_agent_decision_lists:
445+
"localhost:8081":
446+
allow:
447+
- "GPTBot"
448+
449+
global_decision_lists:
450+
challenge:
451+
- 8.8.8.8
452+
*/
453+
prefix := "/auth_request?path="
454+
httpTester(t, []TestResource{
455+
{"GET", "/info", 200, nil, []string{"2025-01-01"}},
456+
// 8.8.8.8 is in global challenge IP list — should be challenged without a UA override
457+
{"GET", prefix + "/ua_ip_challenge", 429, ClientIP("8.8.8.8"), nil},
458+
// GPTBot from 8.8.8.8: per-site UA allow overrides the global IP challenge
459+
{"GET", prefix + "/ua_gptbot_override", 200, ClientIPAndUserAgent("8.8.8.8", "Mozilla/5.0 (compatible; GPTBot/1.0; +https://openai.com/gptbot)"), nil},
460+
// AhrefsBot from 8.8.8.8: global IP challenge fires before global UA block (per-site UA has no AhrefsBot rule)
461+
{"GET", prefix + "/ua_ahref_challenged_ip", 429, ClientIPAndUserAgent("8.8.8.8", "Mozilla/5.0 (compatible; AhrefsBot/7.0)"), nil},
462+
})
463+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
config_version: 2025-01-01_00:00:00
2+
global_decision_lists:
3+
allow:
4+
- 20.20.20.20
5+
nginx_block:
6+
- 70.80.90.100
7+
challenge:
8+
- 8.8.8.8
9+
iptables_ban_seconds: 10
10+
iptables_unbanner_seconds: 5
11+
kafka_brokers:
12+
- "localhost:9092"
13+
kafka_security_protocol: 'ssl'
14+
kafka_ssl_ca: "/etc/banjax/caroot.pem"
15+
kafka_ssl_key: "/etc/banjax/key.pem"
16+
kafka_ssl_key_password: password
17+
kafka_report_topic: 'banjax_report_topic'
18+
kafka_command_topic: 'banjax_command_topic'
19+
password_protected_paths:
20+
"localhost":
21+
- wp-admin
22+
password_protected_path_exceptions:
23+
"localhost":
24+
- wp-admin/admin-ajax.php
25+
password_hashes:
26+
"localhost:8081": "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8"
27+
"localhost": "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8"
28+
sitewide_sha_inv_list:
29+
example.com: block
30+
foobar.com: no_block
31+
server_log_file: /var/log/banjax/banjax-format.log
32+
banning_log_file: /etc/banjax/ban_ip_list.log
33+
expiring_decision_ttl_seconds: 10
34+
too_many_failed_challenges_interval_seconds: 10
35+
too_many_failed_challenges_threshold: 6
36+
password_cookie_ttl_seconds: 14400
37+
sha_inv_cookie_ttl_seconds: 14400
38+
hmac_secret: secret
39+
gin_log_file: /var/log/banjax/gin.log
40+
metrics_log_file: /var/log/banjax/metrics.log
41+
standalone_testing: true
42+
43+
# UA blocking: AhrefsBot globally blocked, Firefox on Mac globally challenged
44+
# GPTBot is allowed on localhost (per-site override of the global IP block for 8.8.8.8)
45+
global_user_agent_decision_lists:
46+
nginx_block:
47+
- "AhrefsBot"
48+
- "SemrushBot"
49+
challenge:
50+
- "Macintosh.*Firefox/\\d+"
51+
52+
per_site_user_agent_decision_lists:
53+
"localhost":
54+
allow:
55+
- "GPTBot"

0 commit comments

Comments
 (0)