Public Access
Add the SSH session layer and the terminal data plane
The throughput harness the plan requires before any UI, plus the SSH plumbing under it. 94 new tests, no WebView involved. Credit-based flow control is what makes `yes` survivable. A terminal renders at 60 Hz at best while a remote produces output as fast as the network allows, and the difference has to accumulate somewhere or be refused. Credit is reserved *before* reading, never after: because the pump cannot read more than the renderer has room for, the coalescing buffer is bounded by the window rather than by how fast the remote can talk. When credit runs out the pump stops reading, SSH's own receive window closes, and the remote sshd blocks -- backpressure to the source with no custom protocol. Verified by falsification, not just by passing: with the credit gate removed three tests fail, including the throughput harness's bounded-memory assertion. Acknowledgements are clamped because they cross into JavaScript, where a buggy or hostile page could otherwise claim to have rendered a gigabyte and talk the host into an unbounded read. Host key trust is enforced by *failing* the connection rather than prompting inside the handshake. SSH.NET raises verification synchronously, so consulting the user there would block the handshake on a UI round trip and deadlock the first time the prompt needed the UI thread. Unknown host and changed key become distinct exceptions the caller resolves asynchronously. A mismatch has no retry path at all: a dialog offering to continue is how users are trained to click through the one warning that actually indicates interception. A legitimately rebuilt server is handled by removing the pin in settings, away from the moment of connecting. The data plane serves the renderer page from the same loopback listener as the socket, which makes Origin predictable -- always http://127.0.0.1:{port} -- where a WebView virtual-host mapping would give a different origin per backend and nothing to validate. The token is substituted at serve time, so it never touches disk and never appears in a URL. Being clear about what that buys: not protection from a process running as this user, which can read our memory anyway, but from a page in the user's browser attempting WebSocket connections to loopback ports, which is a real and routine thing. Two bugs the tests caught. The accept loop handled connections serially, so an upgraded WebSocket parked it inside the receive loop and every later request went unanswered -- the page's own script among them. The suite hung rather than failed, which is how I found it. And SHA-1 is unavoidable here: RFC 6455 mandates it for Sec-WebSocket-Accept, where it authenticates nothing. Suppressed narrowly with that reasoning; the alternative, HttpListener.AcceptWebSocketAsync, throws PlatformNotSupportedException off Windows.
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
using DodoSSH.Client.Ssh;
|
||||
|
||||
namespace DodoSSH.Client.Terminal.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// The wire format between the host and the renderer page.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Byte order is asserted explicitly rather than only round-tripped. Both ends of this protocol are
|
||||
/// ours today, but the JavaScript side reads the header with a <c>DataView</c>, whose default is
|
||||
/// big-endian — a round-trip-only test would pass just as happily with a little-endian header that the
|
||||
/// page then misreads.
|
||||
/// </remarks>
|
||||
public sealed class TerminalFrameTests
|
||||
{
|
||||
[Fact]
|
||||
public void AFrame_RoundTrips()
|
||||
{
|
||||
var frame = TerminalFrame.Create((byte)TerminalServerOpcode.Output, 42, "hello"u8);
|
||||
|
||||
TerminalFrame.TryRead(frame, out var opcode, out var sessionId, out var payload).ShouldBeTrue();
|
||||
|
||||
opcode.ShouldBe((byte)TerminalServerOpcode.Output);
|
||||
sessionId.ShouldBe(42u);
|
||||
payload.ToArray().ShouldBe("hello"u8.ToArray());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TheSessionId_IsBigEndian()
|
||||
{
|
||||
var frame = TerminalFrame.Create((byte)TerminalServerOpcode.Output, 0x01020304, []);
|
||||
|
||||
frame[1].ShouldBe((byte)0x01);
|
||||
frame[2].ShouldBe((byte)0x02);
|
||||
frame[3].ShouldBe((byte)0x03);
|
||||
frame[4].ShouldBe((byte)0x04);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AnEmptyPayload_IsLegal()
|
||||
{
|
||||
// SessionOpened carries nothing.
|
||||
var frame = TerminalFrame.Create((byte)TerminalServerOpcode.SessionOpened, 1, []);
|
||||
|
||||
frame.Length.ShouldBe(TerminalFrame.HeaderLength);
|
||||
TerminalFrame.TryRead(frame, out _, out _, out var payload).ShouldBeTrue();
|
||||
payload.Length.ShouldBe(0);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0)]
|
||||
[InlineData(1)]
|
||||
[InlineData(4)]
|
||||
public void AFrameShorterThanTheHeader_IsRejected(int length)
|
||||
{
|
||||
// These arrive from a WebView page, so a malformed frame is untrusted input to drop rather
|
||||
// than an exceptional condition.
|
||||
TerminalFrame.TryRead(new byte[length], out _, out _, out _).ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Write_RejectsATooSmallDestination()
|
||||
{
|
||||
Should.Throw<ArgumentException>(() =>
|
||||
TerminalFrame.Write(new byte[4], 1, 1, "payload"u8));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AnAcknowledgement_RoundTrips()
|
||||
{
|
||||
var payload = TerminalFrame.CreateAcknowledgementPayload(123_456);
|
||||
|
||||
TerminalFrame.TryReadAcknowledgement(payload, out var rendered).ShouldBeTrue();
|
||||
rendered.ShouldBe(123_456u);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0)]
|
||||
[InlineData(3)]
|
||||
[InlineData(5)]
|
||||
public void AnAcknowledgementOfTheWrongLength_IsRejected(int length)
|
||||
{
|
||||
TerminalFrame.TryReadAcknowledgement(new byte[length], out _).ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AResize_RoundTrips()
|
||||
{
|
||||
var size = new TerminalSize(132, 43, 1320, 1075);
|
||||
var payload = TerminalFrame.CreateResizePayload(size);
|
||||
|
||||
TerminalFrame.TryReadResize(payload, out var decoded).ShouldBeTrue();
|
||||
decoded.ShouldBe(size);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0)]
|
||||
[InlineData(7)]
|
||||
[InlineData(9)]
|
||||
public void AResizeOfTheWrongLength_IsRejected(int length)
|
||||
{
|
||||
TerminalFrame.TryReadResize(new byte[length], out _).ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AResizePayload_OrdersColumnsBeforeRows()
|
||||
{
|
||||
// The page builds this by hand, and columns-before-rows is the SSH convention. Swapping them
|
||||
// produces a terminal that is 24 columns by 80 rows, which looks like a rendering bug.
|
||||
var payload = TerminalFrame.CreateResizePayload(new TerminalSize(80, 24));
|
||||
|
||||
payload[0].ShouldBe((byte)0);
|
||||
payload[1].ShouldBe((byte)80);
|
||||
payload[2].ShouldBe((byte)0);
|
||||
payload[3].ShouldBe((byte)24);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user