Skip to content

chore(server): tighten TRUST_PROXY hop-count parsing to strict integer-only#224

Merged
CryptoJones merged 1 commit into
masterfrom
chore/server-trust-proxy-strict-integer-parse
May 19, 2026
Merged

chore(server): tighten TRUST_PROXY hop-count parsing to strict integer-only#224
CryptoJones merged 1 commit into
masterfrom
chore/server-trust-proxy-strict-integer-parse

Conversation

@CryptoJones
Copy link
Copy Markdown
Owner

Closes #223.

Summary

parseInt is lenient — parseInt('1abc', 10) returns 1. An operator typo TRUST_PROXY=1abc silently set hops=1 instead of falling through to the no-trust default.

Switch to a strict /^\d+$/ regex match. Invalid values now fall through; typos become observable.

Test plan

  • npm run lint clean
  • npm test — 672 passed (no behavior change for valid values; tightened path for invalid ones)

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

…r-only

The TRUST_PROXY=<hop count> branch was:

```js
} else if (trustProxy && !isNaN(parseInt(trustProxy, 10))) {
    app.set('trust proxy', parseInt(trustProxy, 10));
}
```

`parseInt` is lenient — `parseInt('1abc', 10)` returns `1`. An
operator typo like `TRUST_PROXY=1abc` would silently set the hop
count to 1, partially honoring a malformed value instead of
falling back to the implicit Express default (no trust).

Switch to a strict `/^\d+$/` regex match. Now anything that isn't
the literal string `true` or a clean non-negative integer string
falls through to the default. Operator typos surface as
"X-Forwarded-For not trusted" — a missing-trust observation is
debuggable; a partial-trust silent acceptance is not.

Tightened the `trustProxy && !isNaN(...)` check to
`typeof trustProxy === 'string' && /^\d+$/.test(trustProxy)` for
the same reason (the `!isNaN(parseInt(undefined, 10))` short
circuit was already handled by the leading `trustProxy &&`, but
the explicit type guard is clearer about intent).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@CryptoJones CryptoJones merged commit 7b89a86 into master May 19, 2026
3 checks passed
@CryptoJones CryptoJones deleted the chore/server-trust-proxy-strict-integer-parse branch May 19, 2026 11:06
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.

server.js: TRUST_PROXY hop-count uses lenient parseInt — 'TRUST_PROXY=1abc' silently sets hops=1

1 participant