From 5186d7d3ad9963dafe6aeaa87a574c04ea91ae1a Mon Sep 17 00:00:00 2001 From: Jason Cameron Date: Wed, 18 Mar 2026 10:17:28 -0400 Subject: [PATCH] chore: gofix (#1466) Signed-off-by: Jason Cameron --- cmd/anubis/main.go | 4 +-- cmd/robots2policy/main.go | 8 ++--- cmd/robots2policy/robots2policy_test.go | 10 +++---- internal/glob/glob.go | 2 +- internal/hash_bench_test.go | 4 +-- internal/honeypot/naive/naive.go | 4 +-- internal/listor.go | 4 +-- internal/ogtags/mem_test.go | 4 +-- internal/ogtags/ogtags_fuzz_test.go | 17 ++++------- internal/ogtags/parse.go | 7 ++--- internal/test/playwright_test.go | 2 +- lib/anubis.go | 4 +-- lib/anubis_test.go | 4 +-- lib/config/config.go | 6 ++-- lib/config/config_test.go | 3 -- lib/config_test.go | 2 -- lib/http.go | 5 +--- lib/policy/expressions/environment_test.go | 34 +++++++++++----------- lib/policy/policy_test.go | 2 -- lib/store/s3api/s3api_test.go | 5 ++-- lib/store/valkey/factory.go | 2 +- lib/thoth/auth.go | 4 +-- 22 files changed, 59 insertions(+), 78 deletions(-) diff --git a/cmd/anubis/main.go b/cmd/anubis/main.go index e21eeca1..a38cfdb3 100644 --- a/cmd/anubis/main.go +++ b/cmd/anubis/main.go @@ -418,8 +418,8 @@ func main() { var redirectDomainsList []string if *redirectDomains != "" { - domains := strings.Split(*redirectDomains, ",") - for _, domain := range domains { + domains := strings.SplitSeq(*redirectDomains, ",") + for domain := range domains { _, err = url.Parse(domain) if err != nil { log.Fatalf("cannot parse redirect-domain %q: %s", domain, err.Error()) diff --git a/cmd/robots2policy/main.go b/cmd/robots2policy/main.go index 69fb2f96..e79a6262 100644 --- a/cmd/robots2policy/main.go +++ b/cmd/robots2policy/main.go @@ -10,6 +10,7 @@ import ( "net/http" "os" "regexp" + "slices" "strings" "github.com/TecharoHQ/anubis/lib/config" @@ -210,11 +211,8 @@ func parseRobotsTxt(input io.Reader) ([]RobotsRule, error) { // Mark blacklisted user agents (those with "Disallow: /") for i := range rules { - for _, disallow := range rules[i].Disallows { - if disallow == "/" { - rules[i].IsBlacklist = true - break - } + if slices.Contains(rules[i].Disallows, "/") { + rules[i].IsBlacklist = true } } diff --git a/cmd/robots2policy/robots2policy_test.go b/cmd/robots2policy/robots2policy_test.go index 523d69a7..ef3008f4 100644 --- a/cmd/robots2policy/robots2policy_test.go +++ b/cmd/robots2policy/robots2policy_test.go @@ -158,8 +158,8 @@ func TestDataFileConversion(t *testing.T) { } if strings.ToLower(*outputFormat) == "yaml" { - var actualData []interface{} - var expectedData []interface{} + var actualData []any + var expectedData []any err = yaml.Unmarshal(actualOutput, &actualData) if err != nil { @@ -178,8 +178,8 @@ func TestDataFileConversion(t *testing.T) { t.Errorf("Output mismatch for %s\nExpected:\n%s\n\nActual:\n%s", tc.name, expectedStr, actualStr) } } else { - var actualData []interface{} - var expectedData []interface{} + var actualData []any + var expectedData []any err = json.Unmarshal(actualOutput, &actualData) if err != nil { @@ -419,6 +419,6 @@ Disallow: /` // compareData performs a deep comparison of two data structures, // ignoring differences that are semantically equivalent in YAML/JSON -func compareData(actual, expected interface{}) bool { +func compareData(actual, expected any) bool { return reflect.DeepEqual(actual, expected) } diff --git a/internal/glob/glob.go b/internal/glob/glob.go index 44c1a67a..e2e640de 100644 --- a/internal/glob/glob.go +++ b/internal/glob/glob.go @@ -36,7 +36,7 @@ func Glob(pattern, subj string) bool { end := len(parts) - 1 // Go over the leading parts and ensure they match. - for i := 0; i < end; i++ { + for i := range end { idx := strings.Index(subj, parts[i]) switch i { diff --git a/internal/hash_bench_test.go b/internal/hash_bench_test.go index 5384570a..88f87e7c 100644 --- a/internal/hash_bench_test.go +++ b/internal/hash_bench_test.go @@ -184,7 +184,7 @@ func TestHashCollisions(t *testing.T) { for _, prefix := range prefixes { for _, suffix := range suffixes { for _, variation := range variations { - for i := 0; i < 100; i++ { + for i := range 100 { input := fmt.Sprintf("%s%s%s-%d", prefix, suffix, variation, i) hash := XXHash64sum(input) if existing, exists := xxhashHashes[hash]; exists { @@ -211,7 +211,7 @@ func TestHashCollisions(t *testing.T) { seqCount := 0 for _, pattern := range patterns { - for i := 0; i < 10000; i++ { + for i := range 10000 { input := fmt.Sprintf(pattern, i) hash := XXHash64sum(input) if existing, exists := xxhashHashes[hash]; exists { diff --git a/internal/honeypot/naive/naive.go b/internal/honeypot/naive/naive.go index f62f4ef8..95093bcf 100644 --- a/internal/honeypot/naive/naive.go +++ b/internal/honeypot/naive/naive.go @@ -120,7 +120,7 @@ func (i *Impl) makeAffirmations() []string { count := rand.IntN(5) + 1 var result []string - for j := 0; j < count; j++ { + for range count { result = append(result, i.affirmation.Spin()) } @@ -131,7 +131,7 @@ func (i *Impl) makeSpins() []string { count := rand.IntN(5) + 1 var result []string - for j := 0; j < count; j++ { + for range count { result = append(result, i.body.Spin()) } diff --git a/internal/listor.go b/internal/listor.go index b6ba57fe..73110b79 100644 --- a/internal/listor.go +++ b/internal/listor.go @@ -16,7 +16,7 @@ func (lo *ListOr[T]) UnmarshalJSON(data []byte) error { // Check if first non-whitespace character is '[' firstChar := data[0] - for i := 0; i < len(data); i++ { + for i := range data { if data[i] != ' ' && data[i] != '\t' && data[i] != '\n' && data[i] != '\r' { firstChar = data[i] break @@ -36,4 +36,4 @@ func (lo *ListOr[T]) UnmarshalJSON(data []byte) error { } return nil -} \ No newline at end of file +} diff --git a/internal/ogtags/mem_test.go b/internal/ogtags/mem_test.go index 3770a73f..aebb4f6f 100644 --- a/internal/ogtags/mem_test.go +++ b/internal/ogtags/mem_test.go @@ -95,7 +95,7 @@ func TestMemoryUsage(t *testing.T) { // Run getTarget many times u, _ := url.Parse("/path/to/resource?query=1&foo=bar&baz=qux") - for i := 0; i < 10000; i++ { + for range 10000 { _ = cache.getTarget(u) } @@ -129,7 +129,7 @@ func TestMemoryUsage(t *testing.T) { runtime.GC() runtime.ReadMemStats(&m1) - for i := 0; i < 1000; i++ { + for range 1000 { _ = cache.extractOGTags(doc) } diff --git a/internal/ogtags/ogtags_fuzz_test.go b/internal/ogtags/ogtags_fuzz_test.go index 6355eebf..8b7abb91 100644 --- a/internal/ogtags/ogtags_fuzz_test.go +++ b/internal/ogtags/ogtags_fuzz_test.go @@ -3,6 +3,7 @@ package ogtags import ( "context" "net/url" + "slices" "strings" "testing" "unicode/utf8" @@ -78,7 +79,7 @@ func FuzzGetTarget(f *testing.F) { } // Ensure no memory corruption by calling multiple times - for i := 0; i < 3; i++ { + for range 3 { result2 := cache.getTarget(u) if result != result2 { t.Errorf("getTarget not deterministic: %q != %q", result, result2) @@ -148,11 +149,8 @@ func FuzzExtractOGTags(f *testing.F) { } } if !approved { - for _, tag := range cache.approvedTags { - if property == tag { - approved = true - break - } + if slices.Contains(cache.approvedTags, property) { + approved = true } } if !approved { @@ -260,11 +258,8 @@ func FuzzExtractMetaTagInfo(f *testing.F) { } } if !approved { - for _, tag := range cache.approvedTags { - if property == tag { - approved = true - break - } + if slices.Contains(cache.approvedTags, property) { + approved = true } } if !approved { diff --git a/internal/ogtags/parse.go b/internal/ogtags/parse.go index c21fd795..98176558 100644 --- a/internal/ogtags/parse.go +++ b/internal/ogtags/parse.go @@ -1,6 +1,7 @@ package ogtags import ( + "slices" "strings" "golang.org/x/net/html" @@ -65,10 +66,8 @@ func (c *OGTagCache) extractMetaTagInfo(n *html.Node) (property, content string) } // Check exact matches - for _, tag := range c.approvedTags { - if propertyKey == tag { - return propertyKey, content - } + if slices.Contains(c.approvedTags, propertyKey) { + return propertyKey, content } return "", content diff --git a/internal/test/playwright_test.go b/internal/test/playwright_test.go index b1cab340..4d6355ba 100644 --- a/internal/test/playwright_test.go +++ b/internal/test/playwright_test.go @@ -270,7 +270,7 @@ func TestPlaywrightBrowser(t *testing.T) { var performedAction action var err error - for i := 0; i < 5; i++ { + for i := range 5 { performedAction, err = executeTestCase(t, tc, typ, anubisURL) if performedAction == tc.action { break diff --git a/lib/anubis.go b/lib/anubis.go index feff53a3..3223b96d 100644 --- a/lib/anubis.go +++ b/lib/anubis.go @@ -81,11 +81,11 @@ type Server struct { func (s *Server) getTokenKeyfunc() jwt.Keyfunc { // return ED25519 key if HS512 is not set if len(s.hs512Secret) == 0 { - return func(token *jwt.Token) (interface{}, error) { + return func(token *jwt.Token) (any, error) { return s.ed25519Priv.Public().(ed25519.PublicKey), nil } } else { - return func(token *jwt.Token) (interface{}, error) { + return func(token *jwt.Token) (any, error) { return s.hs512Secret, nil } } diff --git a/lib/anubis_test.go b/lib/anubis_test.go index 07785d37..7c0f87d3 100644 --- a/lib/anubis_test.go +++ b/lib/anubis_test.go @@ -38,8 +38,8 @@ func NewTLogWriter(t *testing.T) io.Writer { // Write splits input on newlines and logs each line separately. func (w *TLogWriter) Write(p []byte) (n int, err error) { - lines := strings.Split(string(p), "\n") - for _, line := range lines { + lines := strings.SplitSeq(string(p), "\n") + for line := range lines { if line != "" { w.t.Log(line) } diff --git a/lib/config/config.go b/lib/config/config.go index d50cdae3..01b46576 100644 --- a/lib/config/config.go +++ b/lib/config/config.go @@ -228,8 +228,8 @@ type ImportStatement struct { } func (is *ImportStatement) open() (fs.File, error) { - if strings.HasPrefix(is.Import, "(data)/") { - fname := strings.TrimPrefix(is.Import, "(data)/") + if after, ok := strings.CutPrefix(is.Import, "(data)/"); ok { + fname := after fin, err := data.BotPolicies.Open(fname) return fin, err } @@ -325,7 +325,7 @@ func (sc StatusCodes) Valid() error { } type fileConfig struct { - OpenGraph openGraphFileConfig `json:"openGraph,omitempty"` + OpenGraph openGraphFileConfig `json:"openGraph"` Impressum *Impressum `json:"impressum,omitempty"` Store *Store `json:"store"` Bots []BotOrImport `json:"bots"` diff --git a/lib/config/config_test.go b/lib/config/config_test.go index 1b933759..ce57feed 100644 --- a/lib/config/config_test.go +++ b/lib/config/config_test.go @@ -188,7 +188,6 @@ func TestBotValid(t *testing.T) { } for _, cs := range tests { - cs := cs t.Run(cs.name, func(t *testing.T) { err := cs.bot.Valid() if err == nil && cs.err == nil { @@ -216,7 +215,6 @@ func TestConfigValidKnownGood(t *testing.T) { } for _, st := range finfos { - st := st t.Run(st.Name(), func(t *testing.T) { fin, err := os.Open(filepath.Join("testdata", "good", st.Name())) if err != nil { @@ -303,7 +301,6 @@ func TestConfigValidBad(t *testing.T) { } for _, st := range finfos { - st := st t.Run(st.Name(), func(t *testing.T) { fin, err := os.Open(filepath.Join("testdata", "bad", st.Name())) if err != nil { diff --git a/lib/config_test.go b/lib/config_test.go index 5d392dbb..99aab736 100644 --- a/lib/config_test.go +++ b/lib/config_test.go @@ -24,7 +24,6 @@ func TestBadConfigs(t *testing.T) { } for _, st := range finfos { - st := st t.Run(st.Name(), func(t *testing.T) { if _, err := LoadPoliciesOrDefault(t.Context(), filepath.Join("config", "testdata", "bad", st.Name()), anubis.DefaultDifficulty, "info"); err == nil { t.Fatal(err) @@ -42,7 +41,6 @@ func TestGoodConfigs(t *testing.T) { } for _, st := range finfos { - st := st t.Run(st.Name(), func(t *testing.T) { t.Run("with-thoth", func(t *testing.T) { ctx := thothmock.WithMockThoth(t) diff --git a/lib/http.go b/lib/http.go index 790a4aae..4cb4aa99 100644 --- a/lib/http.go +++ b/lib/http.go @@ -182,10 +182,7 @@ func makeCode(err error) string { enc := base64.StdEncoding.EncodeToString(buf.Bytes()) var builder strings.Builder for i := 0; i < len(enc); i += width { - end := i + width - if end > len(enc) { - end = len(enc) - } + end := min(i+width, len(enc)) builder.WriteString(enc[i:end]) builder.WriteByte('\n') } diff --git a/lib/policy/expressions/environment_test.go b/lib/policy/expressions/environment_test.go index beb1c2c6..c33fbbcb 100644 --- a/lib/policy/expressions/environment_test.go +++ b/lib/policy/expressions/environment_test.go @@ -103,7 +103,7 @@ func TestBotEnvironment(t *testing.T) { t.Fatalf("failed to compile expression %q: %v", tt.expression, err) } - result, _, err := prog.Eval(map[string]interface{}{ + result, _, err := prog.Eval(map[string]any{ "headers": tt.headers, }) if err != nil { @@ -168,7 +168,7 @@ func TestBotEnvironment(t *testing.T) { t.Fatalf("failed to compile expression %q: %v", tt.expression, err) } - result, _, err := prog.Eval(map[string]interface{}{ + result, _, err := prog.Eval(map[string]any{ "path": tt.path, }) if err != nil { @@ -280,7 +280,7 @@ func TestBotEnvironment(t *testing.T) { t.Fatalf("failed to compile expression %q: %v", tt.expression, err) } - result, _, err := prog.Eval(map[string]interface{}{}) + result, _, err := prog.Eval(map[string]any{}) if err != nil { t.Fatalf("failed to evaluate expression %q: %v", tt.expression, err) } @@ -359,7 +359,7 @@ func TestBotEnvironment(t *testing.T) { t.Fatalf("failed to compile expression %q: %v", tt.expression, err) } - result, _, err := prog.Eval(map[string]interface{}{}) + result, _, err := prog.Eval(map[string]any{}) if err != nil { t.Fatalf("failed to evaluate expression %q: %v", tt.expression, err) } @@ -421,7 +421,7 @@ func TestBotEnvironment(t *testing.T) { t.Fatalf("failed to compile expression %q: %v", tt.expression, err) } - result, _, err := prog.Eval(map[string]interface{}{}) + result, _, err := prog.Eval(map[string]any{}) if err != nil { t.Fatalf("failed to evaluate expression %q: %v", tt.expression, err) } @@ -514,7 +514,7 @@ func TestBotEnvironment(t *testing.T) { t.Fatalf("failed to compile expression %q: %v", tt.expression, err) } - result, _, err := prog.Eval(map[string]interface{}{}) + result, _, err := prog.Eval(map[string]any{}) if err != nil { t.Fatalf("failed to evaluate expression %q: %v", tt.expression, err) } @@ -572,7 +572,7 @@ func TestBotEnvironment(t *testing.T) { t.Fatalf("failed to compile expression %q: %v", tt.expression, err) } - result, _, err := prog.Eval(map[string]interface{}{}) + result, _, err := prog.Eval(map[string]any{}) if tt.evalError { if err == nil { t.Errorf("%s: expected an evaluation error, but got none", tt.description) @@ -598,7 +598,7 @@ func TestThresholdEnvironment(t *testing.T) { } tests := []struct { - variables map[string]interface{} + variables map[string]any name string expression string description string @@ -608,7 +608,7 @@ func TestThresholdEnvironment(t *testing.T) { { name: "weight-variable-available", expression: `weight > 100`, - variables: map[string]interface{}{"weight": 150}, + variables: map[string]any{"weight": 150}, expected: types.Bool(true), description: "should support weight variable in expressions", shouldCompile: true, @@ -616,7 +616,7 @@ func TestThresholdEnvironment(t *testing.T) { { name: "weight-variable-false-case", expression: `weight > 100`, - variables: map[string]interface{}{"weight": 50}, + variables: map[string]any{"weight": 50}, expected: types.Bool(false), description: "should correctly evaluate weight comparisons", shouldCompile: true, @@ -624,7 +624,7 @@ func TestThresholdEnvironment(t *testing.T) { { name: "missingHeader-not-available", expression: `missingHeader(headers, "Test")`, - variables: map[string]interface{}{}, + variables: map[string]any{}, expected: types.Bool(false), // not used description: "should not have missingHeader function available", shouldCompile: false, @@ -667,7 +667,7 @@ func TestNewEnvironment(t *testing.T) { tests := []struct { name string expression string - variables map[string]interface{} + variables map[string]any expectBool *bool // nil if we just want to test compilation or non-bool result description string shouldCompile bool @@ -675,7 +675,7 @@ func TestNewEnvironment(t *testing.T) { { name: "randInt-function-compilation", expression: `randInt(10)`, - variables: map[string]interface{}{}, + variables: map[string]any{}, expectBool: nil, // Don't check result, just compilation description: "should compile randInt function", shouldCompile: true, @@ -683,7 +683,7 @@ func TestNewEnvironment(t *testing.T) { { name: "randInt-range-validation", expression: `randInt(10) >= 0 && randInt(10) < 10`, - variables: map[string]interface{}{}, + variables: map[string]any{}, expectBool: boolPtr(true), description: "should return values in correct range", shouldCompile: true, @@ -691,7 +691,7 @@ func TestNewEnvironment(t *testing.T) { { name: "strings-extension-size", expression: `"hello".size() == 5`, - variables: map[string]interface{}{}, + variables: map[string]any{}, expectBool: boolPtr(true), description: "should support string extension functions", shouldCompile: true, @@ -699,7 +699,7 @@ func TestNewEnvironment(t *testing.T) { { name: "strings-extension-contains", expression: `"hello world".contains("world")`, - variables: map[string]interface{}{}, + variables: map[string]any{}, expectBool: boolPtr(true), description: "should support string contains function", shouldCompile: true, @@ -707,7 +707,7 @@ func TestNewEnvironment(t *testing.T) { { name: "strings-extension-startsWith", expression: `"hello world".startsWith("hello")`, - variables: map[string]interface{}{}, + variables: map[string]any{}, expectBool: boolPtr(true), description: "should support string startsWith function", shouldCompile: true, diff --git a/lib/policy/policy_test.go b/lib/policy/policy_test.go index 56ce3731..7f14d3f9 100644 --- a/lib/policy/policy_test.go +++ b/lib/policy/policy_test.go @@ -32,7 +32,6 @@ func TestGoodConfigs(t *testing.T) { } for _, st := range finfos { - st := st t.Run(st.Name(), func(t *testing.T) { t.Run("with-thoth", func(t *testing.T) { fin, err := os.Open(filepath.Join("..", "config", "testdata", "good", st.Name())) @@ -71,7 +70,6 @@ func TestBadConfigs(t *testing.T) { } for _, st := range finfos { - st := st t.Run(st.Name(), func(t *testing.T) { fin, err := os.Open(filepath.Join("..", "config", "testdata", "bad", st.Name())) if err != nil { diff --git a/lib/store/s3api/s3api_test.go b/lib/store/s3api/s3api_test.go index 5b7a9c6b..37d58a76 100644 --- a/lib/store/s3api/s3api_test.go +++ b/lib/store/s3api/s3api_test.go @@ -6,6 +6,7 @@ import ( "encoding/json" "fmt" "io" + "maps" "sync" "testing" "time" @@ -36,9 +37,7 @@ func (m *mockS3) PutObject(ctx context.Context, in *s3.PutObjectInput, _ ...func m.data[aws.ToString(in.Key)] = bytes.Clone(b) if in.Metadata != nil { m.meta[aws.ToString(in.Key)] = map[string]string{} - for k, v := range in.Metadata { - m.meta[aws.ToString(in.Key)][k] = v - } + maps.Copy(m.meta[aws.ToString(in.Key)], in.Metadata) } m.bucket = aws.ToString(in.Bucket) return &s3.PutObjectOutput{}, nil diff --git a/lib/store/valkey/factory.go b/lib/store/valkey/factory.go index 309bf656..49a63469 100644 --- a/lib/store/valkey/factory.go +++ b/lib/store/valkey/factory.go @@ -103,7 +103,7 @@ func (s Sentinel) Valid() error { // redisClient is satisfied by *valkey.Client and *valkey.ClusterClient. type redisClient interface { Get(ctx context.Context, key string) *valkey.StringCmd - Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *valkey.StatusCmd + Set(ctx context.Context, key string, value any, expiration time.Duration) *valkey.StatusCmd Del(ctx context.Context, keys ...string) *valkey.IntCmd Ping(ctx context.Context) *valkey.StatusCmd } diff --git a/lib/thoth/auth.go b/lib/thoth/auth.go index f4d52c6d..5f7d0c91 100644 --- a/lib/thoth/auth.go +++ b/lib/thoth/auth.go @@ -11,8 +11,8 @@ func authUnaryClientInterceptor(token string) grpc.UnaryClientInterceptor { return func( ctx context.Context, method string, - req interface{}, - reply interface{}, + req any, + reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption,