@@ -99,7 +99,7 @@ ctx.request.href;
9999
100100 Get hostname when present. Supports ` X-Forwarded-Host `
101101 when ` app.proxy ` is __ true__ , otherwise ` Host ` is used.
102-
102+
103103 If host is IPv6, Koa delegates parsing to
104104 [ WHATWG URL API] ( https://nodejs.org/dist/latest-v8.x/docs/api/url.html#url_the_whatwg_url_api ) ,
105105 * Note* This may impact performance.
@@ -193,8 +193,46 @@ ctx.body = await db.find('something');
193193### request.ips
194194
195195 When ` X-Forwarded-For ` is present and ` app.proxy ` is enabled an array
196- of these ips is returned, ordered from upstream -> downstream. When disabled
197- an empty array is returned.
196+ of these ips is returned, ordered from upstream -> downstream. When
197+ disabled an empty array is returned.
198+
199+ For example if the value were "client, proxy1, proxy2",
200+ you would receive the array ` ["client", "proxy1", "proxy2"] ` .
201+
202+ Most of the reverse proxy(nginx) set x-forwarded-for via
203+ ` proxy_add_x_forwarded_for ` , which poses a certain security risk.
204+ A malicious attacker can forge a client's ip address by forging
205+ a ` X-Forwarded-For ` request header. The request sent by the client
206+ has an ` X-Forwarded-For ` request header for 'forged'. After being
207+ forwarded by the reverse proxy, ` request.ips ` will be
208+ [ 'forged', 'client', 'proxy1', 'proxy2'] .
209+
210+ Koa offers two options to avoid being bypassed.
211+
212+ If you can control the reverse proxy, you can avoid bypassing
213+ by adjusting the configuration, or use the ` app.proxyIpHeader `
214+ provided by koa to avoid reading ` x-forwarded-for ` to get ips.
215+
216+ ``` js
217+ const app = new Koa ({
218+ proxy: true ,
219+ proxyIpHeader: ' X-Real-IP' ,
220+ });
221+ ```
222+
223+ If you know exactly how many reverse proxies are in front of
224+ the server, you can avoid reading the user's forged request
225+ header by configuring ` app.maxIpsCount ` :
226+
227+ ``` js
228+ const app = new Koa ({
229+ proxy: true ,
230+ maxIpsCount: 1 , // only one proxy in front of the server
231+ });
232+
233+ // request.header['X-Forwarded-For'] === [ '127.0.0.1', '127.0.0.2' ];
234+ // ctx.ips === [ '127.0.0.2' ];
235+ ```
198236
199237### request.subdomains
200238
0 commit comments