|
| 1 | +/** |
| 2 | + * Copyright 2026 GitProxy Contributors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * Thin abstraction over ssh2 internal APIs. |
| 19 | + * |
| 20 | + * ssh2 does not expose a public server-side API for opening an |
| 21 | + * `auth-agent@openssh.com` channel back to the connected client. To implement |
| 22 | + * SSH agent forwarding from the server side we reach into underscore-prefixed |
| 23 | + * internals (`_protocol`, `_chanMgr`, `_handlers`) and into the internal module |
| 24 | + * `ssh2/lib/Channel`. Those symbols have NO semver stability guarantee. |
| 25 | + * |
| 26 | + * This module is the ONLY place allowed to access those internals. Every access |
| 27 | + * is guarded and throws a descriptive, version-aware error when the shape of |
| 28 | + * the internals changes. If ssh2 is upgraded and something breaks, the error |
| 29 | + * will tell you which symbol disappeared and on which installed version. |
| 30 | + * |
| 31 | + * Verified working against ssh2 1.17.x. The package.json pin is `~1.17.0` |
| 32 | + * (patch-only) to force a manual review on minor/major bumps. |
| 33 | + */ |
| 34 | + |
| 35 | +import * as ssh2 from 'ssh2'; |
| 36 | + |
| 37 | +const ssh2Version: string = (() => { |
| 38 | + try { |
| 39 | + |
| 40 | + return require('ssh2/package.json').version; |
| 41 | + } catch { |
| 42 | + return 'unknown'; |
| 43 | + } |
| 44 | +})(); |
| 45 | + |
| 46 | +const VERIFIED_RANGE = '1.17.x'; |
| 47 | + |
| 48 | +function fail(detail: string): never { |
| 49 | + throw new Error( |
| 50 | + `ssh2 internal API changed: ${detail} ` + |
| 51 | + `(installed ssh2 version: ${ssh2Version}, verified working on ${VERIFIED_RANGE}). ` + |
| 52 | + `If you upgraded ssh2, review src/proxy/ssh/sshInternals.ts and pin to a known-working version.`, |
| 53 | + ); |
| 54 | +} |
| 55 | + |
| 56 | +export interface Ssh2Protocol { |
| 57 | + openssh_authAgent(localChan: number, maxWindow: number, packetSize: number): void; |
| 58 | + channelSuccess(channelId: number): void; |
| 59 | + _handlers: Record<string, (...args: unknown[]) => unknown>; |
| 60 | +} |
| 61 | + |
| 62 | +export interface Ssh2ChannelManager { |
| 63 | + _channels: Record<number, unknown>; |
| 64 | + _count: number; |
| 65 | +} |
| 66 | + |
| 67 | +export interface ChannelConstructor { |
| 68 | + new (client: unknown, info: unknown, opts: { server: boolean }): unknown; |
| 69 | +} |
| 70 | + |
| 71 | +export interface ChannelModule { |
| 72 | + Channel: ChannelConstructor; |
| 73 | + MAX_WINDOW: number; |
| 74 | + PACKET_SIZE: number; |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * Retrieve the internal `_protocol` object of an ssh2 Connection. |
| 79 | + * Throws a descriptive error if the internal shape is not as expected. |
| 80 | + */ |
| 81 | +export function getProtocol(client: ssh2.Connection): Ssh2Protocol { |
| 82 | + const proto = (client as unknown as { _protocol?: unknown })._protocol as |
| 83 | + | Partial<Ssh2Protocol> |
| 84 | + | undefined; |
| 85 | + if (!proto) fail('client._protocol is missing'); |
| 86 | + if (typeof proto.openssh_authAgent !== 'function') { |
| 87 | + fail('client._protocol.openssh_authAgent is missing or not a function'); |
| 88 | + } |
| 89 | + if (typeof proto.channelSuccess !== 'function') { |
| 90 | + fail('client._protocol.channelSuccess is missing or not a function'); |
| 91 | + } |
| 92 | + if (!proto._handlers || typeof proto._handlers !== 'object') { |
| 93 | + fail('client._protocol._handlers is missing or not an object'); |
| 94 | + } |
| 95 | + return proto as Ssh2Protocol; |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Retrieve the internal `_chanMgr` object of an ssh2 Connection. |
| 100 | + */ |
| 101 | +export function getChannelManager(client: ssh2.Connection): Ssh2ChannelManager { |
| 102 | + const chanMgr = (client as unknown as { _chanMgr?: unknown })._chanMgr as |
| 103 | + | Partial<Ssh2ChannelManager> |
| 104 | + | undefined; |
| 105 | + if (!chanMgr) fail('client._chanMgr is missing'); |
| 106 | + if (!chanMgr._channels || typeof chanMgr._channels !== 'object') { |
| 107 | + fail('client._chanMgr._channels is missing or not an object'); |
| 108 | + } |
| 109 | + if (typeof chanMgr._count !== 'number') { |
| 110 | + fail('client._chanMgr._count is missing or not a number'); |
| 111 | + } |
| 112 | + return chanMgr as Ssh2ChannelManager; |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * Find the first channel ID not currently in use by the given channel manager. |
| 117 | + * Starts scanning at `startId` (default 1; 0 is typically the main session). |
| 118 | + */ |
| 119 | +export function findAvailableChannelId(chanMgr: Ssh2ChannelManager, startId = 1): number { |
| 120 | + let id = startId; |
| 121 | + while (chanMgr._channels[id] !== undefined) id++; |
| 122 | + return id; |
| 123 | +} |
| 124 | + |
| 125 | +let cachedChannelModule: ChannelModule | null = null; |
| 126 | + |
| 127 | +/** |
| 128 | + * Load the internal `ssh2/lib/Channel` module and validate its exports. |
| 129 | + * The result is cached for subsequent calls. |
| 130 | + */ |
| 131 | +export function getChannelModule(): ChannelModule { |
| 132 | + if (cachedChannelModule) return cachedChannelModule; |
| 133 | + |
| 134 | + let mod: Partial<ChannelModule>; |
| 135 | + try { |
| 136 | + |
| 137 | + mod = require('ssh2/lib/Channel'); |
| 138 | + } catch (err) { |
| 139 | + fail(`cannot require('ssh2/lib/Channel'): ${err instanceof Error ? err.message : String(err)}`); |
| 140 | + } |
| 141 | + if (typeof mod.Channel !== 'function') fail('ssh2/lib/Channel does not export Channel'); |
| 142 | + if (typeof mod.MAX_WINDOW !== 'number') fail('ssh2/lib/Channel does not export MAX_WINDOW'); |
| 143 | + if (typeof mod.PACKET_SIZE !== 'number') fail('ssh2/lib/Channel does not export PACKET_SIZE'); |
| 144 | + |
| 145 | + cachedChannelModule = mod as ChannelModule; |
| 146 | + return cachedChannelModule; |
| 147 | +} |
| 148 | + |
| 149 | +/** |
| 150 | + * Extract the outgoing channel id from an ssh2 server session via its internal |
| 151 | + * `_chanInfo` property. Returns undefined if the info is not available yet. |
| 152 | + * Used to send a manual CHANNEL_SUCCESS when the client sets wantReply=false. |
| 153 | + */ |
| 154 | +export function getSessionOutgoingChannelId(session: unknown): number | undefined { |
| 155 | + const info = (session as { _chanInfo?: { outgoing?: { id?: unknown } } } | undefined)?._chanInfo; |
| 156 | + const id = info?.outgoing?.id; |
| 157 | + return typeof id === 'number' ? id : undefined; |
| 158 | +} |
| 159 | + |
| 160 | +/** |
| 161 | + * For tests: exposes the installed version string. |
| 162 | + */ |
| 163 | +export function getInstalledSsh2Version(): string { |
| 164 | + return ssh2Version; |
| 165 | +} |
0 commit comments