Files
2f0cc97392 feat(paste): devvm paste and devvm paste rm (#38)
feat(paste): devvm paste, an image, PDF or copied file from the clipboard into the VM (#38)

devvm paste reads the host's clipboard and streams what's on it into the
running VM's paste folder over the backend's Exec, with no SSH agent and no
new channel, then copies the file's path in the VM to the host clipboard for
the agent's prompt. devvm paste rm empties the folder after a question, and
a launch that starts a stopped VM offers to.

What it reads:
- an image (PNG, JPEG, GIF, WebP; TIFF is converted to PNG), checked by its
  bytes; browsers hand over a PNG, so an animated GIF arrives as one frame
- PDF data, sent as it is, and preferred over a file reference beside it
  (Preview's page copies)
- one copied file, after a question that shows its path on this machine;
  --send-file answers it only for a file inside the project folder reached
  without links, and never for a --clone VM
- never text; several files are refused

Security:
- macOS reads file URLs only through NSPasteboard (FileURLsOnly): AppleScript's
  «class furl» coercion turned any text, such as a path a web page or the
  VM's OSC 52 put there, into a file to send
- copied files are opened with openat and O_NOFOLLOW|O_NOCTTY|O_NONBLOCK,
  following a root-owned link only in a folder only root can change, and read
  through internal/safefile (Checked), capped at 64 MiB
- osascript and pbcopy by absolute path, Linux's tools only from system
  folders, never PATH
- time limits on every clipboard tool and on paste.sh in the VM; bounded
  reads of everything the VM prints
- the path copied back is built on the host, keeping only the file's
  extension: nothing the VM says reaches the clipboard

Also: the paste-dir setting (default /tmp/devvm-paste, made at each
launch), --json with sent, the agents' guide line, messages with doc links,
stubs, CLI tests, an e2e check in the project section, and docs on
coding-agents, settings, design, security and attack-vectors.

Co-authored-by: dustypolk <[email protected]>
Co-authored-by: w3cj <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-11 22:29:46 -07:00

113 lines
4.8 KiB
Go

package cli
import (
"os"
"path/filepath"
"regexp"
"slices"
"strings"
"testing"
"github.com/spf13/cobra"
)
var (
// commandName is a command's name: lower-case words joined by -.
commandName = regexp.MustCompile(`^[a-z]+(-[a-z]+)*$`)
// useArg is an argument as a Use shows it: a placeholder in capitals
// or a word to type, alternatives joined by |, [optional], ...
// repeated.
useArg = regexp.MustCompile(`^\[?(?:[a-z][a-z-]*|[A-Z][A-Z_]*(?:=[A-Z][A-Z_]*)?)(?:\|(?:[a-z][a-z-]*|[A-Z][A-Z_]*))*(?:\.\.\.)?\]?$`)
)
// The commands users see follow the tree's naming rules: lower-case
// names, no aliases; a noun shows or lists bare, so no command is named
// for showing or listing but devvm ls, the one list of VMs; rm deletes;
// an unX undoes its sibling X; and a Use's arguments are placeholders or
// words, apart, never joined with + or a comma.
func TestCommandNames(t *testing.T) {
t.Parallel()
// names for what a bare noun or rm does
banned := []string{"ls", "list", "show", "get", "delete", "remove"}
u, _, _ := testUI()
root := NewRoot(u)
eachCommand(root, func(c *cobra.Command) {
if c.Hidden || c == root {
return
}
name, path := c.Name(), c.CommandPath()
if !commandName.MatchString(name) || len(c.Aliases) > 0 {
t.Errorf("%s: want a lower-case name without aliases, %q", path, c.Aliases)
}
if path != "devvm ls" && slices.Contains(banned, name) {
t.Errorf("%s: a noun shows or lists bare, and rm deletes", path)
}
if undone, ok := strings.CutPrefix(name, "un"); ok {
if sib, _, err := c.Parent().Find([]string{undone}); err != nil || sib.Name() != undone {
t.Errorf("%s undoes %s, which isn't beside it", path, undone)
}
}
for word := range strings.FieldsSeq(argsOf(c)) {
if !useArg.MatchString(word) {
t.Errorf("%s: Use's argument %q, want a PLACEHOLDER or word, [optional], ... repeated", path, word)
}
}
})
}
// devvm WORD, devvm WORD --help and devvm help WORD, for a word that isn't
// a command, fail alike: a usage error naming devvm --help, and nothing
// runs. A toolchain's, an agent's or a layer's name, the user's layers
// included, or several joined with + or a comma, points to the flags
// that choose them; any other word gets the closest command, when one is
// close. devvm takes no word, and a layer may have a command's name.
func TestUnknownWord(t *testing.T) {
cfg := t.TempDir()
t.Setenv("XDG_CONFIG_HOME", cfg)
t.Setenv("XDG_DATA_HOME", t.TempDir())
if err := os.MkdirAll(filepath.Join(cfg, "devvm", "layers"), 0o700); err != nil {
t.Fatal(err)
}
for _, l := range []string{"pg", "join"} {
if err := os.WriteFile(filepath.Join(cfg, "devvm", "layers", l+".sh"), nil, 0o600); err != nil {
t.Fatal(err)
}
}
for _, tc := range []struct {
args []string
want string // the first line
}{
{[]string{"stoop"}, "no_command_like command=stop word=stoop"},
{[]string{"stoop", "--help"}, "no_command_like command=stop word=stoop"},
{[]string{"help", "stoop"}, "no_command_like command=stop word=stoop"},
{[]string{"imagse"}, "no_command_like command=images word=imagse"},
{[]string{"xyzzy"}, "no_command word=xyzzy"},
{[]string{"node"}, "no_command_part flags=--tools node word=node"},
{[]string{"node", "--help"}, "no_command_part flags=--tools node word=node"},
{[]string{"help", "node"}, "no_command_part flags=--tools node word=node"},
{[]string{"claude"}, "no_command_part flags=--agents claude word=claude"},
{[]string{"docker"}, "no_command_part flags=--layers docker word=docker"},
{[]string{"pg"}, "no_command_part flags=--layers pg word=pg"},
{[]string{"node+claude"}, "no_command_part flags=--tools node --agents claude word=node+claude"},
{[]string{"docker,go+node+codex+node"}, "no_command_part flags=--tools go,node --agents codex --layers docker word=docker,go+node+codex+node"},
{[]string{"base"}, "no_command_like command=paste word=base"}, // an image's name gets no help of its own: base is near paste, as any word is near a command
{[]string{"node+nope"}, "no_command word=node+nope"},
{[]string{"nodee"}, "no_command_like command=code word=nodee"},
{[]string{"pgg"}, "no_command_like command=gc word=pgg"},
{[]string{"help", "tools", "nope"}, "no_subcommand command=devvm tools commands=add, rm, update given=nope"},
} {
s := stubbed(nil)
code := s.run(t, tc.args...)
got, _, _ := strings.Cut(s.errOut.String(), "\n")
if code != usageCode || len(s.ran) > 0 || got != "devvm: "+tc.want {
t.Errorf("devvm %q: exit %d, ran %q, stderr %q; want %d and %q", tc.args, code, s.ran, s.errOut, usageCode, tc.want)
}
}
// A layer named like a command is the command's word, and the layer's
// name for --layers.
s := stubbed(nil)
if code := s.run(t, "--layers", "join"); code != 0 || !slices.Equal(s.ran, []string{"devvm"}) {
t.Errorf("devvm --layers join: exit %d, ran %q, stderr %q", code, s.ran, s.errOut)
}
}