Skip to content

--port has no upper bound: b4 start --port 70000 crashes with Node's ERR_SOCKET_BAD_PORT instead of Invalid port #1047

Closed

Description

@blove

packages/cli/src/lib/dev/parse-port.ts (shared by b4 dev and b4 inspect, lines 10–21) and the deliberately separate copy in packages/cli/src/commands/start.ts (lines 58–72) both validate --port as:

const port = Number(rawPort)
if (!Number.isInteger(port) || port <= 0) {   // start.ts: port < 0
  throw new CliError(`Invalid port: ${rawPort}`, 2)
}
return port

There is no upper bound, and Number() accepts more spellings than a decimal port.

Observed vs expected

Against main (2f1829b), importing parsePort from src/:

"65536"  -> 65536      (expected: throws "Invalid port: 65536")
"70000"  -> 70000      (expected: throws "Invalid port: 70000")
"0x1f90" -> 8080       (expected: throws "Invalid port: 0x1f90")
"1e3"    -> 1000       (expected: throws "Invalid port: 1e3")

End to end, with a minimal fixture app (b4.config.ts = export default {}, package.json = {"type":"module"}, one src/app/support/[tenant]/index.ts route) and process.cwd() set to it:

await runStartCommand({ host: "127.0.0.1", port: "70000" }, io)

rejects with

RangeError: options.port should be >= 0 and < 65536. Received type number (70000).
  code: ERR_SOCKET_BAD_PORT, exitCode: undefined

Expected: CliError("Invalid port: 70000") with exitCode 2, before the app is loaded — the contract test/start-command.test.ts already pins for -1 and not-a-number ("rejects an invalid --port before touching the filesystem").

Why it matters

  • The parser's whole job is to turn a bad flag into the one legible Invalid port: … message with exit code 2. For any value above 65535 it instead loads the app, then surfaces a Node internals error from server.listen (packages/cli/src/lib/dev/serve.ts:262) with no exit code.
  • For b4 dev the value is forwarded to the spawned dev child (dev-session.ts:266); the child's listen fails for a reason other than EADDRINUSE, so the session rethrows the raw startup error rather than its Port N is unavailable fatal.
  • --port 1e3 / --port 0x1f90 silently binding 1000 / 8080 is surprising for a flag documented as --port <number>.

Fix

In both parsers, reject port > 65535 (and, to keep the flag decimal-only, require /^\d+$/.test(rawPort) before Number()), keeping each file's existing lower bound (> 0 in parse-port.ts, >= 0 in start.ts so --port 0 still requests an ephemeral port). Leave the comment in parse-port.ts that records why the two copies differ.

Add cases to test/start-command.test.ts ("rejects an invalid --port before touching the filesystem") for "65536", "70000", "1e3", and a unit test for parsePort from src/lib/dev/parse-port.ts covering the same inputs plus "65535" still accepted.

Verify

pnpm --filter @b4run/cli exec vitest run test/start-command.test.ts
pnpm --filter @b4run/cli test
pnpm lint

Node 24.

Activity

  1. added
    bug
    on Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions