Files
DodoSSH/tests/DodoSSH.Client.Session.Tests/SpelunkLinkTests.cs
jaap-jan 6e39807cc0 Reach a Spelunk-managed server through Spelunk, and open one from a Spelunk link
A server Spelunk manages runs Spelunk's agent, which keeps a connection open
to Spelunk. A host can now be marked "Connect through Spelunk": a connect
asks Spelunk for a ticket, opens a WebSocket, and the agent connects to the
SSH server on the machine's own loopback, so the server needs no open SSH
port. Shells and file transfers both go this way.

Client, in Client.Session:

- SpelunkTunnel implements Spelunk's contract: POST
  {api}/api/servers/{id}/tunnel-tickets with the API key as a bearer token
  and {"port":N}, then a WebSocket to the API's own origin at the path
  Spelunk returns, offering spelunk.tunnel.v1 and ticket.<ticket>. After the
  upgrade the relay's RelayBridge carries the bytes unchanged. Every refusal
  is a sentence: 401 key, 403 role (with Spelunk's detail), 404 server, 503
  agent not connected, 401/503 on the upgrade, and close codes 4502 (agent
  could not reach the port, or is too old: "Unknown stream kind") and 4503
  (agent disconnected). Only a path is taken from the ticket response, so it
  cannot send the ticket to another host.
- IConnectionRoute is what RelayRoute and SpelunkTunnelRoute have in common,
  so the connect path has one shape for both.

Host model: HostSecret.Spelunk is a SpelunkRoute (normalised API base,
server id, and the id of the credential holding the API key), at payload
schema version 8. It is written last and only when present, so every other
host keeps its bytes and its version; an older build reads a Spelunk host
as read-only rather than dropping the route. A half-written route makes the
item unreadable instead of dialled. It merges as one value and is exclusive
with RelayEnabled. The API address must be https, or http to loopback.

The API key is an ordinary keychain credential whose password is the key,
referenced by id, so it is encrypted, synced, merged and rotated like every
other secret, one credential serves every server in the organisation, and
the used-by list and deletion warning count it.

Routing: OpenRouteAsync is the one decision for both transports. A Spelunk
host is never dialled directly, never probed after a failure, and a route
that hung up with a reason replaces SSH.NET's transport error.

UI: a "Connect through Spelunk" card beside the relay card in the desktop
drawer and the phone's editor; ticking one unticks the other.

dodossh://spelunk/connect?api=&server=&name=&host= links:

- SpelunkLink parses them strictly and never reads a credential from one.
- Program reads a link from the arguments; a launch that finds DodoSSH
  running passes it down the doorbell as "dodossh.open-link <link>", which
  the running instance parses again, bounded, and otherwise ignores whole.
  The plain come-forward ring is unchanged.
- A saved host routed to that server on that Spelunk is connected to;
  otherwise the editor opens on a new, unsaved Spelunk host, with the key
  pre-chosen only when another host already sends it to that Spelunk. A link
  that arrives while locked waits for the unlock.
- Windows registers the scheme under HKCU from Velopack's install and update
  hooks and removes it on uninstall only if it still points at this
  executable; the macOS plist template declares CFBundleURLTypes and the app
  takes the URL from Avalonia's activation event; the Linux desktop entry
  gets %u and MimeType=x-scheme-handler/dodossh.
2026-09-28 21:25:04 +02:00

116 lines
4.8 KiB
C#

namespace DodoSSH.Client.Session.Tests;
/// <summary>
/// Reading a <c>dodossh://spelunk/connect</c> link, which anything on the machine can hand the application.
/// </summary>
/// <remarks>
/// A link is data to check, never an instruction. The refusals below are the part that matters: each is a
/// link that is nearly right, and "nearly right" is the shape a crafted one takes.
/// </remarks>
public sealed class SpelunkLinkTests
{
private const string Server = "0192f0c8-bbbb-7c3d-8e4f-5a6b7c8d9e0b";
private const string Link =
"dodossh://spelunk/connect?api=https%3A%2F%2Fapi.spelunk.example.com&server=" + Server
+ "&name=web%2001&host=203.0.113.10";
[Fact]
public void ALinkFromSpelunk_IsRead()
{
SpelunkLink.TryParse(Link, out var link).ShouldBeTrue();
link.ShouldBe(new SpelunkLink("https://api.spelunk.example.com", Guid.Parse(Server), "web 01", "203.0.113.10"));
link.Hostname.ShouldBe("203.0.113.10");
}
[Fact]
public void ALinkWithNoHost_NamesTheHostAfterTheServer()
{
// The name the host key is pinned under, so it has to be stable and this server's alone.
SpelunkLink.TryParse(Link.Replace("&host=203.0.113.10", string.Empty, StringComparison.Ordinal), out var link)
.ShouldBeTrue();
link.Host.ShouldBeNull();
link.Hostname.ShouldBe($"spelunk-{Server}");
}
[Fact]
public void ALink_WritesOutAsItReadsBack()
{
// What a second launch sends the running instance: the one spelling this build writes.
SpelunkLink.TryParse(Link, out var link).ShouldBeTrue();
SpelunkLink.TryParse(link.ToString(), out var again).ShouldBeTrue();
again.ShouldBe(link);
}
[Fact]
public void ANameFormEncoded_AndOneWithControlCharacters_AreCleanedForDisplay()
{
SpelunkLink.TryParse(Link.Replace("web%2001", "web+01%0A%1B%5B31m", StringComparison.Ordinal), out var link)
.ShouldBeTrue();
link.Name.ShouldNotContain('\n');
link.Name.ShouldNotContain('\u001b');
link.Name.ShouldStartWith("web 01");
}
[Fact]
public void AnUnknownParameter_IsIgnored()
{
// So a newer Spelunk can add one without every DodoSSH already installed refusing its links.
SpelunkLink.TryParse(Link + "&port=22", out _).ShouldBeTrue();
}
[Theory]
[InlineData("https://spelunk/connect?api=https%3A%2F%2Fapi.example.com&server=" + Server)]
[InlineData("dodossh://elsewhere/connect?api=https%3A%2F%2Fapi.example.com&server=" + Server)]
[InlineData("dodossh://spelunk/disconnect?api=https%3A%2F%2Fapi.example.com&server=" + Server)]
[InlineData("dodossh://user@spelunk/connect?api=https%3A%2F%2Fapi.example.com&server=" + Server)]
[InlineData("dodossh://spelunk:8080/connect?api=https%3A%2F%2Fapi.example.com&server=" + Server)]
[InlineData("dodossh://spelunk/connect?api=https%3A%2F%2Fapi.example.com&server=" + Server + "#x")]
[InlineData("dodossh://spelunk/connect?server=" + Server)]
[InlineData("dodossh://spelunk/connect?api=https%3A%2F%2Fapi.example.com")]
[InlineData("dodossh://spelunk/connect?api=https%3A%2F%2Fapi.example.com&server=not-a-guid")]
[InlineData("dodossh://spelunk/connect?api=https%3A%2F%2Fapi.example.com&server=00000000-0000-0000-0000-000000000000")]
[InlineData("dodossh://spelunk/connect?api=http%3A%2F%2Fapi.example.com&server=" + Server)]
[InlineData("dodossh://spelunk/connect?api=https%3A%2F%2Fu%3Ap%40api.example.com&server=" + Server)]
[InlineData("dodossh://spelunk/connect?api=https%3A%2F%2Fapi.example.com&api=https%3A%2F%2Fevil.example&server=" + Server)]
[InlineData("dodossh://spelunk/connect?api=https%3A%2F%2Fapi.example.com&server=" + Server + "&host=bad%20host")]
[InlineData("dodossh://spelunk/connect?api=https%3A%2F%2Fapi.example.com&server=" + Server + "&name=a b")]
[InlineData("")]
public void ALinkThatIsNotExactlyThis_IsRefused(string text)
{
SpelunkLink.TryParse(text, out var link).ShouldBeFalse();
link.ShouldBeNull();
}
[Fact]
public void AnOverlongLink_IsRefusedBeforeItIsParsed()
{
SpelunkLink.TryParse(Link + "&name=" + new string('a', SpelunkLink.MaxLength), out _).ShouldBeFalse();
}
[Fact]
public void ANameThatRunsOn_IsCut()
{
SpelunkLink.TryParse(Link.Replace("web%2001", new string('a', 500), StringComparison.Ordinal), out var link)
.ShouldBeTrue();
link.Name.Length.ShouldBe(SpelunkLink.MaxNameLength);
}
[Theory]
[InlineData("dodossh://spelunk/connect", true)]
[InlineData("DODOSSH://anything", true)]
[InlineData("--veloapp-install", false)]
[InlineData(null, false)]
public void TheLauncher_TellsALinkFromAnotherArgument(string? argument, bool looksLikeOne)
{
SpelunkLink.LooksLikeOne(argument).ShouldBe(looksLikeOne);
}
}