Files
archived-orca/src/shared/orcad-bind-address.ts
T
0b5a293fa2 feat(serve): add bind address option (#35095)
* feat(serve): add bind address option

* fix(serve): validate service bind address

* fix(serve): validate bind flag values

* fix(serve): reject scoped bind addresses

* docs(serve): document bind option paths

* docs(serve): note that --mobile-pairing needs an all-interfaces bind

* fix(serve): move bind address parsing to shared so the packaged CLI can load it

The packaged CLI ships out/cli and out/shared but not out/main/orcad, so
cli/runtime/serve-bind-flag.js failed the packaged runtime-import check
(and would fail to load) when it imported main/orcad/orcad-bind-address.
Move the module, unchanged, to src/shared and point orcad, Electron serve
and the CLI at it.

* test(serve): pin down the default listener and the --bind startup paths

- Extract the Electron serve listener pin into resolveServePinnedBindHost
  and test that serve without --bind stays unpinned (all interfaces, as
  before), an explicit --bind pins it from either argv form, and the orcad
  browser sidecar stays on loopback.
- Golden the orcad and Electron serve argv without --bind against the
  pre---bind output.
- Test that the CLI passes bind only when given and refuses --bind with
  --mobile-pairing before launching.
- Guard that serve-bind-flag and the shared serve validation load only CLI
  and shared modules, which is what the packaged CLI ships.

---------

Co-authored-by: lw <13579959+bertramlongwuyan@user.noreply.gitee.com>
Co-authored-by: Neil <neil@stably.ai>
Co-authored-by: xuanchangfeng <210147232+xuanchangfeng@users.noreply.github.com>
2026-11-06 15:47:42 -08:00

56 lines
1.9 KiB
TypeScript

// SSH forwarding needs only loopback; network exposure must be an explicit operator choice.
import { isIP } from 'node:net'
export const ORCAD_LOOPBACK_BIND_HOST = '127.0.0.1'
const ALL_INTERFACES_V4 = '0.0.0.0'
const ALL_INTERFACES_V6 = '::'
export class OrcadBindAddressError extends Error {
readonly code = 'orcad_invalid_bind_address'
}
/** Require literal IPs so DNS cannot silently choose a public/private bind interface. */
export function resolveOrcadBindHost(raw?: string): string {
if (raw === undefined) {
return ORCAD_LOOPBACK_BIND_HOST
}
const value = raw.trim()
if (value === '') {
throw new OrcadBindAddressError('--bind expects an address')
}
if (value === 'localhost') {
return ORCAD_LOOPBACK_BIND_HOST
}
if (value.includes('%')) {
throw new OrcadBindAddressError(
`--bind does not support scoped IPv6 addresses (got '${value}'). Use an unscoped literal IPv6 address.`
)
}
if (isIP(value) === 0) {
throw new OrcadBindAddressError(
`--bind expects a literal IP address (got '${value}'). Hostnames are refused because ` +
'DNS decides which interface would be bound. Use 127.0.0.1 for loopback, or ' +
'0.0.0.0 to expose every interface.'
)
}
return value
}
export function bindHostIsNetworkExposed(host: string): boolean {
if (host === ALL_INTERFACES_V4 || host === ALL_INTERFACES_V6) {
return true
}
if (isIP(host) === 4) {
return !host.startsWith('127.')
}
return host !== '::1'
}
export function describeOrcadBindExposure(host: string): string {
return bindHostIsNetworkExposed(host)
? `orcad is bound to ${host} and is reachable from the network. Anything that can reach ` +
'this port can attempt pairing.'
: `orcad is bound to ${host} (local only). Reach it from another machine with an SSH ` +
'local port-forward, or re-launch with --bind to expose it.'
}