P1: the packet filter holder reads a port's scope from its derived rule's comment

A derived accept rule's one comment says derived <modules> <protocol>/<port>
scope <any|private-network|local-network>, its module list cut with a count
past 128 bytes; allow without a source is refused for a port whose derived
rules name no scope any, read from the comments, never from a rule's shape.

Builds: design 47 H2 §813 (4); H-A-357, H-A-384 (1), H-A-349 DQ-11
This commit is contained in:
2026-11-22 00:55:14 +01:00
parent 6b17965d98
commit ee5e7f232f
2 changed files with 40 additions and 25 deletions
+24 -20
View File
@@ -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 <modules joined by ,> <protocol>/<port> scope <any|private-network|local-network>` (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");
}