ssh: add generic request callbacks - #1236
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
There are confirmed correctness/security issues around silently truncated request names/types being passed to the new generic callbacks (and a readiness-state bug in wolfSSH_SFTP_accept() under app-driven mode) that should be addressed before approval.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR adds “generic” (any-name) callbacks for SSH channel requests and global requests, enabling centralized policy decisions (accept/reject/unhandled) before the existing type-specific callbacks and built-in handling. It also introduces an application-driven channel mode that stops wolfSSH_accept() after authentication so applications can drive channel open/request handling via wolfSSH_worker(), with accompanying agent-channel and SFTP adjustments plus expanded unit/regression coverage.
Changes:
- Add
wolfSSH_CTX_SetChannelReqCb()andwolfSSH_CTX_SetGlobalReqCb()to allow first-pass policy decisions for all channel/global requests. - Add
wolfSSH_{CTX_,}SetAppChannels()and update accept/worker flows to support application-driven channel handling. - Add
wolfSSH_AGENT_ChannelOpen()helper and extend tests (unit.c,regress.c) to validate callback interactions and app-driven semantics.
File summaries
| File | Description |
|---|---|
| wolfssh/ssh.h | Public API additions: generic request callback types, tri-state result enum, and app-driven channel mode API/docs. |
| wolfssh/internal.h | Extend WOLFSSH_CTX/WOLFSSH with generic callback pointers and appChannels flags. |
| wolfssh/agent.h | Add public API for server-side agent channel opening in app-driven mode. |
| src/ssh.c | Implement new setters and modify wolfSSH_accept() to support stopping at authenticated state when app-driven. |
| src/internal.c | Route channel/global requests through new generic callbacks; refactor session request handling. |
| src/wolfsftp.c | Adjust SFTP accept logic to account for app-driven accept state behavior. |
| src/agent.c | Implement wolfSSH_AGENT_ChannelOpen() and refactor agent channel open path. |
| tests/unit.c | Add unit coverage around session-request rejection and app-driven “no default callback acceptance” behavior. |
| tests/regress.c | Add regression tests for generic callbacks, app-driven accept stopping point, and callback bypass rules. |
Review details
- Files reviewed: 9/9 changed files
- Comments generated: 3
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| if (!DoGlobalRequestAny(ssh, name, globReqId, buf, len, begin, | ||
| wantReply, &ret)) { | ||
| switch (globReqId) { |
| word32 channelId; | ||
| word32 typeSz; | ||
| char type[32]; | ||
| byte wantReply; | ||
| int ret, rej = 0; | ||
| int ret, rej = 0, granted = 0; |
| /* check accept is done, if not call wolfSSH accept. In | ||
| * application-driven mode accept() parks at ACCEPT_SERVER_USERAUTH_SENT | ||
| * and never advances, so that state counts as done here. */ | ||
| if (ssh->acceptState < ACCEPT_CLIENT_SESSION_ESTABLISHED | ||
| && !(ssh->appChannels | ||
| && ssh->acceptState >= ACCEPT_SERVER_USERAUTH_SENT)) { | ||
| byte name[] = "sftp"; |
d70cae4 to
0c27f30
Compare
A shell, exec or subsystem request changes the channel only once the callback accepts it. The session type and command are set for the callback to read and put back if it refuses, and CLIENT_DONE follows acceptance alone, so wolfSSH_accept() stays where it is rather than reporting a session it answered CHANNEL_FAILURE as established. - DoChannelRequestSession() carries the three arms, which differed only in the type and the callback consulted - a refusal puts the type and command back, so a grant an earlier request won still stands - FreeChannelCommand() wipes and releases a command line for both ChannelDelete() and the refusal path - unit.c drives a refused shell, exec and subsystem request through DoChannelRequest() and checks nothing was committed - regress.c checks accept() stays at ACCEPT_SERVER_CHANNEL_ACCEPT_SENT on a refused shell, and that neither divert runs Issue: F-8852
wolfSSH_CTX_SetChannelReqCb() and wolfSSH_CTX_SetGlobalReqCb() register a callback consulted first for every channel and global request, with the name and the type-specific part to parse. A tri-state answer grants, refuses, or leaves the request to the callbacks and handling already there, so a policy reaches the types with no hook of their own. - a grant still parses and records what the library needs, so a session request granted here commits the session, and the shell, exec and subsystem callbacks are not consulted - a type the library does not know is answered CHANNEL_SUCCESS on a grant - a granted port-0 tcpip-forward is refused, since only the forward callback can report the port bound, per RFC 4254 7.1 - regress.c covers the answers, the data delivered, and which callbacks each answer leaves out
0c27f30 to
aaa6148
Compare
wolfSSH_CTX_SetChannelReqCb()andwolfSSH_CTX_SetGlobalReqCb()register a callback consulted first for every channel and global request, with the name and the type-specific part to parse.