Skip to content

Commit 3cf05b9

Browse files
committed
Cache the results of #cloudfront_proxies and #trusted_proxies
These were being regenerated each call, which takes several ms. Freeze the results of these methods and #ip_ranges to ensure they don't get accidentally modified by callers. railtie was doing this, inadvertently, so use normal array addition there.
1 parent bc1003f commit 3cf05b9

2 files changed

Lines changed: 4 additions & 4 deletions

File tree

lib/action_pack/cloudfront/ip_ranges.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ def ipaddr
2121
end
2222

2323
def trusted_proxies
24-
cloudfront_proxies + ActionDispatch::RemoteIp::TRUSTED_PROXIES
24+
@trusted_proxies ||= (cloudfront_proxies + ActionDispatch::RemoteIp::TRUSTED_PROXIES).freeze
2525
end
2626

2727
def cloudfront_proxies
28-
ip_ranges.select(&:cloudfront?).map(&:ipaddr)
28+
@cloudfront_proxies ||= (ip_ranges.select(&:cloudfront?).map(&:ipaddr)).freeze
2929
end
3030

3131
def ip_ranges
@@ -35,7 +35,7 @@ def ip_ranges
3535
prefixesv6 = data['ipv6_prefixes']
3636
(prefixes + prefixesv6).map do |attrs|
3737
Range.new(attrs)
38-
end
38+
end.freeze
3939
end
4040
end
4141

lib/action_pack/cloudfront/railtie.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Railtie < ::Rails::Railtie
1212
trusted_proxies = ActionPack::Cloudfront::IpRanges.trusted_proxies
1313
existing_proxies = Array(app.config.action_dispatch.trusted_proxies)
1414

15-
app.config.action_dispatch.trusted_proxies = trusted_proxies.concat(existing_proxies).uniq
15+
app.config.action_dispatch.trusted_proxies = (trusted_proxies + existing_proxies).uniq
1616
end
1717
end
1818

0 commit comments

Comments
 (0)