| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "strings" |
| 9 | |
| 10 | "github.com/sylvinus/agent-vm/internal/gitguard" |
| 11 | "github.com/sylvinus/agent-vm/internal/mounts" |
| 12 | "github.com/sylvinus/agent-vm/internal/vm" |
| 13 | ) |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | func (s *start) promptsDisabledBy() (string, bool) { |
| 20 | switch { |
| 21 | case s.opts.UnsafeNoPrompts: |
| 22 | return "--unsafe-disable-security-prompts", true |
| 23 | case os.Getenv("AGENT_VM_UNSAFE_DISABLE_SECURITY_PROMPTS") == "1": |
| 24 | return "AGENT_VM_UNSAFE_DISABLE_SECURITY_PROMPTS=1", true |
| 25 | } |
| 26 | return "", false |
| 27 | } |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | func (s *start) confirmUnsafe() bool { |
| 33 | if by, ok := s.promptsDisabledBy(); ok { |
| 34 | fmt.Fprintf(s.io.Stderr, "Continuing: %s.\n", by) |
| 35 | return true |
| 36 | } |
| 37 | return s.ui.CanAsk() && s.ui.AskYN("Continue anyway?", false) |
| 38 | } |
| 39 | |
| 40 | func (s *start) warnf(format string, a ...any) { |
| 41 | fmt.Fprintf(s.io.Stderr, format+"\n", a...) |
| 42 | } |
| 43 | |
| 44 | |
| 45 | func (s *start) writableGitWarning() { |
| 46 | why := "AGENT_VM_UNSAFE_WRITABLE_GIT=1" |
| 47 | if s.opts.UnsafeWritableGit { |
| 48 | why = "--unsafe-writable-git" |
| 49 | } |
| 50 | fmt.Fprintf(s.io.Stderr, `!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 51 | !! WARNING: %s. The VM can write every .git. |
| 52 | !! |
| 53 | !! The agent can change .git/config and .git/hooks in the shared folders, and |
| 54 | !! git on this machine runs what they name: on your next commit, and whenever |
| 55 | !! your editor or shell prompt calls git. That is running commands on your |
| 56 | !! host, outside the VM. Without it, .git stays read-only. |
| 57 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 58 | `, why) |
| 59 | } |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | func (s *start) checks() bool { |
| 65 | s.protect, s.names, s.notes = false, nil, "" |
| 66 | if !s.opts.Scratch { |
| 67 | if writableGitOptOut(s.opts.UnsafeWritableGit) { |
| 68 | if !cageWritableGit() { |
| 69 | s.writableGitWarning() |
| 70 | } |
| 71 | } else { |
| 72 | s.protect = true |
| 73 | } |
| 74 | } |
| 75 | if s.protect { |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | scan := s.scan(s.ctx, s.dir) |
| 84 | var hooks []string |
| 85 | for _, h := range scan.Hooks { |
| 86 | rel, inRepo, _ := strings.Cut(h, "\t") |
| 87 | if rel == "" { |
| 88 | continue |
| 89 | } |
| 90 | name, ok := gitguard.HooksName(inRepo) |
| 91 | if s.opts.ReadOnly { |
| 92 | if ok { |
| 93 | hooks = append(hooks, name) |
| 94 | } |
| 95 | continue |
| 96 | } |
| 97 | if !ok { |
| 98 | switch { |
| 99 | case rel == ".": |
| 100 | s.warnf("Warning: git's core.hooksPath is the project directory itself, which the VM can write: git on this machine runs the hooks the agent puts there.") |
| 101 | case inRepo == ".": |
| 102 | s.warnf("Warning: git's core.hooksPath is '%s', which the VM can write, and no read-only name covers it (the top of a repository, or in a writable volume): git on this machine runs the hooks the agent puts there.", rel) |
| 103 | default: |
| 104 | s.warnf("Warning: git's core.hooksPath is '%s', which the VM can write, and its name cannot be made read-only: git on this machine runs the hooks the agent puts there.", rel) |
| 105 | } |
| 106 | if !s.vmUp && !s.confirmUnsafe() { |
| 107 | s.warnf("Aborted. Point core.hooksPath to a folder of the project (agent-vm keeps it read-only), or outside it.") |
| 108 | return false |
| 109 | } |
| 110 | continue |
| 111 | } |
| 112 | if _, off := s.promptsDisabledBy(); !strings.HasPrefix(name, ".") && !s.vmUp && !off && s.ui.CanAsk() && |
| 113 | !s.ui.AskYN(fmt.Sprintf("Git runs hooks from %s. Make every '%s' folder in the project read-only for the VM?", rel, name), true) { |
| 114 | s.warnf("Warning: '%s' stays writable, and git on this machine runs the hooks the agent puts in %s.", name, rel) |
| 115 | if !s.confirmUnsafe() { |
| 116 | s.warnf("Aborted. Answer yes to protect it, or point core.hooksPath outside the project.") |
| 117 | return false |
| 118 | } |
| 119 | continue |
| 120 | } |
| 121 | hooks = append(hooks, name) |
| 122 | s.notes += fmt.Sprintf("Note: git runs hooks from %s (core.hooksPath): every '%s' in the project is read-only for the VM too.\n", rel, name) |
| 123 | } |
| 124 | s.names = gitguard.NamesWith(hooks) |
| 125 | |
| 126 | |
| 127 | if risks := scan.Risks(s.names); !s.opts.ReadOnly && len(risks) > 0 { |
| 128 | s.warnf("Warning: git on this machine uses these, and the VM can write them:") |
| 129 | for _, r := range risks { |
| 130 | s.warnf(" %s", r) |
| 131 | } |
| 132 | if !s.vmUp && !s.confirmUnsafe() { |
| 133 | s.warnf("Aborted. Move them out of the shared folders, or have them name commands outside them.") |
| 134 | return false |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | |
| 140 | if !s.opts.ReadOnly && !s.opts.Scratch { |
| 141 | return s.checkBareRepo() |
| 142 | } |
| 143 | return true |
| 144 | } |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | func (s *start) checkBareRepo() bool { |
| 151 | st := gitguard.BareState(s.ctx) |
| 152 | if st == gitguard.BareOK || st == gitguard.BareNoGit { |
| 153 | return true |
| 154 | } |
| 155 | if st == gitguard.BareUnavailable { |
| 156 | s.warnf("Aborted. Host Git could not be checked. Check the Git installation, then retry.") |
| 157 | return false |
| 158 | } |
| 159 | if s.vmUp { |
| 160 | s.warnf("Warning: git on this machine uses repositories a VM creates under another name than .git ('agent-vm doctor' says more).") |
| 161 | return true |
| 162 | } |
| 163 | s.ui.Box("Git: repositories not named .git", gitguard.BareHint) |
| 164 | if st == gitguard.BareOld { |
| 165 | out, _ := exec.CommandContext(s.ctx, "git", "--version").Output() |
| 166 | s.warnf("Warning: %s is older than 2.38 and ignores that setting.", strings.TrimSpace(string(out))) |
| 167 | if s.confirmUnsafe() { |
| 168 | return true |
| 169 | } |
| 170 | s.warnf("Aborted. Upgrade git, then run the command above.") |
| 171 | return false |
| 172 | } |
| 173 | if _, off := s.promptsDisabledBy(); !off && s.ui.CanAsk() && s.ui.AskYN("Run it now? It changes your global git config.", true) { |
| 174 | err := exec.CommandContext(s.ctx, "git", "config", "--global", "safe.bareRepository", "explicit").Run() |
| 175 | if err == nil && gitguard.BareState(s.ctx) == gitguard.BareOK { |
| 176 | fmt.Fprintln(s.io.Stdout, "Git on this machine now ignores repositories not named .git unless you name them.") |
| 177 | return true |
| 178 | } |
| 179 | s.warnf("Warning: the setting did not take.") |
| 180 | } |
| 181 | s.warnf("Warning: not set. Until it is, git on this machine can run what a VM writes.") |
| 182 | if s.confirmUnsafe() { |
| 183 | return true |
| 184 | } |
| 185 | s.warnf("Aborted. Run the command above, then run agent-vm again.") |
| 186 | return false |
| 187 | } |
| 188 | |
| 189 | |
| 190 | type start struct { |
| 191 | *app |
| 192 | ctx context.Context |
| 193 | opts VMOpts |
| 194 | name string |
| 195 | dir string |
| 196 | |
| 197 | created bool |
| 198 | vmUp bool |
| 199 | protect bool |
| 200 | names []string |
| 201 | notes string |
| 202 | |
| 203 | built []vm.Mount |
| 204 | builtFiles []mounts.FileMount |
| 205 | builtKey string |
| 206 | } |