feat: 1.4.6 unban from the UI, operator allows, day-counter fallback for long windows
Release / Publish tags (push) Successful in 16s
Release / Plan (reuse the published image?) (push) Successful in 4s
Release / ci (push) Successful in 1m47s
Release / Build image (amd64) (push) Successful in 21s
Release / Build image (arm64) (push) Successful in 23s
CI / Format and vet (push) Successful in 27s
CI / Tests (Linux, race detector) (push) Successful in 1m20s
CI / Dependency audit (push) Successful in 31s
CI / Docker image (build and smoke test) (push) Skipped
Release / Gitea release (push) Successful in 5s
Release / Publish tags (push) Successful in 16s
Release / Plan (reuse the published image?) (push) Successful in 4s
Release / ci (push) Successful in 1m47s
Release / Build image (amd64) (push) Successful in 21s
Release / Build image (arm64) (push) Successful in 23s
CI / Format and vet (push) Successful in 27s
CI / Tests (Linux, race detector) (push) Successful in 1m20s
CI / Dependency audit (push) Successful in 31s
CI / Docker image (build and smoke test) (push) Skipped
Release / Gitea release (push) Successful in 5s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1ef5a0ce9f
commit
da333d70f1
25 files changed
+841
-33
No files matched your search
@@ -0,0 +1,119 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/enforce"
|
||||
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store"
|
||||
)
|
||||
|
||||
func unbanRig(t *testing.T) (http.Handler, *enforce.Engine, string) {
|
||||
t.Helper()
|
||||
dir := t.TempDir()
|
||||
st := store.New(store.Config{Name: "oracle", Version: "t", Retention: 720 * time.Hour})
|
||||
e := enforce.New(enforce.Config{Mode: enforce.ModeOn, Path: filepath.Join(dir, "decisions.log"), UnbanPath: filepath.Join(dir, "unban.log"),
|
||||
Host: "oracle", Threshold: 3, Share: true})
|
||||
SetEnforce(e)
|
||||
t.Cleanup(func() { SetEnforce(nil) })
|
||||
return New(Options{Token: userTok, PeerTokens: []string{peerTok}, Version: "t", Store: st}), e, filepath.Join(dir, "unban.log")
|
||||
}
|
||||
|
||||
func req(h http.Handler, method, path, body string, hdr map[string]string) *httptest.ResponseRecorder {
|
||||
r := httptest.NewRequest(method, path, strings.NewReader(body))
|
||||
r.RemoteAddr = "198.51.100.1:1234"
|
||||
for k, v := range hdr {
|
||||
r.Header.Set(k, v)
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
h.ServeHTTP(w, r)
|
||||
return w
|
||||
}
|
||||
|
||||
func TestUnbanRoute(t *testing.T) {
|
||||
h, e, file := unbanRig(t)
|
||||
auth := map[string]string{"Authorization": "Bearer " + userTok}
|
||||
if w := req(h, "POST", "/api/v1/enforce/unban", `{"ip":"203.0.113.7","allow_s":60}`, nil); w.Code != 401 {
|
||||
t.Fatalf("no auth: %d", w.Code)
|
||||
}
|
||||
if w := req(h, "POST", "/api/v1/enforce/unban", `{"ip":"203.0.113.7"}`, map[string]string{"Authorization": "Bearer " + peerTok}); w.Code != 403 {
|
||||
t.Fatalf("peer token: %d", w.Code)
|
||||
}
|
||||
cross := map[string]string{"Authorization": "Bearer " + userTok, "Sec-Fetch-Site": "cross-site"}
|
||||
if w := req(h, "POST", "/api/v1/enforce/unban", `{"ip":"203.0.113.7"}`, cross); w.Code != 403 {
|
||||
t.Fatalf("cross-site: %d", w.Code)
|
||||
}
|
||||
for _, b := range []string{`nope`, `{"ip":"203.0.113.7","x":1}`, `{"ip":"203.0.113.7\nroot","allow_s":1}`, `{"ip":"1.2.3.4 allow=9","allow_s":0}`,
|
||||
`{"ip":"fe80::1%eth0"}`, `{"ip":"203.0.113.7","allow_s":-1}`, `{"ip":"203.0.113.7","allow_s":2592001}`, `{"ip":"999.1.1.1"}`,
|
||||
`{"ip":"203.0.113.7"}{"ip":"1.1.1.1"}`, `{"ip":"` + strings.Repeat("a", 2000) + `"}`} {
|
||||
if w := req(h, "POST", "/api/v1/enforce/unban", b, auth); w.Code != 400 {
|
||||
t.Errorf("%.40q: %d", b, w.Code)
|
||||
}
|
||||
}
|
||||
if b, _ := os.ReadFile(file); len(b) != 0 {
|
||||
t.Fatalf("invalid requests wrote: %q", b)
|
||||
}
|
||||
if w := req(h, "POST", "/api/v1/enforce/unban", `{"ip":"203.0.113.7","allow_s":86400}`, auth); w.Code != 200 {
|
||||
t.Fatalf("ok: %d %s", w.Code, w.Body)
|
||||
}
|
||||
if w := req(h, "POST", "/api/v1/enforce/unban", `{"ip":"::ffff:198.51.100.9"}`, auth); w.Code != 200 {
|
||||
t.Fatalf("mapped: %d", w.Code)
|
||||
}
|
||||
b, _ := os.ReadFile(file)
|
||||
lines := strings.Split(strings.TrimSpace(string(b)), "\n")
|
||||
re := regexp.MustCompile(`^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\dZ urutau unban ip=203\.0\.113\.7 allow=86400$`)
|
||||
if len(lines) != 2 || !re.MatchString(lines[0]) || !strings.HasSuffix(lines[1], " urutau unban ip=198.51.100.9 allow=0") {
|
||||
t.Fatalf("file: %q", b)
|
||||
}
|
||||
d := getJSON(t, h, "/api/v1/enforce?range=24h", auth)
|
||||
al := d["allows"].([]any)
|
||||
if len(al) != 1 || al[0].(map[string]any)["ip"] != "203.0.113.7" || al[0].(map[string]any)["by"] != "operator" || d["unban_log_writable"] != true {
|
||||
t.Fatalf("allows: %v", d)
|
||||
}
|
||||
// DELETE
|
||||
if w := req(h, "DELETE", "/api/v1/enforce/allow?ip=bad", "", auth); w.Code != 400 {
|
||||
t.Fatalf("bad ip: %d", w.Code)
|
||||
}
|
||||
if w := req(h, "DELETE", "/api/v1/enforce/allow?ip=203.0.113.7", "", cross); w.Code != 403 {
|
||||
t.Fatalf("delete cross-site: %d", w.Code)
|
||||
}
|
||||
if w := req(h, "DELETE", "/api/v1/enforce/allow?ip=203.0.113.7", "", auth); w.Code != 200 {
|
||||
t.Fatalf("delete: %d", w.Code)
|
||||
}
|
||||
if w := req(h, "DELETE", "/api/v1/enforce/allow?ip=203.0.113.7", "", auth); w.Code != 404 {
|
||||
t.Fatalf("delete again: %d", w.Code)
|
||||
}
|
||||
if len(e.Allows()) != 0 {
|
||||
t.Fatal("allow remains")
|
||||
}
|
||||
if after, _ := os.ReadFile(file); string(after) != string(b) {
|
||||
t.Fatal("DELETE wrote to the host file")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPeerUnbanRoute(t *testing.T) {
|
||||
h, e, file := unbanRig(t)
|
||||
peer := map[string]string{"Authorization": "Bearer " + peerTok}
|
||||
if w := req(h, "POST", "/api/v1/peer/unban?from=hostinger", `{"unbans":[{"ip":"203.0.113.7","allow":3600},{"ip":"bad","allow":1}]}`, map[string]string{"Authorization": "Bearer " + userTok}); w.Code == 200 {
|
||||
t.Fatalf("user token accepted on peer route")
|
||||
}
|
||||
w := req(h, "POST", "/api/v1/peer/unban?from=hostinger", `{"unbans":[{"ip":"203.0.113.7","allow":3600},{"ip":"bad","allow":1}]}`, peer)
|
||||
if w.Code != 200 || !strings.Contains(w.Body.String(), `"accepted":1`) {
|
||||
t.Fatalf("%d %s", w.Code, w.Body)
|
||||
}
|
||||
if len(e.Allows()) != 1 {
|
||||
t.Fatal("peer allow not applied")
|
||||
}
|
||||
if b, _ := os.ReadFile(file); !strings.Contains(string(b), "ip=203.0.113.7 allow=3600") {
|
||||
t.Fatalf("file: %q", b)
|
||||
}
|
||||
if len(e.UnbansSince(0)) != 0 {
|
||||
t.Fatal("peer unban must not be echoed back")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user