mirror of
https://github.com/tiennm99/goclaw.git
synced 2026-10-12 20:15:42 +00:00
* feat(hooks/observe): add structured output validation hook (ObserveHook) - hooks/types.go: Add ObserveHook interface + BuiltInHookType enum - hooks/dispatcher.go: HookDispatcher emits ObserveHook with PhaseResult payload - pipeline/observe_stage.go: ObserveStage emits hook after ObserveResult built - pipeline/substates.go: ObserveStageResult carries hook results + validation errors - hooks/config.go: BuiltInHookTypeObserve added to BuiltInHookType enum PhaseResult carries structured output, token usage, tool calls + validation errors emitted post-ObserveStage. ObserveHook implementations can validate structured output against schemas, detect tool-call loops, enforce token budgets, etc. Hook fires after ObserveStage produces ObserveResult, before results propagate to next stage. ValidationError returned by hook halts pipeline and propagates error to caller without further stage execution. Co-Authored-By: Claude <noreply@anthropic.com> * ui(hooks): add post_model_response event to web UI - Add event to Zod schema, filter dropdown, and form dialog - Implement conditional test panel UI for model response payload - Add translations (en/zh/vi) for new test panel fields - Updated beta description to reference the new event Co-Authored-By: Claude <noreply@anthropic.com> * feat(mcp): add MCP CRUD server exposing goclaw resources at /api/mcp/ with Bearer token auth and X-GoClaw-Tenant-Id header, default to master tenant * feat(mcp): add goclaw_skills_write_file tool to edit skill files on disk The CRUD MCP server's goclaw_skills_update only touched skill DB metadata, with no way to edit a skill's SKILL.md/file content on the filesystem. Extract the versioned write logic from the web UI's skill file editor (SkillsHandler.handleWriteFile) into skills.WriteVersionedFile so both surfaces share identical validation and versioning, and expose it as a new MCP tool. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> --------- Co-authored-by: Bruno Clermont <bruno.clermont@gmail.com> Co-authored-by: Claude <noreply@anthropic.com>
113 lines
3.7 KiB
Go
113 lines
3.7 KiB
Go
package gateway
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
// ---- mcpServerTokenAuthMiddleware ----
|
|
//
|
|
// Gates the CRUD MCP server (/api/mcp/) with its own bearer token, independent
|
|
// from tokenAuthMiddleware's general gateway token (see server.go). Mirrors
|
|
// TestTokenAuthMiddleware_* above; kept separate since the two middlewares
|
|
// are intentionally distinct code paths (different security logging) even
|
|
// though their pass/fail logic is currently identical.
|
|
|
|
func TestMCPServerTokenAuthMiddleware_ValidToken_PassesThrough(t *testing.T) {
|
|
token := "mcp-secret"
|
|
called := false
|
|
next := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
called = true
|
|
w.WriteHeader(http.StatusOK)
|
|
})
|
|
handler := mcpServerTokenAuthMiddleware(token, next)
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/api/mcp/", nil)
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
w := httptest.NewRecorder()
|
|
handler.ServeHTTP(w, req)
|
|
|
|
if !called {
|
|
t.Error("next handler should have been called with valid token")
|
|
}
|
|
if w.Code != http.StatusOK {
|
|
t.Errorf("status = %d, want 200", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestMCPServerTokenAuthMiddleware_WrongToken_Returns401(t *testing.T) {
|
|
next := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
})
|
|
handler := mcpServerTokenAuthMiddleware("correct-token", next)
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/api/mcp/", nil)
|
|
req.Header.Set("Authorization", "Bearer wrong-token")
|
|
w := httptest.NewRecorder()
|
|
handler.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusUnauthorized {
|
|
t.Errorf("status = %d, want 401", w.Code)
|
|
}
|
|
// Note: http.Error() (used internally) always overwrites Content-Type to
|
|
// text/plain regardless of what the handler set beforehand — this is a
|
|
// stdlib quirk, not a bug in mcpServerTokenAuthMiddleware, so we only
|
|
// assert on the body content here rather than the header.
|
|
if body := w.Body.String(); body == "" {
|
|
t.Error("expected a non-empty error body")
|
|
}
|
|
}
|
|
|
|
func TestMCPServerTokenAuthMiddleware_MissingAuthHeader_Returns401(t *testing.T) {
|
|
next := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
})
|
|
handler := mcpServerTokenAuthMiddleware("some-token", next)
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/api/mcp/", nil)
|
|
w := httptest.NewRecorder()
|
|
handler.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusUnauthorized {
|
|
t.Errorf("status = %d, want 401", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestMCPServerTokenAuthMiddleware_NonBearerScheme_Returns401(t *testing.T) {
|
|
next := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
})
|
|
handler := mcpServerTokenAuthMiddleware("token123", next)
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/api/mcp/", nil)
|
|
req.Header.Set("Authorization", "Basic dXNlcjpwYXNz")
|
|
w := httptest.NewRecorder()
|
|
handler.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusUnauthorized {
|
|
t.Errorf("status = %d, want 401", w.Code)
|
|
}
|
|
}
|
|
|
|
// TestMCPServerTokenAuthMiddleware_DistinctFromGatewayToken proves the two
|
|
// bearer tokens are independent: a request bearing the *general* gateway
|
|
// token must NOT satisfy the MCP server's own token gate.
|
|
func TestMCPServerTokenAuthMiddleware_DistinctFromGatewayToken(t *testing.T) {
|
|
gatewayToken := "gateway-token"
|
|
mcpToken := "mcp-token"
|
|
next := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
})
|
|
handler := mcpServerTokenAuthMiddleware(mcpToken, next)
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/api/mcp/", nil)
|
|
req.Header.Set("Authorization", "Bearer "+gatewayToken)
|
|
w := httptest.NewRecorder()
|
|
handler.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusUnauthorized {
|
|
t.Errorf("status = %d, want 401 (gateway token must not unlock the MCP server)", w.Code)
|
|
}
|
|
}
|