This is an archive of the discontinued Mercurial Phabricator instance.

sshpeer: initial definition and implementation of new SSH protocol
ClosedPublic

Authored by indygreg on Feb 6 2018, 2:31 PM.

Details

Summary

The existing SSH protocol has several design flaws. Future commits
will elaborate on these flaws as new features are introduced
to combat these flaws. For now, hopefully you can take me for my
word that a ground up rewrite of the SSH protocol is needed.

This commit lays the foundation for a new SSH protocol by defining
a mechanism to upgrade the SSH transport channel away from the
default (version 1) protocol to something modern (which we'll call
"version 2" for now).

This upgrade process is detailed in the internals documentation
for the wire protocol. The gist of it is the client sends a
request line preceding the "hello" command/line which basically
says "I'm requesting an upgrade: here's what I support." If the
server recognizes that line, it processes the upgrade request and
the transport channel is switched to use the new version of the
protocol. If not, it sends an empty response, which is how all
Mercurial SSH servers from the beginning of time reacted to unknown
commands. The upgrade request is effectively ignored and the client
continues to use the existing version of the protocol as if nothing
happened.

The new version of the SSH protocol is completely identical to
version 1 aside from the upgrade dance and the bytes that follow.
The immediate bytes that follow the protocol switch are defined to
be a length framed "capabilities: " line containing the remote's
advertised capabilities. In reality, this looks very similar to
what the "hello" response would look like. But it will evolve
quickly.

The methodology by which the protocol will evolve is important.

I'm not going to introduce the new protocol all at once. That would
likely lead to endless bike shedding and forward progress would
stall. Instead, I intend to tricle out new features and diversions
from the existing protocol in small, incremental changes.

To support the gradual evolution of the protocol, the on-the-wire
advertised protocol name contains an "exp" to denote "experimental"
and a 4 digit field to capture the sub-version of the protocol.
Whenever we make a BC change to the wire protocol, we can increment
this version and lock out all older clients because it will appear
as a completely different protocol version. This means we can incur
as many breaking changes as we want. We don't have to commit to
supporting any one feature or idea for a long period of time. We
can even evolve the handshake mechanism, because that is defined
as being an implementation detail of the negotiated protocol version!
Hopefully this lowers the barrier to accepting changes to the
protocol and for experimenting with "radical" ideas during its
development.

In core, sshpeer received most of the attention. We haven't even
implemented the server bits for the new protocol in core yet.
Instead, we add very primitive support to our test server, mainly
just to exercise the added code paths in sshpeer.

Diff Detail

Repository
rHG Mercurial
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.

Event Timeline

indygreg created this revision.Feb 6 2018, 2:31 PM

I want to emphasize that I'm not committed to any implementation detail at this point in time. I've very opened minded about alternatives and making backwards incompatible changes throughout the 4.6 release cycle.

That being said, I am trying to make forward progress on a ton of wire protocol changes. These are blocking my planned work for shallow clone this release cycle. (I don't want to deploy shallow clone on the existing wire protocol for various reasons.) So, I would prefer we fall forward and take commits even if there are open bikesheds. I'm more than happy to rework the protocol later. I just don't want my local work to be dozens of changesets ahead of what's reviewed and have to spend hours reworking my code because of a bikeshed. I'd rather commit the flawed work, fix it at the head of my local queue, and move forward. If nothing else, this approach will lead to a more feature complete protocol landing sooner. And only once it is feature complete will we all have the full perspective to bikeshed the protocol.

mercurial/help/internals/wireprotocol.txt
241–243

I chose the query string format here because I don't like reinventing wheels. However, if we wanted to make it a list of space delimited atoms (like the existing capabilities string), I'd be OK with that.

We can always change this later, since we're not locked into any BC guarantees at this juncture.

245–247

In the future, I want to advertise:

  • compression engine support
  • compression engine settings (e.g. max window size for zstandard so a server won't choose a compression level that will result in excessive memory usage for client)
  • max concurrent responses limit (in the future, the protocol will gain the ability to stream multiple responses for a single request concurrently)
joerg.sonnenberger added inline comments.
tests/test-ssh-proto.t
396

I'm a bit concerned about the order here. I would prefer to stay with the spirit of the original SSH protocol and go with the following order instead:

  • client sends hello to the server
  • server sends its capabilities, including the supported "modern" protocol versions
  • client upgrades to the "best" version it supports and sends any additional wire capabilities it has

I don't think this changes the number of round-trips, but it slightly affects the number of commands that are "unversioned".

durin42 added a subscriber: durin42.Feb 7 2018, 5:04 PM
durin42 added inline comments.
tests/test-ssh-proto.t
396

I don't feel super-strongly. I think my bias is to let the upgrade happen prior to the hello so that we can have custom servers in the future not have to support the bad old framing. I'll land the patch as is, but we can continue discussing it a bit here or on the list if I haven't convinced you. :)

durin42 accepted this revision.Feb 7 2018, 5:04 PM
This revision is now accepted and ready to land.Feb 7 2018, 5:04 PM
This revision was automatically updated to reflect the committed changes.