Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Send Enable Banking a PSU IP only when it is a public address (#4304)

jaysbeekaya12a125ff34c
diff --git a/app/models/enable_banking_item/provided.rb b/app/models/enable_banking_item/provided.rb
index 1466d79cb5..1818b06c37 100644
--- a/app/models/enable_banking_item/provided.rb
+++ b/app/models/enable_banking_item/provided.rb
@@ -23,8 +23,17 @@ module EnableBankingItem::Provided
# Only attempt to satisfy the headers if the only required one is Psu-Ip-Address
# (the one we can populate from stored data)
satisfiable = required.all? { |h| h == "psu-ip-address" }
- return {} unless satisfiable && last_psu_ip.present?
+ return {} unless satisfiable && public_psu_ip.present?
- { "Psu-Ip-Address" => last_psu_ip }
+ { "Psu-Ip-Address" => public_psu_ip }
end
+
+ private
+ # last_psu_ip is the browser's address as Rails saw it. On a self-hosted
+ # instance reached over a LAN or VPN that is a private or other
+ # special-purpose address, which banks reject as an invalid PSU IP, so only
+ # a public one is sent.
+ def public_psu_ip
+ last_psu_ip if NetworkAddress.public?(last_psu_ip)
+ end
end
diff --git a/app/models/network_address.rb b/app/models/network_address.rb
new file mode 100644
index 0000000000..fa2294de53
--- /dev/null
+++ b/app/models/network_address.rb
@@ -0,0 +1,45 @@
+# What an IP address is, as opposed to what a caller may do with it.
+# Provider::Simplefin decides which hosts it may connect to, and that depends
+# on the app mode; EnableBankingItem decides whether a browser's address is
+# one a bank will accept. Both need an IPv6 literal that carries an IPv4
+# address read as that address, so the unwrapping lives here once.
+module NetworkAddress
+ NAT64_NETWORK = IPAddr.new("64:ff9b::/96")
+ IPV4_TRANSLATED_NETWORK = IPAddr.new("::ffff:0:0:0/96")
+ SIX_TO_FOUR_NETWORK = IPAddr.new("2002::/16")
+ IPV4_COMPATIBLE_NETWORK = IPAddr.new("::/96")
+
+ # IANA special-purpose ranges that are not globally reachable, beyond what
+ # IPAddr#private?, #loopback? and #link_local? already answer.
+ NON_PUBLIC_NETWORKS = %w[
+ 0.0.0.0/8 100.64.0.0/10 192.0.0.0/24 192.0.2.0/24 198.18.0.0/15
+ 198.51.100.0/24 203.0.113.0/24 224.0.0.0/4 240.0.0.0/4
+ ::/128 64:ff9b:1::/48 100::/64 2001:2::/48 2001:db8::/32 ff00::/8
+ ].map { |range| IPAddr.new(range) }.freeze
+
+ module_function
+
+ # IPv4-mapped, IPv4-compatible, NAT64 and 6to4 addresses reach the IPv4
+ # address they carry, so they are checked as that address.
+ def embedded_ipv4(ip)
+ return ip unless ip.ipv6?
+ return ip.native if ip.ipv4_mapped?
+
+ if NAT64_NETWORK.include?(ip) || IPV4_TRANSLATED_NETWORK.include?(ip) || (IPV4_COMPATIBLE_NETWORK.include?(ip) && ip.to_i > 1)
+ IPAddr.new(ip.to_i & 0xffff_ffff, Socket::AF_INET)
+ elsif SIX_TO_FOUR_NETWORK.include?(ip)
+ IPAddr.new((ip.to_i >> 80) & 0xffff_ffff, Socket::AF_INET)
+ else
+ ip
+ end
+ end
+
+ # True for an address that is globally reachable. Anything unparseable is
+ # not.
+ def public?(address)
+ ip = embedded_ipv4(IPAddr.new(address.to_s))
+ !(ip.private? || ip.loopback? || ip.link_local? || NON_PUBLIC_NETWORKS.any? { |network| network.include?(ip) })
+ rescue IPAddr::Error
+ false
+ end
+end
diff --git a/app/models/provider/simplefin.rb b/app/models/provider/simplefin.rb
index 14e78b4398..2ae1581aab 100644
--- a/app/models/provider/simplefin.rb
+++ b/app/models/provider/simplefin.rb
@@ -38,11 +38,6 @@ class Provider::Simplefin
# Blocked on managed instances besides loopback and private ranges:
# carrier-grade NAT, IETF protocol assignments and benchmarking ranges.
MANAGED_BLOCKED_NETWORKS = %w[100.64.0.0/10 192.0.0.0/24 198.18.0.0/15].map { |range| IPAddr.new(range) }.freeze
- # IPv6 ranges that carry an IPv4 address, checked like that IPv4 address.
- NAT64_NETWORK = IPAddr.new("64:ff9b::/96")
- IPV4_TRANSLATED_NETWORK = IPAddr.new("::ffff:0:0:0/96")
- SIX_TO_FOUR_NETWORK = IPAddr.new("2002::/16")
- IPV4_COMPATIBLE_NETWORK = IPAddr.new("::/96")
# Connects to the address that ensure_allowed_url! checked instead of
# resolving the host a second time, so DNS rebinding cannot swap in an
@@ -250,16 +245,7 @@ class Provider::Simplefin
# IPv4-mapped, IPv4-compatible, NAT64 and 6to4 addresses reach the IPv4
# address they carry, so they are checked as that address.
def embedded_ipv4(ip)
- return ip unless ip.ipv6?
- return ip.native if ip.ipv4_mapped?
-
- if NAT64_NETWORK.include?(ip) || IPV4_TRANSLATED_NETWORK.include?(ip) || (IPV4_COMPATIBLE_NETWORK.include?(ip) && ip.to_i > 1)
- IPAddr.new(ip.to_i & 0xffff_ffff, Socket::AF_INET)
- elsif SIX_TO_FOUR_NETWORK.include?(ip)
- IPAddr.new((ip.to_i >> 80) & 0xffff_ffff, Socket::AF_INET)
- else
- ip
- end
+ NetworkAddress.embedded_ipv4(ip)
end
def managed_mode?
diff --git a/test/models/enable_banking_item_test.rb b/test/models/enable_banking_item_test.rb
index 80b025a88d..39dea8d1df 100644
--- a/test/models/enable_banking_item_test.rb
+++ b/test/models/enable_banking_item_test.rb
@@ -234,4 +234,62 @@ class EnableBankingItemTest < ActiveSupport::TestCase
assert_nil item.authorization_id
assert_nil item.last_psu_ip
end
+
+ test "build_psu_headers sends a public PSU IP to a bank that requires it" do
+ @item.aspsp_required_psu_headers = [ "Psu-Ip-Address" ]
+
+ %w[81.2.3.4 2a00:1450:4001::1 64:ff9b::5102:304].each do |ip|
+ @item.last_psu_ip = ip
+ assert_equal({ "Psu-Ip-Address" => ip }, @item.build_psu_headers, ip)
+ end
+ end
+
+ # A self-hosted instance reached over a LAN or VPN records the browser's own
+ # address, which a bank rejects as an invalid PSU IP. Leaving the header out
+ # is what the bank accepts.
+ test "build_psu_headers leaves out a PSU IP that is not a public address" do
+ @item.aspsp_required_psu_headers = [ "Psu-Ip-Address" ]
+
+ {
+ "192.168.1.20" => "a home LAN",
+ "10.0.0.5" => "a private network",
+ "172.17.0.1" => "the Docker bridge",
+ "100.101.1.2" => "a carrier-grade NAT or Tailscale address",
+ "127.0.0.1" => "loopback",
+ "::1" => "IPv6 loopback",
+ "fd12:3456::1" => "an IPv6 unique local address",
+ "fe80::1" => "an IPv6 link-local address",
+ "169.254.1.1" => "an IPv4 link-local address",
+ "::ffff:100.64.0.1" => "a carrier-grade NAT address written as IPv6",
+ "0.0.0.0" => "the unspecified address",
+ "::" => "the IPv6 unspecified address",
+ "198.18.0.1" => "a benchmarking address a VPN may assign",
+ "192.0.0.8" => "an IETF protocol assignment",
+ "192.0.2.1" => "a documentation address",
+ "198.51.100.7" => "another documentation address",
+ "203.0.113.5" => "a third documentation address",
+ "224.0.0.1" => "a multicast address",
+ "240.0.0.1" => "a reserved address",
+ "255.255.255.255" => "the broadcast address",
+ "100::1" => "an IPv6 discard-only address",
+ "2001:2::1" => "an IPv6 benchmarking address",
+ "64:ff9b:1::1" => "a local-use NAT64 address",
+ "2001:db8::1" => "an IPv6 documentation address",
+ "ff02::1" => "an IPv6 multicast address",
+ "64:ff9b::c0a8:114" => "a home LAN address written as NAT64",
+ "2002:c0a8:114::1" => "a home LAN address written as 6to4",
+ "::c0a8:114" => "a home LAN address written as IPv4-compatible",
+ "not-an-ip" => "an unparseable value"
+ }.each do |ip, kind|
+ @item.last_psu_ip = ip
+ assert_equal({}, @item.build_psu_headers, kind)
+ end
+ end
+
+ test "build_psu_headers sends nothing when the bank also requires a header it can't supply" do
+ @item.aspsp_required_psu_headers = [ "Psu-Ip-Address", "Psu-User-Agent" ]
+ @item.last_psu_ip = "81.2.3.4"
+
+ assert_equal({}, @item.build_psu_headers)
+ end
end