Skip to content

fix(cli): validate PORT environment variable - #1064

Open
sharmao07 wants to merge 1 commit into
cacheplane:mainfrom
sharmao07:fix/validate-port-env
Open

sharmao07 wants to merge 1 commit into
cacheplane:mainfrom
sharmao07:fix/validate-port-env

Conversation

@sharmao07

Copy link
Copy Markdown

Summary

Fixes #1063.

  • Validate the PORT environment variable as decimal digits representing a port between 0 and 65535, inclusive.
  • Fall back to port 8000 for invalid values, including negative numbers, fractional values, hexadecimal notation, scientific notation, and out-of-range ports.
  • Preserve explicit port precedence, including port 0 for an ephemeral port.
  • Add regression tests for valid, invalid, boundary, and explicit-port cases.
  • Add a patch changeset for @b4run/cli.

Validation

  • Focused tests: pnpm --filter @b4run/cli test test/serve-runtime.test.ts — 10 tests passed.
  • Biome check on both modified TypeScript files — passed.
  • pnpm --filter @b4run/cli typecheck — passed.
  • pnpm exec changeset status — passed; the patch changeset was recognized.
  • pnpm lint — full repository lint not run; scoped Biome check passed.
  • pnpm build — not run.
  • pnpm typecheck — full monorepo typecheck not run; CLI package typecheck passed.
  • pnpm test — full test suite not run; focused tests passed.
  • node scripts/check-docs.mjs — not run.
  • pnpm pack:check — not run.
  • Harness verification — not run; CLI runtime behavior changed.

Release Notes

  • Changeset added: .changeset/resolve-serve-port-validation.md
  • The changeset declares a patch release for @b4run/cli.

Screenshots Or Artifacts

Not applicable. This change affects CLI port validation and tests; no UI screenshots or generated artifacts are required.

Checklist

  • I read CONTRIBUTING.md.
  • I followed CODE_OF_CONDUCT.md.
  • I did not include secrets, credentials, or private data.
  • I am not disclosing a security vulnerability publicly.

@sharmao07
sharmao07 requested a review from blove as a code owner October 21, 2026 21:38
@vercel

vercel Bot commented Oct 21, 2026

Copy link
Copy Markdown

@sharmao07 is attempting to deploy a commit to the cacheplane Team on Vercel.

A member of the Team first needs to authorize it.

@blove

blove commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

Hey @sharmao07, thank you for your first PR to B4.run! 🎉

This is really nice work. The digits-only check, the 0–65535 bounds, and keeping 0 for an ephemeral port are exactly right.

The timing just didn't work out: the same fix for #1063 landed in #1066 about an hour after you opened this, so it now conflicts with main.

That said, your tests cover a few cases we don't have. If you're up for a rebase, I'd love to merge just those:

  • PORT="70000" → 8000
  • PORT="-1" → 8000
  • PORT="1e3" → 8000
  • PORT="0" → 0
  • explicit 5555 wins over PORT="8000"

Please keep your docstring update too, since ours is out of date. Drop the change to resolveServePort, and swap the changeset for pnpm changeset --empty.

If not, no worries at all. I'll close this out. Either way, thank you!

@sharmao07
sharmao07 force-pushed the fix/validate-port-env branch from a4af902 to 36ae23f Compare October 27, 2026 09:24
@sharmao07

Copy link
Copy Markdown
Author

Hey @blove, thanks for the feedback! I've updated the PR against the latest main, removed the duplicate resolveServePort implementation change, retained the five requested regression cases and the docstring update, and replaced the patch changeset with an empty changeset generated using pnpm changeset --empty.

Local validation passed:

  • pnpm --filter @b4run/cli test test/serve-runtime.test.ts — 11 tests passed
  • pnpm --filter @b4run/cli test test/serve-port.test.ts — 4 tests passed
  • Scoped Biome check — passed
  • git diff --check — passed

Thanks for pointing out the additional regression coverage!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants