Skip to content

Commit a0b145d

Browse files
authored
Merge pull request #101 from JackMyers001/feat/trust-extra-proxies
feat: add env var for additional trusted proxy IPs/subnets
2 parents 89c541a + e0e50ec commit a0b145d

3 files changed

Lines changed: 33 additions & 2 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ This image is executed as non root by default and is fully compliant with Kubern
1919
- [Basic Usage](#basic-usage)
2020
- [Choose your ports](#choose-your-ports)
2121
- [Use your own certificates](#use-your-own-certificates)
22+
- [Trust additional proxy IPs](#trust-additional-proxy-ips)
2223
- [Decode JWT header](#decode-jwt-header)
2324
- [Disable ExpressJS log lines](#disable-expressjs-log-lines)
2425
- [Do not log specific path](#do-not-log-specific-path)
@@ -96,6 +97,13 @@ You can use volume mounting to substitute the certificate and private key with y
9697
You can use the environment variables `HTTPS_CERT_FILE` and `HTTPS_KEY_FILE` to define the location of existing certificate and private key inside container.
9798

9899

100+
## Trust additional proxy IPs
101+
102+
By default, Express is configured to trust forwarded proxy headers from loopback, link-local, and private IP ranges. To also trust additional proxy IPs or subnets, set `ADDITIONAL_TRUSTED_PROXIES` to a single value or a comma-separated list.
103+
104+
docker run -e ADDITIONAL_TRUSTED_PROXIES="2001:db8::/32,203.0.113.10" -p 8080:8080 -p 8443:8443 --rm -t mendhak/http-https-echo:40
105+
106+
99107
## Decode JWT header
100108

101109
If you specify the header that contains the JWT, the echo output will contain the decoded JWT. Use the `JWT_HEADER` environment variable for this.

index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,18 @@ const {
1717
PROMETHEUS_WITH_METHOD = 'true',
1818
PROMETHEUS_WITH_STATUS = 'true',
1919
PROMETHEUS_METRIC_TYPE = 'summary',
20-
MAX_HEADER_SIZE = 1048576
20+
MAX_HEADER_SIZE = 1048576,
21+
ADDITIONAL_TRUSTED_PROXIES
2122
} = process.env
2223

2324
const maxHeaderSize = parseInt(MAX_HEADER_SIZE, 10) || 1048576;
2425

26+
const trustProxy = [
27+
'loopback',
28+
'linklocal',
29+
'uniquelocal',
30+
...(ADDITIONAL_TRUSTED_PROXIES || '').split(',').map(proxy => proxy.trim()).filter(Boolean)
31+
];
2532

2633
const sleep = promisify(setTimeout);
2734
const metricsMiddleware = promBundle({
@@ -34,7 +41,7 @@ const metricsMiddleware = promBundle({
3441

3542
const app = express()
3643
app.set('json spaces', 2);
37-
app.set('trust proxy', ['loopback', 'linklocal', 'uniquelocal']);
44+
app.set('trust proxy', trustProxy);
3845

3946
if(PROMETHEUS_ENABLED === 'true') {
4047
app.use(metricsMiddleware);

tests.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,22 @@ fi
237237
message " Stop containers "
238238
stop_and_remove
239239

240+
message " Start container with additional trusted proxies "
241+
docker run -d --rm -e ADDITIONAL_TRUSTED_PROXIES="2001:db8::/32,198.51.100.0/24" --name http-echo-tests -p 8080:8080 -p 8443:8443 -t mendhak/http-https-echo:testing
242+
wait_for_ready
243+
244+
REQUEST=$(curl -s -H "X-Forwarded-For: 203.0.113.10, 198.51.100.1, 2001:db8::1" http://localhost:8080/)
245+
if [[ "$(echo "$REQUEST" | jq -r '.ip')" == '203.0.113.10' ]]; then
246+
passed "Additional trusted proxies passed."
247+
else
248+
failed "Additional trusted proxies failed."
249+
echo "$REQUEST" | jq
250+
exit 1
251+
fi
252+
253+
message " Stop containers "
254+
stop_and_remove
255+
240256
message " Start container with different internal ports "
241257
docker run -d --rm -e HTTP_PORT=8888 -e HTTPS_PORT=9999 --name http-echo-tests -p 8080:8888 -p 8443:9999 -t mendhak/http-https-echo:testing
242258
wait_for_ready

0 commit comments

Comments
 (0)