This upgrades the Masyunis fork from Anubis v1.25.0 to the latest stable release, **v1.28.2** (“Wuk Lamat Echo 2”), while keeping the fork’s custom styling and removals. ## Approach The Gitea repo is a tree snapshot of v1.25.0, not a git-level fork of [TecharoHQ/anubis](https://github.com/TecharoHQ/anubis). The histories share no commits, so a merge or rebase onto the upstream tag is not usable. The first commit on this branch replaces the tree with a snapshot of the **v1.28.2** tag (`git rm -r` plus `git checkout v1.28.2 -- .`; the import tree is identical to the tag). A short patch series on top restores the Masyunis customizations. ## Customizations kept - Masyunya mascots (`web/static/img/{happy,pensive,reject}.webp`), hashes match current `master`. - `web/static/img/ATTRIBUTIONS.txt` stays deleted. - Challenge footer: drop the `made_with` (Canada / maple-leaf) line and the CELPHASE mascot credit; keep “Protected by Anubis from Techaro”. The generated `web/index_templ.go` matches `go generate ./web`. - Short README (the old `anubisc` typo is fixed). - Version stamped `v1.28.2/masyunis` via Makefile ldflags. `anubis.go` `init()` does not overwrite a link-time version; `go run` / `go test` still report `devel` on purpose. - Upstream `.github`, `.devcontainer`, `.husky`, and `.vscode` stay deleted. - `"uk"` is removed from `lib/localization/locales/manifest.json` **on purpose**, because of Alpine locale problems. `uk.json` and its test entry stay; the served manifest lists 27 languages. - `package.json` `prepare` no longer runs `husky` (the hooks directory is gone). The rest of the script is unchanged. `husky` remains in devDependencies, as upstream left it. ## Deploy notes - **Cookie rename (v1.27):** with the default `DYNAMIC_COOKIE_SUFFIX=true`, cookie names become e.g. `techaro.lol-anubis-auth-<hash>`. Existing clients get a **one-time re-challenge**. That is expected, not a loop. - **Docker / ko / yeet builds** skip the Makefile ldflags, so the displayed version is `<git-version>/masyunis`. It is `v1.28.2/masyunis` only when built from a `v1.28.2` tag (or via `make build` / `make prebaked-build`). - Go 1.27.3 is required (upstream bump in v1.28.2). Thoth (`THOTH_URL` / `THOTH_TOKEN`) was removed upstream; those settings now warn and do nothing. New WASM challenge methods stay disabled unless opted in. ## Verification - `make build` succeeds. `./var/anubis --version` prints `Anubis v1.28.2/masyunis`. - `SKIP_INTEGRATION=1 go test ./...` passes (unit tests; integration package is skipped). - Local challenge/reject pages (en and ru) show Masyunya, version `v1.28.2/masyunis`, and no Canada / CELPHASE / mascot-credit text. Screenshots:   - Upstream Playwright tests (`make test` without `SKIP_INTEGRATION`) fail on this box because the browser download fails. A clean v1.28.2 tree fails the same three Playwright tests here, so this is not a fork regression. - `npm audit` reports **4** findings in upstream’s lockfile (2 moderate, 2 high: `brace-expansion`, `fast-uri`, `lodash.trim`, `source-map-js`). Left alone. Please review and merge when ready. I will not merge this myself. Reviewed-on: #1 Co-authored-by: Lead <3+lead_bot@noreply.git.unvr.site>
394 lines
8.9 KiB
Go
394 lines
8.9 KiB
Go
package policy
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/TecharoHQ/anubis"
|
|
"github.com/TecharoHQ/anubis/data"
|
|
"github.com/TecharoHQ/anubis/internal"
|
|
"github.com/TecharoHQ/anubis/lib/config"
|
|
"github.com/TecharoHQ/anubis/lib/geoip/geoiptest"
|
|
)
|
|
|
|
func TestDefaultPolicyMustParse(t *testing.T) {
|
|
ctx := geoiptest.WithMockGeoIP(t)
|
|
|
|
fin, err := data.BotPolicies.Open("botPolicies.yaml")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer fin.Close() //nolint:errcheck
|
|
|
|
if _, err := ParseConfig(ctx, fin, "botPolicies.yaml", anubis.DefaultDifficulty, "info", false); err != nil {
|
|
t.Fatalf("can't parse config: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestGoodConfigs(t *testing.T) {
|
|
|
|
finfos, err := os.ReadDir("../config/testdata/good")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
for _, st := range finfos {
|
|
t.Run(st.Name(), func(t *testing.T) {
|
|
t.Run("with-geoip", func(t *testing.T) {
|
|
fin, err := os.Open(filepath.Join("..", "config", "testdata", "good", st.Name()))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer fin.Close() //nolint:errcheck
|
|
|
|
ctx := geoiptest.WithMockGeoIP(t)
|
|
if _, err := ParseConfig(ctx, fin, fin.Name(), anubis.DefaultDifficulty, "info", false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
})
|
|
|
|
t.Run("without-geoip", func(t *testing.T) {
|
|
fin, err := os.Open(filepath.Join("..", "config", "testdata", "good", st.Name()))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer fin.Close() //nolint:errcheck
|
|
|
|
if _, err := ParseConfig(t.Context(), fin, fin.Name(), anubis.DefaultDifficulty, "info", false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestBadConfigs(t *testing.T) {
|
|
ctx := geoiptest.WithMockGeoIP(t)
|
|
|
|
finfos, err := os.ReadDir("../config/testdata/bad")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
for _, st := range finfos {
|
|
t.Run(st.Name(), func(t *testing.T) {
|
|
fin, err := os.Open(filepath.Join("..", "config", "testdata", "bad", st.Name()))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer fin.Close() //nolint:errcheck
|
|
|
|
if _, err := ParseConfig(ctx, fin, fin.Name(), anubis.DefaultDifficulty, "info", false); err == nil {
|
|
t.Fatal(err)
|
|
} else {
|
|
t.Log(err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPathCheckerStripsForwardedURIQuery(t *testing.T) {
|
|
checker, err := NewPathChecker("^/admin$", true)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
req := httptest.NewRequest(http.MethodGet, "https://anubis.local/.within.website/x/cmd/anubis/api/check", nil)
|
|
req.Header.Set("X-Forwarded-Uri", "/admin?x=1")
|
|
matched, err := checker.Check(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !matched {
|
|
t.Fatalf("expected exact path checker to match forwarded URI when query string is appended")
|
|
}
|
|
req.Header.Set("X-Forwarded-Uri", "/admin")
|
|
matched, err = checker.Check(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !matched {
|
|
t.Fatalf("expected exact path checker to match forwarded URI without query string")
|
|
}
|
|
}
|
|
|
|
func TestConfigReferencesJA4H(t *testing.T) {
|
|
for _, tt := range []struct {
|
|
name string
|
|
bots []config.BotConfig
|
|
want bool
|
|
}{
|
|
{
|
|
name: "no bots",
|
|
bots: nil,
|
|
want: false,
|
|
},
|
|
{
|
|
name: "unrelated rules",
|
|
bots: []config.BotConfig{
|
|
{Name: "ua", HeadersRegex: map[string]string{"User-Agent": "curl"}},
|
|
{Name: "expr", Expression: &config.ExpressionOrList{Expression: `userAgent.contains("bot")`}},
|
|
},
|
|
want: false,
|
|
},
|
|
{
|
|
name: "headers_regex exact match",
|
|
bots: []config.BotConfig{
|
|
{Name: "ja4h", HeadersRegex: map[string]string{internal.JA4HHeaderName: "t13d.*"}},
|
|
},
|
|
want: true,
|
|
},
|
|
{
|
|
name: "headers_regex case-insensitive match",
|
|
bots: []config.BotConfig{
|
|
{Name: "ja4h", HeadersRegex: map[string]string{"x-http-fingerprint-ja4h": ".*"}},
|
|
},
|
|
want: true,
|
|
},
|
|
{
|
|
name: "expression references header",
|
|
bots: []config.BotConfig{
|
|
{Name: "ja4h", Expression: &config.ExpressionOrList{Expression: `headers["X-Http-Fingerprint-Ja4h"] == "t13d"`}},
|
|
},
|
|
want: true,
|
|
},
|
|
{
|
|
name: "expression list references header",
|
|
bots: []config.BotConfig{
|
|
{Name: "ja4h", Expression: &config.ExpressionOrList{Any: []string{
|
|
`userAgent.contains("bot")`,
|
|
`headers["X-Http-Fingerprint-Ja4h"] == "t13d"`,
|
|
}}},
|
|
},
|
|
want: true,
|
|
},
|
|
{
|
|
name: "expression missingHeader references header",
|
|
bots: []config.BotConfig{
|
|
{Name: "ja4h", Expression: &config.ExpressionOrList{Expression: `!missingHeader(headers, "X-Http-Fingerprint-Ja4h")`}},
|
|
},
|
|
want: true,
|
|
},
|
|
} {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := configReferencesJA4H(tt.bots); got != tt.want {
|
|
t.Errorf("configReferencesJA4H() = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGeoIPDatabasesBlock(t *testing.T) {
|
|
asnPath, countryPath := geoiptest.WriteFixtures(t)
|
|
|
|
const rules = `
|
|
bots:
|
|
- name: cloudflare
|
|
action: DENY
|
|
asns:
|
|
match:
|
|
- 13335
|
|
- name: canada
|
|
action: DENY
|
|
geoip:
|
|
countries:
|
|
- CA
|
|
- name: everything
|
|
action: CHALLENGE
|
|
path_regex: .*
|
|
`
|
|
|
|
for _, tt := range []struct {
|
|
name string
|
|
geoipBlock string
|
|
withCtx bool
|
|
wantBots []string
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "both databases",
|
|
geoipBlock: `
|
|
geoip:
|
|
asn:
|
|
path: ` + asnPath + `
|
|
country:
|
|
path: ` + countryPath,
|
|
wantBots: []string{"cloudflare", "canada", "everything"},
|
|
},
|
|
{
|
|
name: "asn only skips geoip rules",
|
|
geoipBlock: `
|
|
geoip:
|
|
asn:
|
|
path: ` + asnPath,
|
|
wantBots: []string{"cloudflare", "everything"},
|
|
},
|
|
{
|
|
name: "policy block wins over context",
|
|
geoipBlock: `
|
|
geoip:
|
|
country:
|
|
path: ` + countryPath,
|
|
withCtx: true,
|
|
wantBots: []string{"canada", "everything"},
|
|
},
|
|
{
|
|
name: "context only",
|
|
withCtx: true,
|
|
wantBots: []string{"cloudflare", "canada", "everything"},
|
|
},
|
|
{
|
|
name: "nothing configured skips both",
|
|
wantBots: []string{"everything"},
|
|
},
|
|
{
|
|
name: "missing database file",
|
|
geoipBlock: `
|
|
geoip:
|
|
asn:
|
|
path: ` + filepath.Join(t.TempDir(), "nope.mmdb"),
|
|
wantErr: true,
|
|
},
|
|
} {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
ctx := t.Context()
|
|
if tt.withCtx {
|
|
ctx = geoiptest.WithMockGeoIP(t)
|
|
}
|
|
|
|
pc, err := ParseConfig(ctx, strings.NewReader(rules+tt.geoipBlock), "test.yaml", anubis.DefaultDifficulty, "info", false)
|
|
if tt.wantErr {
|
|
if err == nil {
|
|
t.Fatal("wanted an error, got nil")
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
var got []string
|
|
for _, b := range pc.Bots {
|
|
got = append(got, b.Name)
|
|
}
|
|
|
|
if !slices.Equal(got, tt.wantBots) {
|
|
t.Fatalf("bots: want %v, got %v", tt.wantBots, got)
|
|
}
|
|
|
|
for _, b := range pc.Bots {
|
|
var ip string
|
|
switch b.Name {
|
|
case "cloudflare":
|
|
ip = "1.1.1.1"
|
|
case "canada":
|
|
ip = "2.2.2.2"
|
|
default:
|
|
continue
|
|
}
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
req.Header.Set("X-Real-IP", ip)
|
|
ok, err := b.Rules.Check(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !ok {
|
|
t.Errorf("%s rule did not match %s", b.Name, ip)
|
|
}
|
|
|
|
req.Header.Set("X-Real-IP", "8.8.8.8")
|
|
if ok, _ := b.Rules.Check(req); ok {
|
|
t.Errorf("%s rule matched 8.8.8.8", b.Name)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParseConfigRemoteAddressesURL(t *testing.T) {
|
|
body := `{
|
|
"creationTime": "2025-01-02T03:04:05.000000",
|
|
"prefixes": [{"ipv4Prefix": "20.42.10.176/28"}]
|
|
}`
|
|
|
|
release := make(chan struct{})
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
select {
|
|
case <-release:
|
|
case <-r.Context().Done():
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = io.WriteString(w, body)
|
|
}))
|
|
t.Cleanup(ts.Close)
|
|
|
|
yaml := fmt.Sprintf(`
|
|
bots:
|
|
- name: openai-searchbot
|
|
action: ALLOW
|
|
remote_addresses_url: %q
|
|
`, ts.URL)
|
|
|
|
type parseResult struct {
|
|
cfg *ParsedConfig
|
|
err error
|
|
}
|
|
ch := make(chan parseResult, 1)
|
|
go func() {
|
|
cfg, err := ParseConfig(t.Context(), strings.NewReader(yaml), "dynamic.yaml", anubis.DefaultDifficulty, "error", false)
|
|
ch <- parseResult{cfg, err}
|
|
}()
|
|
|
|
var parsed *ParsedConfig
|
|
select {
|
|
case res := <-ch:
|
|
if res.err != nil {
|
|
t.Fatalf("ParseConfig: %v", res.err)
|
|
}
|
|
parsed = res.cfg
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("ParseConfig blocked on remote fetch")
|
|
}
|
|
|
|
if len(parsed.Bots) != 1 {
|
|
t.Fatalf("got %d bots, want 1", len(parsed.Bots))
|
|
}
|
|
|
|
close(release)
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
req.Header.Set("X-Real-IP", "20.42.10.176")
|
|
|
|
deadline := time.Now().Add(2 * time.Second)
|
|
for time.Now().Before(deadline) {
|
|
ok, err := parsed.Bots[0].Rules.Check(req)
|
|
if err != nil {
|
|
t.Fatalf("Check: %v", err)
|
|
}
|
|
if ok {
|
|
miss := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
miss.Header.Set("X-Real-IP", "1.1.1.1")
|
|
matched, err := parsed.Bots[0].Rules.Check(miss)
|
|
if err != nil {
|
|
t.Fatalf("Check unrelated IP: %v", err)
|
|
}
|
|
if matched {
|
|
t.Fatal("unrelated IP should not match")
|
|
}
|
|
return
|
|
}
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
|
|
t.Fatal("remote addresses url list was not loaded in time")
|
|
}
|