Skip to content

Commit 6d35054

Browse files
committed
fix: enhance proxy trust configuration for Express app
1 parent 81283aa commit 6d35054

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@ GitHub Value is a free and open-source application designed to help measure the
1616

1717
github-value will take you through setup the first time you run it. You can manually configure it by copying the [`.env.example`](./.env.example) file to `.env` and configure the environment variables.
1818

19+
#### Security Configuration
20+
21+
When deploying behind load balancers, reverse proxies, or CDNs, configure the `TRUST_PROXY` environment variable:
22+
23+
- **Development**: `TRUST_PROXY=false` (default)
24+
- **Production behind single proxy**: `TRUST_PROXY=1` (recommended)
25+
- **Production behind multiple proxies**: `TRUST_PROXY=2` (or number of hops)
26+
- **High security**: `TRUST_PROXY=10.0.0.1,172.16.0.0/12` (specific IPs/ranges)
27+
28+
This ensures accurate rate limiting and IP detection while maintaining security.
29+
30+
📖 **[Full Proxy Configuration Guide](./docs/PROXY_CONFIGURATION.md)**
31+
1932
<details>
2033
<summary>Docker Compose</summary>
2134

backend/.env.example

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@
55
# This will be requested during the setup process but you can set it manually
66
MONGODB_URI=mongodb://localhost:27017/github-value
77

8+
# Proxy Trust Configuration
9+
# Controls how Express handles X-Forwarded-For headers for rate limiting and IP detection
10+
# Options:
11+
# - 'true': Trust all proxies (least secure, use only in development)
12+
# - 'false': Don't trust any proxies (default for development)
13+
# - '1': Trust first proxy hop (recommended for production behind single load balancer)
14+
# - '2': Trust first 2 proxy hops (for multiple proxy layers)
15+
# - '10.0.0.1,172.16.0.0/12': Comma-separated trusted proxy IPs/ranges (most secure)
16+
# If not set, defaults to '1' in production, 'false' in development
17+
# TRUST_PROXY=1
18+
819
# GitHub App configuration
920
# This will be automatically populated during the setup process but you can set it manually
1021
# GITHUB_WEBHOOK_SECRET=

backend/src/app.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,33 @@ class App {
114114
}
115115

116116
private setupExpress() {
117-
// Trust proxy when running behind load balancers/reverse proxies
118-
this.e.set('trust proxy', true);
117+
// Configure proxy trust based on environment variable
118+
// Common values: 'true', 'false', number (proxy hops), or comma-separated IPs
119+
const trustProxy = process.env.TRUST_PROXY;
120+
121+
if (trustProxy !== undefined) {
122+
// Parse the environment variable
123+
if (trustProxy === 'true') {
124+
this.e.set('trust proxy', true);
125+
} else if (trustProxy === 'false') {
126+
this.e.set('trust proxy', false);
127+
} else if (/^\d+$/.test(trustProxy)) {
128+
// Number of proxy hops (e.g., "1", "2")
129+
this.e.set('trust proxy', parseInt(trustProxy, 10));
130+
} else if (trustProxy.includes(',')) {
131+
// Comma-separated IP addresses/ranges
132+
this.e.set('trust proxy', trustProxy.split(',').map(ip => ip.trim()));
133+
} else {
134+
// Single IP address or invalid value
135+
logger.warn(`Invalid TRUST_PROXY value: ${trustProxy}. Using default (false).`);
136+
this.e.set('trust proxy', false);
137+
}
138+
} else {
139+
// Default behavior: trust proxy in production environments
140+
const isProd = process.env.NODE_ENV === 'production';
141+
this.e.set('trust proxy', isProd ? 1 : false);
142+
logger.info(`TRUST_PROXY not set. Using default: ${isProd ? '1 (production)' : 'false (development)'}`);
143+
}
119144

120145
this.e.use(cors());
121146
this.e.use((req, res, next) => {

0 commit comments

Comments
 (0)