package dibaguard import ( "net/http" "net/http/httptest" "net/url" "path/filepath" "strings" "testing" "time" ) type mapState map[string]string func (m mapState) Lookup(ip string, now float64) string { if a, ok := m[ip]; ok { return a } return ActionAllow } func testDecider(tb testing.TB, thr float64) *Decider { tb.Helper() m, err := LoadHTTP2(filepath.Join("testdata", "http2_model.bin")) if err != nil { tb.Skip("no http2 fixture model:", err) } return &Decider{Model: m, Threshold: thr, Inline: true, State: mapState{}, Limiter: NewLimiter(5, 10), Secret: []byte("k")} } func testRequest(target, ip string, hdr map[string]string) Request { u, err := url.ParseRequestURI(target) if err != nil { panic(err) } h := http.Header{} h.Set("X-Forwarded-For", ip) h.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0) Chrome/121") for k, v := range hdr { h.Set(k, v) } return Request{Method: "GET", URL: u, Header: h, ClientIP: ip} } // the cases of cmd/dibaguard-proxy TestProxyFastPath, decided directly func TestDecideProxyCases(t *testing.T) { d := testDecider(t, 1e9) now := time.Now() cases := []struct { name, target string hdr map[string]string action, sig string }{ {"benign", "/product/42?page=2", nil, ActionAllow, ""}, {"persian benign", "/search?q=" + url.QueryEscape("\u06a9\u062a\u0627\u0628 \u0641\u0627\u0631\u0633\u06cc"), nil, ActionAllow, ""}, {"sqli", "/items?id=" + url.QueryEscape("1' or 1=1-- -"), nil, ActionBlock, "A15"}, {"log4shell", "/", map[string]string{"X-Api-Version": "${jndi:ldap://evil/a}"}, ActionBlock, "A08"}, {"traversal", "/download?f=" + url.QueryEscape("../../../../etc/passwd"), nil, ActionBlock, "A17"}, {"command injection", "/ping?host=" + url.QueryEscape("127.0.0.1;cat /etc/passwd"), nil, ActionBlock, "A18"}, {"sensitive file probe", "/.git/config", nil, ActionChallenge, "A21"}, } for _, c := range cases { v := d.Decide(testRequest(c.target, "1.1.1.1", c.hdr), now) if v.Action != c.action { t.Errorf("%s: action %s want %s (%+v)", c.name, v.Action, c.action, v) } if c.sig != "" && (len(v.SigIDs) == 0 || v.SigIDs[0] != c.sig || v.Reason != "sig:"+c.sig) { t.Errorf("%s: sigs %v reason %q want %s", c.name, v.SigIDs, v.Reason, c.sig) } if !v.Scored || v.Text == "" { t.Errorf("%s: not scored / no canonical text", c.name) } if (c.action != ActionAllow) != v.Flagged { t.Errorf("%s: flagged %v", c.name, v.Flagged) } } // a client that passed the challenge is not challenged again for a probe; a hard signature still blocks tok := ChallengeToken(d.Secret, "1.1.1.1", now) if v := d.Decide(testRequest("/.git/config", "1.1.1.1", map[string]string{"Cookie": ChallengeCookie + "=" + tok}), now); v.Action != ActionAllow || !v.Flagged { t.Errorf("challenge passed: %+v", v) } // the cookie is bound to the address if v := d.Decide(testRequest("/.git/config", "2.2.2.2", map[string]string{"Cookie": ChallengeCookie + "=" + tok}), now); v.Action != ActionChallenge { t.Errorf("another address's cookie: %+v", v) } // Inline off: content only flags d.Inline = false if v := d.Decide(testRequest("/items?id="+url.QueryEscape("1' or 1=1-- -"), "1.1.1.1", nil), now); v.Action != ActionAllow || !v.Flagged { t.Errorf("inline off: %+v", v) } } func TestDecideModelPath(t *testing.T) { // a threshold everything is far over: the model flags and (score > thr+3) challenges a clean-signature request d := testDecider(t, -1e6) v := d.Decide(testRequest("/product/42", "1.1.1.1", nil), time.Now()) if v.Action != ActionChallenge || v.Reason != "model" || !v.Flagged { t.Errorf("model path: %+v", v) } // no model: signatures still decide, nothing is scored d.Model = nil if v := d.Decide(testRequest("/product/42", "1.1.1.1", nil), time.Now()); v.Action != ActionAllow || v.Scored || v.Flagged { t.Errorf("no model benign: %+v", v) } if v := d.Decide(testRequest("/items?id="+url.QueryEscape("1 union select password from users"), "1.1.1.1", nil), time.Now()); v.Action != ActionBlock { t.Errorf("no model sqli: %+v", v) } } func TestDecideSource(t *testing.T) { d := testDecider(t, 1e9) d.State = mapState{"7.7.7.7": ActionBlock, "6.6.6.6": ActionChallenge, "5.5.5.5": ActionRateLimit} d.Limiter = NewLimiter(1, 2) now := time.Now() if v := d.Decide(testRequest("/product/1", "7.7.7.7", nil), now); v.Action != ActionBlock || v.Reason != "sidecar:block" || v.Text != "" { t.Errorf("blocked source: %+v", v) } if v := d.Decide(testRequest("/product/1", "6.6.6.6", nil), now); v.Action != ActionChallenge || v.Reason != "sidecar:challenge" { t.Errorf("challenged source: %+v", v) } ok := map[string]string{"Cookie": ChallengeCookie + "=" + ChallengeToken(d.Secret, "6.6.6.6", now)} if v := d.Decide(testRequest("/product/1", "6.6.6.6", ok), now); v.Action != ActionAllow { t.Errorf("challenged source with cookie: %+v", v) } got := []string{} for i := 0; i < 4; i++ { got = append(got, d.Decide(testRequest("/product/1", "5.5.5.5", nil), now).Action) } if strings.Join(got, ",") != "allow,allow,rate_limit,rate_limit" { t.Errorf("rate-limited source (burst 2): %v", got) } } func TestSourceActionDeadlines(t *testing.T) { now := 1000.0 b, c, r := map[string]float64{"a": 1001, "x": 999}, map[string]float64{"a": 2000, "b": 1001}, map[string]float64{"c": 1001} for ip, want := range map[string]string{"a": ActionBlock, "x": ActionAllow, "b": ActionChallenge, "c": ActionRateLimit, "z": ActionAllow} { if got := SourceAction(b, c, r, ip, now); got != want { t.Errorf("%s: %s want %s", ip, got, want) } } if got := SourceAction(nil, nil, nil, "a", now); got != ActionAllow { t.Errorf("nil maps: %s", got) } } func TestClientIP(t *testing.T) { tp, err := ParseTrustedProxies("127.0.0.1/32, 10.0.0.0/8, ::1, 2001:db8::/32") if err != nil { t.Fatal(err) } xff := []string{"X-Forwarded-For", "X-Real-IP"} cases := []struct { name, peer string hdr map[string][]string want string }{ {"untrusted peer, spoofed XFF", "203.0.113.9:5555", map[string][]string{"X-Forwarded-For": {"1.2.3.4"}}, "203.0.113.9"}, {"untrusted peer, spoofed X-Real-IP", "203.0.113.9:5555", map[string][]string{"X-Real-Ip": {"1.2.3.4"}}, "203.0.113.9"}, {"trusted peer, XFF", "127.0.0.1:4000", map[string][]string{"X-Forwarded-For": {"9.9.9.9"}}, "9.9.9.9"}, {"trusted peer, client-written XFF left of the real hop", "127.0.0.1:4000", map[string][]string{"X-Forwarded-For": {"1.2.3.4, 9.9.9.9"}}, "9.9.9.9"}, {"trusted chain", "127.0.0.1:4000", map[string][]string{"X-Forwarded-For": {"9.9.9.9, 10.1.2.3"}}, "9.9.9.9"}, {"multiple XFF lines", "127.0.0.1:4000", map[string][]string{"X-Forwarded-For": {"1.2.3.4", "9.9.9.9, 10.0.0.1"}}, "9.9.9.9"}, {"garbage stops the walk", "127.0.0.1:4000", map[string][]string{"X-Forwarded-For": {"9.9.9.9, evil, 10.0.0.7"}}, "10.0.0.7"}, {"hop with port", "127.0.0.1:4000", map[string][]string{"X-Forwarded-For": {"9.9.9.9:443"}}, "9.9.9.9"}, {"v6 hop", "[::1]:4000", map[string][]string{"X-Forwarded-For": {"[2a00:1450::1]:443"}}, "2a00:1450::1"}, {"v4-mapped peer", "[::ffff:127.0.0.1]:4000", map[string][]string{"X-Forwarded-For": {"9.9.9.9"}}, "9.9.9.9"}, {"X-Real-IP fallback", "127.0.0.1:4000", map[string][]string{"X-Real-Ip": {"9.9.9.9"}}, "9.9.9.9"}, {"invalid X-Real-IP", "127.0.0.1:4000", map[string][]string{"X-Real-Ip": {"nope"}}, "127.0.0.1"}, {"no headers", "127.0.0.1:4000", nil, "127.0.0.1"}, {"unix socket peer", "@", map[string][]string{"X-Forwarded-For": {"9.9.9.9"}}, "9.9.9.9"}, } for _, c := range cases { h := http.Header{} for k, vs := range c.hdr { for _, v := range vs { h.Add(k, v) } } if got := tp.ClientIP(c.peer, h, xff); got != c.want { t.Errorf("%s: %s want %s", c.name, got, c.want) } } if _, err := ParseTrustedProxies("10.0.0.0/33"); err == nil { t.Error("bad CIDR accepted") } if none, _ := ParseTrustedProxies(""); none.ClientIP("127.0.0.1:1", http.Header{"X-Forwarded-For": {"9.9.9.9"}}, xff) != "127.0.0.1" { t.Error("empty trusted list must trust nobody") } } // the page is byte-for-byte the one dibaguard-proxy served before the refactor func TestChallengePage(t *testing.T) { w := httptest.NewRecorder() ServeChallenge(w, "TOK") want := `One moment

Checking your browser… / ` + "\u062f\u0631 \u062d\u0627\u0644 \u0628\u0631\u0631\u0633\u06cc \u0645\u0631\u0648\u0631\u06af\u0631 \u0634\u0645\u0627" + `…

` if w.Code != 429 || w.Body.String() != want || w.Header().Get("Content-Type") != "text/html; charset=utf-8" || w.Header().Get("Cache-Control") != "no-store" { t.Errorf("challenge page changed: %d %v\n%s", w.Code, w.Header(), w.Body.String()) } bs := "\\" // JSON escapes < and > as backslash-u003c / backslash-u003e inside the JS string esc := strings.NewReplacer("<", bs+"u003c", ">", bs+"u003e").Replace for rd, js := range map[string]string{ "/product/1?a=b": `location.replace("/product/1?a=b")`, "/x