mirror of
https://github.com/db9979/sshtermy.git
synced 2026-12-06 12:44:08 +01:00
* Absicherung: Tests auf Windows/macOS, govulncheck, Dependabot, Fuzz-Tests, Signatur-Anleitung - CI: Job platforms (windows-latest, macos-latest), vuln (govulncheck), fuzz (15 s je Ziel) - Go immer neuester 1.26-Patch (check-latest) statt fest 1.26.0 - Dependabot für Go-Module und Actions (wöchentlich, gruppiert) - Fuzz-Tests: Terminal-Emulator, Bildsequenzen, Telnet, PPK, LLDP/ARP/MAC, Import, Host-Übersicht, trzsz-Dateinamen - PPK: Argon2-Werte aus der Datei begrenzt (1 GiB, 1000 Durchläufe) - release.yml: Warnung bei unsigniertem Release, Abbruch bei Public Key ohne Secret - docs/signatur.md, make fuzz / make vuln Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018i4GUifSEzjZLFvqVjcUxz * Signatur-Anleitung: keygen im Repository-Ordner ausführen Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018i4GUifSEzjZLFvqVjcUxz * Host-Übersicht macOS: Laufzeit aus kern.boottime richtig lesen (sed nahm usec statt sec) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018i4GUifSEzjZLFvqVjcUxz * Import ssh_config: Include nur normale Dateien bis 1 MiB, höchstens 64 (Fuzz-Fund: Import hing) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018i4GUifSEzjZLFvqVjcUxz * Windows: Switch-Auflegen auch bei WSAECONNRESET erkennen, Latenz nie 0; Tests plattformfest - isHangup: WSAECONNRESET/WSAECONNABORTED per Fehlercode (deutscher Windows-Text half der Textsuche nicht) - Keepalive-Latenz mindestens 1 ns (Windows-Uhr gröber als localhost-Rundreise) - Tests: USERPROFILE neben HOME, Unix-Rechte nur außerhalb Windows prüfen, Server-Skript-Test nicht unter Git-Bash, Windows-Pfad mit Laufwerk Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018i4GUifSEzjZLFvqVjcUxz * Sixel: Zahlen deckeln (Fuzz-Fund: überlaufende Wiederholung hielt das Terminal Sekunden bis endlos fest) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018i4GUifSEzjZLFvqVjcUxz * Eigenes Fenster (Windows): Named Pipe mit Puffer, Schreiben blockiert nicht mehr bis zum Lesen Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018i4GUifSEzjZLFvqVjcUxz --------- Co-authored-by: Claude <noreply@anthropic.com>
176 lines
4.6 KiB
Go
176 lines
4.6 KiB
Go
package sshx
|
|
|
|
import (
|
|
"context"
|
|
"crypto/ed25519"
|
|
"crypto/rand"
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
"github.com/db9979/sshtermy/internal/domain"
|
|
)
|
|
|
|
// stallServer beantwortet die ersten answer Keepalives und dann keine mehr
|
|
// (wie eine Leitung, die mitten in der Sitzung stumm wird).
|
|
type stallServer struct {
|
|
addr string
|
|
answer int32
|
|
seen atomic.Int32
|
|
conns chan net.Conn
|
|
openTCP chan net.Conn
|
|
}
|
|
|
|
func startStallServer(t *testing.T, answer int32) *stallServer {
|
|
t.Helper()
|
|
_, priv, _ := ed25519.GenerateKey(rand.Reader)
|
|
signer, _ := ssh.NewSignerFromKey(priv)
|
|
cfg := &ssh.ServerConfig{
|
|
PasswordCallback: func(ssh.ConnMetadata, []byte) (*ssh.Permissions, error) { return nil, nil },
|
|
ServerVersion: "SSH-2.0-OpenSSH_9.6",
|
|
}
|
|
cfg.AddHostKey(signer)
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { ln.Close() })
|
|
s := &stallServer{addr: ln.Addr().String(), answer: answer, openTCP: make(chan net.Conn, 4)}
|
|
go func() {
|
|
for {
|
|
nc, err := ln.Accept()
|
|
if err != nil {
|
|
return
|
|
}
|
|
s.openTCP <- nc
|
|
go func() {
|
|
sc, chans, reqs, err := ssh.NewServerConn(nc, cfg)
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer sc.Close()
|
|
go func() {
|
|
for r := range reqs {
|
|
if s.seen.Add(1) <= s.answer && r.WantReply {
|
|
r.Reply(false, nil)
|
|
}
|
|
}
|
|
}()
|
|
for nch := range chans {
|
|
nch.Reject(ssh.Prohibited, "nein")
|
|
}
|
|
}()
|
|
}
|
|
}()
|
|
return s
|
|
}
|
|
|
|
func dialStall(t *testing.T, s *stallServer) *Client {
|
|
t.Helper()
|
|
hs, _ := NewHostKeyStore(filepath.Join(t.TempDir(), "known_hosts"))
|
|
host, port, _ := net.SplitHostPort(s.addr)
|
|
p, _ := strconv.Atoi(port)
|
|
c := domain.Connection{Alias: "x", Host: host, Port: p, Username: "u", Auth: domain.AuthPassword,
|
|
Password: "x", Device: domain.DeviceServer}
|
|
cl, err := Dial(context.Background(), c, Options{HostKeys: hs, Prompt: func(string, string, string) bool { return true }, Timeout: 5 * time.Second})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return cl
|
|
}
|
|
|
|
func TestDroppedWhenPeerGoesAway(t *testing.T) {
|
|
s := startStallServer(t, 100)
|
|
cl := dialStall(t, s)
|
|
defer cl.Close()
|
|
if cl.Dropped() {
|
|
t.Fatal("frische Verbindung gilt als abgebrochen")
|
|
}
|
|
(<-s.openTCP).Close() // Server/Netz weg
|
|
if !cl.WaitDropped(3 * time.Second) {
|
|
t.Fatal("Abbruch nicht erkannt")
|
|
}
|
|
}
|
|
|
|
func TestCloseIsNotADrop(t *testing.T) {
|
|
s := startStallServer(t, 100)
|
|
cl := dialStall(t, s)
|
|
cl.Close()
|
|
if cl.WaitDropped(500 * time.Millisecond) {
|
|
t.Fatal("eigenes Close darf nicht als Abbruch zählen")
|
|
}
|
|
}
|
|
|
|
func TestKeepAliveDetectsSilentLine(t *testing.T) {
|
|
s := startStallServer(t, 1)
|
|
cl := dialStall(t, s)
|
|
defer cl.Close()
|
|
cl.cancel() // Standard-Keepalive (10 s) stoppen, schnelle Variante testen
|
|
time.Sleep(50 * time.Millisecond)
|
|
s.seen.Store(0)
|
|
go cl.keepAliveLoop(context.Background(), 30*time.Millisecond, 300*time.Millisecond)
|
|
if !cl.WaitDropped(3 * time.Second) {
|
|
t.Fatal("stumme Leitung nicht erkannt")
|
|
}
|
|
if cl.Latency() <= 0 {
|
|
t.Fatal("Latenz der ersten Antwort fehlt")
|
|
}
|
|
}
|
|
|
|
func TestKeepAliveKeepsNeverAnsweringDevice(t *testing.T) {
|
|
// Gegenstelle antwortet nie auf Keepalives (manche Switches): nicht trennen.
|
|
s := startStallServer(t, 0)
|
|
cl := dialStall(t, s)
|
|
defer cl.Close()
|
|
cl.cancel()
|
|
go cl.keepAliveLoop(context.Background(), 20*time.Millisecond, 100*time.Millisecond)
|
|
if cl.WaitDropped(600 * time.Millisecond) {
|
|
t.Fatal("Verbindung ohne Keepalive-Antworten wurde getrennt")
|
|
}
|
|
}
|
|
|
|
func TestEncoding(t *testing.T) {
|
|
for in, want := range map[string]string{
|
|
"de_DE.UTF-8": "UTF-8", "en_US.utf8": "UTF-8", "C": "ASCII", "": "",
|
|
"de_DE.ISO-8859-1": "ISO-8859-1", "de_DE.UTF-8@euro": "UTF-8", "C.UTF-8": "UTF-8",
|
|
} {
|
|
if got := (RemoteInfo{Locale: in}).Encoding(); got != want {
|
|
t.Errorf("%q: %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPlainName(t *testing.T) {
|
|
for _, n := range []string{"a.txt", ".bashrc", "x y", "1:2.log"} {
|
|
if !plainName(n) {
|
|
t.Errorf("plainName(%q) = false", n)
|
|
}
|
|
}
|
|
for _, n := range []string{"", ".", "..", "../x", "a/b", `..\..\x`, "/etc/passwd"} {
|
|
if plainName(n) {
|
|
t.Errorf("plainName(%q) = true", n)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Windows meldet einen Reset als WSAECONNRESET mit deutschem oder
|
|
// englischem Text: zählt trotzdem als „Switch legt auf“ (neuer Versuch).
|
|
func TestIsHangupWindowsReset(t *testing.T) {
|
|
op := &net.OpError{Op: "read", Net: "tcp", Err: os.NewSyscallError("wsarecv", wsaECONNRESET)}
|
|
err := fmt.Errorf("ssh: handshake failed: %w", op)
|
|
if !isHangup(err) {
|
|
t.Fatalf("nicht erkannt: %v", err)
|
|
}
|
|
if isHangup(errors.New("ssh: unable to authenticate")) {
|
|
t.Fatal("Anmeldefehler als Auflegen gewertet")
|
|
}
|
|
}
|