From a03e5a8e201850d360e911770ca092088f25d62d Mon Sep 17 00:00:00 2001 From: Willie Date: Sun, 22 Feb 2026 20:05:00 +0800 Subject: [PATCH] test: add isSecurityError unit test and server audit gate rollback failure tests - cmd/skillshare/update_test.go: table-driven test covering all isSecurityError keywords (security audit, post-update audit, rolled back, rollback failed) and negative cases - internal/server/handler_update_test.go: three tests for auditGateTrackedRepo: rollback failure warning on HIGH findings, rollback failure warning on scan error, and clean repo returns nil --- cmd/skillshare/update_test.go | 51 ++++++++++ internal/server/handler_update_test.go | 132 +++++++++++++++++++++++++ 2 files changed, 183 insertions(+) create mode 100644 cmd/skillshare/update_test.go create mode 100644 internal/server/handler_update_test.go diff --git a/cmd/skillshare/update_test.go b/cmd/skillshare/update_test.go new file mode 100644 index 000000000..e9a913930 --- /dev/null +++ b/cmd/skillshare/update_test.go @@ -0,0 +1,51 @@ +package main + +import ( + "fmt" + "testing" +) + +func TestIsSecurityError(t *testing.T) { + tests := []struct { + err error + want bool + }{ + {nil, false}, + {fmt.Errorf("connection timeout"), false}, + {fmt.Errorf("skill not found"), false}, + {fmt.Errorf("has uncommitted changes"), false}, + + // auditGateAfterPull errors + {fmt.Errorf("security audit failed: scan error (use --skip-audit to bypass)"), true}, + {fmt.Errorf("security audit found HIGH/CRITICAL findings — rolled back (use --skip-audit to bypass)"), true}, + {fmt.Errorf("update rejected by user after security audit"), true}, + {fmt.Errorf("rollback failed: permission denied"), true}, + + // install.Install post-update audit errors + {fmt.Errorf("post-update audit failed: scan error (use --skip-audit to bypass)"), true}, + {fmt.Errorf("post-update audit found HIGH/CRITICAL findings — rolled back (use --skip-audit to bypass)"), true}, + + // install.Install install-time audit errors + {fmt.Errorf("security audit failed — findings at/above HIGH detected"), true}, + + // Partial keyword matches should still work + {fmt.Errorf("something security audit something"), true}, + {fmt.Errorf("was rolled back successfully"), true}, + } + + for _, tt := range tests { + name := "nil" + if tt.err != nil { + name = tt.err.Error() + if len(name) > 60 { + name = name[:60] + } + } + t.Run(name, func(t *testing.T) { + got := isSecurityError(tt.err) + if got != tt.want { + t.Errorf("isSecurityError(%v) = %v, want %v", tt.err, got, tt.want) + } + }) + } +} diff --git a/internal/server/handler_update_test.go b/internal/server/handler_update_test.go new file mode 100644 index 000000000..24bf25046 --- /dev/null +++ b/internal/server/handler_update_test.go @@ -0,0 +1,132 @@ +package server + +import ( + "os" + "os/exec" + "path/filepath" + "strings" + "testing" + + "skillshare/internal/config" +) + +// initGitRepo creates a minimal git repo with an initial commit. +func initGitRepo(t *testing.T, dir string) { + t.Helper() + for _, args := range [][]string{ + {"init"}, + {"config", "user.email", "test@test.com"}, + {"config", "user.name", "Test"}, + } { + cmd := exec.Command("git", args...) + cmd.Dir = dir + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("git %v failed: %s %v", args, out, err) + } + } + // Create initial commit + f := filepath.Join(dir, "README.md") + os.WriteFile(f, []byte("# init"), 0644) + for _, args := range [][]string{ + {"add", "."}, + {"commit", "-m", "init"}, + } { + cmd := exec.Command("git", args...) + cmd.Dir = dir + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("git %v failed: %s %v", args, out, err) + } + } +} + +func TestAuditGateTrackedRepo_RollbackFailure_ReportsWarning(t *testing.T) { + // Create a git repo with a HIGH-severity finding + repoDir := t.TempDir() + initGitRepo(t, repoDir) + + // Add a file that triggers a HIGH finding (prompt injection in HTML comment) + malicious := filepath.Join(repoDir, "SKILL.md") + os.WriteFile(malicious, []byte("# Skill\n\n"), 0644) + for _, args := range [][]string{ + {"add", "."}, + {"commit", "-m", "add malicious content"}, + } { + cmd := exec.Command("git", args...) + cmd.Dir = repoDir + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("git %v failed: %s %v", args, out, err) + } + } + + // Create a minimal server + cfg := &config.Config{Source: t.TempDir()} + s := &Server{cfg: cfg} + + // Pass an invalid beforeHash so git reset --hard will fail + result := s.auditGateTrackedRepo("test-repo", repoDir, "0000000000000000000000000000000000000000") + + if result == nil { + t.Fatal("expected blocked result, got nil (audit should detect HIGH finding)") + } + if result.Action != "blocked" { + t.Errorf("expected action=blocked, got %q", result.Action) + } + if !strings.Contains(result.Message, "rollback") { + t.Errorf("expected rollback mention in message, got %q", result.Message) + } + if !strings.Contains(result.Message, "WARNING") { + t.Errorf("expected WARNING about failed rollback, got %q", result.Message) + } + if !strings.Contains(result.Message, "malicious content may remain") { + t.Errorf("expected 'malicious content may remain' warning, got %q", result.Message) + } +} + +func TestAuditGateTrackedRepo_ScanError_RollbackFailure_ReportsWarning(t *testing.T) { + cfg := &config.Config{Source: t.TempDir()} + s := &Server{cfg: cfg} + + // Non-existent path → audit.ScanSkill returns error, git.ResetHard also fails + nonExistentPath := filepath.Join(t.TempDir(), "does-not-exist") + result := s.auditGateTrackedRepo("test-repo", nonExistentPath, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef") + + if result == nil { + t.Fatal("expected blocked result") + } + if result.Action != "blocked" { + t.Errorf("expected action=blocked, got %q", result.Action) + } + if !strings.Contains(result.Message, "security audit failed") { + t.Errorf("expected 'security audit failed' in message, got %q", result.Message) + } + if !strings.Contains(result.Message, "WARNING") { + t.Errorf("expected WARNING in message, got %q", result.Message) + } + if !strings.Contains(result.Message, "malicious content may remain") { + t.Errorf("expected 'malicious content may remain' warning, got %q", result.Message) + } +} + +func TestAuditGateTrackedRepo_Clean_ReturnsNil(t *testing.T) { + repoDir := t.TempDir() + initGitRepo(t, repoDir) + + // Only clean content — no findings + os.WriteFile(filepath.Join(repoDir, "SKILL.md"), []byte("# A clean skill\nJust helpful instructions.\n"), 0644) + for _, args := range [][]string{ + {"add", "."}, + {"commit", "-m", "clean"}, + } { + cmd := exec.Command("git", args...) + cmd.Dir = repoDir + cmd.CombinedOutput() + } + + cfg := &config.Config{Source: t.TempDir()} + s := &Server{cfg: cfg} + + result := s.auditGateTrackedRepo("clean-repo", repoDir, "doesntmatter") + if result != nil { + t.Errorf("expected nil for clean repo, got action=%q message=%q", result.Action, result.Message) + } +}