Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## Unreleased

- Confirm policy: agent text may no longer hold any `!`. A `!` before a
blank, `=` or `(` was allowed, and the editing keys agents keep turned
it into a history reference afterwards (`echo ! `, Backspace, `-2`):
with Space bound to magic-space the shell drew the command before last,
one typed in private mode included, on the screen without an Enter.

- Build: go.mod names Go 1.27.4 (CI builds with that line); the CI
actions are pinned by commit SHA, and Dependabot proposes Go module
and action updates weekly.
Expand Down
13 changes: 8 additions & 5 deletions docs/broker.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,14 @@ on its own:
them C-p, C-r, M-. and M-< recall history, and C-x C-e, M-C-e
(shell-expand-line) and C-o run or submit the line.
- In text: tabs, control characters other than Enter, Backspace and C-c,
escape sequences, history expansion (`!` followed by anything but a
blank, `=` or `(`, and `^`), and, when the shell's locale is not UTF-8,
bytes above 0x7f. Space bound to magic-space (oh-my-zsh does that)
expands `!!` or `^old^new` on the screen without an Enter, and after an
Enter the expanded line, not the one you approved, would run. In a C or
escape sequences, the history expansion characters `!` and `^`, and,
when the shell's locale is not UTF-8, bytes above 0x7f. Space bound to
magic-space (oh-my-zsh does that) expands `!!` or `^old^new` on the
screen without an Enter, and after an Enter the expanded line, not the
one you approved, would run. Every `!` is refused, also where a shell
would not expand it (`!=`, `! cmd`): with Left, Backspace and more text
an agent turns `echo ! ` into `echo !-2`, which the next Space expands
(an agent writes `-ne`, `test -z` or a script file instead). In a C or
POSIX locale readline reads bytes above 0x7f as Meta keys, and M-C-e
runs the command substitutions of the line.

Expand Down
2 changes: 1 addition & 1 deletion docs/protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ Agent `type` and `keys` are refused, with these codes, in this order:
| `keyboard_held` | the person holds the keyboard |
| `person_typing` | the person typed in the last 1.5 s; `retry_after_ms` set |
| `confirmation_pending` | an agent line waits for the person's answer |
| `policy_denied` | under the confirm policy: keys other than Enter, Backspace, Delete, Space, Left, Right, Home, End and C-c; in `text`, tabs, control characters other than Enter, Backspace and C-c, escape sequences, history expansion (`!` not followed by a blank, `=` or `(`, and `^`) and, when the shell's locale is not UTF-8, bytes above 0x7f (broker.md, Confirm policy) |
| `policy_denied` | under the confirm policy: keys other than Enter, Backspace, Delete, Space, Left, Right, Home, End and C-c; in `text`, tabs, control characters other than Enter, Backspace and C-c, escape sequences, the history expansion characters `!` and `^` and, when the shell's locale is not UTF-8, bytes above 0x7f (broker.md, Confirm policy) |

