Skip to content

cli: a non-integer or out-of-range PORT env var reaches net.listen and crashes b4 start #1063

Description

@blove

What happens

resolveServePort in packages/cli/src/lib/dev/serve-runtime.ts decides the port b4 start listens on when --port is not given: the PORT env var, else 8000. Its doc comment says an empty or non-numeric PORT is treated as unset and falls back to 8000, in lockstep with the Dockerfile healthcheck's PORT||8000. The implementation only checks Number.isFinite(Number(trimmed)), so every finite number is honoured as it is:

PORT=3.5   -> 3.5
PORT=-1    -> -1
PORT=70000 -> 70000
PORT=0x50  -> 80
PORT=1e3   -> 1000

3.5, -1 and 70000 go straight to net.listen, which throws RangeError [ERR_SOCKET_BAD_PORT], so b4 start crashes with an uncaught Node error. 0x50 and 1e3 bind a port the operator never wrote. Meanwhile the sibling flag parser (parsePort in commands/start.ts, fixed for the upper bound in #1047) rejects all of these with Invalid port.

Expected

PORT is honoured only when it is a decimal integer in 0..65535 (digits only, so no sign, no exponent, no hex, no fraction). Any other value is treated as unset and falls back to 8000, exactly as the doc comment already says for a non-numeric value: the env var is a deployment default, and the Dockerfile's PORT||8000 healthcheck is the behaviour to match. An explicit --port keeps winning, including 0.

PORT=65535 -> 65535
PORT=3.5   -> 8000
PORT=-1    -> 8000
PORT=70000 -> 8000
PORT=0x50  -> 8000
PORT=1e3   -> 8000
PORT=""    -> 8000   (unchanged)
PORT=abc   -> 8000   (unchanged)

Where

packages/cli/src/lib/dev/serve-runtime.ts, resolveServePort, the last two lines: const parsed = Number(trimmed); return Number.isFinite(parsed) ? parsed : DEFAULT_SERVE_PORT.

Scope

Only packages/cli/src/lib/dev/serve-runtime.ts needs to change. No change to --port parsing, to DEFAULT_SERVE_PORT, or to any exported type.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions