From 80f73a01547650710e7d6846298028c6b41977dc Mon Sep 17 00:00:00 2001 From: mnovak Date: Wed, 25 Nov 2026 08:54:51 +0100 Subject: [PATCH] [roads] QA F-02: going direct never takes what the tunnel carries out of it A goes-direct subnet was only checked to be canonical, and the kill switch dropped it only when it reached the internet, so 100.96.0.0/16 (every mesh address) or the home LAN that the plan routes into the tunnel compiled cleanly. Now, decided here because the plan has to say which route wins: - a goes-direct subnet other than /0 that holds a mesh address is refused (direct.mesh); - per device, a direct subnet inside a net the plan tunnels is dropped and reported (direct.tunnelled); - a direct subnet that holds tunnelled nets keeps them in the tunnel, listed in the new plan field direct[].except. A platform that cannot honour except (an app-level bypass) must not apply the entry. The F-02 test now checks that every overlap is covered by except, and that only the mesh subnet is refused. Golden plans and the pinned jana-phone digest are regenerated: the only change is the except lists. Plan format 1 has not been released, so it changes in place. --- crates/proto/tests/jana-phone.digest | 2 +- crates/roads/src/compile.rs | 66 +++++++++++++++++++ crates/roads/src/plan.rs | 3 + crates/roads/src/rules.rs | 4 ++ crates/roads/src/snapshot.rs | 5 ++ .../fixtures/home/expected/friend-node.json | 3 + .../fixtures/home/expected/home-server.json | 6 ++ .../fixtures/home/expected/jana-laptop.json | 3 + .../fixtures/home/expected/jana-phone.json | 3 + .../fixtures/home/expected/petr-phone.json | 1 + .../tests/fixtures/home/expected/router.json | 3 + .../roads/tests/fixtures/invalid/expect.txt | 2 + .../invalid/roads/a00048-direct-mesh.md | 7 ++ .../invalid/roads/a00049-direct-tunnelled.md | 7 ++ crates/roads/tests/qa_findings.rs | 16 ++++- crates/roads/tests/validation.rs | 1 + docs/FORMATS.md | 5 +- 17 files changed, 132 insertions(+), 5 deletions(-) create mode 100644 crates/roads/tests/fixtures/invalid/roads/a00048-direct-mesh.md create mode 100644 crates/roads/tests/fixtures/invalid/roads/a00049-direct-tunnelled.md diff --git a/crates/proto/tests/jana-phone.digest b/crates/proto/tests/jana-phone.digest index eb9fd94..a4ab07f 100644 --- a/crates/proto/tests/jana-phone.digest +++ b/crates/proto/tests/jana-phone.digest @@ -1 +1 @@ -da086f1cc965aee30351547c6ac466939c627e2f915d5c67314a73357f38f68a +f16046353b91971cb181e7453b6631a4b38c08adb5fcc5ef603e3c7514c65699 diff --git a/crates/roads/src/compile.rs b/crates/roads/src/compile.rs index a8a99ab..19d0111 100644 --- a/crates/roads/src/compile.rs +++ b/crates/roads/src/compile.rs @@ -256,6 +256,27 @@ fn check( Some(d) if d.classic => classic_peers.push(&d.name), Some(_) => {} }, + Some(Place::Subnet(n)) if road.verb == Verb::GoesDirect && n.prefix_len() > 0 => { + // Going direct never takes the mesh's own addresses out of the tunnel (QA F-02). The default + // route is the one exception: it keeps every tunnelled net in, as `except` (below). + let mut inside: Vec<&str> = devs + .mesh_addresses() + .filter(|(_, a)| n.contains_addr(&a.0)) + .map(|(d, _)| d.name.as_str()) + .collect(); + if !inside.is_empty() { + inside.sort(); + inside.dedup(); + out.push(p( + "object", + r::DIRECT_MESH, + format!( + "{n} holds the mesh address of {}: mesh traffic never goes direct", + inside.join(", ") + ), + )); + } + } Some(Place::Subnet(_)) => {} None => {} } @@ -629,6 +650,50 @@ pub fn compile(store: &Store, now: Instant) -> Compiled { build_plan(key, dev, devs, &mine, extras.remove(key), expiry.get(key)), ); } + + // What the tunnel carries wins over going direct (QA F-02). A direct subnet inside a net this plan sends + // through the tunnel is dropped; one that holds such nets keeps them in the tunnel, listed in `except`, + // so no platform has to guess which route wins. + for plan in out.plans.values_mut() { + let tunnelled: BTreeSet = plan + .peers + .iter() + .flat_map(|p| p.allowed_ips.iter().copied()) + .filter(|n| n.prefix_len() > 0) + .collect(); + let mut kept = Vec::new(); + for mut d in std::mem::take(&mut plan.direct) { + if let Some(t) = tunnelled.iter().find(|t| t.contains(&d.subnet)) { + for id in &d.roads { + let road = store + .roads + .iter() + .find(|r| &r.id == id) + .expect("a granting road"); + out.problems.push( + Problem::inactive( + &road.path, + Some("object"), + r::DIRECT_TUNNELLED, + format!( + "for {}: {} is inside {t}, which goes through the tunnel; it does not go direct", + plan.name, d.subnet + ), + ) + .at(road.line_of("object")), + ); + } + continue; + } + d.except = tunnelled + .iter() + .filter(|t| d.subnet.overlaps(t)) + .copied() + .collect(); + kept.push(d); + } + plan.direct = kept; + } out.problems.sort(); out } @@ -819,6 +884,7 @@ fn build_plan( .map(|((app, subnet), roads)| plan::Direct { app, subnet, + except: Vec::new(), roads: roads.into_iter().collect(), }) .collect(), diff --git a/crates/roads/src/plan.rs b/crates/roads/src/plan.rs index b619bea..88deb15 100644 --- a/crates/roads/src/plan.rs +++ b/crates/roads/src/plan.rs @@ -134,6 +134,9 @@ pub struct Direct { /// `None`: all traffic to `subnet`; `Some`: only that app's. pub app: Option, pub subnet: Net, + /// The nets inside `subnet` that this plan sends through the tunnel: they stay in it (QA F-02). A + /// platform that cannot keep them in (an app-level bypass, say) must not apply this entry at all. + pub except: Vec, pub roads: Vec, } diff --git a/crates/roads/src/rules.rs b/crates/roads/src/rules.rs index 42a0a92..04ccee5 100644 --- a/crates/roads/src/rules.rs +++ b/crates/roads/src/rules.rs @@ -29,6 +29,7 @@ pub const OBJECT_UNKNOWN: &str = "object.unknown"; pub const OBJECT_NOT_EXIT: &str = "object.not-exit"; pub const SUBNET_NOT_CANONICAL: &str = "subnet.not-canonical"; pub const SUBNET_UNROUTED: &str = "subnet.unrouted"; +pub const DIRECT_MESH: &str = "direct.mesh"; pub const UNTIL_FORMAT: &str = "until.format"; pub const CREATED_FORMAT: &str = "created.format"; pub const PORTS_FORMAT: &str = "ports.format"; @@ -49,6 +50,7 @@ pub const ROAD_SELF: &str = "road.self"; pub const EXIT_CONFLICT: &str = "exit.conflict"; pub const DIRECT_KILL_SWITCH: &str = "direct.kill-switch"; pub const ROAD_FAMILY: &str = "road.family"; +pub const DIRECT_TUNNELLED: &str = "direct.tunnelled"; // Snapshots pub const SNAPSHOT_JSON: &str = "snapshot.json"; @@ -97,6 +99,7 @@ pub const ALL: &[&str] = &[ OBJECT_NOT_EXIT, SUBNET_NOT_CANONICAL, SUBNET_UNROUTED, + DIRECT_MESH, UNTIL_FORMAT, CREATED_FORMAT, PORTS_FORMAT, @@ -115,6 +118,7 @@ pub const ALL: &[&str] = &[ EXIT_CONFLICT, DIRECT_KILL_SWITCH, ROAD_FAMILY, + DIRECT_TUNNELLED, SNAPSHOT_JSON, SNAPSHOT_FORMAT, DIRECTORY_ID, diff --git a/crates/roads/src/snapshot.rs b/crates/roads/src/snapshot.rs index 0c92056..d2300f0 100644 --- a/crates/roads/src/snapshot.rs +++ b/crates/roads/src/snapshot.rs @@ -79,6 +79,11 @@ impl Devices { .find_map(|d| d.services.iter().find(|s| &s.host == host).map(|s| (d, s))) } + /// Every mesh address: each device's own. Never goes direct (QA F-02). + pub fn mesh_addresses(&self) -> impl Iterator { + self.devices.values().map(|d| (d, d.address)) + } + /// The one device whose routes cover `net`. pub fn router_of(&self, net: &Net) -> Option<&Device> { self.devices diff --git a/crates/roads/tests/fixtures/home/expected/friend-node.json b/crates/roads/tests/fixtures/home/expected/friend-node.json index 2fad880..7dd1ee1 100644 --- a/crates/roads/tests/fixtures/home/expected/friend-node.json +++ b/crates/roads/tests/fixtures/home/expected/friend-node.json @@ -42,6 +42,9 @@ { "app": "steam", "subnet": "0.0.0.0/0", + "except": [ + "100.96.0.7/32" + ], "roads": [ "e5yj1s" ] diff --git a/crates/roads/tests/fixtures/home/expected/home-server.json b/crates/roads/tests/fixtures/home/expected/home-server.json index cae2767..b6d5406 100644 --- a/crates/roads/tests/fixtures/home/expected/home-server.json +++ b/crates/roads/tests/fixtures/home/expected/home-server.json @@ -177,6 +177,12 @@ { "app": "steam", "subnet": "0.0.0.0/0", + "except": [ + "100.96.0.1/32", + "100.96.0.3/32", + "100.96.0.4/32", + "100.96.0.5/32" + ], "roads": [ "e5yj1s" ] diff --git a/crates/roads/tests/fixtures/home/expected/jana-laptop.json b/crates/roads/tests/fixtures/home/expected/jana-laptop.json index 48d7921..e847431 100644 --- a/crates/roads/tests/fixtures/home/expected/jana-laptop.json +++ b/crates/roads/tests/fixtures/home/expected/jana-laptop.json @@ -40,6 +40,9 @@ { "app": "steam", "subnet": "0.0.0.0/0", + "except": [ + "100.96.0.2/32" + ], "roads": [ "e5yj1s" ] diff --git a/crates/roads/tests/fixtures/home/expected/jana-phone.json b/crates/roads/tests/fixtures/home/expected/jana-phone.json index fcce017..e8ebffe 100644 --- a/crates/roads/tests/fixtures/home/expected/jana-phone.json +++ b/crates/roads/tests/fixtures/home/expected/jana-phone.json @@ -40,6 +40,9 @@ { "app": "steam", "subnet": "0.0.0.0/0", + "except": [ + "100.96.0.2/32" + ], "roads": [ "e5yj1s" ] diff --git a/crates/roads/tests/fixtures/home/expected/petr-phone.json b/crates/roads/tests/fixtures/home/expected/petr-phone.json index d63918b..d6c3d3d 100644 --- a/crates/roads/tests/fixtures/home/expected/petr-phone.json +++ b/crates/roads/tests/fixtures/home/expected/petr-phone.json @@ -37,6 +37,7 @@ { "app": "steam", "subnet": "0.0.0.0/0", + "except": [], "roads": [ "e5yj1s" ] diff --git a/crates/roads/tests/fixtures/home/expected/router.json b/crates/roads/tests/fixtures/home/expected/router.json index 603357e..a52be62 100644 --- a/crates/roads/tests/fixtures/home/expected/router.json +++ b/crates/roads/tests/fixtures/home/expected/router.json @@ -31,6 +31,9 @@ { "app": "steam", "subnet": "0.0.0.0/0", + "except": [ + "100.96.0.2/32" + ], "roads": [ "e5yj1s" ] diff --git a/crates/roads/tests/fixtures/invalid/expect.txt b/crates/roads/tests/fixtures/invalid/expect.txt index f792de9..db7c940 100644 --- a/crates/roads/tests/fixtures/invalid/expect.txt +++ b/crates/roads/tests/fixtures/invalid/expect.txt @@ -46,6 +46,8 @@ road a00044-tab.md yaml.refused - road a00045-empty-until.md key.empty until road a00046-until-comment.md key.empty until road a00047-empty-ports.md key.empty ports +road a00048-direct-mesh.md direct.mesh object +road a00049-direct-tunnelled.md direct.tunnelled object directory unknown-field.directory.json snapshot.json - directory format-two.directory.json snapshot.format format directory bad-person-id.directory.json directory.id people[3].id diff --git a/crates/roads/tests/fixtures/invalid/roads/a00048-direct-mesh.md b/crates/roads/tests/fixtures/invalid/roads/a00048-direct-mesh.md new file mode 100644 index 0000000..a4add75 --- /dev/null +++ b/crates/roads/tests/fixtures/invalid/roads/a00048-direct-mesh.md @@ -0,0 +1,7 @@ +--- +id: a00048 +subject: person:jana +verb: goes-direct +object: subnet:100.96.0.0/24 +created: 2026-11-25T10:00:00+01:00 +--- diff --git a/crates/roads/tests/fixtures/invalid/roads/a00049-direct-tunnelled.md b/crates/roads/tests/fixtures/invalid/roads/a00049-direct-tunnelled.md new file mode 100644 index 0000000..e1cba32 --- /dev/null +++ b/crates/roads/tests/fixtures/invalid/roads/a00049-direct-tunnelled.md @@ -0,0 +1,7 @@ +--- +id: a00049 +subject: person:mark +verb: goes-direct +object: subnet:192.168.100.0/25 +created: 2026-11-25T10:00:00+01:00 +--- diff --git a/crates/roads/tests/qa_findings.rs b/crates/roads/tests/qa_findings.rs index a92fd89..814c379 100644 --- a/crates/roads/tests/qa_findings.rs +++ b/crates/roads/tests/qa_findings.rs @@ -52,7 +52,6 @@ fn f01_a_service_road_grants_only_that_service() { /// plan also routes into the tunnel is told to bypass it, on whatever network the device is on, and the /// kill switch does not stop it because the subnet is "private". #[test] -#[ignore = "QA F-02 (major): goes-direct can take mesh addresses and tunnelled subnets out of the tunnel"] fn f02_going_direct_never_overlaps_what_the_tunnel_carries() { let mark_pc = key(1); for subnet in ["100.96.0.0/16", "192.168.100.0/24", "192.168.0.0/16"] { @@ -77,15 +76,28 @@ fn f02_going_direct_never_overlaps_what_the_tunnel_carries() { .flat_map(|p| p.allowed_ips.iter()) .filter(|n| n.prefix_len() > 0) .collect(); + // Decision (owner session): a direct subnet that holds tunnelled nets keeps them in the tunnel, listed + // in `except`; one inside a tunnelled net is dropped; one holding a mesh address is refused. So every + // overlap must be covered by `except`. for d in &plan.direct { for n in &tunnelled { assert!( - !d.subnet.overlaps(n), + !d.subnet.overlaps(n) || d.except.iter().any(|e| e.contains(n)), "goes-direct {} overlaps {n}, which the plan sends through the tunnel", d.subnet ); } } + let refused = c + .problems + .iter() + .any(|p| p.path.contains("f02aaa") && p.code == roads::rules::DIRECT_MESH); + assert_eq!( + refused, + subnet == "100.96.0.0/16", + "{subnet}: {:?}", + c.problems + ); } } diff --git a/crates/roads/tests/validation.rs b/crates/roads/tests/validation.rs index ef5866b..635ba7d 100644 --- a/crates/roads/tests/validation.rs +++ b/crates/roads/tests/validation.rs @@ -43,6 +43,7 @@ fn inactive_code(code: &str) -> bool { rules::EXIT_CONFLICT, rules::DIRECT_KILL_SWITCH, rules::ROAD_FAMILY, + rules::DIRECT_TUNNELLED, ] .contains(&code) } diff --git a/docs/FORMATS.md b/docs/FORMATS.md index c488042..6d04192 100644 --- a/docs/FORMATS.md +++ b/docs/FORMATS.md @@ -57,7 +57,7 @@ is not `key: value`, or a bad quoted string, is `yaml.syntax`. | `id` | yes | 6 characters of `0123456789abcdefghjkmnpqrstvwxyz` (no `i l o u`), random | `id.format`; must equal the file name's prefix (`id.file-name`); unique in the store (`id.duplicate`, both files grant nothing) | | `subject` | yes | a reference (below): `person:`, `group:`, `device:` or `app:` | `ref.format`, `subject.kind` (not allowed for the verb), `subject.unknown` (not in the snapshots) | | `verb` | yes | `can-reach`, `exits-through`, `goes-direct`, `never-online-without`, `custom` | `verb.unknown` | -| `object` | yes, except `custom` (where it is not allowed) | a reference: `device:`, `subnet:`, `service:` or `exit:` | `ref.format`, `object.kind`, `object.unknown`, `object.not-exit`, `subnet.not-canonical`, `subnet.unrouted` | +| `object` | yes, except `custom` (where it is not allowed) | a reference: `device:`, `subnet:`, `service:` or `exit:` | `ref.format`, `object.kind`, `object.unknown`, `object.not-exit`, `subnet.not-canonical`, `subnet.unrouted`; a `goes-direct` subnet other than `/0` that holds a mesh address is `direct.mesh` (mesh traffic never goes direct, QA F-02) | | `until` | no | RFC 3339 with an offset: `2026-11-29T23:59:59+01:00` or `…Z`; exclusive | `until.format`; `until <= now` makes the road *inactive* (`road.expired`) | | `ports` | no; only `can-reach` | a list of `tcp/443`, `udp/53`, `tcp/8000-8100`, `tcp`, `udp`, `icmp`; absent means all traffic | `ports.format`; for a service, every port must be one it serves (`ports.not-served`) | | `via` | no; only `can-reach` | `device:` of a device with `relay` or `exit` | `via.format`, `via.unknown`, `via.not-relay` | @@ -125,6 +125,7 @@ Valid roads that grant nothing right now, reported with their reason (not errors | `road.no-devices` | the subject has no devices | | `road.self` | every subject device is the object's own device | | `exit.conflict` | for one device: active `exits-through` roads to two different exits (that device gets neither; reported per road, naming the device) | +| `direct.tunnelled` | for one device: a `goes-direct` subnet inside a net its plan sends through the tunnel (a route it reaches, a peer's address); the tunnel wins and the entry is dropped (reported per road, naming the device, QA F-02) | | `road.family` | a `can-reach` road from devices whose mesh address is IPv4 to an IPv6 place, or the other way: those devices get nothing (a rule that could never match), reported naming them (QA F-06) | | `direct.kill-switch` | for one device with a kill switch: a `goes-direct` road to a subnet that reaches the internet; the kill switch wins and the subnet stays in the tunnel (reported per road, naming the device). Going direct to a private subnet stays allowed | @@ -201,7 +202,7 @@ entry. | `dns[]` | `host`, `address`, `roads`: names this device may resolve to a mesh address | | `exit` | `{through, roads}` or `null` | | `kill_switch` | `{through, roads}` or `null`. `through` is `null` (only the mesh) when two kill-switch roads disagree, or when a kill-switch road has an error: a restriction fails closed, it is never dropped (QA F-03). `roads` lists the erroneous road too | -| `direct[]` | `{app, subnet, roads}`: traffic to `subnet` bypasses the tunnel (split tunnelling); `app` is `null` for all traffic, or the one app whose traffic goes direct | +| `direct[]` | `{app, subnet, except, roads}`: traffic to `subnet` bypasses the tunnel (split tunnelling); `app` is `null` for all traffic, or the one app whose traffic goes direct. `except` lists the nets inside `subnet` that this plan sends through the tunnel (its peers' non-default allowed IPs): they stay in the tunnel, whatever the platform's route order. A platform that cannot keep them in (an app-level bypass) must not apply the entry (QA F-02) | | `custom[]` | `{road, classic, raw}`: the raw block verbatim, **never checked by the compiler** | `internet` is every address except `0.0.0.0/8`, `10.0.0.0/8`, `100.64.0.0/10`, `127.0.0.0/8`, `169.254.0.0/16`,