From 62045be0f7337ef6c1e65e0edcbfc82726278826 Mon Sep 17 00:00:00 2001 From: Roman Priesol Date: Sun, 29 Nov 2026 17:19:13 +0100 Subject: [PATCH] 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 --- app.rb | 20 +++++++++++++++++--- docs/deployment.adoc | 5 +++-- test/test_app.rb | 6 ++++++ 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/app.rb b/app.rb index 1411a10..3eabf93 100644 --- a/app.rb +++ b/app.rb @@ -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 diff --git a/docs/deployment.adoc b/docs/deployment.adoc index bf0dfcf..b38dbf4 100644 --- a/docs/deployment.adoc +++ b/docs/deployment.adoc @@ -184,9 +184,10 @@ working tree rsynced into `build/`), not by CI. 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 - 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. * `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. diff --git a/test/test_app.rb b/test/test_app.rb index d939383..d5f5b6e 100644 --- a/test/test_app.rb +++ b/test/test_app.rb @@ -148,6 +148,12 @@ class AppTest < Minitest::Test assert_equal 401, request.get("/api/sync/maps/file", params: { "path" => "offline.pmtiles" }).status 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 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]