sugarcraft / sugar-ssh-server
SSH server middleware framework — inspired by charmbracelet/wish, developed as a native PHP project. Composable middleware (auth, logging, rate-limiting, channel handling) and a PTY supervisor that runs a shell or a SugarCraft Program under a host OpenSSH daemon's ForceCommand. It does not implement
Requires
- php: ^8.3
- react/event-loop: ^1.6
- react/promise: ^3.3
- react/promise-timer: ^1.9
- sugarcraft/sugar-core: dev-master
- sugarcraft/sugar-pty: dev-master
Requires (Dev)
- phpunit/phpunit: ^10.5
- sugarcraft/sugar-testing: dev-master
Suggests
- ext-ffi: sugar-pty binds libc through FFI to allocate the PTY; without it the default InProcessTransport cannot run, while the legacy HostSshdTransport needs nothing. sugar-pty's own manifest still hard-requires ext-ffi, so install-time enforcement is transitive.
- ext-pcntl: Forks and reaps the InProcessTransport session child and forwards SIGWINCH resizes; runChild() raises a PtyException when it is missing, and the legacy HostSshdTransport needs nothing.
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-20 01:04:12 UTC
README
SugarSshServer
Formerly published as
sugarcraft/candy-wish(renamed 2026-10).Namespace moved from
SugarCraft\Wish\toSugarCraft\SshServer\.
sugar-ssh-server — an SSH server middleware framework for PHP 8.3+ that lets you
build TUIs anyone can ssh user@host to run.
composer require sugarcraft/sugar-ssh-server
Runtime requirements
- PHP 8.3+ — declared in
composer.json. - A POSIX host (Linux or macOS) — sugar-ssh-server's default transport builds on sugar-pty's POSIX PTY layer.
- OpenSSH
sshdfronts the wire. sugar-ssh-server does not implement the SSH transport protocol; each connection runs the middleware stack in a fresh PHP process spawned bysshdviaForceCommand(or acommand="..."line inauthorized_keys). Authentication at the SSH layer (including RFC 4256 keyboard-interactive over the network) is sshd's job; the bundled auth middlewares prompt on the session's own stdio. - The default
InProcessTransport(PTY supervisor) additionally requires:ext-ffi— sugar-pty binds libc through FFI to allocate the PTY; without it the transport cannot run (and Windows is not a target). This library's own manifest listsext-ffiundersuggest, notrequire— the code in this package never touches FFI, and the legacy transport below runs without it. Install-time enforcement nonetheless still applies: the sugar-pty dependency hard-requiresext-ffiin its own manifest, socomposer requirefails transitively on a host without it until that cross-lib manifest is relaxed (a pending upstream ruling). To install the legacy combination on such a host, add--ignore-platform-reqs=ext-ffi— the default transport will then refuse to run, exactly as above.ext-pcntl— forking and reaping the session child.runChild()raises aPtyExceptionwhen pcntl is missing; SIGWINCH size forwarding alone degrades gracefully without it.- a usable
/dev/ptmx— PTY allocation fails in restricted containers and on many shared hosts. Remedies: grant the device and a devpts to the container (--device=/dev/ptmxplus a devpts mount at/dev/pts), or keep the connection behind sshd and use the legacy transport below, which needs none of the three InProcessTransport requirements.
- The legacy
HostSshdTransportruns the middleware chain inline against the PTY sshd already allocated — it needs none of the three above. ext-ssh2is not used by this library at all and the manifest does not suggest it (see ext-ssh2 below).
Tests that exercise the PTY stack gate themselves on these capabilities, so the suite also passes on hosts without them (the default transport is then simply not covered).
Shared foundations
SugarSshServer builds on sugar-pty (the PTY supervisor InProcessTransport
spawns children through) and sugar-core (Lang i18n wrapper,
AtomicJsonFile persistence). It performs no terminal-capability probing of
its own — Session::fromEnvironment() reads SSH environment variables
(TERM, SSH_CONNECTION, …) purely as session metadata, never as a
feature sniff. Should capability probing ever be added, route it through a
terminal probe rather than raw getenv()/terminfo reads.
Architecture
SugarSshServer leans on the host's OpenSSH daemon rather than implementing the SSH wire protocol from scratch. Each SSH connection forks a fresh PHP process under sshd (via ForceCommand). What that PHP process does internally depends on the active transport:
InProcessTransport (default)
[client] ─ssh─▶ [sshd] ─ForceCommand──▶ [php supervisor] ──▶ [middleware stack]
│ │
└─pump bytes──┐ └─Spawn middleware
│ │
▼ ▼
[sugar-pty master ◀──── slave / inner cmd]
(bash, vim, custom binary)
The supervisor allocates a sugar-pty master/slave pair, spawns the user's cmd as a subprocess with full controlling-terminal semantics (Ctrl+C → SIGINT, SIGWINCH-driven resize, job control), and pumps bytes between the supervisor's STDIN/STDOUT (= sshd's PTY slave) and the sugar-pty master. The terminal middleware is Spawn, which produces the cmd from the Session.
HostSshdTransport (legacy, opt-in)
[client] ─ssh─▶ [sshd] ─ForceCommand──▶ [php supervisor] ──▶ [middleware stack] ──▶ [SugarCraft Program reading STDIN, writing STDOUT]
The pre-PTY-upgrade architecture: middleware run inline in the supervisor, and the terminal middleware (BubbleTea) mounts a SugarCraft Program directly on the supervisor's STDIN/STDOUT. Pin via Server::new()->withTransport(new HostSshdTransport()). Use this if your existing entry script reads STDIN/echoes STDOUT directly without a subprocess.
Picking a transport
InProcessTransportwhen you want to spawn arbitrary shells (bash -i,zsh,fish), editors (vim,less), or compiled TUI binaries — anything that needs a controlling terminal. Costs a subprocess per connection — every session forks a fresh child (the target program or/bin/bash -l, not PHP) — but buys full PTY semantics.HostSshdTransportwhen your TUI is a SugarCraftProgramand you want zero subprocess overhead, or when you have an inline-STDIN-reading middleware (banner-style). No subprocess, but no controlling-terminal isolation.
The byte pump needs a blocking STDOUT
InProcessTransport::runChild() flips two of its three streams to
non-blocking — the PTY master and STDIN — because the pump polls them with
stream_select(). It deliberately leaves STDOUT alone, and that is not an
oversight: the pump it delegates to (sugar-pty's PosixPump) writes
whatever it reads from the master in a single fwrite() per chunk and
counts a short write as success. On the blocking pipe a ForceCommand child
inherits from sshd, a full buffer just slows the pump. On a stream you have
flipped non-blocking — an async host that took over its own stdio, for
example — the chunk beyond the pipe buffer is dropped on the floor and the
user sees their terminal output cut off with no error anywhere.
So: keep $stdout blocking when you call runChild(), and do not add the
missing stream_set_blocking($stdout, false) line. Retrying the unwritten
tail is sugar-pty's job (buffering the remainder and re-arming on
writability); the note in InProcessTransport::runChild()'s docblock names
the upstream method that needs it. The admin guide records the same
constraint under
Failure modes.
See also — Git serving (candy-git-server)
SugarSshServer is a general SSH middleware/session framework. For serving Git
repositories specifically — a git:// daemon, the HTTP smart protocol, and a
ForceCommand-style SSH git-shell gate — see the sibling
candy-git-server. Both libraries run under the host's OpenSSH
daemon; neither implements the SSH wire protocol itself.
Quickstart
0. Provision the account, the directory, and the package
Everything below assumes three things a fresh host does not have. As root (Debian/Ubuntu spellings — RHEL-family differs in package and service names):
apt-get install -y openssh-server useradd -m -s /bin/bash wishuser install -d -m 0755 -o wishuser -g wishuser /opt/wish
-
wishuseris the account theMatchblock in step 1 forces commands on, and the account the entry script runs as — the step 2 paths are provisioned for its uid. -
/opt/wish/vendor/autoload.phpis hard-required as the first executable line of the entry script below. Nothing in sshd's configuration creates it: it is produced by a deploy-time install of this package into/opt/wish, run as the same user that will own the directory:sudo -u wishuser sh -c 'cd /opt/wish && composer require sugarcraft/sugar-ssh-server'
Until that install completes, every forced connection lands on PHP's "Could not open input file" — which is exactly the failure the probe in step 1 teaches you to read.
1. Configure sshd
Add to /etc/ssh/sshd_config.d/wish.conf:
Match User wishuser
ForceCommand /usr/bin/php /opt/wish/server.php
AllowTcpForwarding no
PermitTTY yes
X11Forwarding no
PasswordAuthentication no
PermitEmptyPasswords no
Keep the forced account pubkey-only: this library CONSUMES identity, it
does not verify credentials — the ceiling is sshd's auth policy, and every
fingerprint and pairing assumption below it rides the strength of whatever
sshd accepted before ForceCommand ran.
Then systemctl reload sshd, and check that it took effect before you
depend on it:
ssh -o BatchMode=yes wishuser@host; echo "status=$?"
That runs your entry script, not your shell — which is the point. At this
point in the quickstart the entry script does not exist yet (sections 2 and 3
provision it below), so a correct Match block lands on PHP's
"Could not open input file" — measured on PHP 8.3, that exits 1, and
255 is reserved for parse errors and in-script fatals (once the file
exists). 0 and 129 are what the same probe answers once server.php
exists. Any of those three — 1, 0, 129 — means
ForceCommand reached the account (see Exit
status, which splits the two
PHP failure shapes); a login that drops you
into an interactive shell means the Match block never applied, and every
assumption below it about rate limits and pairing is currently unenforced. Do
not write the probe as ssh wishuser@host echo ok: the argument is discarded
before PHP starts
(Healthcheck).
2. Provision the paths the stack writes
Both Logger and RateLimit write where you point them, and neither can
do it from a directory the forced-command user cannot touch. The example
below writes to /var/log/wish.jsonl and /var/lib/wish/buckets.json,
so as root:
install -d -m 0750 -o wishuser -g wishuser /var/lib/wish install -d -m 0755 -o root -g adm /var/log # usually already there touch /var/log/wish.jsonl && chown wishuser:adm /var/log/wish.jsonl
If you prefer not to hand-create anything, let systemd own it — the service that runs your entry script (or a socket-activated wrapper) gets:
[Service]
StateDirectory=wish
LogsDirectory=wish
and the paths become /var/lib/wish/state/buckets.json and
/var/log/wish/wish.jsonl, both pre-created and owned by the service
user. tmpfiles.d(5) is the equivalent for a host without systemd.
What happens when you skip this, in the order you will notice it:
Loggerresolves its sink in its constructor, so an unwritable path throwsRuntimeExceptionbefore the first session: exit 255, and underForceCommandthe PHP trace rides the client's channel rather than a log line. Loud, but at the wrong recipient — check it at deploy time instead (below).RateLimitcreates its directory0700and its files0600when it can. When it cannot, it announces on its$stderrstream — which is the client-visible channel unless you injected one — and, with the defaultfailClosed: false, lets the connection through: your throttle is simply off, and nobody but the operator running the probe in step 1 sees it.
A five-line preflight turns both into a deploy-time failure. Put it at
the top of the entry script (or in a --preflight mode you run from a
provisioning check):
foreach (['/var/log/wish.jsonl', '/var/lib/wish/buckets.json'] as $path) { $dir = \dirname($path); if (!is_dir($dir) || !is_writable($dir)) { \fwrite(STDERR, "not writable: {$dir}\n"); exit(1); } if (file_exists($path) && !is_writable($path)) { \fwrite(STDERR, "not writable: {$path}\n"); exit(1); } } $logger = Logger::toFile('/var/log/wish.jsonl'); \fwrite(STDERR, 'sink: ' . $logger->target() . "\n");
Note that a new file is fine as long as its directory is writable:
Logger creates it 0600 and RateLimit publishes its state 0600.
The preflight above only refuses what the process genuinely cannot open.
3. Write the entry script
InProcessTransport (default) — spawn an interactive shell:
<?php // /opt/wish/server.php require '/opt/wish/vendor/autoload.php'; use SugarCraft\SshServer\Server; use SugarCraft\SshServer\Middleware\Logger; use SugarCraft\SshServer\Middleware\Auth; use SugarCraft\SshServer\Middleware\RateLimit; use SugarCraft\SshServer\Middleware\Spawn; use SugarCraft\SshServer\Session; exit(Server::new() ->use(Logger::toFile('/var/log/wish.jsonl')) ->use(new RateLimit('/var/lib/wish/buckets.json', burst: 5, ratePerSec: 0.5)) ->use(new Auth(users: ['wishuser'])) ->use(new Spawn(fn (Session $s) => [ 'cmd' => ['/bin/bash', '-l'], 'env' => [ 'TERM' => $s->term, 'USER' => $s->user, 'HOME' => "/home/{$s->user}", 'PATH' => '/usr/local/bin:/usr/bin:/bin', ], ])) ->serve());
HostSshdTransport (legacy) — mount a SugarCraft Program inline:
<?php // /opt/wish/server.php require '/opt/wish/vendor/autoload.php'; use SugarCraft\SshServer\Server; use SugarCraft\SshServer\Middleware\Logger; use SugarCraft\SshServer\Middleware\Auth; use SugarCraft\SshServer\Middleware\RateLimit; use SugarCraft\SshServer\Middleware\BubbleTea; use SugarCraft\SshServer\Transport\HostSshdTransport; exit(Server::new() ->withTransport(new HostSshdTransport()) ->use(Logger::toFile('/var/log/wish.jsonl')) ->use(new RateLimit('/var/lib/wish/buckets.json', burst: 5, ratePerSec: 0.5)) ->use(new Auth(users: ['wishuser'])) ->use(new BubbleTea(fn ($session) => new MyApp($session))) ->serve());
The wishuser in both Auth allowlists is what the Step 1 Match block
makes true, not an oversight: ForceCommand runs the entry script as the
target account, so $s->user — read from USER/LOGNAME in
Session::fromEnvironment() — is wishuser on every connection, and an
allowlist of human names here refuses 100% of logins with exit
Server::EXIT_DENIED. Authenticating real humans is the SSH layer's job
before the script starts; per-user separation inside the middleware needs a
per-user Match block or a command="..." line in each person's own
authorized_keys, and each such identity then runs its own entry script
that sees its own name.
Both examples exit(...) on serve() deliberately: that return value is
the session's answer — the child's own exit code, Server::EXIT_DENIED
(129) when a gate refused and nothing ran, or 0 when the walk finished
with nothing to spawn. A script that calls serve() and falls off the end
of the file reports success to whichever cron entry or CI step asked.
docs/admin/sugar-ssh-server.md
tabulates the three.
Language. Call ->withLocale('de') on the Server (or run
\SugarCraft\Core\I18n\T::setLocale() yourself) — nothing selects a
locale implicitly, and the peer never does; see
Language.
4. Connect
ssh wishuser@your-host
Middleware
The table names middleware bare. Four of the Auth-suite members —
PasswordAuth, CertificateAuth, AuthMethods, KeyboardInteractive —
live under SugarCraft\SshServer\Middleware\Auth\, while the Auth class
itself is SugarCraft\SshServer\Middleware\Auth: the class and the
sub-namespace share one name, so the quickstart's
use SugarCraft\SshServer\Middleware\Auth; imports the class, and the four
named above import one level deeper
(use SugarCraft\SshServer\Middleware\Auth\PasswordAuth;).
That holds wherever this README names them bare. The Subsystem section
below already spells its sub-namespace explicitly
(Middleware\Subsystem\SftpStub); this note closes the gap for the Auth
suite once, rather than parenthetically at every appearance.
| Middleware | Transport | Purpose |
|---|---|---|
Logger |
both | One-line JSON event at session start + end, with elapsed time and connection meta. Sink matters: Logger::auto() (and new Logger()) append to $SSH_SERVER_LOG when set, else php://stderr — which under ForceCommand is the connecting client's extended-data channel, not journald. Pass a file path (we create it 0600; an existing file keeps its mode) or Logger::toStream($handle) you control. target() reports the choice. |
Auth |
both | Username allowlist, key-fingerprint allowlist, or the paired userKeys map (name => that name's keys). The two flat lists are a cross product, not a pairing: with users ['alice','bob'] and keyFingerprints [alice-key, bob-key], bob presenting alice's key is admitted as alice. Use userKeys when the question is whose key this is ([Auth::ANY_KEY] admits the login with-or-without key evidence, that name only). An empty list arms nothing — new Auth() admits every session. |
PasswordAuth |
both | Wrapper-only. Compares a password from SSH_PASSWORD against a caller-supplied callback. The variable is expected to reach this process from a wrapper that launches the entry script and exports it, or from a client-supplied value an operator lets through AcceptEnv; that AcceptEnv value is peer-asserted rather than proven. This repo does not set SSH_PASSWORD, and whether sshd's ForceCommand path exports it is unverified against OpenSSH. The gate does not authenticate the peer by itself. A session that delivered no variable at all is refused before the validator is consulted. Absent means denied, with a path-free notice. |
CertificateAuth |
both | Hands a certificate string from CertificateAuth::ENV_KEYS (SSL_CLIENT_CERT, SSH_CLIENT_CERT, CERTIFICATE) to a caller-supplied validator. Stock OpenSSH exports none of them, so unless your wrapper maps one in, the gate announces that it checked nothing — refusing at required: true, forwarding at required: false, and saying so either way. |
AuthMethods |
both | Declares accepted auth methods and stores the list in Context (auth.methods, readable with AuthMethods::fromContext()). The SSH_AUTH_METHODS line on STDOUT is opt-in (announce: true) and off by default — under ForceCommand stdout is the client's data channel, so that line is text in the user's terminal, not a protocol field. |
KeyboardInteractive |
both | Prompts on STDOUT, answers read from STDIN. Shaped like RFC 4256, but it is not: no SSH messages exist here. echo is a label, not terminal state — input stays visible (the exchange says so when any prompt asks for silence), each answer is capped at MAX_RESPONSE_BYTES, and there is no deadline but the stream you inject and the session Context between prompts. |
RateLimit |
both | Per-IP (optionally per-username) token bucket persisted to a JSON state file. Writers serialize on a stable <state-path>.lock sidecar — never on the payload inode, which the atomic rename replaces — and refilled idle buckets are pruned on every publish. Default policy on a state error is fail-open; penalize() exists so a failed credential can drain a bucket faster than connection attempts alone, but no gate calls it directly — wrap the gate in AuthRateLimitBridge (or wire it into your own auth path). The per-IP key is the direct TCP peer's address — the one sshd saw. Behind a bastion, load balancer, or ProxyJump, every client beyond that hop shares one bucket, so enable $userBurst or throttle at the fronting layer. See docs/admin/sugar-ssh-server.md. |
AuthRateLimitBridge |
both | Composition wrapper: takes any synchronous auth Middleware plus the RateLimit instance, and charges the session's buckets penalty tokens (default 3.0) whenever the wrapped gate refuses (never invokes $next). Admits pass through untouched — same Context, same Session, one call. An async (promise-returning) gate is refused at handle() time with a LogicException: its verdict does not exist yet, and charging on non-continuation would penalise in-flight auth. |
Spawn |
InProcess only | Terminal — spawns a child cmd in a sugar-pty controlled by the supervisor. |
BubbleTea |
HostSshd only | Terminal — mounts a SugarCraft Program inline reading STDIN, writing STDOUT. |
Subsystem |
both | Terminal — parses subsystem <name> from Session::command, dispatches to a registered SubsystemHandler. Non-subsystem requests pass through to $next. |
AsyncMiddleware |
both | Abstract base for middleware that needs async I/O (LDAP, OAuth, database auth) — return a PromiseInterface from handleAsync(). The transport waits for the promise to settle before continuing the chain. |
ClosureMiddleware |
both | A bare callable as middleware — appended for you by Server::onSession() (below). The closure's return value passes through untouched: nothing means sync, a PromiseInterface is awaited at the shared settle point, and any stray value dies loudly in the dispatcher (the one enforcement point, unchanged by this door). |
Rate limiting keys on the direct TCP peer
RateLimit's ip:<host> bucket is Session::$clientHost — the source
sshd recorded for this connection: the direct TCP peer, not the human
behind it. A bastion, load balancer, or ProxyJump hop collapses many
operators into one bucket, so one abusive client locks out everyone else
sharing that path — enable the $userBurst username bucket, or move
throttling to the fronting layer and treat this one as the per-peer
backstop. The state file and its .lock sidecar must also live on a
local filesystem: flock on NFS or other weak-locking mounts can be
silently absent, and then the bucket arithmetic on shared/network storage
is best-effort (the JSON payload itself stays atomic, so the failure is
invisible, not loud). See the Rate Limiting and Files-written sections in
docs/admin/sugar-ssh-server.md.
Server doors (not middleware)
A few behaviours are configured on the Server rather than appended to the
stack — they act on the transport, not the request path, so dressing them as
middleware would hide when they actually run.
- Keepalive — installed by
Server::withKeepalive($seconds). When the pump has idled past the interval it writes a\0byte to the child's stdin through the PTY master. It is not an SSH-level keepalive — no wire messages exist here — a raw-mode TUI never echoes it, so nothing reaches the client, and under HostSshd it is a no-op: rely on sshdClientAliveInterval. TheKeepalivevalue object is stored on the server and handed to the transport'sKeepaliveArmingseam atserve()entry; it is no longer aMiddleware, so->use(new Keepalive(30))now raises aTypeError(pass the interval through the door instead). - Locale — installed by
Server::withLocale($code); see Language. - Transport — installed by
Server::withTransport($t); see Architecture. - Session hook — installed by
Server::onSession($fn); appends aClosureMiddlewareat exactly the stack position of the call, the class-tax-free spelling of the middleware contract below (the closure receives($ctx, $session, $next)and its return value passes through untouched — samevoid/PromiseInterfacelaw as any hand-written middleware).
All middleware receives a Context as the first argument, along with
the Session and a $next continuation. Implement SugarCraft\SshServer\Middleware:
use SugarCraft\SshServer\Context; use SugarCraft\SshServer\Middleware; use SugarCraft\SshServer\Session; final class HelloBanner implements Middleware { public function handle(Context $ctx, Session $s, callable $next): void { echo "Welcome, {$s->user}!\n"; $next($ctx, $s); } }
Observability surface (minor accessors)
Server::transport()returns the transportserve()will run;Keepalive::interval()returns the seconds the armed value object carries.DefaultChannelHandlerkeeps its negotiation state readable for a custom handler that wraps it:execAllowed($session, $argv)is the two-gate decision itself, andptyAllocated()(?booluntil the pty-request is answered),shellRequested(),execRequested(),pendingCommand()andenvVars()report what the channel has recorded so far.InProcessTransport::setKeepaliveCallback()is the low-level seat under the keepalive story (its only shipped caller isarmKeepalive()): the zero-argument callback fires in the pump loop wheneverstream_selectreports no ready streams, with no further guarantees — embedders wantServer::withKeepalive(), documented above.InProcessTransport::pty()returns the liveMasterPty, valid only while the pump loop runs (it throws otherwise) — which is how a custom pump callback can write to the master as well.- The
Sessionconstructor also takes the five protocol-metadata fields (sessionId,authMethod,keyFingerprint,clientVersion,serverVersion) as optional named args — the direct-ctor shape beside the transport-shaped one:withProtocolMetadata()(see Session metadata) is how the transport fills them, andexamples/showcase.phpshows the ctor consumed named-argument-style.
Constructor options
Every shipped gate takes its knobs at construction, and several defaults are
security-relevant, so the signatures are tabulated centrally here rather than
inferred from examples. Names in bold are the ones that change behaviour
when left alone, and the Auth-suite four live under
SugarCraft\SshServer\Middleware\Auth\ (namespace note under
Middleware).
| Middleware | Constructor | Defaults worth knowing |
|---|---|---|
Logger |
Logger::auto(), ::toFile($path), ::toStream($r), ::toStderr(); new Logger($target) still works |
auto() appends to $SSH_SERVER_LOG when set, and to php://stderr otherwise — which under ForceCommand is the connecting peer. A file this class creates is 0600; an existing file keeps its mode. target() says which you got. |
Auth |
new Auth($users, $keyFingerprints, $stderr, $fingerprintValidator, $userKeys) |
Empty $users and $keyFingerprints arm nothing: a bare new Auth() admits every session. $userKeys is the pairing surface ([Auth::ANY_KEY] = any key for that name); notices are sanitized and truncated at Auth::MAX_ECHOED_BYTES (128). The named doors Auth::allowingUsers($users) and Auth::pairs($userKeys) refuse an empty axis with a LogicException — construction that cannot be accidentally unarmed. |
PasswordAuth |
new PasswordAuth($validate, $stderr) |
Wrapper-only: reads SSH_PASSWORD. This repo does not set it, and whether sshd's ForceCommand path exports it is unverified against OpenSSH. It is expected to arrive from your wrapper's export or from an operator's AcceptEnv letting the client send it (peer-asserted). A session that delivered no variable is refused before $validate runs; an explicitly empty one reaches it. |
CertificateAuth |
new CertificateAuth($validate, $required = true, $stderr) |
Probes CertificateAuth::ENV_KEYS (SSL_CLIENT_CERT, SSH_CLIENT_CERT, CERTIFICATE); stock OpenSSH exports none. $required = false forwards — after announcing that it checked nothing. |
AuthMethods |
new AuthMethods($methods, $stdout, $announce = false) |
Stores the list in Context. The SSH_AUTH_METHODS stdout line is off by default; announce: true opts in and owns the placement. |
KeyboardInteractive |
new KeyboardInteractive($challenges, $validate, $name, $instruction, $stdout, $stdin, $stderr) |
No $validate means any answer passes. Answers capped at KeyboardInteractive::MAX_RESPONSE_BYTES (4096); echo is a label, not terminal state, and says so when a prompt asks for silence. No deadline but the stream you inject and the session Context between prompts. |
RateLimit |
new RateLimit($statePath, $burst = 5, $ratePerSec = 0.5, $stderr, $failClosed = false, $userBurst = null, $userRatePerSec = null) |
$failClosed = false: a lock or decode problem lets the connection through with a notice. State serializes on a <state>.lock sidecar; idle refilled buckets are pruned; $userBurst adds a per-username bucket. |
AuthRateLimitBridge |
new AuthRateLimitBridge($gate, $rateLimit, $penalty = 3.0) |
Refusals by $gate charge $rateLimit $penalty extra tokens (detection: the gate did not continue the chain). Synchronous gates only — an async gate draws a LogicException at handle(); $penalty <= 0 at construction. |
Spawn |
new Spawn($factory) where fn (Session): array{cmd: list<string>, env?: array} returns the child command |
Terminal. The factory's cmd is argv — no shell is involved, and nothing here vetoes it. |
BubbleTea |
new BubbleTea($factory) where fn (Session): object builds the Program |
The returned object must expose a run() method — anything else raises RuntimeException with the Lang key bubbletea.bad_factory when the middleware handles the session. Mounts a Program on the supervisor's own stdio, which is only sound under HostSshdTransport. Under the default InProcessTransport it throws bubbletea.requires_host_sshd — there is no override flag; the alternatives are Spawn plus a wrapper script (examples/run-program.php) or the legacy transport. |
Subsystem |
new Subsystem() then ->register($name, $handler) |
Terminal for registered names. An unregistered name falls through on purpose, so subsystem sftp reaches Spawn and lands in a shell unless you register the name (or a rejecting handler). |
AsyncMiddleware |
abstract; subclass and implement protected handleAsync($ctx, $session, $next): PromiseInterface |
The transport's single settle point drives your promise to completion — never call PromiseAwait::settle() yourself, and never await from inside a loop you already run. |
See Defaults that are open until you close them for the four that most often ship open by accident.
Defaults that are open until you close them
Four shipped defaults admit traffic. Each is a deliberate choice — the
alternative is a middleware that locks everyone out when you forget an
argument — but none of them is a security gate you can rely on by
accident. tests/SecurityDefaultsTest.php runs all four against the
table below.
| Middleware | Default that is open | What closes it |
|---|---|---|
Auth |
new Auth() has no user list and no fingerprint list, so every session passes. |
Pass users, keyFingerprints, or the pairing map userKeys (see below). |
KeyboardInteractive |
No $validate callable means any answer authenticates; the exchange is a form, not a check. |
Pass a validator. There is no built-in PAM/password backend in this library. |
RateLimit |
failClosed is false, so an unreadable or locked state file lets the connection through with a notice. |
Construct with failClosed: true to deny when state cannot be read. |
Server |
serve() runs exactly the middleware you use() — no auth is inserted for you — and a stack that runs out of middleware without spawning answers 0. |
->use(new Auth(...))->use(new RateLimit(...)), then exit($server->serve()): a gate that refuses now answers Server::EXIT_DENIED (129), a child reports its own code, and 0 means there was nothing to run (full precedence in The exit contract). |
The docblocks on those four classes say the same thing in longer words, and
docs/admin/sugar-ssh-server.md describes the deployment posture. If you
skim only this README, skim this table.
Sinks: who actually reads what you write
There is no server process to log into. The code runs as sshd's
forced command, so the streams it inherits belong to the session:
under ForceCommand, fd 1 is the client's data channel and fd 2 is
the client's extended-data channel — OpenSSH forwards both to the
person who just connected. Anything written there is user-facing,
on a path that may not even be authenticated yet. journald captures
what sshd itself writes, never what its child writes.
Consequences worth knowing before you deploy:
Loggerwith no target appends to$SSH_SERVER_LOGif you set it (Logger::auto()), and falls back to stderr otherwise. A live panel wants the file branch orLogger::toFile()/Logger::toStream();target()returns which one you got, so a boot line can say it out loud. Files this class creates are0600— the records carry usernames and client addresses.Auth,RateLimit,KeyboardInteractiveandCertificateAuth(the last two underMiddleware\Auth\) take a$stderrstream. Left alone, their notices (including "fail-open, allowing") reach the prober. Inject a real operator sink there; the messages themselves carry no paths and no posture detail by design.AuthMethodsrecords its declared methods in the Context and prints nothing. The optionalSSH_AUTH_METHODSline (announce: true) is the one message here that is meant for the client — though a real client never parses it:sshdcompleted the auth exchange before this process started, so the honest surface for that message is sshd's ownBanner=/MOTD, not your forced command.
For the fail-open/fail-closed posture of every gate, see
Authentication: pairing a key to a name
and docs/admin/sugar-ssh-server.md.
What sshd actually hands your middleware
There is no protocol conversation to inspect: the child learns about the connection from its environment. The environment's contribution is shorter than people expect, and who fills each slot changes what you may trust in it.
| Variable | Who sets it | Read by | Trust |
|---|---|---|---|
USER, LOGNAME |
sshd, for the target account | Session::fromEnvironment() |
Good — sshd writes it for the account it launched; which account to ask for was the client's choice. |
SSH_CONNECTION |
sshd: client-ip client-port server-ip server-port |
Session::fromEnvironment() |
Good — socket-derived, not peer-asserted. The socket is the direct TCP peer: behind a bastion or ProxyJump this is the proxy's address, which is exactly what RateLimit keys on (see Rate limiting keys on the direct TCP peer). |
SSH_CLIENT |
sshd: client-ip client-port server-port (older, 3 fields) |
Session::fromEnvironment() fallback |
Good, but it has no server-IP slot: fromEnvironment() counts the fields and leaves $serverHost empty rather than storing the port in it. |
SSH_ORIGINAL_COMMAND |
sshd, when ForceCommand rewrote command ssh host some command |
Session::$command, then your decoder |
Good — but see the exec policy: the program string is peer-chosen. |
SSH_TTY |
sshd when a PTY was allocated | Session::$tty |
Good. |
TERM |
sshd, from the client's pty-request | Session::$term |
Peer-asserted. Shape it before it reaches a terminfo lookup. |
LANG, LC_* |
only if the client sends them and AcceptEnv allows it |
Session::$lang (LANG only) |
Peer-asserted, for LANG alone. LC_* reach nothing in this library: they matter solely to sugar-core's T::detect(), which nothing here calls. This is how a visitor picks your UI language; see the i18n note below. |
COLUMNS, LINES |
not by sshd — the window size lives on the tty (the transport queries the tty size via ioctl, or accepts withSizeProvider()) |
Session::$cols/$rows |
Whatever the transport measured — with one leak: a peer that smuggles them through AcceptEnv reaches Session::fromEnvironment(), which seeds declaredSize from the env value (80/24 defaults, and values above 4096×4096 are clamped). |
SSH_USER_AUTH |
sshd only with ExposeAuthInfo (see sshd_config(5) on your release): the authorized_keys line that matched |
private Auth::fingerprint() |
Good — server-side, since sshd picked the line. |
SSH_USER_KEY_FINGERPRINT |
not sshd; a wrapper that parses SSH_USER_AUTH, or your own AuthorizedKeysCommand |
private Auth::fingerprint() |
Depends entirely on your wrapper. |
KEY_FINGERPRINT |
same producers — the shorter alternate name the same gate reads | private Auth::fingerprint() |
Depends entirely on your wrapper. |
SSH_PASSWORD |
not set by this repo; whether sshd's ForceCommand path exports it is unverified against OpenSSH | PasswordAuth |
Operator-injected out of band, or peer-asserted through AcceptEnv; neither is a credential proof by itself. |
SSL_CLIENT_CERT, SSH_CLIENT_CERT, CERTIFICATE |
nothing; the first is Apache mod_ssl, the others are aspirational | CertificateAuth::ENV_KEYS |
If your wrapper does not export one, the gate sees nothing. |
SSH_CLIENT_VERSION, SSH_SERVER_VERSION |
nothing in stock OpenSSH | Session::$clientVersion/$serverVersion |
Usually empty; do not treat them as audit columns. |
Two consequences deserve the front of your mind:
- Pairing without delivery is a lockout — for real fingerprint lists.
Auth'suserKeysmap (and a non-emptykeyFingerprintslist) rejects a session that presents no fingerprint. WithExposeAuthInfooff, no fingerprint arrives, so every login against such an entry is refused. The two ways out are not equivalent: emptying the key lists disarms pairing altogether (name-only authentication, with the cross-product footgun above), while an[Auth::ANY_KEY]entry says honestly "any key for this name" — there is no key evidence to check, so it passes with or without one, for that name only. Turn the delivery on, or pair withANY_KEYwhere no fingerprint can arrive, and prove the shape with assh -vround trip before you enable it on a live account. - Peer-asserted slots are input, not fact. Anything that arrived via
AcceptEnvis a string the client chose. The middleware in this library never reads anAcceptEnvname as an authority;Authuseshash_equals()on fingerprints, and the strings that do land are run through a private sanitizer (public cap:Auth::MAX_ECHOED_BYTES) before they are echoed anywhere.
Session::fromEnvironment() takes no arguments and reads the process
environment; nothing here offers a language-selection API yet, so
Session::$lang is today whatever LANG happened to be.
Authentication: pairing a key to a name
Auth takes three allowlist surfaces, and they answer different
questions:
use SugarCraft\SshServer\Middleware\Auth; // WRONG for a multi-user box: two independent flat lists. Every user // listed in `users` may log in with ANY key listed in // `keyFingerprints` — bob with alice's key is admitted AS alice. new Auth(users: ['alice', 'bob'], keyFingerprints: ['SHA256:aaa', 'SHA256:bbb']); // RIGHT: the pairing map. Each name owns its own keys, and membership // is checked with hash_equals() over the whole list. new Auth(userKeys: [ 'alice' => ['SHA256:aaa'], 'bob' => ['SHA256:bbb'], ]); // A name with no key evidence in this process (the host sshd did the // crypto and you are only restricting WHO may use the forced command): new Auth(userKeys: ['alice' => [Auth::ANY_KEY], 'bob' => ['SHA256:bbb']]);
With userKeys non-empty it is authoritative: a name absent from the
map is refused regardless of the key it presents, and a key listed under a
different name never admits you. users and keyFingerprints, when also
supplied, apply as additional gates — combining surfaces can only
narrow, never widen.
Every list is opt-in by being non-empty. new Auth() — no arguments —
performs no check at all and admits every session; that is the
start-simple default, not a deny-by-default gate. See the admin guide
(docs/admin/sugar-ssh-server.md) for the deployment posture.
Async middleware
Middleware handle() may return void (synchronous) or a
\React\Promise\PromiseInterface. The transport waits for the promise
to settle before continuing the chain, enabling async back-ends like
LDAP, OAuth, or database authentication.
Extend SugarCraft\SshServer\Middleware\AsyncMiddleware to implement async
middleware. Override handleAsync() to perform async work and return
a promise; resolve the promise (or let it reject) to control whether
the chain continues.
use SugarCraft\SshServer\Context; use SugarCraft\SshServer\Middleware\AsyncMiddleware; use SugarCraft\SshServer\Session; use React\Promise\PromiseInterface; final class LdapAuth extends AsyncMiddleware { public function __construct(private readonly \LdapConnection $ldap) { } protected function handleAsync(Context $ctx, Session $session, callable $next): PromiseInterface { return $this->ldap->verify($session->user)->then( fn () => $next($ctx, $session), fn (\Throwable $e) => throw new \RuntimeException('auth failed: ' . $e->getMessage(), 0, $e), ); } }
Pass it in when you build the chain: ->use(new LdapAuth($connection)).
The promise returned by handleAsync() resolves when async work is
done and the chain should proceed to $next; rejects to short-circuit
the chain. The transport's single settle point
(Transport\DispatchesMiddlewareStack::dispatch() →
PromiseAwait::settle()) drives the loop until that promise is decided,
then hands control back: the answer, or the 30-second ceiling, ends the
wait — whichever comes first.
Who owns the loop. settle() calls run() and stops it as soon as
the await is decided, so exactly one thing drives the loop per phase.
The transports shipped here build the chain synchronously, so nothing is
running underneath and awaiting is safe. If you run an event loop in
the same process, do not call settle() (or dispatch a stack containing
async middleware) from inside a loop callback — the stop would end your
loop. Read PromiseAwait's class docblock before wiring a custom
transport.
Language: nothing selects it for you
Sixteen catalogues ship under lang/, and by default none of them is
selected. Nothing in this library calls T::setLocale() implicitly —
the one shipped door is Server::withLocale(), which applies it at
serve() entry when you arm it. An unarmed server renders every notice —
the throttle's fail-open announcement, the exec refusal, the
Unauthorized. (…) line — from lang/en.php. Choosing a language is a
one-line decision at the top of your entry script, through either door:
use SugarCraft\Core\I18n\T; $server = Server::new()->withLocale('de'); // applied at every serve() // or, without the Server door: T::setLocale('de'); // once, before building the Server
Prefer pinning it per service to detecting it. T::detect() reads
LC_ALL, LC_MESSAGES and LANG, and LANG only reaches a ForceCommand
child when sshd's AcceptEnv lists it (What sshd actually hands your
middleware) — so detection lets
the connecting peer choose the language your server answers in, on top of the
name it already chose. Session::$lang is that same peer-asserted
value: log it, do not obey it.
Coverage is uneven, and the suite keeps the list honest rather than letting
it grow quietly: eleven of the thirty-five keys are still English placeholders
in all fifteen translated locales (ACCEPTED_UNTRANSLATED in
tests/LangRosterTest.php; curing one means deleting it from the roster).
Session metadata
Session::fromEnvironment() reads the standard sshd-supplied environment:
$s->user; // 'alice' $s->clientHost; // '203.0.113.7' $s->clientPort; // 54321 $s->term; // 'xterm-256color' $s->cols; // 120 $s->rows; // 40 $s->tty; // '/dev/pts/3' (null when non-interactive) $s->command; // SSH_ORIGINAL_COMMAND if set -- what the peer asked to run, // which ForceCommand ignores unless you decode it // (see the Exec policy section below) $s->serverHost; // '198.51.100.2' -- empty on the 3-field SSH_CLIENT fallback, // which carries a server port and no server address $s->serverPort; // 22 $s->lang; // 'en_US.UTF-8' -- the peer's declared LANG; log it, never // obey it (see [Language](#language-nothing-selects-it-for-you)) $s->isInteractive(); $s->toLogContext();
$s->toLogContext() is what the bundled Logger writes, and it carries nine
fields: the user, client address (host:port), term, tty, pty size
(cols x rows) and command, plus session, auth_method
and key_fingerprint (a dash stands for "not carried", so a line never loses
the authentication column silently).
There is no handshake for those to come from — this library speaks no SSH — so
withProtocolMetadata() is called by the transport on the way into the
stack, with values derived locally — and only by InProcessTransport::run():
under HostSshdTransport nothing ever calls it, so the session,
auth_method and key_fingerprint columns are permanently - there:
$s->sessionId; // minted per process: bin2hex(random_bytes(16)), not a wire id $s->authMethod; // sniffed from SSH_USER_AUTH when a wrapper exported it $s->keyFingerprint; // no producer in this library today — see the pairing note below $s->clientVersion; // read from SSH_CLIENT_VERSION: stock OpenSSH exports neither $s->serverVersion; // ...so both are usually empty and are not audit columns // Build a new Session with protocol metadata attached $s = $s->withProtocolMetadata( sessionId: $sessionId, authMethod: $authMethod, keyFingerprint: $keyFingerprint, clientVersion: $clientVersion, serverVersion: $serverVersion, );
Because sessionId is minted here, it ties one process's log lines
to each other and nothing more: it will not match a sshd log line, a
bastion's record, or the same user's next login. Treat the two version fields
the same way — an empty one is the normal case, and a filled one means your
wrapper exported it (see What sshd actually hands your
middleware).
The exit contract: refuse, complete, and 129
Server::EXIT_DENIED is not a docblock rumor — it is the other half of a
two-method contract every bundled gate keeps, and it is worth writing the
same way yourself. The stack can end without anything running in two
different ways, and they are different answers to the process exit; it is
middleware's job to keep them distinguishable. A gate refuses by marking
the session and not calling $next:
public function handle(Context $ctx, Session $session, callable $next): void { if (!$this->admits($session)) { $session->markRefused(); // the exit code carries the refusal return; // not calling $next IS the refusal } $next($ctx, $session); }
Auth, PasswordAuth, KeyboardInteractive, CertificateAuth,
RateLimit, the transport-side DispatchesMiddlewareStack and — for refused
exec requests — DefaultChannelHandler all do exactly this — mark once at
the deny site, then write the human-facing notice wherever it can actually be
seen. markRefused() is a one-way
latch: a session that has been refused stays refused.
Both transports read the outcome through one resolver,
Server::resolveStatus(Session): int, so refusal and completion cannot
drift apart by transport:
| Session state | Resolved status | Meaning |
|---|---|---|
exitStatus() non-null |
that number | a child actually ran — its code is the most specific truth available, and it wins |
refused(), no recorded child status |
Server::EXIT_DENIED (129) |
a gate stopped this session; nothing ran |
| neither | 0 |
the walk finished with nothing to spawn — that is success |
exitStatus() stays null until a spawner calls recordExitStatus();
that pair belongs to transports, not to gates. Resizes travel another
door entirely: withSize(int $cols, int $rows): self returns a new
Session carrying fresh dimensions, which DefaultChannelHandler swaps
into the walk on each frame. A ForceCommand script hands the resolved
number to exit() — see the exit(...) paragraph under Write the entry
script.
Context propagation
Every request starts with a root Context created by Context::background().
The context is immutable — each with*() method returns a new derived
context that forms a parent chain. Middleware can attach key-value metadata
via withValue(), set a deadline via withDeadline(), or make the context
cancelable via withCancelable(). The terminal middleware (Spawn /
BubbleTea) never call $next, short-circuiting the chain. A context bounds
the machinery cooperatively — between dispatch hops, at await entry, at the
in-process pump's loop head and idle ticks, and between interactive-auth
prompts — never by preempting a blocking read, and never as a leash on a
ForceCommand child's lifetime under sshd (that is SIGHUP's job).
| Context method | What it does |
|---|---|
Context::background() |
Root context — never done, no values, not cancelable |
->withValue(string $k, mixed $v) |
Return a new context with $k → $v attached |
->withDeadline(\DateTimeImmutable) |
Return a new cancelable context that is done when the deadline passes |
->withCancelable() |
Return a new cancelable context (no deadline; must call ->cancel() explicitly) |
->cancel(?\Throwable $reason) |
Mark the context (and all derived contexts) as cancelled; returns whether the signal took effect |
->done() |
Returns true when cancelled or deadline-exceeded |
->err() |
Returns the cancellation error or DeadlineExceededException / CancellationException |
->deadline() |
Returns the earliest deadline in the chain, or null when nothing is armed |
->value(string $k) |
Walk the parent chain looking for $k; returns null if not found |
use SugarCraft\SshServer\Context; // Derive a cancelable context with a 30-second deadline $ctx = Context::background() ->withValue('requestId', $uuid) ->withDeadline(new \DateTimeImmutable('+30 seconds')); if ($ctx->done()) { throw $ctx->err(); // DeadlineExceededException or CancellationException }
Embedding this library
Four constraints that are invisible from the class list, learned from the audit, and worth knowing before you wire this into a long-lived process.
One session per process. Session::fromEnvironment() reads
getenv()/$_SERVER, and that is where the peer's identity, the tty and any
injected credential live. Nothing in the stack carries those values through a
parameter, so a second concurrent session inside the same PHP process would
authenticate against the first one's environment. Run one process per
connection — which is what a ForceCommand line gives you for free — and
do not put this behind a worker pool that reuses a process across peers.
Geometry arrives from SIGWINCH, or from you. runChild() reads the
size once from the stream it is given and then follows SIGWINCH. There is
no live-resize call to make: if a resize reaches your process some other way
(a control message, a panel tick), supply a provider with
withSizeProvider(callable $provider): self and it will be polled at the
same points the signal path uses. The provider is consulted per pump idle
tick, not cached, so a stale closure is a live bug rather than a frozen one.
The pump's knobs are not reachable from here. runChild() builds
PumpOptions::sshDefault()->withOnIdle($onIdle) internally. sugar-pty's
options carry the real recorder, chunk size and withPumpDeadlineUs() — the
only true bound a pump can honor — and this transport never exposes them,
so a session that needs one has to fork the pump loop itself. That is why the
admin guide's failure table says a hanging child hangs here, and why the
Context::withDeadline() in the section above is a value you carry, not an
enforcement: done() is consulted when the stack walks in, never inside the
byte loop.
ChildSpawner is narrower than the class behind it. The complete
interface Spawn type-hints is two methods:
public function runChild(Session $session, array $cmd, ?array $env = null): int; public function signalChild(int $signal): void;
signalChild() is load-bearing, not an afterthought: every mapped
SignalMsg the default channel handler receives — INT, TERM, HUP, QUIT,
KILL, USR1, USR2 and the SIGWINCH resize path — is forwarded to the live
child through this one call (DefaultChannelHandler::handleSignal).
It is a no-op when no child is running, and delivery must not depend on
ext-posix — sugar-pty's Libc::kill() falls back to libc kill(2) over
FFI. A custom spawner written from only the runChild signature does not
fail at spawn time; it fails at class load with a missing-method fatal,
so copy both.
Peer signals are pid-scoped — RFC 4254 signals name a process — but the teardown ladder is group-scoped: the spawned command is a session leader, so its final SIGKILL sweeps the child's whole process group, backgrounded descendants included. The documented boundary is a leader that itself died of the ladder's SIGHUP: by the time the escalation runs, survivors of its group are no longer ours to signal.
InProcessTransport::runChild() accepts two more arguments — $stdin and
$stdout — and they are what make a testable, stream-injecting middleware
possible. Code holding the interface cannot pass them, so anything that wants
to redirect the child's streams must instanceof InProcessTransport and
narrow, or take the concrete class in its own constructor. Widening the
contract is a deliberate no for now: the fake in SpawnTest implements the
three-parameter form, and the interface exists so that fake needs no PTY.
TransportAware is the other half of the same story — the transport calls
setTransport() on every aware middleware at walk time, before the first
handle(), and implementations must tolerate being called twice because a
Server object is reused across sessions.
Exceptions
| Exception | When it's thrown |
|---|---|
CancellationException |
Never thrown: Context->cancel() records it and err() hands it back — after a bare cancel(), or whenever done() is true with no stored reason |
DeadlineExceededException |
The context deadline (withDeadline()) has passed |
Both extend \RuntimeException.
ext-ssh2
Nothing in this library uses the PECL ssh2 extension, and composer.json
says so: it suggests ext-ffi (which sugar-pty needs to allocate the PTY at
all — sugar-pty's own manifest still hard-requires it, so install-time
enforcement survives transitively) and ext-pcntl (which the default
transport needs to fork and reap the session child), and lists no
ext-ssh2 — an earlier revision
suggested it for a "Ssh2 client middleware (forwarding, sftp helpers)" that
was never built. There is no outbound client here, no port forwarding, and no
SFTP server: SftpStub is a wiring example, quarantined only by its own docblock, and the
subsystem middleware only routes a named subsystem to whatever handler you
register. Subsystem middleware
records what that does and does not buy you.
ssh2 becomes relevant only if your middleware opens an outbound SSH
session from inside one (an SFTP file picker, a remote-control agent). That is
ordinary application code, with its own trust questions: the peer chose the
target, so treat it as input, not as configuration.
Channel handler (InProcessTransport)
The InProcessTransport dispatches SSH channel-level messages through a
ChannelHandler rather than handling them inline. This lets you replace the
default PTY/shell wiring with a custom implementation. To override only
some verbs, extend DelegatingChannelHandler: hand it a delegate (e.g.
new DefaultChannelHandler()), override just the verb you care about, and
every other member — cols()/rows() included — forwards verbatim, so
DefaultChannelHandler itself stays final.
Which messages actually arrive. The library mints exactly one
channel message itself: WindowChangeMsg, from a SIGWINCH observed while
pumping. pty-req, shell, exec, env, signal and break are not fed from the
wire — this transport sits behind sshd's ForceCommand, so sshd has
already made the shell/exec/subsystem choice before your script starts, and the
protocol-level request never reaches PHP. Those six classes exist for embedders
that dispatch them (a test, or your own decoder for a real channel layer), and
DefaultChannelHandler is a live object with a dormant input. Do not plan on
handleExec() running for a client that typed ssh host some-command: that
request reaches you as the SSH_ORIGINAL_COMMAND environment variable inside
the forced script, and Session::$command is the field that carries it.
| Class | Purpose |
|---|---|
ChannelHandler |
Interface — implement to handle pty-req, window-change, shell, exec, signal, env, break |
ChannelMsg |
Abstract base for all channel messages (RFC 4254) |
DefaultChannelHandler |
Default impl — tracks PTY state, env vars, cols/rows, drives ChildSpawner on shell/exec; exec is default-closed ($allowedExec + $execGate) |
PtyReqMsg |
wantPty, term, cols, rows, widthPx, heightPx |
WindowChangeMsg |
cols, rows, widthPx, heightPx |
ShellMsg |
wantShell, subsystem |
ExecMsg |
command (raw string — decoded by the public DefaultChannelHandler::decodeCommandString(), the same entry point to use for SSH_ORIGINAL_COMMAND) |
SignalMsg |
signalName |
EnvMsg |
name, value |
BreakMsg |
Break request — carries breakLengthMs (int, defaults to 0 when the wire frame omits a length) |
Exec policy: the program comes from the peer
An exec request names the binary the server should run, so handleExec()
spawns nothing unless the operator has said otherwise — two conditions, both
required:
$handler = new DefaultChannelHandler( spawner: $transport, session: $session, allowedExec: ['/bin/ls', '/usr/bin/rsync'], // exact argv[0]; '' = refuse all execGate: function (Session $s, array $argv): bool { return $s->user === 'backup'; // argument/user policy, optional }, stderr: $noticeSink, // default php://stderr = the CLIENT );
$allowedExec matches the whole path, never a prefix (/bin/ls does not admit
/bin/lsblk). $execGate must return exactly true; a gate that throws, or
returns a truthy non-bool, refuses — a policy that cannot answer has not given
permission; a gate that is not callable at all is refused by the engine at
construction. Refusals are announced with one line naming the requested program
and nothing about the allowlist behind it, and the program name is scrubbed of
control characters first (it is peer-chosen text). Note the sink default: under
ForceCommand a notice on stderr travels to the connecting client, not to your
journal — see Sinks: who actually reads what you write.
There is no shell in this path. decodeCommandString() is a quoting tokenizer
(single/double quotes, backslash escapes, empty quoted args preserved), so
;, $(…), backticks, $HOME and * arrive as inert argv tokens rather than
as commands or expansions — the property the allowlist rests on. Empty or
whitespace-only requests decode to [] and are not announced at all.
Shell requests are deliberately not gated, because they name no program; the interpreter they get resolves per the note below.
The denylist that makes that sentence true is DANGEROUS_VARS, and it is
applied even to names an operator put in $acceptEnv: the loader hooks
(LD_PRELOAD, LD_LIBRARY_PATH, LD_AUDIT, and the two macOS spellings
DYLD_INSERT_LIBRARIES and DYLD_LIBRARY_PATH), the
locale-conversion path (GCONV_PATH), the files shells source before running
anything (BASH_ENV, ENV) and the code bash runs at each prompt
(PROMPT_COMMAND), plus IFS, PATH, SHELL, HOSTALIASES and
POSIXLY_CORRECT. The rule for the list is that a value must not be able to
decide what code the child runs, which library it loads, or where it looks for
one — that is the server configuring its own child, not the peer describing
its terminal. Two exceptions are honest ones: the child's PATH comes from
the daemon (a search-less child cannot find /bin/sh), and HOME is derived
as /home/<user> rather than read from the passwd database, so a deployment
whose homes differ passes HOME itself — and when a session carries no user
name at all the floor omits HOME rather than inventing /home/ for nobody.
Peer-supplied entries are also bounded where they are stored: at most
ENV_MAX_ENTRIES (64) distinct names, names of ENV_MAX_NAME_BYTES (256)
bytes, values of ENV_MAX_VALUE_BYTES (4 KiB); anything past that is dropped
with a counted drop and a one-time notice on the refusal stream.
Replacing the handler replaces the policy. ChannelHandler is an interface, so
a custom implementation of handleExec() is bound by nothing here: if you
substitute one, the default-closed question — may this peer run this program? —
becomes yours to answer inside your method.
On a shell request, DefaultChannelHandler resolves the login shell in this
order: the explicit $shell configured at construction time, then the
server-side SHELL environment variable (under sshd that is the authenticated
user's login shell), then FALLBACK_SHELL (/bin/sh). The spawn is always
<shell> -l. A client-supplied SHELL env var can never select the
interpreter — SHELL is stripped from the child environment as a
dangerous variable, and resolution reads only the server's own configuration.
use SugarCraft\SshServer\Channel\ChannelHandler; use SugarCraft\SshServer\Channel\ChannelMsg; use SugarCraft\SshServer\Channel\Msg\PtyReqMsg; use SugarCraft\SshServer\Channel\Msg\WindowChangeMsg; use SugarCraft\SshServer\Channel\Msg\ShellMsg; use SugarCraft\SshServer\Channel\Msg\ExecMsg; use SugarCraft\SshServer\Channel\Msg\SignalMsg; use SugarCraft\SshServer\Channel\Msg\EnvMsg; use SugarCraft\SshServer\Channel\Msg\BreakMsg; use SugarCraft\SshServer\Session; use SugarCraft\SshServer\Transport\InProcessTransport; // WARNING (read back to the client): under ForceCommand STDERR *is* the // client's extended-data channel, so every fwrite below — including the // peer-controlled `exec:` command and raw `env:` values, written here // unscrubbed — lands on the terminal of whoever just connected. This is // a debug specimen: scrub through Logger sinks instead of raw writes, // and never ship this handler to production. final class DebugChannelHandler implements ChannelHandler { private int $cols = 80; private int $rows = 24; public function cols(): int { return $this->cols; } public function rows(): int { return $this->rows; } public function handlePtyReq(PtyReqMsg $msg, Session $session): void { $this->cols = $msg->cols; $this->rows = $msg->rows; fwrite(STDERR, "pty-req: wantPty={$msg->wantPty} cols={$msg->cols} rows={$msg->rows}\n"); } public function handleWindowChange(WindowChangeMsg $msg, Session $session): void { fwrite(STDERR, "window-change: cols={$msg->cols} rows={$msg->rows}\n"); } public function handleShell(ShellMsg $msg, Session $session): void { fwrite(STDERR, "shell: wantShell={$msg->wantShell}\n"); } public function handleExec(ExecMsg $msg, Session $session): void { // Implementing the interface means implementing the policy: the // default-closed allowlist below belongs to DefaultChannelHandler, // and nothing inherits it into this custom handler. fwrite(STDERR, "exec: {$msg->command}\n"); } public function handleSignal(SignalMsg $msg, Session $session): void { fwrite(STDERR, "signal: {$msg->signalName}\n"); } public function handleEnv(EnvMsg $msg, Session $session): void { fwrite(STDERR, "env: {$msg->name}={$msg->value}\n"); } public function handleBreak(BreakMsg $msg, Session $session): void { fwrite(STDERR, "break\n"); } } // Hand the handler to InProcessTransport (handler seam is a setter — // the constructor takes only an optional PtySystem, and defaults it). $transport = new InProcessTransport(); $transport->setChannelHandler(new DebugChannelHandler());
Subsystem middleware (InProcessTransport)
SSH clients can request a named subsystem by sending subsystem <name> as
the original command. The Subsystem middleware parses this prefix, looks up
a registered handler, invokes it, and stops the chain — subsystem handlers
are terminal by design.
| Class | Purpose |
|---|---|
Subsystem |
Middleware — parses subsystem <name>, dispatches to registered handler |
SubsystemHandler |
Interface — implement handle(Context, Session): void for a named subsystem |
SftpStub |
Example impl — stub demonstrating wiring; not a real SFTP server |
use SugarCraft\SshServer\Middleware\Subsystem; use SugarCraft\SshServer\Middleware\Subsystem\SftpStub; $subsystem = new Subsystem(); $subsystem->register('sftp', new SftpStub()); exit(Server::new() ->use(Logger::toFile('/var/log/wish.jsonl')) // Flat users list = name-only; sshd remains the credential gate — // see 'Defaults that are open until you close them' before shipping. ->use(new Auth(['alice', 'bob'])) ->use($subsystem) // handles subsystem sftp; others pass through to Spawn ->use(new Spawn(fn (Session $s) => ['cmd' => ['/bin/bash', '-l']])) ->serve());
A production SFTP implementation would implement SubsystemHandler to speak
the SFTP protocol over the session's stdin/stdout after Subsystem
extracts the name and dispatches.
Status
🟢 Context propagation and the middleware suite (logging, auth suite,
keepalive, rate limit, spawn, subsystem, async bridge, BubbleTea) are complete,
and the ChannelHandler/ChannelMsg types plus the 7 message classes ship as
a dispatch API. That API is honest about its input: only WindowChangeMsg is
produced by this library today, so the other six messages are dormant unless
your own code dispatches them (see
Channel handler). The wire decoder that
would feed them belongs to a transport that speaks the SSH protocol itself, and
this library deliberately does not — see
Runtime requirements.
Two upstream siblings go unmentioned because they were considered and
dropped, not missed: wish/agent (agent-channel forwarding) is a wire
protocol owned by the host sshd — this library speaks no SSH — and
wish/transfer (scp/sftp transfer helpers) has nothing to forward: scp
arrives as a plain exec request and sftp as a subsystem request, both
already covered by Spawn, Subsystem and the channel handler wherever
policy allows them.
Runnable examples:
examples/hello-server.php— banner-only stack you can ForceCommand against.examples/showcase.php— middleware-stack tour.examples/spawn-bash.php— interactive bash throughSpawn.examples/spawn-program.php— arbitrary child program throughSpawn.examples/run-program.php— the wrapperSpawnruns to mount aProgramon its own PTY process.
Credits & inspiration
Originally inspired by the Go Charm ecosystem; SugarCraft is developed as a native PHP project.
Used by within the monorepo: sugar-metrics carries
sugarcraft/sugar-ssh-server as a hard require in its manifest.