picoclaw/pkg/channels/magicform/magicform.go
nuestraai ac6e77d7c9
CLI overrides, workspace config, MagicForm channel, and security hardening (#1)
* feat(cli): add workspace, config-dir, tools, and skills override flags

Add --workspace, --config-dir, --tools, and --skills flags to
`picoclaw agent` for single-shot invocations. Supports workspace
override with bootstrap file injection (AGENTS.md, IDENTITY.md,
SOUL.md, USER.md), tool allowlisting, and skills filtering.

Also extracts FormatSkillsSummary from SkillsLoader for reuse,
adds SetSkillsFilter to ContextBuilder, and wires SkillsFilter
from agent config into the context builder.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* feat(channels): add MagicForm webhook channel plugin

Implement MagicForm channel for webhook-based agentic task delegation.
Supports Bearer token auth, async processing with HTTP callback,
workspace path validation against configurable workspace_root,
bootstrap file injection, tool/skill filtering via metadata, and
per-conversation session key isolation.

Includes Bus() accessor on BaseChannel for direct message publishing,
MagicFormConfig with workspace_root for path traversal prevention,
request body size limits, and TTL cleanup for stale request contexts.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* feat(agent): support per-request workspace, tool, and skill overrides

Add workspace override, tool allowlisting, and skills filtering to the
agent loop via processOptions metadata. When a workspace override is
active (e.g. from MagicForm channel), creates isolated SessionManager
and ContextBuilder instances per request.

Threads effSessions/effContextBuilder through all downstream paths
including runLLMIteration, forceCompression, maybeSummarize, and
summarizeSession to ensure full workspace isolation. Adds defense-in-
depth tool execution guard alongside LLM-facing tool definition filter.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* security(auth): add response body size limits to OAuth HTTP calls

Wrap all io.ReadAll(resp.Body) calls in the OAuth flow with
io.LimitReader capped at 1 MB to prevent memory exhaustion from
malicious or unexpectedly large server responses.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* security(tools): add response body size limits to search providers

Add 2 MB LimitReader to BraveSearch, Tavily, DuckDuckGo, and
Perplexity search providers to prevent memory exhaustion from
unexpectedly large responses. Matches existing pattern used by
GLMSearchProvider.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* security(tools): add 20 MB file write size limit

Reject writes exceeding 20 MB in WriteFileTool to prevent disk
exhaustion from unexpectedly large LLM-generated file content.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* security(tools): use crypto/rand for temp file naming

Replace predictable PID+nanosecond temp file names with
cryptographically random hex strings to prevent symlink
pre-placement attacks.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* security(skills): validate GitHub repo format and limit download size

Add regex validation for 'owner/repo' format to prevent URL injection
in InstallFromGitHub. Add 5 MB LimitReader on skill file downloads
to prevent memory exhaustion.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* security(auth): allow OAuth credentials to be overridden via env vars

Add PICOCLAW_OPENAI_CLIENT_ID, PICOCLAW_GOOGLE_CLIENT_ID, and
PICOCLAW_GOOGLE_CLIENT_SECRET env var overrides. Hardcoded values
remain as fallback defaults for backward compatibility.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* security(auth): sanitize OAuth error messages

Replace raw HTTP response bodies in error messages with generic
status-code-only messages. Full response details are logged at
debug level for troubleshooting.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* security(launcher): HTML-escape error messages in auth callback

Use html.EscapeString on all error text rendered in HTML responses
to prevent XSS via crafted OAuth error parameters or error messages.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* security(launcher): remove internal details from HTTP error responses

Log detailed errors server-side and return generic messages to
clients to prevent information disclosure of file paths and
internal state.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* security(launcher): add HTTP security headers middleware

Add SecurityHeaders middleware setting X-Content-Type-Options,
X-Frame-Options, and Content-Security-Policy on all responses
to mitigate MIME sniffing, clickjacking, and content injection.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* security(skills): validate ClawHub registry base URL scheme

Reject non-HTTPS registry URLs (unless localhost) to prevent
config-based redirection to malicious skill servers. Falls back
to the default https://clawhub.ai with a warning.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* security(skills): prevent workspace skills from shadowing builtins

Builtin skill names are now reserved — workspace and global skills
with the same name as a builtin are skipped with a warning log.
LoadSkill also checks builtins first. This prevents supply chain
attacks where a malicious skill replaces a trusted builtin.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* security(skills): warn when safety metadata is unavailable

Add MetadataAvailable flag to InstallResult. When the registry
metadata fetch fails (silent fallback), the user now sees a
warning that safety checks could not be completed.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* security(shell): add opt-in environment variable filtering

When filter_env is enabled in config, shell commands run with a minimal
allowlist of environment variables (PATH, HOME, LANG, TERM, etc. plus
PICOCLAW_* prefix), preventing accidental leakage of sensitive env vars
like API keys to spawned processes. Disabled by default for backward compat.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* feat(config): add workspace-local config.json overlay for per-tenant isolation

Support per-workspace config overrides via --config-dir (CLI) or configDir
(webhook), enabling different API keys, models, and agent settings per tenant.

- Add LoadWorkspaceConfig/MergeWorkspaceConfig with raw JSON overlay to
  preserve unmentioned bool fields (prevents clobbering tool enabled flags)
- Add Config.Clone() for safe per-request config copies in gateway mode
- Gateway: per-request provider creation from workspace config, with
  effProvider/effModel threaded through all LLM call sites and summarization
- CLI: workspace config merged before provider creation, CLI flags win
- MagicForm: replace inline bootstrap fields with configDir path;
  agent loop copies bootstrap files and loads config.json from configDir
- Fix --session flag: format as agent:main:cli:{key} so router honors it
- Fix cron session key: format as agent:main:cron:{id} for isolation
- Add shared CopyBootstrapFiles helper in pkg/agent/bootstrap.go
- Add config/workspace.config.example.json

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix(agent): honor workspace config overrides in model routing

selectCandidates uses agent.Model/Candidates baked in at startup, which
ignores per-request workspace config overrides (effProvider/effModel).
When the workspace overrides the provider, skip routing and fallback
candidates entirely to avoid cross-provider credential issues. When only
the model is overridden, allow routing but use the effective model.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* style: fix linter errors (gci, gofumpt, golines, govet, unused)

- gci: alphabetize magicform import in gateway/helpers.go
- gofumpt: reformat multi-line function signature in agent/helpers.go
- golines: wrap lines exceeding 120 chars across 5 files
- govet: rename shadowed variables (err → wcErr, s → trimmed)
- unused: remove maybeSummarize, forceCompression, summarizeSession
  wrappers superseded by their *With variants

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

---------

Co-authored-by: nuestraai <nuestraai@users.noreply.github.com>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: admin-mf <admin@magicform.ai>
2026-03-06 10:16:36 -06:00

420 lines
12 KiB
Go

package magicform
import (
"bytes"
"context"
"crypto/subtle"
"encoding/json"
"fmt"
"io"
"net/http"
"path/filepath"
"strings"
"sync"
"time"
"github.com/sipeed/picoclaw/pkg/bus"
"github.com/sipeed/picoclaw/pkg/channels"
"github.com/sipeed/picoclaw/pkg/config"
"github.com/sipeed/picoclaw/pkg/logger"
)
// WebhookPayload is the inbound payload from MagicForm.
type WebhookPayload struct {
StackID string `json:"stackId"`
ConversationID string `json:"conversationId"`
UserID string `json:"userId"`
Message string `json:"message"`
Workspace string `json:"workspace"` // e.g. "s1/c1" — agent working directory (relative to workspace_root)
ConfigDir string `json:"configDir,omitempty"` // e.g. "s1/config" — pre-provisioned config directory (relative to workspace_root)
CallbackURL string `json:"callbackUrl"`
// Tool/skill filtering
AllowedTools []string `json:"allowedTools,omitempty"` // Tool allowlist (empty = all)
AllowedSkills []string `json:"allowedSkills,omitempty"` // Skill filter (empty = all)
}
// CallbackPayload is the outbound payload sent back to MagicForm.
type CallbackPayload struct {
StackID string `json:"stackId"`
ConversationID string `json:"conversationId"`
Response string `json:"response"`
Type string `json:"type"` // "final"
}
// requestContext stores per-request state so Send() can resolve callback info.
type requestContext struct {
stackID string
conversationID string
userID string
callbackURL string
createdAt time.Time
}
// MagicFormChannel implements the MagicForm channel plugin.
type MagicFormChannel struct {
*channels.BaseChannel
config config.MagicFormConfig
httpClient *http.Client
requests sync.Map // chatID → *requestContext
ctx context.Context
cancel context.CancelFunc
}
// NewMagicFormChannel creates a new MagicForm channel.
func NewMagicFormChannel(cfg config.MagicFormConfig, msgBus *bus.MessageBus) (*MagicFormChannel, error) {
base := channels.NewBaseChannel(
"magicform",
cfg,
msgBus,
cfg.AllowFrom,
)
ctx, cancel := context.WithCancel(context.Background())
ch := &MagicFormChannel{
BaseChannel: base,
config: cfg,
httpClient: &http.Client{
Timeout: 30 * time.Second,
},
ctx: ctx,
cancel: cancel,
}
base.SetOwner(ch)
return ch, nil
}
// Start begins the channel and starts the TTL cleanup goroutine.
func (c *MagicFormChannel) Start(_ context.Context) error {
c.SetRunning(true)
// Background goroutine to clean up stale request contexts
go c.cleanupLoop()
logger.InfoCF("magicform", "MagicForm channel started", nil)
return nil
}
// Stop shuts down the channel.
func (c *MagicFormChannel) Stop(_ context.Context) error {
c.cancel()
c.SetRunning(false)
logger.InfoCF("magicform", "MagicForm channel stopped", nil)
return nil
}
// WebhookPath returns the HTTP path for the inbound webhook.
func (c *MagicFormChannel) WebhookPath() string {
if c.config.WebhookPath != "" {
return c.config.WebhookPath
}
return "/hooks/magicform"
}
// HealthPath returns the HTTP path for the health check endpoint.
func (c *MagicFormChannel) HealthPath() string {
return "/health/magicform"
}
// HealthHandler handles the health check HTTP request.
func (c *MagicFormChannel) HealthHandler(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]string{
"status": "ok",
"channel": "magicform",
})
}
// maxWebhookBodySize is the maximum allowed size for inbound webhook payloads (1 MB).
const maxWebhookBodySize = 1 << 20
// ServeHTTP handles inbound webhook requests from MagicForm.
func (c *MagicFormChannel) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// Limit request body size to prevent memory exhaustion
r.Body = http.MaxBytesReader(w, r.Body, maxWebhookBodySize)
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Request body too large", http.StatusRequestEntityTooLarge)
return
}
defer r.Body.Close()
// Verify Bearer token
if !c.verifyToken(r) {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
var payload WebhookPayload
if err := json.Unmarshal(body, &payload); err != nil {
http.Error(w, "Invalid JSON", http.StatusBadRequest)
return
}
// Validate required fields
if payload.StackID == "" || payload.ConversationID == "" || payload.Message == "" {
http.Error(w, "Missing required fields: stackId, conversationId, message", http.StatusBadRequest)
return
}
// Validate workspace path: must be relative and resolve under workspace_root
if payload.Workspace != "" {
resolved, err := c.resolveWorkspace(payload.Workspace)
if err != nil {
http.Error(w, fmt.Sprintf("Invalid workspace: %v", err), http.StatusBadRequest)
return
}
payload.Workspace = resolved
}
// Validate configDir path against workspace_root
if payload.ConfigDir != "" {
resolved, err := c.resolveWorkspace(payload.ConfigDir)
if err != nil {
http.Error(w, fmt.Sprintf("Invalid configDir: %v", err), http.StatusBadRequest)
return
}
payload.ConfigDir = resolved
}
// Return 200 immediately, process asynchronously
w.WriteHeader(http.StatusOK)
go c.processWebhook(c.ctx, payload)
}
// resolveWorkspace validates and resolves the workspace path.
// If workspace_root is configured, the workspace must be a relative path that
// resolves under the root. If workspace_root is not configured, workspace is
// rejected (no arbitrary path writes allowed).
func (c *MagicFormChannel) resolveWorkspace(workspace string) (string, error) {
root := c.config.WorkspaceRoot
if root == "" {
return "", fmt.Errorf("workspace_root not configured; workspace overrides are not allowed")
}
absRoot, err := filepath.Abs(root)
if err != nil {
return "", fmt.Errorf("invalid workspace_root: %w", err)
}
// Join the root with the provided workspace (which may be relative)
resolved := filepath.Join(absRoot, workspace)
resolved = filepath.Clean(resolved)
// Ensure the resolved path is under the root (prevents ../../../etc traversal)
if !strings.HasPrefix(resolved, absRoot+string(filepath.Separator)) && resolved != absRoot {
return "", fmt.Errorf("workspace path escapes workspace_root")
}
return resolved, nil
}
// verifyToken checks the Authorization Bearer token using constant-time comparison.
func (c *MagicFormChannel) verifyToken(r *http.Request) bool {
if c.config.Token == "" {
return true // No token configured = allow all (dev mode)
}
auth := r.Header.Get("Authorization")
if !strings.HasPrefix(auth, "Bearer ") {
return false
}
token := strings.TrimPrefix(auth, "Bearer ")
return subtle.ConstantTimeCompare([]byte(token), []byte(c.config.Token)) == 1
}
// processWebhook handles an inbound webhook payload asynchronously.
func (c *MagicFormChannel) processWebhook(ctx context.Context, p WebhookPayload) {
// Check for shutdown before doing any work
if ctx.Err() != nil {
logger.WarnCF("magicform", "Skipping webhook processing: channel shutting down",
map[string]any{"stack_id": p.StackID, "conversation_id": p.ConversationID})
return
}
chatID := "magicform:" + p.ConversationID
senderID := p.UserID
if senderID == "" {
senderID = "anonymous"
}
// Store request context for Send() to look up later
c.requests.Store(chatID, &requestContext{
stackID: p.StackID,
conversationID: p.ConversationID,
userID: p.UserID,
callbackURL: p.CallbackURL,
createdAt: time.Now(),
})
peer := bus.Peer{Kind: "direct", ID: p.ConversationID}
sender := bus.SenderInfo{
Platform: "magicform",
PlatformID: senderID,
CanonicalID: "magicform:" + senderID,
}
// Session key: per-stack per-conversation isolation
sessionKey := fmt.Sprintf("agent:main:magicform:%s:%s", p.StackID, p.ConversationID)
metadata := map[string]string{
"platform": "magicform",
"stack_id": p.StackID,
"conversation_id": p.ConversationID,
}
if p.CallbackURL != "" {
metadata["callback_url"] = p.CallbackURL
}
// Workspace override — agent loop will pick this up
if p.Workspace != "" {
metadata["workspace_override"] = p.Workspace
}
// Config directory — agent loop reads config.json and copies bootstrap files
if p.ConfigDir != "" {
metadata["config_dir"] = p.ConfigDir
}
// Tool/skill filtering — passed via metadata, picked up by agent loop
if len(p.AllowedTools) > 0 {
metadata["allowed_tools"] = strings.Join(trimSlice(p.AllowedTools), ",")
}
if len(p.AllowedSkills) > 0 {
metadata["allowed_skills"] = strings.Join(trimSlice(p.AllowedSkills), ",")
}
messageID := fmt.Sprintf("mf-%s-%d", p.ConversationID, time.Now().UnixMilli())
// Build InboundMessage directly (not via HandleMessage) to set SessionKey.
// MagicForm is API-to-API, so typing/reaction/placeholder don't apply.
msg := bus.InboundMessage{
Channel: "magicform",
SenderID: sender.CanonicalID,
Sender: sender,
ChatID: chatID,
Content: p.Message,
Peer: peer,
MessageID: messageID,
SessionKey: sessionKey,
Metadata: metadata,
}
if err := c.Bus().PublishInbound(ctx, msg); err != nil {
logger.ErrorCF("magicform", "Failed to publish inbound message",
map[string]any{
"chat_id": chatID,
"stack_id": p.StackID,
"conversation_id": p.ConversationID,
"error": err.Error(),
})
}
}
// trimSlice trims whitespace from each element in the slice.
func trimSlice(s []string) []string {
out := make([]string, len(s))
for i, v := range s {
out[i] = strings.TrimSpace(v)
}
return out
}
// Send delivers the agent response back to MagicForm via HTTP callback.
func (c *MagicFormChannel) Send(ctx context.Context, msg bus.OutboundMessage) error {
if !c.IsRunning() {
return channels.ErrNotRunning
}
// Look up request context
val, ok := c.requests.LoadAndDelete(msg.ChatID)
if !ok {
return fmt.Errorf("%w: no request context for chatID %s", channels.ErrSendFailed, msg.ChatID)
}
reqCtx := val.(*requestContext)
// Resolve callback URL
callbackURL := reqCtx.callbackURL
if callbackURL == "" {
callbackURL = c.config.BackendURL + "/claw-agent/callback"
}
if callbackURL == "" {
return fmt.Errorf("%w: no callback URL available", channels.ErrSendFailed)
}
// Build callback payload
payload := CallbackPayload{
StackID: reqCtx.stackID,
ConversationID: reqCtx.conversationID,
Response: msg.Content,
Type: "final",
}
body, err := json.Marshal(payload)
if err != nil {
return fmt.Errorf("%w: marshal callback payload: %v", channels.ErrSendFailed, err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, callbackURL, bytes.NewReader(body))
if err != nil {
return fmt.Errorf("%w: create callback request: %v", channels.ErrSendFailed, err)
}
req.Header.Set("Content-Type", "application/json")
if c.config.Token != "" {
req.Header.Set("Authorization", "Bearer "+c.config.Token)
}
resp, err := c.httpClient.Do(req)
if err != nil {
return channels.ClassifyNetError(err)
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
respBody, _ := io.ReadAll(resp.Body)
return channels.ClassifySendError(resp.StatusCode, fmt.Errorf("callback error: %s", respBody))
}
logger.InfoCF("magicform", "Callback sent",
map[string]any{
"conversation_id": reqCtx.conversationID,
"status": resp.StatusCode,
})
return nil
}
// cleanupLoop periodically removes stale request contexts.
func (c *MagicFormChannel) cleanupLoop() {
ticker := time.NewTicker(60 * time.Second)
defer ticker.Stop()
for {
select {
case <-c.ctx.Done():
return
case <-ticker.C:
c.requests.Range(func(key, value any) bool {
rc := value.(*requestContext)
if time.Since(rc.createdAt) > 10*time.Minute {
c.requests.Delete(key)
logger.DebugCF("magicform", "Cleaned up stale request context",
map[string]any{"chat_id": key})
}
return true
})
}
}
}