Under the confirm policy the text before an Enter is typed, the Enter is
held and the person sees "agent wants to run: LINE" (or, when the line
Expand Down
2 changes: 1 addition & 1 deletion internal/broker/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -1360,7 +1360,7 @@ func (b *Broker) agentType(c *conn, text string, enter bool, keys []string) (pro
}
if confirm && !confirmTextOK(text, b.shellUTF8) {
return deny(proto.Errorf(proto.ErrPolicyDenied, "under the confirm policy agent text may not hold tabs (completion runs programs), "+
"control characters, escape sequences, history expansion (! followed by anything but a blank, = or (, and ^) "+
"control characters, escape sequences, history expansion characters (! and ^; write != as -ne or a script file) "+
"or, outside a UTF-8 locale, bytes above 0x7f"))
}
seq = []byte(text)
Expand Down
30 changes: 12 additions & 18 deletions internal/broker/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ var fKeys = map[string]string{
// line or runs anything.
// - Space inserts a blank. Where it is bound to magic-space (oh-my-zsh
// does that) it expands history references already on the line; the
// characters that start one are refused in text (confirmTextOK).
// characters that start one are refused in text (confirmTextOK), so
// no agent edit can leave one there.
// - C-c interrupts; it never runs anything.
//
// Refused, although they do not submit a line:
Expand All @@ -128,13 +129,15 @@ var confirmKeys = map[string]bool{
// control character and escape sequence (C-o, C-x C-e, Meta
// sequences), which could submit or edit a line in ways the person
// does not see coming;
// - history expansion: a '!' that starts a history reference in bash and
// zsh (any '!' not followed by a blank, '=' or '('; a '!' at the end
// of the text too, as the next text may complete it) and '^' (quick
// substitution, ^old^new): with Space bound to magic-space they draw
// earlier commands, private ones included, on the screen without an
// Enter, and after an Enter the line that runs is not the line the
// person approved;
// - history expansion: every '!' (bash and zsh history references) and
// '^' (quick substitution, ^old^new): with Space bound to magic-space
// they draw earlier commands, private ones included, on the screen
// without an Enter, and after an Enter the line that runs is not the
// line the person approved. A '!' the shells would not expand where
// it stands (before a blank, '=' or '(') is refused too: the editing
// keys an agent keeps turn it into a reference afterwards ("echo ! ",
// Backspace, "-2", Space expands the command before last), and the
// broker cannot tell what the line holds once it was edited;
// - bytes with the eighth bit set unless the shell's locale is UTF-8
// (utf8): with readline's convert-meta, on by default in 8-bit
// locales such as C or POSIX, each such byte is a Meta key, and
Expand All @@ -146,17 +149,8 @@ func confirmTextOK(s string, utf8 bool) bool {
case c == '\r' || c == '\n' || c == 0x7f || c == 0x08 || c == 0x03:
case c < 0x20:
return false
case c == '^':
case c == '^' || c == '!':
return false
case c == '!':
if i+1 == len(s) {
return false
}
switch s[i+1] {
case ' ', '\t', '\r', '\n', '=', '(':
default:
return false
}
case c >= 0x80 && !utf8:
return false
}
Expand Down
7 changes: 6 additions & 1 deletion internal/broker/unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestKeys(t *testing.T) {
}

func TestConfirmText(t *testing.T) {
for _, s := range []string{"ls -la\r", "x\x7f", "git commit -m 'café'", "[ $a != b ]", "if ! true; then :; fi", "echo $((1 + 2))"} {
for _, s := range []string{"ls -la\r", "x\x7f", "git commit -m 'café'", "[ $a = b ] || echo differ", "echo $((1 + 2))"} {
if !confirmTextOK(s, true) {
t.Errorf("%q refused under confirm", s)
}
Expand All @@ -62,6 +62,11 @@ func TestConfirmText(t *testing.T) {
"./configure --\t", "make \t",
// History expansion draws earlier commands, private ones too (COT-3).
"echo !!", "!-1 ", "echo !?TOKEN?", "!$", "echo hi!", "^e^e ", "x^y",
// Every '!', even one a shell would not expand where it stands: the
// editing keys the agent keeps (Left, Backspace, more text) can
// turn "! " or "!=" into a history reference that magic-space then
// expands without an Enter.
"[ $a != b ]", "if ! true; then :; fi", "echo ! ", "echo !=", "!(x)", "echo '!'",
} {
if confirmTextOK(s, true) {
t.Errorf("%q allowed under confirm", s)
Expand Down
2 changes: 1 addition & 1 deletion internal/mcp/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var hints = map[string]string{
proto.ErrPersonTyping: "The person is typing right now. Wait until retry_after_ms passes and try again; never type over them.",
proto.ErrConfirmationPending: "Your previous line waits for the person's approval (confirm policy). " +
"Call term_wait with for=confirmation, then read the screen.",
proto.ErrPolicyDenied: "This session uses the confirm policy: send plain text without tabs or history expansion (! and ^), " +
proto.ErrPolicyDenied: "This session uses the confirm policy: send plain text without tabs, ! or ^ (they expand shell history; use -ne, test -z or a script file instead of !), " +
"and only the keys Enter, Backspace, Delete, Space, Left, Right, Home, End and C-c. Type whole commands; do not complete them with Tab.",
proto.ErrNotShared: "The person has not shared this session with agents. Ask them to run `cotty share SESSION`, " +
"or to start a shared session with `cotty new --ai`.",
Expand Down
208 changes: 208 additions & 0 deletions tests/integration/confirm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
//go:build vmtest

package integration

import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"

"github.com/openbasalt/cotty/internal/proto"
)

// confirmShells are the shells the confirm policy tests run in, with the
// line the person types to load the shell's completion and the one that
// binds Space to magic-space (as oh-my-zsh does for zsh).
var confirmShells = []struct {
name string
argv []string
completion string
magicSpace string
}{
{"bash", []string{"bash", "--norc", "--noprofile"},
"source /usr/share/bash-completion/bash_completion",
`bind '" ": magic-space'`},
{"zsh", []string{"zsh", "-f"},
"autoload -U compinit && compinit -u",
"bindkey ' ' magic-space"},
}

func needShell(t *testing.T, argv0 string) {
t.Helper()
if _, err := exec.LookPath(argv0); err != nil {
t.Skipf("%s not installed", argv0)
}
}

// wantPolicyDenied fails unless err is policy_denied.
func wantPolicyDenied(t *testing.T, what string, err error) {
t.Helper()
if proto.ErrorCode(err) != proto.ErrPolicyDenied {
t.Errorf("%s under --policy confirm: got %v, want policy_denied", what, err)
}
}

// TestConfirmRefusesRecallAndCompletion (review COT-1, COT-3): under
// --policy confirm the keys and text that complete, recall history or
// expand history references are refused, in bash and zsh; plain text with
// an approved Enter still runs. A project's ./configure and Makefile that
// run code when completed (bash-completion and zsh's compsys run them)
// stay untouched while the agent tries to complete them.
func TestConfirmRefusesRecallAndCompletion(t *testing.T) {
for _, sh := range confirmShells {
t.Run(sh.name, func(t *testing.T) {
needShell(t, sh.argv[0])
dir := t.TempDir()
configure := "#!/bin/sh\ntouch \"$(dirname \"$0\")/configure-ran\"\necho ' --prefix=DIR install prefix'\n"
if err := os.WriteFile(filepath.Join(dir, "configure"), []byte(configure), 0o755); err != nil {
t.Fatal(err)
}
mk := "X := $(shell touch " + filepath.Join(dir, "make-ran") + ")\nall:\n\t@true\nbuild:\n\t@true\n"
if err := os.WriteFile(filepath.Join(dir, "Makefile"), []byte(mk), 0o644); err != nil {
t.Fatal(err)
}
ran := func() []string {
var out []string
for _, f := range []string{"configure-ran", "make-ran"} {
if _, err := os.Stat(filepath.Join(dir, f)); err == nil {
out = append(out, f)
}
}
return out
}

p, a, _ := newSession(t, []string{"--policy", "confirm"}, sh.argv...)
p.typ("PS1='$ '; PROMPT='$ '; cd " + dir + "\r")
p.typ(sh.completion + "\r")
waitQuiet(t, a)

for _, k := range []string{"Tab", "BTab", "Up", "Down", "PgUp", "PgDn"} {
wantPolicyDenied(t, "key "+k, a.call("keys", map[string]any{"keys": []string{k}}, nil))
}
for _, txt := range []string{"echo a\tb", "echo !!", "echo !1", "echo !-1", "echo hi!", "^a^b", "ls !$", "echo !echo"} {
wantPolicyDenied(t, "text "+strings.ReplaceAll(txt, "\t", `\t`), a.call("type", map[string]any{"text": txt}, nil))
}

// The completion reproduction: the agent types the start of the
// command and asks for completion (key or tab in text).
for _, line := range []string{"./configure --", "make "} {
if err := a.call("type", map[string]any{"text": line}, nil); err != nil {
t.Fatalf("type %q: %v", line, err)
}
wantPolicyDenied(t, "Tab after "+line, a.call("keys", map[string]any{"keys": []string{"Tab"}}, nil))
wantPolicyDenied(t, "Tab Tab after "+line, a.call("keys", map[string]any{"keys": []string{"Tab", "Tab"}}, nil))
wantPolicyDenied(t, "tab in text after "+line, a.call("type", map[string]any{"text": "\t"}, nil))
if err := a.call("keys", map[string]any{"keys": []string{"C-c"}}, nil); err != nil {
t.Fatalf("C-c: %v", err)
}
time.Sleep(300 * time.Millisecond)
}
time.Sleep(time.Second)
if r := ran(); len(r) > 0 {
t.Errorf("project files ran while the agent tried to complete: %v", r)
}

// Plain text with an approved Enter still works.
waitQuiet(t, a)
var sent proto.Sent
if err := a.call("type", map[string]any{"text": "echo plain-$((40+2)) = x", "enter": true}, &sent); err != nil || !sent.HeldEnter {
t.Fatalf("plain text under confirm: %+v %v", sent, err)
}
p.waitScreen("agent wants to run", 3*time.Second)
from := p.mark()
p.typ("\x1dy")
p.waitOutput(from, "plain-42 = x", 5*time.Second)

// Control: the same files do run code when the person completes,
// so the refusals above are what kept them from running.
waitQuiet(t, a)
p.typeSlowly("./configure --\t\t")
time.Sleep(1500 * time.Millisecond)
p.typ("\x03")
p.typeSlowly("make \t\t")
time.Sleep(1500 * time.Millisecond)
p.typ("\x03")
t.Logf("%s: person completion ran %v (control: shows the payload is live)", sh.name, ran())
})
}
}

// TestConfirmNoHistoryExpansionByEditing: the text rule lets an agent
// type a '!' followed by a blank, '=' or '('. It must not be able to turn
// that into a history reference with the editing keys it may still send
// (Left, Backspace, more text) and have magic-space expand it, which
// draws a command the person typed in private mode on the screen without
// any Enter (review COT-3).
func TestConfirmNoHistoryExpansionByEditing(t *testing.T) {
for _, sh := range confirmShells {
t.Run(sh.name, func(t *testing.T) {
needShell(t, sh.argv[0])
p, a, _ := newSession(t, []string{"--policy", "confirm"}, sh.argv...)
p.typ("PS1='$ '; PROMPT='$ '\r")
p.typ(sh.magicSpace + "\r")
priv := marker("HX")
p.typ("\x1dp")
deadline := time.Now().Add(3 * time.Second)
for !a.state().Private && time.Now().Before(deadline) {
time.Sleep(30 * time.Millisecond)
}
p.typ("echo " + priv + "\r")
time.Sleep(300 * time.Millisecond)
p.typ("clear\r")
time.Sleep(300 * time.Millisecond)
p.typ("\x1dp")
for a.state().Private && time.Now().Before(deadline.Add(3*time.Second)) {
time.Sleep(30 * time.Millisecond)
}
waitQuiet(t, a)

// Each step alone is allowed or refused; together they must not
// leave "!-2" (the private command) on the line for Space.
steps := []struct {
op string
args map[string]any
}{
{"type", map[string]any{"text": "echo !="}},
{"keys", map[string]any{"keys": []string{"Left"}}},
{"type", map[string]any{"text": "-2"}},
{"keys", map[string]any{"keys": []string{"Space"}}},
}
for _, s := range steps {
err := a.call(s.op, s.args, nil)
t.Logf("%s %v: %v", s.op, s.args, err)
time.Sleep(200 * time.Millisecond)
}
time.Sleep(500 * time.Millisecond)
scr := a.screenText()
if strings.Contains(scr, priv) {
t.Errorf("history expansion by editing drew a private command on the agent's screen:\n%s", scr)
}
// Same with Backspace instead of Left: "echo ! " then delete the
// blank and type the event.
_ = a.call("keys", map[string]any{"keys": []string{"C-c"}}, nil)
time.Sleep(300 * time.Millisecond)
for _, s := range []struct {
op string
args map[string]any
}{
{"type", map[string]any{"text": "echo ! "}},
{"keys", map[string]any{"keys": []string{"Backspace"}}},
{"type", map[string]any{"text": "-2"}},
{"type", map[string]any{"text": " "}},
} {
err := a.call(s.op, s.args, nil)
t.Logf("%s %v: %v", s.op, s.args, err)
time.Sleep(200 * time.Millisecond)
}
time.Sleep(500 * time.Millisecond)
if scr := a.screenText(); strings.Contains(scr, priv) {
t.Errorf("history expansion after Backspace drew a private command on the agent's screen:\n%s", scr)
}
assertNoLeak(t, a, priv)
_ = a.call("keys", map[string]any{"keys": []string{"C-c"}}, nil)
})
}
}
Loading