Day 2: roads, the compiler, explain, CBOR control messages, the via command #1
@@ -1,7 +1,8 @@
|
||||
//! The compiler: roads + people directory + device list + `now` → one [`Plan`] per device.
|
||||
//!
|
||||
//! Deny by default. A road with any error grants nothing; a valid road that is expired, or whose subject has no
|
||||
//! devices, grants nothing and is reported as inactive. Without both snapshots no plan is produced at all.
|
||||
//! devices, grants nothing and is reported as inactive. Restrictions fail closed instead: a kill-switch road
|
||||
//! with an error still turns its subject's kill switch on, through no exit (only the mesh), QA F-03. Without both snapshots no plan is produced at all.
|
||||
//! Every collection on the output path is ordered, so the same input gives the same bytes.
|
||||
|
||||
use crate::id::RoadId;
|
||||
@@ -83,9 +84,17 @@ type RuleKey = (Direction, Net, Dst, Proto, Option<[u16; 2]>);
|
||||
struct Extras {
|
||||
direct: BTreeMap<DirectKey, BTreeSet<RoadId>>,
|
||||
kill: BTreeMap<DeviceKey, BTreeSet<RoadId>>,
|
||||
/// Kill-switch roads with an error: applied closed, through no exit.
|
||||
kill_closed: BTreeSet<RoadId>,
|
||||
custom: Vec<plan::Custom>,
|
||||
}
|
||||
|
||||
impl Extras {
|
||||
fn has_kill_switch(&self) -> bool {
|
||||
!self.kill.is_empty() || !self.kill_closed.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
/// The devices a subject covers: a person's are the devices they own; a group's, its members'; an app's, every
|
||||
/// device.
|
||||
pub fn subject_devices(subject: &Subject, dir: &Directory, devs: &Devices) -> BTreeSet<DeviceKey> {
|
||||
@@ -318,6 +327,27 @@ pub fn compile(store: &Store, now: Instant) -> Compiled {
|
||||
let problems = check(road, dir, devs, &subject);
|
||||
if !problems.is_empty() {
|
||||
out.problems.extend(problems);
|
||||
// "Grants nothing" is right for a grant and wrong for a restriction: a kill switch whose exit stops
|
||||
// being one, or leaves devices.json, must not switch the protection off (QA F-03). It stays on,
|
||||
// through no exit: only the mesh. An expired kill switch ends as written; one that does not parse
|
||||
// is not in the store at all (`via check` reports it).
|
||||
if road.verb == Verb::NeverOnlineWithout {
|
||||
let exit = match &road.object {
|
||||
Some(Place::Exit(k)) => Some(k),
|
||||
_ => None,
|
||||
};
|
||||
let live = road.until.as_ref().is_none_or(|u| u.at() > now);
|
||||
for d in subject.iter().filter(|d| Some(*d) != exit) {
|
||||
if live {
|
||||
touch(d, road);
|
||||
extras
|
||||
.entry(d.clone())
|
||||
.or_default()
|
||||
.kill_closed
|
||||
.insert(road.id.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let inactive = |field: Option<&str>, code, msg: String| {
|
||||
@@ -511,10 +541,18 @@ pub fn compile(store: &Store, now: Instant) -> Compiled {
|
||||
// traffic outside the tunnel, whatever a goes-direct road says (QA R-47). Going direct to a private subnet
|
||||
// (the local network) stays allowed.
|
||||
for (key, ex) in extras.iter_mut() {
|
||||
if ex.kill.is_empty() {
|
||||
if !ex.has_kill_switch() {
|
||||
continue;
|
||||
}
|
||||
let kill_roads: Vec<String> = ex.kill.values().flatten().map(|r| r.to_string()).collect();
|
||||
let kill_roads: Vec<String> = ex
|
||||
.kill
|
||||
.values()
|
||||
.flatten()
|
||||
.chain(&ex.kill_closed)
|
||||
.collect::<BTreeSet<_>>()
|
||||
.into_iter()
|
||||
.map(|r| r.to_string())
|
||||
.collect();
|
||||
let dropped: Vec<(DirectKey, BTreeSet<RoadId>)> = ex
|
||||
.direct
|
||||
.iter()
|
||||
@@ -673,8 +711,10 @@ fn build_plan(
|
||||
}
|
||||
|
||||
let extras = extras.unwrap_or_default();
|
||||
let kill_switch = (!extras.kill.is_empty()).then(|| plan::KillSwitch {
|
||||
through: if extras.kill.len() == 1 {
|
||||
let kill_switch = extras.has_kill_switch().then(|| plan::KillSwitch {
|
||||
// One exit, from valid roads only: through it. Two exits, or any kill-switch road with an error: only
|
||||
// the mesh.
|
||||
through: if extras.kill.len() == 1 && extras.kill_closed.is_empty() {
|
||||
extras.kill.keys().next().cloned()
|
||||
} else {
|
||||
None
|
||||
@@ -683,6 +723,7 @@ fn build_plan(
|
||||
.kill
|
||||
.values()
|
||||
.flatten()
|
||||
.chain(&extras.kill_closed)
|
||||
.cloned()
|
||||
.collect::<BTreeSet<_>>()
|
||||
.into_iter()
|
||||
|
||||
@@ -93,7 +93,6 @@ fn f02_going_direct_never_overlaps_what_the_tunnel_carries() {
|
||||
/// typo: the exit device stops offering itself as an exit (or leaves devices.json), and every kill switch
|
||||
/// through it silently disappears while the rest of the plan compiles.
|
||||
#[test]
|
||||
#[ignore = "QA F-03 (major): an invalid kill-switch road fails open"]
|
||||
fn f03_a_kill_switch_whose_exit_goes_away_fails_closed() {
|
||||
let parts = Parts::home().edit_devices(|d| {
|
||||
d.replace(
|
||||
@@ -109,10 +108,48 @@ fn f03_a_kill_switch_whose_exit_goes_away_fails_closed() {
|
||||
.any(|p| p.path.contains("w9c3ke") && p.is_error()),
|
||||
"the kill-switch road is reported"
|
||||
);
|
||||
let ks = c.plans[&key(1)].kill_switch.as_ref();
|
||||
assert!(
|
||||
c.plans[&key(1)].kill_switch.is_some(),
|
||||
ks.is_some(),
|
||||
"mark-pc lost its kill switch because its exit stopped being one: it now fails open"
|
||||
);
|
||||
assert_eq!(
|
||||
ks.unwrap().through,
|
||||
None,
|
||||
"closed means through no exit: only the mesh"
|
||||
);
|
||||
// Mutation guard: the same road made valid again keeps its exit.
|
||||
assert_eq!(
|
||||
Parts::home().compile().plans[&key(1)]
|
||||
.kill_switch
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.through,
|
||||
Some(key(2))
|
||||
);
|
||||
}
|
||||
|
||||
/// F-03, the other references: an unknown exit, and an exit road that has expired, which ends as written.
|
||||
#[test]
|
||||
fn f03_an_unknown_exit_fails_closed_and_an_expired_kill_switch_ends() {
|
||||
let text = Parts::home().road_text("w9c3ke");
|
||||
let unknown = text.replace(
|
||||
"exit:AgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgI",
|
||||
"exit:CQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQk",
|
||||
);
|
||||
let c = Parts::home().replace_road("w9c3ke", &unknown).compile();
|
||||
assert!(
|
||||
c.problems
|
||||
.iter()
|
||||
.any(|p| p.path.contains("w9c3ke") && p.is_error())
|
||||
);
|
||||
assert_eq!(c.plans[&key(1)].kill_switch.as_ref().unwrap().through, None);
|
||||
let expired = unknown.replace("created:", "until: 2026-11-01T00:00:00+01:00\ncreated:");
|
||||
let c = Parts::home().replace_road("w9c3ke", &expired).compile();
|
||||
assert!(
|
||||
c.plans[&key(1)].kill_switch.is_none(),
|
||||
"an expired kill switch ends as written"
|
||||
);
|
||||
}
|
||||
|
||||
/// F-04. Deleting a person from the directory (the upstream event, without also editing devices.json) leaves
|
||||
|
||||
+1
-1
@@ -199,7 +199,7 @@ entry.
|
||||
| `firewall.rules[]` | `direction` (`in`: to this device's address; `forward`: to a subnet it routes, or to the internet as an exit), `src` (CIDR), `dst` (CIDR or `"internet"`), `proto` (`any`, `tcp`, `udp`, `icmp`), `ports` (`[lo, hi]` or `null`), `roads` |
|
||||
| `dns[]` | `host`, `address`, `roads`: names this device may resolve to a mesh address |
|
||||
| `exit` | `{through, roads}` or `null` |
|
||||
| `kill_switch` | `{through, roads}` (`through` is `null` when two kill-switch roads disagree: only the mesh) 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 |
|
||||
| `custom[]` | `{road, classic, raw}`: the raw block verbatim, **never checked by the compiler** |
|
||||
|
||||
|
||||
@@ -6,7 +6,9 @@
|
||||
//! point (`render(parse(render(r))) == render(r)`);
|
||||
//! 3. `compile` never panics (its `expect`/`unreachable!` paths assume the parser's checks);
|
||||
//! 4. deny by default: a file that does not parse, or a road the compiler reports an error for, leaves every
|
||||
//! plan byte-identical to the home store without it.
|
||||
//! plan byte-identical to the home store without it. The one exception fails closed (QA F-03): a kill-switch
|
||||
//! road with an error may turn a kill switch on through no exit, and so drop going direct to the internet,
|
||||
//! and nothing else.
|
||||
//!
|
||||
//! Run on Linux with nightly: `cargo fuzz run road_file -- -max_total_time=600`.
|
||||
//!
|
||||
@@ -80,6 +82,17 @@ fn baseline() -> &'static Vec<String> {
|
||||
})
|
||||
}
|
||||
|
||||
fn baseline_plans() -> &'static std::collections::BTreeMap<roads::DeviceKey, roads::plan::Plan> {
|
||||
static B: OnceLock<std::collections::BTreeMap<roads::DeviceKey, roads::plan::Plan>> = OnceLock::new();
|
||||
B.get_or_init(|| {
|
||||
compile(
|
||||
&Store::from_parts(&files(None), Some(DIRECTORY), Some(DEVICES)),
|
||||
now(),
|
||||
)
|
||||
.plans
|
||||
})
|
||||
}
|
||||
|
||||
/// The id the file claims, so the file name can match it and the fuzzer reaches past `id.file-name`.
|
||||
fn claimed_id(data: &[u8]) -> String {
|
||||
std::str::from_utf8(data)
|
||||
@@ -142,6 +155,30 @@ fuzz_target!(|data: &[u8]| {
|
||||
let collides = HOME.iter().any(|(n, _)| n.starts_with(&format!("{id}-")));
|
||||
let refused = parsed.is_err() || c.errors().any(|p| p.path == path);
|
||||
if refused && !collides {
|
||||
assert_eq!(&plans, baseline(), "a refused road changed a plan");
|
||||
let kill = parsed
|
||||
.as_ref()
|
||||
.is_ok_and(|r| r.verb == roads::Verb::NeverOnlineWithout);
|
||||
if kill {
|
||||
let base = baseline_plans();
|
||||
for (k, p) in &c.plans {
|
||||
let b = &base[k];
|
||||
let mut p = p.clone();
|
||||
let ks = p.kill_switch.take();
|
||||
assert!(
|
||||
ks.as_ref().is_none_or(|ks| ks.through.is_none()) || ks == b.kill_switch,
|
||||
"a refused kill switch named an exit"
|
||||
);
|
||||
assert!(
|
||||
p.direct.iter().all(|d| b.direct.contains(d)),
|
||||
"a refused kill switch added a direct subnet"
|
||||
);
|
||||
p.direct = b.direct.clone();
|
||||
p.kill_switch = b.kill_switch.clone();
|
||||
p.expires = b.expires.clone();
|
||||
assert_eq!(&p, b, "a refused kill-switch road changed more than the kill switch");
|
||||
}
|
||||
} else {
|
||||
assert_eq!(&plans, baseline(), "a refused road changed a plan");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user