vole
Site
Clone
Read only, no credentials:
https://rodent.software/vole.git
Write over SSH with a registered key:
git clone ssh://git@rodent.software/vole.git
Self-hosted forge that lives in your Git repo.
| 1 | // Package sshwire is the local protocol between `vole ssh-exec`, which sshd runs |
| 2 | // as a forced command, and `vole serve`. |
| 3 | // |
| 4 | // One request per connection: a hello naming the session token, the command the |
| 5 | // client asked for, and the connection it arrived on; one accept or refusal; |
| 6 | // then the session's streams and its exit status. Every message is a |
| 7 | // length-prefixed frame, so stdout and stderr stay separate by construction and |
| 8 | // a slow reader on either end pushes back on the other instead of buffering. |
| 9 | // |
| 10 | // The socket is local and private to the service, so this is a protocol, not a |
| 11 | // cryptographic boundary: the session token is what authenticates a request. |
| 12 | // Framing exists so one byte stream carries four kinds of message without any of |
| 13 | // them being parsed out of another. |
| 14 | package sshwire |
| 15 | |
| 16 | import ( |
| 17 | "encoding/binary" |
| 18 | "encoding/json" |
| 19 | "fmt" |
| 20 | "io" |
| 21 | ) |
| 22 | |
| 23 | // MaxFrame bounds one frame. Git moves a pack in chunks; this is the largest |
| 24 | // chunk either side will accept, and it keeps a corrupt or hostile length from |
| 25 | // becoming an allocation. |
| 26 | const MaxFrame = 64 << 10 |
| 27 | |
| 28 | // Kind is a frame's first byte. |
| 29 | type Kind byte |
| 30 | |
| 31 | const ( |
| 32 | // KindHello is the client's one request, as JSON. |
| 33 | KindHello Kind = 'H' |
| 34 | // KindAccept answers the hello, as JSON. |
| 35 | KindAccept Kind = 'A' |
| 36 | // KindStdin is a chunk of the client's standard input. |
| 37 | KindStdin Kind = 'i' |
| 38 | // KindStdinEOF says the client's standard input ended. |
| 39 | KindStdinEOF Kind = 'I' |
| 40 | // KindStdout is a chunk of the command's standard output. |
| 41 | KindStdout Kind = 'o' |
| 42 | // KindStderr is a chunk of the command's standard error. |
| 43 | KindStderr Kind = 'e' |
| 44 | // KindExit is the command's exit status, as JSON. It is the last frame. |
| 45 | KindExit Kind = 'x' |
| 46 | ) |
| 47 | |
| 48 | // Hello is what the forced command asks for. |
| 49 | type Hello struct { |
| 50 | // Session is the token `vole ssh-authorized-keys` minted for this connection. |
| 51 | Session string `json:"session"` |
| 52 | // Command is the client's SSH_ORIGINAL_COMMAND, verbatim. |
| 53 | Command string `json:"command"` |
| 54 | // Remote is the client's SSH_CONNECTION, which the service compares with the |
| 55 | // endpoint the token was minted for. |
| 56 | Remote string `json:"remote"` |
| 57 | } |
| 58 | |
| 59 | // Accept is the service's answer to a hello. A refusal carries the reason the |
| 60 | // client should print. |
| 61 | type Accept struct { |
| 62 | OK bool `json:"ok"` |
| 63 | Error string `json:"error,omitempty"` |
| 64 | } |
| 65 | |
| 66 | // Exit is a command's status. |
| 67 | type Exit struct { |
| 68 | Code int `json:"code"` |
| 69 | } |
| 70 | |
| 71 | // WriteFrame writes one frame: the kind, a big-endian length, then the payload. |
| 72 | func WriteFrame(w io.Writer, kind Kind, payload []byte) error { |
| 73 | if len(payload) > MaxFrame { |
| 74 | return fmt.Errorf("sshwire: %d-byte frame is larger than %d", len(payload), MaxFrame) |
| 75 | } |
| 76 | head := [5]byte{byte(kind)} |
| 77 | binary.BigEndian.PutUint32(head[1:], uint32(len(payload))) |
| 78 | if _, err := w.Write(head[:]); err != nil { |
| 79 | return err |
| 80 | } |
| 81 | if len(payload) == 0 { |
| 82 | return nil |
| 83 | } |
| 84 | _, err := w.Write(payload) |
| 85 | return err |
| 86 | } |
| 87 | |
| 88 | // ReadFrame reads one frame and its payload. A payload is reused by the caller |
| 89 | // immediately, so it is returned as a slice of a fresh buffer. |
| 90 | func ReadFrame(r io.Reader) (Kind, []byte, error) { |
| 91 | var head [5]byte |
| 92 | if _, err := io.ReadFull(r, head[:]); err != nil { |
| 93 | return 0, nil, err |
| 94 | } |
| 95 | kind := Kind(head[0]) |
| 96 | n := binary.BigEndian.Uint32(head[1:]) |
| 97 | if n > MaxFrame { |
| 98 | return 0, nil, fmt.Errorf("sshwire: %d-byte frame is larger than %d", n, MaxFrame) |
| 99 | } |
| 100 | if n == 0 { |
| 101 | return kind, nil, nil |
| 102 | } |
| 103 | payload := make([]byte, n) |
| 104 | if _, err := io.ReadFull(r, payload); err != nil { |
| 105 | return 0, nil, err |
| 106 | } |
| 107 | return kind, payload, nil |
| 108 | } |
| 109 | |
| 110 | // WriteJSON writes one framed JSON message. |
| 111 | func WriteJSON(w io.Writer, kind Kind, v any) error { |
| 112 | payload, err := json.Marshal(v) |
| 113 | if err != nil { |
| 114 | return err |
| 115 | } |
| 116 | return WriteFrame(w, kind, payload) |
| 117 | } |
| 118 |