|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/BackendStack21/odek/internal/config" |
| 11 | + "github.com/BackendStack21/odek/internal/danger" |
| 12 | + "github.com/BackendStack21/odek/internal/mcpclient" |
| 13 | +) |
| 14 | + |
| 15 | +const hostileApprovalText = "echo ok\x1b[2K\r\x1b]0;safe\x07 \u202egnirts\u202c \u2066x\u2069 \u200bz" |
| 16 | + |
| 17 | +func assertNoRawControl(t *testing.T, label, s string) { |
| 18 | + t.Helper() |
| 19 | + for _, r := range s { |
| 20 | + if r == '\n' || r == '\t' { |
| 21 | + continue |
| 22 | + } |
| 23 | + if r < 0x20 || r == 0x7f || (r >= 0x80 && r <= 0x9f) || r == 0x200b || |
| 24 | + (r >= 0x202a && r <= 0x202e) || (r >= 0x2066 && r <= 0x2069) { |
| 25 | + t.Errorf("%s holds raw control or bidi character U+%04X: %q", label, r, s) |
| 26 | + return |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +// The WebSocket approval frame carries sanitized command and description text: |
| 32 | +// the UI renders these fields as text, so control and bidi characters must |
| 33 | +// already be visible escapes. |
| 34 | +func TestWSApprover_FramesCarrySanitizedText(t *testing.T) { |
| 35 | + frames := make(chan approvalRequest, 1) |
| 36 | + a := newWSApprover(func(v any) error { |
| 37 | + if req, ok := v.(approvalRequest); ok { |
| 38 | + frames <- req |
| 39 | + } |
| 40 | + return nil |
| 41 | + }) |
| 42 | + done := make(chan error, 1) |
| 43 | + go func() { |
| 44 | + done <- a.PromptCommand(danger.NetworkEgress, hostileApprovalText, "why "+hostileApprovalText) |
| 45 | + }() |
| 46 | + select { |
| 47 | + case req := <-frames: |
| 48 | + assertNoRawControl(t, "command", req.Command) |
| 49 | + assertNoRawControl(t, "description", req.Description) |
| 50 | + if !strings.Contains(req.Command, `\x1b`) || !strings.Contains(req.Command, `\u202e`) { |
| 51 | + t.Errorf("command escapes not visible: %q", req.Command) |
| 52 | + } |
| 53 | + a.HandleResponse(req.ID, "deny") |
| 54 | + case <-time.After(5 * time.Second): |
| 55 | + t.Fatal("no approval frame") |
| 56 | + } |
| 57 | + <-done |
| 58 | +} |
| 59 | + |
| 60 | +func TestWSApprover_OperationFramesAreSanitized(t *testing.T) { |
| 61 | + frames := make(chan approvalRequest, 1) |
| 62 | + a := newWSApprover(func(v any) error { |
| 63 | + if req, ok := v.(approvalRequest); ok { |
| 64 | + frames <- req |
| 65 | + } |
| 66 | + return nil |
| 67 | + }) |
| 68 | + done := make(chan error, 1) |
| 69 | + go func() { |
| 70 | + done <- a.PromptOperation(danger.ToolOperation{Name: "write_file\x1b[2K", Resource: "/tmp/\u202eexe.txt", Risk: danger.LocalWrite}) |
| 71 | + }() |
| 72 | + select { |
| 73 | + case req := <-frames: |
| 74 | + assertNoRawControl(t, "command", req.Command) |
| 75 | + assertNoRawControl(t, "description", req.Description) |
| 76 | + a.HandleResponse(req.ID, "deny") |
| 77 | + case <-time.After(5 * time.Second): |
| 78 | + t.Fatal("no approval frame") |
| 79 | + } |
| 80 | + <-done |
| 81 | +} |
| 82 | + |
| 83 | +// The project MCP approval prompt prints repo-controlled command, args and |
| 84 | +// env values; none of them may carry raw control or bidi characters. |
| 85 | +func TestMCPApprovalPrompt_SanitizesProjectControlledText(t *testing.T) { |
| 86 | + setupTestHome(t) |
| 87 | + t.Setenv("ODEK_APPROVE_MCP", "") |
| 88 | + nonce := fmt.Sprintf("srv-%d", time.Now().UnixNano()) |
| 89 | + resolved := config.ResolvedConfig{ |
| 90 | + MCPServers: map[string]mcpclient.ServerConfig{ |
| 91 | + "project": { |
| 92 | + Command: "node\x1b[2K" + nonce, |
| 93 | + Args: []string{"\u202egnirts", "a\rb"}, |
| 94 | + Env: map[string]string{"K\x07": "v\x1b]0;x\x07"}, |
| 95 | + }, |
| 96 | + }, |
| 97 | + ProjectMCPServerNames: []string{"project"}, |
| 98 | + } |
| 99 | + var out bytes.Buffer |
| 100 | + _ = approveMCPServersWithTTY(resolved, strings.NewReader("\n"), &out, true) |
| 101 | + assertNoRawControl(t, "MCP approval prompt", out.String()) |
| 102 | + if !strings.Contains(out.String(), `\x1b[2K`) { |
| 103 | + t.Errorf("escape not visible in prompt: %q", out.String()) |
| 104 | + } |
| 105 | +} |
0 commit comments