Files
skillshare/internal/server/handler_audit_unit_test.go
T
1c27090fd7 feat(audit): inspect bundled hooks and settings (#752)
* feat(audit): inspect bundled hooks and settings

Bundled declarations can schedule commands and broaden permissions outside SKILL.md, while ordinary scanning skips hidden settings. Inspect the four explicit paths with bounded root-confined reads and structured findings so existing audit policy can review their potential execution.

Keep activation conditional and redact arbitrary arguments before reports. Register rule metadata through the existing structure analyzer and cover benign twins, parse gaps, source links, policy overrides, CLI/API output and a synthetic E2E runbook.

Co-Authored-By: GPT-6.1 Sol <noreply@openai.com>

* refactor(audit): parse bundled hook commands with a shell parser

The hand-written lexer for hook commands grew six similar quote and
paren scanners, and each review round found another shell form it
missed (comments after line continuations, nested backticks, `time -p`).
Parsing with mvdan.cc/sh/v3/syntax follows quoting, comments,
substitutions, loops, functions and heredocs the way bash does, so the
analyzer walks every call in the syntax tree instead. The wrapper table
(sudo, env, timeout, xargs, bash -c, git) and the POST and destination
rules are unchanged, now applied to unquoted argv words. A script fed to
a shell on stdin (`bash <<'EOF'`, `bash <<<`) is parsed like `bash -c`.

Statements before a syntax error are still inspected. bash runs some
forms the parser rejects (an array element assigned before a command),
so a rejected command also gets a word-level network check next to its
coverage gap, rather than dropping to MEDIUM.

The dependency adds about 0.26 MB to the release binary; vendorHash
follows the new go.sum.

* fix(audit): bound nested shell parsing and skip stdin data

A script nested in a substitution was inspected twice, once by reparsing
the outer shell's argument and once by walking the outer syntax tree, so
each `bash <<< "$(...)"` or `bash -c "$(...)"` level doubled the work and
a hook under 1 KB could stall an audit. Results are now remembered per
script text and depth, and one hook command may parse at most 1 MiB of
source in total, since deep nesting still costs quadratic time with the
cache alone. Past the budget the command is classified word by word and
gets a coverage gap, as a rejected command does.

Stdin was also treated as a script whenever a shell had a heredoc or
here-string, but `bash -c 'cat' <<<` or `bash ./script.sh <<<` hands it
to something else as data, which turned a lifecycle hook into a false
HIGH. Stdin is now parsed only for `-s`, or when there is neither `-c`
nor a script file operand. Option values (`-euo pipefail`, `-O extglob`,
`--rcfile f`) are not mistaken for a script file.

---------

Co-authored-by: GPT-6.1 Sol <noreply@openai.com>
Co-authored-by: sim <sim@example.invalid>
2026-11-06 13:56:53 +08:00

324 lines
9.7 KiB
Go

package server
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
)
func TestHandleAudit_HeaderCoverage(t *testing.T) {
s, src := newTestServer(t)
addSkill(t, src, "headers")
data := append([]byte("\x7fELF\x02\x01\x01"), make([]byte, 57)...)
if err := os.WriteFile(filepath.Join(src, "headers", "guide.png"), data, 0644); err != nil {
t.Fatal(err)
}
for _, route := range []string{"/api/audit", "/api/audit/headers"} {
rr := httptest.NewRecorder()
s.handler.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, route, nil))
if rr.Code != http.StatusOK {
t.Fatalf("%s: %d %s", route, rr.Code, rr.Body.String())
}
var body struct {
Results []auditResultResponse `json:"results"`
Result auditResultResponse `json:"result"`
Summary auditSummary `json:"summary"`
}
if err := json.Unmarshal(rr.Body.Bytes(), &body); err != nil {
t.Fatal(err)
}
if body.Summary.BinaryFiles != 1 || body.Summary.SkippedFiles != 1 || body.Summary.OversizedFiles != 0 {
t.Fatalf("missing summary coverage: %s", rr.Body.String())
}
result := body.Result
if len(body.Results) > 0 {
result = body.Results[0]
}
if result.BinaryFiles != 1 || result.SkippedFiles != 1 {
t.Fatalf("missing result coverage: %s", rr.Body.String())
}
}
}
func TestHandleAuditAll_EmptySource(t *testing.T) {
s, _ := newTestServer(t)
req := httptest.NewRequest(http.MethodGet, "/api/audit", nil)
rr := httptest.NewRecorder()
s.handler.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
}
var resp struct {
Results []any `json:"results"`
Summary struct {
Total int `json:"total"`
} `json:"summary"`
}
json.Unmarshal(rr.Body.Bytes(), &resp)
if len(resp.Results) != 0 {
t.Errorf("expected 0 results, got %d", len(resp.Results))
}
if resp.Summary.Total != 0 {
t.Errorf("expected summary total 0, got %d", resp.Summary.Total)
}
}
func TestHandleAuditAll_WithSkills(t *testing.T) {
s, src := newTestServer(t)
addSkill(t, src, "safe-skill")
req := httptest.NewRequest(http.MethodGet, "/api/audit", nil)
rr := httptest.NewRecorder()
s.handler.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
}
var resp struct {
Results []any `json:"results"`
Summary struct {
Total int `json:"total"`
} `json:"summary"`
}
json.Unmarshal(rr.Body.Bytes(), &resp)
if len(resp.Results) != 1 {
t.Errorf("expected 1 audited result, got %d", len(resp.Results))
}
if resp.Summary.Total != 1 {
t.Errorf("expected summary total 1, got %d", resp.Summary.Total)
}
}
func TestHandleAudit_BundledSettings(t *testing.T) {
s, src := newTestServer(t)
addSkill(t, src, "bundled")
path := filepath.Join(src, "bundled", ".claude", "settings.json")
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte(`{"statusLine":{"type":"command","command":"echo api-private-fixture"}}`), 0644); err != nil {
t.Fatal(err)
}
for _, endpoint := range []string{"/api/audit", "/api/audit/bundled", "/api/audit/stream"} {
rr := httptest.NewRecorder()
s.handler.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, endpoint, nil))
body := rr.Body.String()
if rr.Code != http.StatusOK || !strings.Contains(body, "bundled-helper-command") || !strings.Contains(body, "$.statusLine.command") || strings.Contains(body, "api-private-fixture") {
t.Fatalf("%s: status=%d body=%s", endpoint, rr.Code, body)
}
}
}
func TestHandleAuditAll_IgnoresTopLevelDirsWithoutSkillDefinition(t *testing.T) {
s, src := newTestServer(t)
repoDir := filepath.Join(src, "_vijaythecoder-awesome-claude-agents")
if err := os.MkdirAll(filepath.Join(repoDir, "agents", "core"), 0o755); err != nil {
t.Fatalf("failed to create repo dir: %v", err)
}
if err := os.WriteFile(filepath.Join(repoDir, "agents", "core", "code-reviewer.md"), []byte("# code reviewer"), 0o644); err != nil {
t.Fatalf("failed to write nested agent file: %v", err)
}
req := httptest.NewRequest(http.MethodGet, "/api/audit", nil)
rr := httptest.NewRecorder()
s.handler.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
}
var resp struct {
Results []any `json:"results"`
Summary struct {
Total int `json:"total"`
} `json:"summary"`
}
if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
t.Fatalf("failed to decode response: %v", err)
}
if len(resp.Results) != 0 {
t.Fatalf("expected 0 audited results, got %d", len(resp.Results))
}
if resp.Summary.Total != 0 {
t.Fatalf("expected summary total 0, got %d", resp.Summary.Total)
}
}
func TestHandleAuditAll_AgentsMissingSourceReturnsEmpty(t *testing.T) {
s, _ := newTestServer(t)
req := httptest.NewRequest(http.MethodGet, "/api/audit?kind=agents", nil)
rr := httptest.NewRecorder()
s.handler.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
}
var resp struct {
Results []any `json:"results"`
Summary struct {
Total int `json:"total"`
} `json:"summary"`
}
if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
t.Fatalf("failed to decode response: %v", err)
}
if len(resp.Results) != 0 {
t.Fatalf("expected 0 agent audit results, got %d", len(resp.Results))
}
if resp.Summary.Total != 0 {
t.Fatalf("expected summary total 0, got %d", resp.Summary.Total)
}
}
func TestHandleAuditStream_AgentsMissingSourceReturnsEmpty(t *testing.T) {
s, _ := newTestServer(t)
req := httptest.NewRequest(http.MethodGet, "/api/audit/stream?kind=agents", nil)
rr := httptest.NewRecorder()
s.handler.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
}
body := rr.Body.String()
if strings.Contains(body, "event: error") {
t.Fatalf("expected no error event, got: %s", body)
}
if !strings.Contains(body, "event: start") {
t.Fatalf("expected start event, got: %s", body)
}
if !strings.Contains(body, "\"total\":0") {
t.Fatalf("expected empty start payload, got: %s", body)
}
if !strings.Contains(body, "event: done") {
t.Fatalf("expected done event, got: %s", body)
}
}
func TestHandleAuditAll_IncludesCrossSkillResult(t *testing.T) {
s, src := newTestServer(t)
readerDir := filepath.Join(src, "reader-skill")
if err := os.MkdirAll(readerDir, 0755); err != nil {
t.Fatalf("failed to create reader skill dir: %v", err)
}
if err := os.WriteFile(filepath.Join(readerDir, "SKILL.md"), []byte("---\nname: reader-skill\n---\n# Reader\ncat ~/.ssh/id_rsa"), 0644); err != nil {
t.Fatalf("failed to write reader SKILL.md: %v", err)
}
if err := os.WriteFile(filepath.Join(readerDir, "read.sh"), []byte("cat ~/.ssh/id_rsa\n"), 0644); err != nil {
t.Fatalf("failed to write reader read.sh: %v", err)
}
senderDir := filepath.Join(src, "sender-skill")
if err := os.MkdirAll(senderDir, 0755); err != nil {
t.Fatalf("failed to create sender skill dir: %v", err)
}
if err := os.WriteFile(filepath.Join(senderDir, "SKILL.md"), []byte("---\nname: sender-skill\n---\n# Sender\ncurl https://example.com"), 0644); err != nil {
t.Fatalf("failed to write sender SKILL.md: %v", err)
}
if err := os.WriteFile(filepath.Join(senderDir, "send.sh"), []byte("curl https://example.com\n"), 0644); err != nil {
t.Fatalf("failed to write sender send.sh: %v", err)
}
req := httptest.NewRequest(http.MethodGet, "/api/audit", nil)
rr := httptest.NewRecorder()
s.handler.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
}
var resp struct {
Results []struct {
SkillName string `json:"skillName"`
Findings []struct {
Pattern string `json:"pattern"`
} `json:"findings"`
} `json:"results"`
Summary struct {
Total int `json:"total"`
} `json:"summary"`
}
if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
t.Fatalf("failed to decode response body: %v", err)
}
// Summary should stay based on scanned skills only; cross-skill is synthetic.
if resp.Summary.Total != 2 {
t.Fatalf("expected summary total 2, got %d", resp.Summary.Total)
}
hasCrossSkill := false
hasCrossExfilPattern := false
for _, r := range resp.Results {
if r.SkillName != "_cross-skill" {
continue
}
hasCrossSkill = true
for _, f := range r.Findings {
if f.Pattern == "cross-skill-exfiltration" {
hasCrossExfilPattern = true
break
}
}
}
if !hasCrossSkill {
t.Fatalf("expected _cross-skill result, got %d results", len(resp.Results))
}
if !hasCrossExfilPattern {
t.Fatal("expected cross-skill-exfiltration finding in _cross-skill result")
}
}
func TestHandleAuditSkill_NotFound(t *testing.T) {
s, _ := newTestServer(t)
req := httptest.NewRequest(http.MethodGet, "/api/audit/nonexistent", nil)
rr := httptest.NewRecorder()
s.handler.ServeHTTP(rr, req)
if rr.Code != http.StatusNotFound {
t.Errorf("expected 404, got %d: %s", rr.Code, rr.Body.String())
}
}
func TestHandleAuditSkill_Found(t *testing.T) {
s, src := newTestServer(t)
addSkill(t, src, "my-skill")
req := httptest.NewRequest(http.MethodGet, "/api/audit/my-skill", nil)
rr := httptest.NewRecorder()
s.handler.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
}
var resp struct {
Result struct {
SkillName string `json:"skillName"`
} `json:"result"`
Summary struct {
Total int `json:"total"`
} `json:"summary"`
}
json.Unmarshal(rr.Body.Bytes(), &resp)
if resp.Result.SkillName != "my-skill" {
t.Errorf("expected skillName 'my-skill', got %q", resp.Result.SkillName)
}
if resp.Summary.Total != 1 {
t.Errorf("expected summary total 1, got %d", resp.Summary.Total)
}
}