| 1 | # SSH protocol proposal |
| 2 | |
| 3 | We'd like to implement a protocol for Git LFS that uses SSH protocol |
| 4 | exclusively, avoiding the need to use HTTPS altogether. This will make |
| 5 | deployment and use easier in a variety of situations where access to certain |
| 6 | ports is limited. |
| 7 | |
| 8 | This is merely a proposal, not a commitment to implement for either the client |
| 9 | or server side. Implementers who prefer to use HTTP can continue to do so. |
| 10 | |
| 11 | ## What not to do |
| 12 | |
| 13 | There are several possible approaches that could be adopted. SSH provides a |
| 14 | native capability for the SFTP protocol, which can be used to transfer files. |
| 15 | However, in order to implement this on the server side, each access (upload or |
| 16 | download) must have an access control check instead of one at the beginning of |
| 17 | the operation. This would be inefficient in some server-side implementations, |
| 18 | and nearly impossible to implement securely for implementations that use the |
| 19 | system OpenSSH for implementation. |
| 20 | |
| 21 | ## A more usable approach |
| 22 | |
| 23 | Git already has some places we can look for inspiration. Its SSH protocol is |
| 24 | based on the Git native protocol, which is based on the pkt-line scheme. |
| 25 | Recently, Git has learned about protocol version 2, which provides better |
| 26 | support for expressing and negotiating capabilities. |
| 27 | |
| 28 | Ideally, we would allow multiple operations to occur on a single connection for |
| 29 | efficiency's sake, especially on high-latency connections, where the cost of SSH |
| 30 | connection setup may be high due to multiple round trips. In addition, a |
| 31 | protocol which maps well onto HTTP may be beneficial for those server-side |
| 32 | implementations which would like to proxy connections to an HTTP-based backend. |
| 33 | |
| 34 | ## Preliminary design |
| 35 | |
| 36 | This design assumes a reference to Git's pkt-line and protocol v2 documentation. |
| 37 | pkt-line headers for this document may contain values up to 65519 decimal. |
| 38 | |
| 39 | To initiate a connection, Git LFS should run the following command: |
| 40 | |
| 41 | $ ssh [{user}@]{server} git-lfs-transfer {path} {operation} |
| 42 | |
| 43 | If authentication fails, or some other connection error occurs, errors will be |
| 44 | read from standard error and displayed to the user. The operation may be |
| 45 | `upload`, `download`. Other operations may be implemented in the future. |
| 46 | |
| 47 | Once the connection is established, the server should send a capability |
| 48 | advertisement: |
| 49 | |
| 50 | ``` |
| 51 | capability-advertisement = capability-list flush-pkt |
| 52 | capability-list = *capability |
| 53 | capability = PKT-LINE(key[=value] LF) |
| 54 | |
| 55 | key = 1*(ALPHA | DIGIT | "-_") |
| 56 | value = 1*(ALPHA | DIGIT | "-_.,?\/{}[]()<>!@#$%^&*+=:;") |
| 57 | ``` |
| 58 | |
| 59 | Unlike the Git protocol, but like IMAP, the protocol version is specified as a |
| 60 | capability. This document defines protocol version 1, which is specified as |
| 61 | `version=1`. If the server supports other protocol versions, it may enumerate |
| 62 | them here as well. |
| 63 | |
| 64 | If the server supports locking, the `locking` capability should be advertised, |
| 65 | and the client may then use the `lock`, `unlock`, and `list-lock` commands. |
| 66 | |
| 67 | No capabilities other than the base functionality specified here are enabled |
| 68 | without the client explicitly enabling them. Note that the `value` production |
| 69 | here, unlike in Git, does not include the space character, since it is used as a |
| 70 | delimiter in parts of the protocol. |
| 71 | |
| 72 | The client will then issue an appropriate version command: |
| 73 | |
| 74 | ``` |
| 75 | version-request = PKT-LINE("version " number LF) flush-pkt |
| 76 | number = 1*DIGIT |
| 77 | ``` |
| 78 | |
| 79 | The response from the server will look like the following: |
| 80 | |
| 81 | ``` |
| 82 | version-response = status-command |
| 83 | delim-pkt |
| 84 | error-message |
| 85 | flush-pkt |
| 86 | status-command = PKT-LINE("status " http-status-code LF) |
| 87 | http-status-code = 3DIGIT |
| 88 | error-message = *PKT-LINE(data LF) |
| 89 | ``` |
| 90 | |
| 91 | The `http-status-code` portion of the response is an HTTP status code, identical |
| 92 | to those used if the request is made over HTTP. |
| 93 | |
| 94 | The response code should be 200 if the version is accepted or 400 if it is not. |
| 95 | Other values are possible if other errors occur. |
| 96 | |
| 97 | When the protocol is complete, the client sends a quit request: |
| 98 | |
| 99 | ``` |
| 100 | quit-request = PKT-LINE("quit" LF) flush-pkt |
| 101 | ``` |
| 102 | |
| 103 | The server must respond with success and then terminate the connection: |
| 104 | |
| 105 | ``` |
| 106 | quit-response = PKT-LINE("status 200" LF) |
| 107 | flush-pkt |
| 108 | ``` |
| 109 | |
| 110 | This command exists to help distinguish expected termination from unexpected |
| 111 | termination. |
| 112 | |
| 113 | ### Requests to transfer objects |
| 114 | |
| 115 | These commands may be used if the operation was `upload` or `download`. |
| 116 | |
| 117 | The `batch` command is used to specify a JSON command identical those used at |
| 118 | the `info/lfs/object/batch` endpoint: |
| 119 | |
| 120 | ``` |
| 121 | batch-request = batch-command |
| 122 | *argument |
| 123 | delim-pkt |
| 124 | *oid-line |
| 125 | flush-pkt |
| 126 | batch-command = PKT-LINE("batch" LF) |
| 127 | argument = PKT-LINE(key=[data] LF) |
| 128 | oid-line = PKT-LINE(oid size *(key=[value]) LF) |
| 129 | oid = 1*("a-f0-9") |
| 130 | size = 1*DIGIT |
| 131 | ``` |
| 132 | |
| 133 | The `transfer` argument is equivalent to the corresponding value in the HTTP |
| 134 | JSON API. The `refname` argument is equivalent to the `name` argument of the |
| 135 | `ref` object in the HTTP JSON API. The `hash-algo` argument is equivalent to |
| 136 | the `hash_algo` argument in the HTTP JSON API. |
| 137 | |
| 138 | Unknown arguments should be ignored, as should unknown key-value pairs in the |
| 139 | `oid-line` production. |
| 140 | |
| 141 | The response from the server will look like the following: |
| 142 | |
| 143 | ``` |
| 144 | batch-response = status-command |
| 145 | *argument |
| 146 | delim-pkt |
| 147 | (*batch-oid-line | error-message) |
| 148 | flush-pkt |
| 149 | batch-oid-line = PKT-LINE(oid size action *(key=[value]) LF) |
| 150 | ``` |
| 151 | |
| 152 | If the status command is successful (that is, the status is not 200-series |
| 153 | response), the data provided matches the `*batch-oid-line` production; |
| 154 | otherwise, the data provided represents a user-visible error message. |
| 155 | |
| 156 | The server response should contain one pkt-line per oid-size-action tuple. That |
| 157 | is, the same oid and size may be repeated if there are multiple actions. If the |
| 158 | server has no actions that are valid for an object, it should be listed once in |
| 159 | the response with the `noop` action. |
| 160 | |
| 161 | The response for an oid may include a string, `id`, which is an opaque |
| 162 | identifier relevant only to the server to help it identify the object, and |
| 163 | another string, `token`, which is an opaque identifier relevant only to the |
| 164 | server to help it manage authentication. These strings must meet the syntax for |
| 165 | the `value` production above; if arbitrary bytes are needed, Base64 encoding is |
| 166 | recommended. |
| 167 | |
| 168 | The `hash-algo` argument has the same meaning as the `hash_algo` field in the |
| 169 | HTTP JSON API. |
| 170 | |
| 171 | The `expires-in` and `expires-at` key-value pairs have the same meaning as their |
| 172 | corresponding items from the HTTP JSON API. |
| 173 | |
| 174 | These values, if specified, must be passed as arguments to the `get-object` and |
| 175 | `put-object` commands. |
| 176 | |
| 177 | ### Downloads |
| 178 | |
| 179 | These commands may be used if the operation was `download`. |
| 180 | |
| 181 | If the operation was `download`, the command `get-object` may be used to retrieve an object: |
| 182 | |
| 183 | ``` |
| 184 | get-object-request = get-object-command |
| 185 | *arguments |
| 186 | flush-pkt |
| 187 | get-object-command = PKT-LINE("get-object " oid LF) |
| 188 | |
| 189 | ``` |
| 190 | |
| 191 | If the `id` or `token` responses were provided in the response to `batch`, they |
| 192 | must be specified as arguments here. The server may choose to ignore the `oid` |
| 193 | field specified in favor of looking up the data using the `id` field. |
| 194 | |
| 195 | The response looks like the following: |
| 196 | |
| 197 | ``` |
| 198 | status-data-response = status-success-response | status-error-response |
| 199 | status-success-response = status-success-command |
| 200 | *argument |
| 201 | delim-pkt |
| 202 | binary-data |
| 203 | flush-pkt |
| 204 | status-success-command = PKT-LINE("status 200" LF) |
| 205 | binary-data = *PKT-LINE(data) |
| 206 | status-error-response = status-error-command |
| 207 | *argument |
| 208 | delim-pkt |
| 209 | error-message |
| 210 | flush-pkt |
| 211 | status-error-command = PKT-LINE("status " http-error-code LF) |
| 212 | http-error-code = ("4" | "5") 2DIGIT |
| 213 | ``` |
| 214 | |
| 215 | The `size` argument is required on `status` responses to `get-object` commands. |
| 216 | |
| 217 | ### Uploads |
| 218 | |
| 219 | These commands may be used if the operation was `upload`. |
| 220 | |
| 221 | If the operation was `upload`, the commands `put-object` and `verify-object` |
| 222 | may be specified. `put-object` is used to upload an object to the server: |
| 223 | |
| 224 | ``` |
| 225 | put-object-request = put-object-command |
| 226 | *argument |
| 227 | delim-pkt |
| 228 | binary-data |
| 229 | flush-pkt |
| 230 | put-object-command = PKT-LINE("put-object " oid LF) |
| 231 | ``` |
| 232 | |
| 233 | As above, the `size` command is required and `id` and `token` are required if |
| 234 | provided by the server. The response matches the `status-data-response` |
| 235 | production. The `binary-data` returned on success is not meaningful and should |
| 236 | be empty. |
| 237 | |
| 238 | The `verify-object` command is used to verify an object: |
| 239 | |
| 240 | ``` |
| 241 | verify-object-request = verify-object-command |
| 242 | *argument |
| 243 | flush-pkt |
| 244 | verify-object-command = PKT-LINE("verify-object " oid LF) |
| 245 | ``` |
| 246 | |
| 247 | A `size` argument identical to the one used in `put-object` is mandatory. The |
| 248 | `id` and `token` items from the batch request must also be passed as arguments |
| 249 | here, if specified. |
| 250 | |
| 251 | The response matches the following: |
| 252 | |
| 253 | ``` |
| 254 | generic-status-response = generic-success-response | status-error-response |
| 255 | generic-success-response = generic-success-command |
| 256 | *argument |
| 257 | flush-pkt |
| 258 | generic-success-command = PKT-LINE("status 200" LF) |
| 259 | ``` |
| 260 | |
| 261 | ### Locks |
| 262 | |
| 263 | The `lock` command may be used to lock a file on a ref: |
| 264 | ``` |
| 265 | lock-request = lock-command |
| 266 | *argument |
| 267 | flush-pkt |
| 268 | lock-command = PKT-LINE("lock" LF) |
| 269 | ``` |
| 270 | |
| 271 | The `path` and `refname` arguments correspond to the `path` component and the |
| 272 | `name` component of the `ref` object in the HTTP JSON API. |
| 273 | |
| 274 | The response is as follows: |
| 275 | |
| 276 | |
| 277 | ``` |
| 278 | lock-response = lock-success-response | status-error-response |
| 279 | lock-success-response = lock-success-command |
| 280 | *argument |
| 281 | flush-pkt |
| 282 | lock-success-command = PKT-LINE("status 201" LF) |
| 283 | ``` |
| 284 | |
| 285 | If the response is either successful or a 409 response, the arguments `id`, |
| 286 | `path`, `locked-at`, and `ownername` are provided. In case of a successful |
| 287 | response, these attributes represent the created lock; if the response is a 409, |
| 288 | then the attributes represent the conflicting lock. |
| 289 | |
| 290 | The `list-lock` command may be used to list and verify locks: |
| 291 | |
| 292 | ``` |
| 293 | list-lock-request = list-lock-command |
| 294 | *argument |
| 295 | flush-pkt |
| 296 | list-lock-command = PKT-LINE("list-lock" LF) |
| 297 | ``` |
| 298 | |
| 299 | The `path`, `id`, `cursor`, `limit`, and `refspec` correspond to the items in |
| 300 | the HTTP JSON API. |
| 301 | |
| 302 | ``` |
| 303 | list-lock-response = list-lock-success-response | status-error-response |
| 304 | list-lock-success-response = list-lock-success-command |
| 305 | *argument |
| 306 | delim-pkt |
| 307 | *lock-spec |
| 308 | flush-pkt |
| 309 | list-lock-success-command = PKT-LINE("status 200" LF) |
| 310 | lock-spec = lock-decl |
| 311 | path-id |
| 312 | locked-at |
| 313 | ownername-id |
| 314 | *owner-id |
| 315 | lock-decl = PKT-LINE("lock " lock-id LF) |
| 316 | lock-id = value |
| 317 | path-id = PKT-LINE("path " lock-id path LF) |
| 318 | path = data |
| 319 | locked-at = PKT-LINE("locked-at " lock-id timestamp LF) |
| 320 | ownername-id = PKT-LINE("ownername " lock-id ownername LF) |
| 321 | ownername = data |
| 322 | owner-id = PKT-LINE("owner " lock-id who LF) |
| 323 | who = ("ours" | "theirs") |
| 324 | ``` |
| 325 | |
| 326 | The `lock-decl` production declares a new lock. The `lock-id` production refers |
| 327 | to the ID provided by the server. The same ID is repeated in each line to allow |
| 328 | for easier parsing. |
| 329 | |
| 330 | The `next-cursor` argument indicates the next value of the `cursor` argument to |
| 331 | be passed to the `list-lock` command. If there is no `next-cursor` argument, |
| 332 | this is the final response. |
| 333 | |
| 334 | The `owner-id` specification is optional if the user specified the command as |
| 335 | `download` but is required if the command is `upload`. |
| 336 | |
| 337 | ``` |
| 338 | unlock-request = unlock-command |
| 339 | *argument |
| 340 | flush-pkt |
| 341 | unlock-command = PKT-LINE("unlock " lock-id LF) |
| 342 | ``` |
| 343 | |
| 344 | The `force` and `refname` arguments have the same meaning as their corresponding |
| 345 | values in the HTTP JSON API. The response is as follows: |
| 346 | |
| 347 | ``` |
| 348 | unlock-response = unlock-success-response | status-error-response |
| 349 | unlock-success-response = unlock-success-command |
| 350 | *argument |
| 351 | flush-pkt |
| 352 | unlock-success-command = PKT-LINE("status 200" LF) |
| 353 | ``` |
| 354 | |
| 355 | The `lock` and `unlock` commands may be issued when the command was `upload`. |
| 356 | If the remote side has a concept of a repository administrator, it is |
| 357 | recommended that unlocking a lock that the user does not own be reserved to the |
| 358 | administrator. |
| 359 | |
| 360 | The `list-lock` commands may be issued when the command was `upload` or |
| 361 | `download`. |