@@ -125,8 +125,8 @@ def calculate_ip
125
125
remote_addr = ips_from ( @req . remote_addr ) . last
126
126
127
127
# Could be a CSV list and/or repeated headers that were concatenated.
128
- client_ips = ips_from ( @req . client_ip ) . reverse
129
- forwarded_ips = ips_from ( @req . x_forwarded_for ) . reverse
128
+ client_ips = ips_from ( @req . client_ip ) . reverse!
129
+ forwarded_ips = ips_from ( @req . x_forwarded_for ) . reverse!
130
130
131
131
# +Client-Ip+ and +X-Forwarded-For+ should not, generally, both be set.
132
132
# If they are both set, it means that either:
@@ -154,7 +154,8 @@ def calculate_ip
154
154
# - X-Forwarded-For will be a list of IPs, one per proxy, or blank
155
155
# - Client-Ip is propagated from the outermost proxy, or is blank
156
156
# - REMOTE_ADDR will be the IP that made the request to Rack
157
- ips = [ forwarded_ips , client_ips ] . flatten . compact
157
+ ips = forwarded_ips + client_ips
158
+ ips . compact!
158
159
159
160
# If every single IP option is in the trusted list, return the IP
160
161
# that's furthest away
@@ -172,14 +173,15 @@ def ips_from(header) # :doc:
172
173
return [ ] unless header
173
174
# Split the comma-separated list into an array of strings.
174
175
ips = header . strip . split ( /[,\s ]+/ )
175
- ips . select do |ip |
176
+ ips . select! do |ip |
176
177
# Only return IPs that are valid according to the IPAddr#new method.
177
178
range = IPAddr . new ( ip ) . to_range
178
179
# We want to make sure nobody is sneaking a netmask in.
179
180
range . begin == range . end
180
181
rescue ArgumentError
181
182
nil
182
183
end
184
+ ips
183
185
end
184
186
185
187
def filter_proxies ( ips ) # :doc:
0 commit comments