diff --git a/modules/nftables/allowances.ts b/modules/nftables/allowances.ts index ce60ceb39..69826a2c6 100644 --- a/modules/nftables/allowances.ts +++ b/modules/nftables/allowances.ts @@ -324,8 +324,8 @@ export class Allowances { const until = untilOf(args.until, now); const why = whyOf(args.why); if (!source && privateOnly(await this.asks.meshTable(), protocol, port)) { - refuse("port", `${protocol} ${port} is admitted by a derived rule of the loaded mesh table from given sources only (the private network's, such as the store's ` + - "port or the broker's management port); an allowance from any source would widen it: give a source"); + refuse("port", `${protocol} ${port} is admitted by a derived rule of the loaded mesh table from given sources only (its scope private-network or ` + + "local-network, such as the store's port or the broker's management port); an allowance from any source would widen it: give a source"); } const most = await this.asks.most(); const kept = await this.ownKeys(); @@ -442,23 +442,27 @@ export function markLoaded(allowances: Allowance[], meshTable: string): { }; } -/** Whether the loaded mesh table's input chain accepts `protocol` `port` only with a source match (`ip saddr` or `ip6 - * saddr`) and never from any source: a port a derived rule admits from the private network only (§3, H-A-315 (7), - * H-A-319). */ -export function privateOnly(meshTable: string, protocol: Protocol, port: number): boolean { - const input = /chain input \{([\s\S]*?)\n\t?\}/.exec(meshTable)?.[1] ?? ""; - let withSource = false; - for (const line of input.split("\n")) { - if (!/\baccept\b/.test(line) || !line.includes(`${protocol} dport `)) continue; - const ports = /dport (\{[^}]*\}|\S+)/.exec(line)?.[1] ?? ""; - const names = ports.replace(/[{}]/g, "").split(",").map((p) => p.trim()).filter(Boolean); - const named = names.some((p) => { - const [lo, hi] = p.split("-").map(Number); - return hi === undefined ? lo === port : lo <= port && port <= hi; - }); - if (!named) continue; - if (!/\bip6? saddr\b/.test(line)) return false; - withSource = true; +/** The scopes the node-engine's render writes on a derived accept rule (design 47 H2 §813 (4), H-A-357). */ +export type Scope = "any" | "private-network" | "local-network"; + +/** + * The scopes the loaded mesh table's derived accept rules give `protocol` `port`, read from each rule's one comment + * `derived / scope ` (design 47 H2 §813 (4), + * H-A-357), the module list cut with a count past 128 bytes (`derived a,b,+4 tcp/443 scope any`, H-A-384 (1)); never + * from the rule's shape. A port is one port or a range `lo-hi`. + */ +export function portScopes(meshTable: string, protocol: Protocol, port: number): Scope[] { + const out: Scope[] = []; + for (const m of meshTable.matchAll(/comment "derived \S+ (tcp|udp)\/(\d+)(?:-(\d+))? scope (any|private-network|local-network)"/g)) { + const lo = Number(m[2]), hi = m[3] === undefined ? lo : Number(m[3]); + if (m[1] === protocol && lo <= port && port <= hi) out.push(m[4] as Scope); } - return withSource; + return out; +} + +/** Whether a derived rule admits `protocol` `port` from given sources only (the private network or the local network) + * and none from any source: its scope read from the rules' comments (§813 (4), H-A-315 (7), H-A-319). */ +export function privateOnly(meshTable: string, protocol: Protocol, port: number): boolean { + const scopes = portScopes(meshTable, protocol, port); + return scopes.length > 0 && !scopes.includes("any"); } diff --git a/modules/nftables/test/allowances.test.ts b/modules/nftables/test/allowances.test.ts index f905db821..44b0342a5 100644 --- a/modules/nftables/test/allowances.test.ts +++ b/modules/nftables/test/allowances.test.ts @@ -10,10 +10,18 @@ import { interface Timer { ms: number; fire: () => void; cancelled: boolean } -// The loaded mesh table: the store's port from the private network only, 80 from anywhere and 8443 from both. +// The loaded mesh table: the store's port from the private network only, 80 from anywhere and 8443 from both; each derived +// accept rule carries the one comment the node-engine's render writes, `derived / scope ` +// (47 H2 §813 (4), H-A-357), its module list cut with a count past 128 bytes (H-A-384 (1)). const TABLE = 'table inet mesh {\n\tchain input {\n\t\ttype filter hook input priority filter; policy drop;\n' + - '\t\tip saddr { 10.77.0.1, 10.77.0.2 } tcp dport 5432 accept\n\t\tip6 saddr { fd77::1 } tcp dport 5432 accept\n' + - '\t\ttcp dport 80 accept\n\t\tip saddr { 10.77.0.1 } tcp dport { 8443, 9500-9600 } accept\n\t\ttcp dport 8443 accept\n\t}\n}\n'; + '\t\tip saddr { 10.77.0.1, 10.77.0.2 } tcp dport 5432 accept comment "derived postgres tcp/5432 scope private-network"\n' + + '\t\tip6 saddr { fd77::1 } tcp dport 5432 accept comment "derived postgres tcp/5432 scope private-network"\n' + + '\t\ttcp dport 80 accept comment "derived route-proxy tcp/80 scope any"\n' + + '\t\tip saddr { 10.77.0.1 } tcp dport 8443 accept comment "derived a,b,+4 tcp/8443 scope private-network"\n' + + '\t\ttcp dport 8443 accept comment "derived c tcp/8443 scope any"\n' + + '\t\tip saddr { 192.168.1.0/24 } tcp dport 9500-9600 accept comment "derived nats tcp/9500-9600 scope local-network"\n' + + '\t\ttcp dport 7000 accept comment "derived x,+2 tcp/7000 scope private-network"\n' + + '\t\tip saddr { 10.77.0.1 } tcp dport 7100 accept\n\t}\n}\n'; // gate, when given, holds every delete until it opens, and every get waits a turn of the event loop: a slow store. function fake(at = "2026-11-21T20:00:00Z", node = "shanks", kept = new Map(), gate?: Promise) { @@ -189,9 +197,12 @@ test("allowed asks converged again while none answered, and refuses when it does assert.equal(asks.asked, asked + 2, "an answer held is not asked again"); }); -test("privateOnly: a port accepted only with a source match, never from any source", () => { +test("privateOnly: a port a derived rule's comment scopes to the private or the local network, none to any", () => { assert.equal(privateOnly(TABLE, "tcp", 5432), true); - assert.equal(privateOnly(TABLE, "tcp", 9550), true, "a range of a set"); + assert.equal(privateOnly(TABLE, "tcp", 9550), true, "a range, local-network"); + // 47 H2 §813 (4): the scope is read from the rule's comment, never from its shape. + assert.equal(privateOnly(TABLE, "tcp", 7000), true, "a comment cut with a count says private-network, whatever the shape"); + assert.equal(privateOnly(TABLE, "tcp", 7100), false, "a rule with a source match and no derived comment names no scope"); assert.equal(privateOnly(TABLE, "tcp", 80), false); assert.equal(privateOnly(TABLE, "tcp", 8443), false, "accepted from anywhere too"); assert.equal(privateOnly(TABLE, "udp", 5432), false);