Commit 5f72092
Antonio Mika
·
2026-10-22 10:46:32 -0400 EDT
parent d642d4b
fix(pico): print help for unknown commands and sessions without a pty
1 files changed,
+24,
-3
+24,
-3
| ... | ... | @@ -6,6 +6,7 @@ import ( | |
| 6 | 6 | "encoding/json" | |
| 7 | 7 | "fmt" | |
| 8 | 8 | "log/slog" | |
| 9 | + | "path/filepath" | |
| 9 | 10 | "strings" | |
| 10 | 11 | "time" | |
| 11 | 12 |
| ... | ... | @@ -194,6 +195,12 @@ func Middleware(handler *CliHandler) pssh.SSHServerMiddleware { | |
| 194 | 195 | return func(sesh *pssh.SSHServerConnSession) error { | |
| 195 | 196 | args := sesh.Command() | |
| 196 | 197 | if len(args) == 0 { | |
| 198 | + | // Without a pty there is no TUI, and the upload middleware would wait on stdin. | |
| 199 | + | if _, _, ok := sesh.Pty(); !ok { | |
| 200 | + | opts := Cmd{Session: sesh} | |
| 201 | + | opts.help() | |
| 202 | + | return nil | |
| 203 | + | } | |
| 197 | 204 | return next(sesh) | |
| 198 | 205 | } | |
| 199 | 206 |
| ... | ... | @@ -284,8 +291,6 @@ func Middleware(handler *CliHandler) pssh.SSHServerMiddleware { | |
| 284 | 291 | sesh.Fatal(err) | |
| 285 | 292 | } | |
| 286 | 293 | return nil | |
| 287 | - | default: | |
| 288 | - | return next(sesh) | |
| 289 | 294 | } | |
| 290 | 295 | } | |
| 291 | 296 |
| ... | ... | @@ -309,7 +314,23 @@ func Middleware(handler *CliHandler) pssh.SSHServerMiddleware { | |
| 309 | 314 | return nil | |
| 310 | 315 | } | |
| 311 | 316 | ||
| 312 | - | return next(sesh) | |
| 317 | + | if passthrough(cmd) { | |
| 318 | + | return next(sesh) | |
| 319 | + | } | |
| 320 | + | ||
| 321 | + | opts.help() | |
| 322 | + | sesh.Fatal(fmt.Errorf("unknown command: %s", cmd)) | |
| 323 | + | return nil | |
| 313 | 324 | } | |
| 314 | 325 | } | |
| 315 | 326 | } | |
| 327 | + | ||
| 328 | + | // passthrough reports whether a later middleware handles cmd. Anything else | |
| 329 | + | // would reach the upload middleware, which waits on stdin. | |
| 330 | + | func passthrough(cmd string) bool { | |
| 331 | + | switch cmd { | |
| 332 | + | case "scp", "rsync", "command": | |
| 333 | + | return true | |
| 334 | + | } | |
| 335 | + | return filepath.Base(cmd) == "authorized_keys" | |
| 336 | + | } |