TRUSTED_NETWORKS=connected: requests from the subnets the server's interfaces are on right now count as logged in (the car's server, whichever Wi-Fi it's on); CIDRs still work alongside
CI / check (push) Successful in 21s
CI / deploy (push) Successful in 23s

This commit is contained in:
randy committed 2026-11-29 17:19:13 +01:00
1 parent ea14b110e6
commit 62045be0f7
3 files changed
+26 -5

No files matched your search

+17 -3
View File
@@ -211,8 +211,22 @@ module Maptool
# these networks (comma-separated CIDRs) count as logged in. By the
# socket's own peer address, never X-Forwarded-For — and only for a
# server no reverse proxy sits in front of (behind one every request
# comes from the proxy's address).
TRUSTED_NETWORKS = ENV.fetch("TRUSTED_NETWORKS", "").split(",").map(&:strip).reject(&:empty?).map { |cidr| IPAddr.new(cidr) }
# comes from the proxy's address). "connected" stands for the subnets
# the server's own interfaces are on right now (host networking: the
# Pi's LAN, whichever Wi-Fi it's on), looked up on each request.
TRUSTED_NETWORKS = ENV.fetch("TRUSTED_NETWORKS", "").split(",").map(&:strip).reject(&:empty?)
.map { |cidr| cidr == "connected" ? :connected : IPAddr.new(cidr) }
# The directly connected subnets: each interface address under its netmask.
def self.connected_networks
Socket.getifaddrs.filter_map do |i|
next unless i.addr&.ip? && i.netmask&.ip?
IPAddr.new("#{i.addr.ip_address.split('%').first}/#{i.netmask.ip_address}")
rescue IPAddr::Error
nil
end
end
# Library sync (Sync), OsmAnd-style: brick is the server, a client
# (the car's) has SYNC_SERVER_URL and syncs on its Sync button. The same
@@ -249,7 +263,7 @@ module Maptool
return false if TRUSTED_NETWORKS.empty?
address = IPAddr.new(request.env["REMOTE_ADDR"].to_s.delete_prefix("::ffff:"))
TRUSTED_NETWORKS.any? { |network| network.include?(address) }
TRUSTED_NETWORKS.flat_map { |n| n == :connected ? App.connected_networks : [n] }.any? { |network| network.include?(address) }
rescue IPAddr::Error
false
end