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>
309 lines
8.3 KiB
Go
309 lines
8.3 KiB
Go
package dns
|
|
|
|
import (
|
|
"errors"
|
|
"net"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/TecharoHQ/anubis/lib/store/memory"
|
|
"github.com/neilotoole/slogt/v2"
|
|
)
|
|
|
|
// newTestDNS is a helper function to create a new Dns object with an in-memory cache for testing.
|
|
func newTestDNS(t *testing.T, forwardTTL int, reverseTTL int) *Dns {
|
|
t.Helper()
|
|
memStore := memory.New(t.Context())
|
|
cache := NewDNSCache(forwardTTL, reverseTTL, memStore)
|
|
return New(t.Context(), cache, slogt.New(t))
|
|
}
|
|
|
|
// mockLookupAddr is a mock implementation of the net.LookupAddr function.
|
|
func mockLookupAddr(addr string) ([]string, error) {
|
|
switch addr {
|
|
case "8.8.8.8":
|
|
return []string{"dns.google."}, nil
|
|
case "1.1.1.1":
|
|
return []string{"one.one.one.one."}, nil
|
|
case "208.67.222.222":
|
|
return []string{"resolver1.opendns.com."}, nil
|
|
case "9.9.9.9":
|
|
return nil, &net.DNSError{Err: "no such host", Name: "9.9.9.9", IsNotFound: true}
|
|
case "1.2.3.4":
|
|
return nil, errors.New("unknown error")
|
|
default:
|
|
return nil, &net.DNSError{Err: "no such host", Name: addr, IsNotFound: true}
|
|
}
|
|
}
|
|
|
|
// mockLookupHost is a mock implementation of the net.LookupHost function.
|
|
func mockLookupHost(host string) ([]string, error) {
|
|
switch host {
|
|
case "dns.google":
|
|
return []string{"8.8.8.8", "8.8.4.4"}, nil
|
|
case "one.one.one.one":
|
|
return []string{"1.1.1.1", "1.0.0.1"}, nil
|
|
case "resolver1.opendns.com":
|
|
return []string{"208.67.222.222"}, nil
|
|
case "example.com":
|
|
return nil, &net.DNSError{Err: "no such host", Name: "example.com", IsNotFound: true}
|
|
default:
|
|
return nil, &net.DNSError{Err: "no such host", Name: host, IsNotFound: true}
|
|
}
|
|
}
|
|
|
|
func TestMain(m *testing.M) {
|
|
// Before all tests
|
|
originalLookupAddr := DNSLookupAddr
|
|
originalLookupHost := DNSLookupHost
|
|
|
|
DNSLookupAddr = mockLookupAddr
|
|
DNSLookupHost = mockLookupHost
|
|
|
|
// Run tests
|
|
exitCode := m.Run()
|
|
|
|
// After all tests
|
|
DNSLookupAddr = originalLookupAddr
|
|
DNSLookupHost = originalLookupHost
|
|
|
|
// Exit
|
|
if exitCode != 0 {
|
|
panic(exitCode)
|
|
}
|
|
}
|
|
|
|
func TestDns_ArpaReverseIP(t *testing.T) {
|
|
d := newTestDNS(t, 0, 0)
|
|
tests := []struct {
|
|
name string
|
|
ip string
|
|
want string
|
|
wantErr bool
|
|
}{
|
|
{"ipv4", "192.0.2.1", "1.2.0.192", false},
|
|
{"ipv6", "2001:db8::1", "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2", false},
|
|
{"invalid ip", "invalid", "invalid", true},
|
|
{"ipv4-mapped ipv6", "::ffff:192.0.2.1", "1.2.0.192", false},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := d.ArpaReverseIP(tt.ip)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("ArpaReverseIP() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if got != tt.want {
|
|
t.Errorf("ArpaReverseIP() = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDns_ReverseDNS(t *testing.T) {
|
|
d := newTestDNS(t, 1, 1) // short TTL for testing cache
|
|
|
|
// First call - cache miss
|
|
t.Run("cache miss", func(t *testing.T) {
|
|
got, err := d.ReverseDNS("8.8.8.8")
|
|
if err != nil {
|
|
t.Fatalf("ReverseDNS() error = %v", err)
|
|
}
|
|
want := []string{"dns.google"}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Errorf("ReverseDNS() = %v, want %v", got, want)
|
|
}
|
|
})
|
|
|
|
// Second call - cache hit
|
|
t.Run("cache hit", func(t *testing.T) {
|
|
// Temporarily replace lookup function to ensure cache is used
|
|
originalLookupAddr := DNSLookupAddr
|
|
DNSLookupAddr = func(addr string) ([]string, error) {
|
|
return nil, errors.New("should not be called")
|
|
}
|
|
defer func() { DNSLookupAddr = originalLookupAddr }()
|
|
|
|
got, err := d.ReverseDNS("8.8.8.8")
|
|
if err != nil {
|
|
t.Fatalf("ReverseDNS() error = %v", err)
|
|
}
|
|
want := []string{"dns.google"}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Errorf("ReverseDNS() = %v, want %v", got, want)
|
|
}
|
|
})
|
|
|
|
// Test cache expiration
|
|
t.Run("cache expiration", func(t *testing.T) {
|
|
time.Sleep(2 * time.Second)
|
|
// Now the cache should be expired
|
|
// We expect the mock to be called again
|
|
// To test this we will change the mock to return something different
|
|
originalLookupAddr := DNSLookupAddr
|
|
DNSLookupAddr = func(addr string) ([]string, error) {
|
|
if addr == "8.8.8.8" {
|
|
return []string{"expired.google."}, nil
|
|
}
|
|
return mockLookupAddr(addr)
|
|
}
|
|
defer func() { DNSLookupAddr = originalLookupAddr }()
|
|
|
|
got, err := d.ReverseDNS("8.8.8.8")
|
|
if err != nil {
|
|
t.Fatalf("ReverseDNS() error = %v", err)
|
|
}
|
|
want := []string{"expired.google"}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Errorf("ReverseDNS() = %v, want %v", got, want)
|
|
}
|
|
})
|
|
|
|
// Test not found
|
|
t.Run("not found", func(t *testing.T) {
|
|
got, err := d.ReverseDNS("9.9.9.9")
|
|
if err != nil {
|
|
t.Fatalf("ReverseDNS() error = %v", err)
|
|
}
|
|
if len(got) != 0 {
|
|
t.Errorf("ReverseDNS() = %v, want empty slice", got)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestDns_LookupHost(t *testing.T) {
|
|
d := newTestDNS(t, 1, 1)
|
|
|
|
t.Run("cache miss", func(t *testing.T) {
|
|
got, err := d.LookupHost("dns.google")
|
|
if err != nil {
|
|
t.Fatalf("LookupHost() error = %v", err)
|
|
}
|
|
want := []string{"8.8.8.8", "8.8.4.4"}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Errorf("LookupHost() = %v, want %v", got, want)
|
|
}
|
|
})
|
|
|
|
t.Run("cache hit", func(t *testing.T) {
|
|
originalLookupHost := DNSLookupHost
|
|
DNSLookupHost = func(host string) ([]string, error) {
|
|
return nil, errors.New("should not be called")
|
|
}
|
|
defer func() { DNSLookupHost = originalLookupHost }()
|
|
|
|
got, err := d.LookupHost("dns.google")
|
|
if err != nil {
|
|
t.Fatalf("LookupHost() error = %v", err)
|
|
}
|
|
want := []string{"8.8.8.8", "8.8.4.4"}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Errorf("LookupHost() = %v, want %v", got, want)
|
|
}
|
|
})
|
|
|
|
t.Run("cache expiration", func(t *testing.T) {
|
|
time.Sleep(2 * time.Second)
|
|
originalLookupHost := DNSLookupHost
|
|
DNSLookupHost = func(host string) ([]string, error) {
|
|
if host == "dns.google" {
|
|
return []string{"9.9.9.9"}, nil
|
|
}
|
|
return mockLookupHost(host)
|
|
}
|
|
defer func() { DNSLookupHost = originalLookupHost }()
|
|
|
|
got, err := d.LookupHost("dns.google")
|
|
if err != nil {
|
|
t.Fatalf("LookupHost() error = %v", err)
|
|
}
|
|
want := []string{"9.9.9.9"}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Errorf("LookupHost() = %v, want %v", got, want)
|
|
}
|
|
})
|
|
|
|
t.Run("not found", func(t *testing.T) {
|
|
got, err := d.LookupHost("example.com")
|
|
if err != nil {
|
|
t.Fatalf("LookupHost() error = %v", err)
|
|
}
|
|
if len(got) != 0 {
|
|
t.Errorf("LookupHost() = %v, want empty slice", got)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestDns_VerifyFCrDNS(t *testing.T) {
|
|
d := newTestDNS(t, 1, 1)
|
|
|
|
// Helper to convert string to *string
|
|
p := func(s string) *string {
|
|
return &s
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
ip string
|
|
pattern *string
|
|
want bool
|
|
}{
|
|
// Cases without pattern
|
|
{"valid no pattern", "8.8.8.8", nil, true},
|
|
{"valid partial no pattern", "1.1.1.1", nil, true},
|
|
{"not found no pattern", "9.9.9.9", nil, true},
|
|
{"unknown error no pattern", "1.2.3.4", nil, false},
|
|
|
|
// Cases with pattern
|
|
{"valid match", "8.8.8.8", p(`.*\.google$`), true},
|
|
{"valid no match", "8.8.8.8", p(`\.com$`), false},
|
|
{"not found with pattern", "9.9.9.9", p(".*"), false},
|
|
{"unknown error with pattern", "1.2.3.4", p(".*"), false},
|
|
{"invalid pattern", "8.8.8.8", p(`[`), false},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := d.VerifyFCrDNS(tt.ip, tt.pattern); got != tt.want {
|
|
t.Errorf("VerifyFCrDNS() = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
|
|
t.Run("reverse cache hit", func(t *testing.T) {
|
|
// Prime the cache
|
|
if got := d.VerifyFCrDNS("8.8.8.8", nil); got != true {
|
|
t.Fatalf("VerifyFCrDNS() priming failed, got %v, want true", got)
|
|
}
|
|
|
|
// Now test with a failing lookup to ensure cache is used
|
|
originalLookupAddr := DNSLookupAddr
|
|
DNSLookupAddr = func(addr string) ([]string, error) {
|
|
return nil, errors.New("should not be called")
|
|
}
|
|
defer func() { DNSLookupAddr = originalLookupAddr }()
|
|
|
|
if got := d.VerifyFCrDNS("8.8.8.8", nil); got != true {
|
|
t.Errorf("VerifyFCrDNS() = %v, want true", got)
|
|
}
|
|
})
|
|
|
|
t.Run("forward cache hit", func(t *testing.T) {
|
|
// Prime the cache
|
|
if got := d.VerifyFCrDNS("8.8.8.8", nil); got != true {
|
|
t.Fatalf("VerifyFCrDNS() priming failed, got %v, want true", got)
|
|
}
|
|
|
|
// Now test with a failing lookup to ensure cache is used
|
|
originalLookupHost := DNSLookupHost
|
|
DNSLookupHost = func(host string) ([]string, error) {
|
|
return nil, errors.New("should not be called")
|
|
}
|
|
defer func() { DNSLookupHost = originalLookupHost }()
|
|
|
|
if got := d.VerifyFCrDNS("8.8.8.8", nil); got != true {
|
|
t.Errorf("VerifyFCrDNS() = %v, want true", got)
|
|
}
|
|
})
|
|
}
|