Skip to content

chore(env): tighten DB_PORT parsing to non-zero finite integers#293

Merged
CryptoJones merged 1 commit into
masterfrom
chore/env-strict-db-port-parsing
May 19, 2026
Merged

chore(env): tighten DB_PORT parsing to non-zero finite integers#293
CryptoJones merged 1 commit into
masterfrom
chore/env-strict-db-port-parsing

Conversation

@CryptoJones
Copy link
Copy Markdown
Owner

Summary

Same parseInt-leniency class fixed for PORT in #124. The line was:

port: parseInt(process.env.DB_PORT, 10) || 5432,

which means DB_PORT="5432abc" (typo) silently becomes 5432 — the operator gets the default port without knowing their value was rejected. Same shape was a real bug for PORT=0 (kernel-pick-a-free-port → 3000) and got fixed there; carrying the same guard to DB_PORT removes the typo-eats-your-config footgun for DB connectivity.

What changed

Replaced the falsy-fallback with Number.isFinite + > 0. Note: unlike server's PORT where 0 is a valid sentinel, DB_PORT=0 wouldn't actually connect (postgres doesn't listen on port 0), so > 0 is the right floor here.

Test plan

  • npm run lint && npm test — 761 passing.
  • No behavior change for any sane DB_PORT value.

Proudly Made in Nebraska. Go Big Red! 🌽 https://xkcd.com/2347/

Same parseInt-leniency class fixed for \`PORT\` in #124. The line was
\`port: parseInt(process.env.DB_PORT, 10) || 5432\`, which means
\`DB_PORT="5432abc"\` (typo) silently becomes 5432 — the operator
gets the default port without knowing their value was rejected.

Guard with Number.isFinite + > 0 so only NaN and non-positive
values fall through. (Unlike server.js's PORT where 0 is a valid
"kernel pick a free port" sentinel, DB_PORT=0 wouldn't actually
connect — postgres doesn't listen on 0 — so > 0 is the right floor
here.)

No behavior change for sane DB_PORT values. 761 tests still pass.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@CryptoJones CryptoJones merged commit dc0fe93 into master May 19, 2026
3 checks passed
@CryptoJones CryptoJones deleted the chore/env-strict-db-port-parsing branch May 19, 2026 16:19
CryptoJones added a commit that referenced this pull request May 19, 2026
#293 tightened DB_PORT parsing to non-zero finite integers — only
NaN / negative / zero values fall through to the 5432 default.
Existing env-validation tests covered the DB_PASSWORD empty-warn
path but not the port logic, so the new behavior had no regression
pin.

Added 6 test cases via spawnSync into a child node process that
prints env.port to stdout:
  - valid integer parsed verbatim ("5433" → 5433)
  - missing → default (5432)
  - garbage non-numeric → default
  - parseInt-typo "5433abc" → 5433 (documents the guard's exact
    reach: Number.isFinite passes here because parseInt returns
    a finite number, just one without the trailing chars)
  - "0" → default (postgres doesn't listen on 0)
  - negative → default

Test count: 761 → 767. All passing.

Co-authored-by: Aaron K. Clark <akclark@thenetwerk.net>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
CryptoJones added a commit that referenced this pull request May 19, 2026
Same parseInt-leniency footgun as PORT (#124) and DB_PORT (#293):
`parseInt('100abc', 10)` returns 100, so a typo'd
`RATE_LIMIT_MAX=1abc` silently sets max=1 — a near-total block on
/v1/* — instead of falling back to the documented default of 100.
The operator would only notice this after every legitimate client
started seeing 429s.

Add a `strictParseIntEnv(raw)` helper that pre-validates the raw
string against `^-?\d+$` before parseInt. Anything else returns
NaN so the existing `Number.isFinite + > 0` fallback below picks
the default. Apply to both `RATE_LIMIT_MAX` and
`RATE_LIMIT_WINDOW_MS`.

No test added — the helper is module-private and the existing
server-boots smoke test doesn't drive env-permutation paths. The
unit-test-equivalent coverage lives in
`tests/unit/env-validation.test.js` for the DB_PORT case (#293);
a future env-validation expansion can fold these in.

Co-authored-by: Aaron K. Clark <akclark@thenetwerk.net>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.

1 participant