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 # these networks (comma-separated CIDRs) count as logged in. By the
# socket's own peer address, never X-Forwarded-For — and only for a # 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 # server no reverse proxy sits in front of (behind one every request
# comes from the proxy's address). # comes from the proxy's address). "connected" stands for the subnets
TRUSTED_NETWORKS = ENV.fetch("TRUSTED_NETWORKS", "").split(",").map(&:strip).reject(&:empty?).map { |cidr| IPAddr.new(cidr) } # 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 # 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 # (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? return false if TRUSTED_NETWORKS.empty?
address = IPAddr.new(request.env["REMOTE_ADDR"].to_s.delete_prefix("::ffff:")) 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 rescue IPAddr::Error
false false
end end
+3 -2
View File
@@ -184,9 +184,10 @@ working tree rsynced into `build/`), not by CI.
Its `.env` adds: Its `.env` adds:
* `TRUSTED_NETWORKS` — the home/car LAN (and loopback): those requests * `TRUSTED_NETWORKS=connected` — requests from the subnets the Pi is
directly on (whichever Wi-Fi/LAN, looked up per request; loopback too)
count as logged in, since Gitea's OAuth needs internet and its redirect count as logged in, since Gitea's OAuth needs internet and its redirect
URL is brick's. Only on a server no reverse proxy sits in front of — URL is brick's. Comma-separated CIDRs work too, also next to `connected`. Only on a server no reverse proxy sits in front of —
never on brick, where every request comes from the proxy. never on brick, where every request comes from the proxy.
* `TAKEOUT_HOST_DIR` — an empty directory (the Takeout import is brick's). * `TAKEOUT_HOST_DIR` — an empty directory (the Takeout import is brick's).
* `SYNC_SERVER_URL` (`https://map.priesol.net`) and `SYNC_TOKEN`: it's a sync client. * `SYNC_SERVER_URL` (`https://map.priesol.net`) and `SYNC_TOKEN`: it's a sync client.
+6
View File
@@ -148,6 +148,12 @@ class AppTest < Minitest::Test
assert_equal 401, request.get("/api/sync/maps/file", params: { "path" => "offline.pmtiles" }).status assert_equal 401, request.get("/api/sync/maps/file", params: { "path" => "offline.pmtiles" }).status
end end
def test_connected_networks_are_the_interfaces_subnets
networks = Maptool::App.connected_networks
assert(networks.any? { |n| n.include?(IPAddr.new("127.0.0.1")) }, "loopback's 127.0.0.0/8")
refute(networks.any? { |n| n.include?(IPAddr.new("8.8.8.8")) })
end
def test_map_data_offered_to_clients_is_only_the_map_files def test_map_data_offered_to_clients_is_only_the_map_files
data = Maptool::App::DATA_DIR data = Maptool::App::DATA_DIR
files = %w[terrain.pmtiles places/SK.sqlite places/.rsync-partial/SK.sqlite routing/tiles.tar.part routing/service.sh can.json] files = %w[terrain.pmtiles places/SK.sqlite places/.rsync-partial/SK.sqlite routing/tiles.tar.part routing/service.sh can.json]