chore(server): tighten TRUST_PROXY hop-count parsing to strict integer-only#224
Merged
CryptoJones merged 1 commit intoMay 19, 2026
Merged
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #223.
Summary
parseIntis lenient —parseInt('1abc', 10)returns1. An operator typoTRUST_PROXY=1abcsilently 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 lintcleannpm 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/