@@ -172,16 +172,15 @@ protected function determineClientIpAddress($request)
172172 }
173173 }
174174
175- $ checkProxyHeaders = $ this -> checkProxyHeaders ;
176- if ($ checkProxyHeaders ) {
175+ $ checkProxyHeaders = false ;
176+ if ($ this -> checkProxyHeaders ) {
177177 // Exact Match
178- if ($ this ->trustedProxies && ! in_array ($ ipAddress , $ this ->trustedProxies )) {
179- $ checkProxyHeaders = false ;
178+ if ($ this ->trustedProxies && in_array ($ ipAddress , $ this ->trustedProxies )) {
179+ $ checkProxyHeaders = true ;
180180 }
181181
182182 // Wildcard Match
183- if ($ checkProxyHeaders && $ this ->trustedWildcard ) {
184- $ checkProxyHeaders = false ;
183+ if ($ this ->checkProxyHeaders && $ this ->trustedWildcard ) {
185184 // IPv4 has 4 parts separated by '.'
186185 // IPv6 has 8 parts separated by ':'
187186 if (strpos ($ ipAddress , '. ' ) > 0 ) {
@@ -196,19 +195,22 @@ protected function determineClientIpAddress($request)
196195 if (count ($ proxy ) !== $ parts ) {
197196 continue ; // IP version does not match
198197 }
198+ $ match = true ;
199199 foreach ($ proxy as $ i => $ part ) {
200200 if ($ part !== '* ' && $ part !== $ ipAddrParts [$ i ]) {
201- break 2 ;// IP does not match, move to next proxy
201+ $ match = false ;
202+ break ;// IP does not match, move to next proxy
202203 }
203204 }
204- $ checkProxyHeaders = true ;
205- break ;
205+ if ($ match ) {
206+ $ checkProxyHeaders = true ;
207+ break ;
208+ }
206209 }
207210 }
208211
209212 // CIDR Match
210- if ($ checkProxyHeaders && $ this ->trustedCidr ) {
211- $ checkProxyHeaders = false ;
213+ if ($ this ->checkProxyHeaders && $ this ->trustedCidr ) {
212214 // Only IPv4 is supported for CIDR matching
213215 $ ipAsLong = ip2long ($ ipAddress );
214216 if ($ ipAsLong ) {
@@ -220,15 +222,19 @@ protected function determineClientIpAddress($request)
220222 }
221223 }
222224 }
223- }
224225
225- if ($ checkProxyHeaders ) {
226- foreach ($ this ->headersToInspect as $ header ) {
227- if ($ request ->hasHeader ($ header )) {
228- $ ip = $ this ->getFirstIpAddressFromHeader ($ request , $ header );
229- if ($ this ->isValidIpAddress ($ ip )) {
230- $ ipAddress = $ ip ;
231- break ;
226+ if (!$ this ->trustedProxies && !$ this ->trustedWildcard && !$ this ->trustedCidr ) {
227+ $ checkProxyHeaders = true ;
228+ }
229+
230+ if ($ checkProxyHeaders ) {
231+ foreach ($ this ->headersToInspect as $ header ) {
232+ if ($ request ->hasHeader ($ header )) {
233+ $ ip = $ this ->getFirstIpAddressFromHeader ($ request , $ header );
234+ if ($ this ->isValidIpAddress ($ ip )) {
235+ $ ipAddress = $ ip ;
236+ break ;
237+ }
232238 }
233239 }
234240 }
0 commit comments