From 6b3139218d9e0998f105dd0361fd1f5fa6ed6383 Mon Sep 17 00:00:00 2001 From: jochen Date: Sat, 21 Nov 2026 23:07:16 +0100 Subject: [PATCH] E6: mark each derived accept rule's scope in its one comment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The holder of node-packet-filter reads a port's scope from rules, not from the rule's shape; the older firewall's mesh-host mark stays. Builds: design 47 H2 §813 (4); H-A-357 --- internal/compose/resolution/filtering.go | 53 ++++++++++++------- .../compose/resolution/localnetwork_test.go | 10 +++- 2 files changed, 44 insertions(+), 19 deletions(-) diff --git a/internal/compose/resolution/filtering.go b/internal/compose/resolution/filtering.go index 7d244245..e3bdab86 100644 --- a/internal/compose/resolution/filtering.go +++ b/internal/compose/resolution/filtering.go @@ -145,6 +145,23 @@ func Exposure(m mcat.Manifest, layers []mcat.Layer) (map[int]string, error) { return out, nil } +// scopeWords are the words a derived accept rule's comment names its source by (design 47 H2 §813 (4), H-A-357): the +// holder of node-packet-filter reads a port's scope from them, never from the rule's shape. A machine-only rule renders +// no rule, so it has none. +var scopeWords = map[string]string{ + mcat.FromEverywhere: "any", + mcat.FromMesh: "private-network", + mcat.FromLocalNetwork: "local-network", +} + +// derivedComment is the one comment of a derived accept rule, `derived / scope +// `, with the space before it (design 47 H2 §813 (4), H-A-357). It carries no mark: +// the table is loaded whole, never reconciled rule by rule. +func derivedComment(rule mcat.Rule) string { + return fmt.Sprintf(" comment %q", fmt.Sprintf("derived %s %s/%d scope %s", + strings.Join(rule.Because, ","), rule.Protocol, rule.Port, scopeWords[rule.From])) +} + // widest drops a rule that another already covers. // // A port open to anywhere is not additionally restricted by a second rule opening it to the mesh: @@ -374,12 +391,12 @@ func AsNftablesAllowing(rules []mcat.Rule, mesh []string, outward bool, foundati } four, six := byFamily(mesh) if len(four) > 0 { - b.WriteString(fmt.Sprintf("\t\tip saddr { %s } %s dport %d accept\n", - strings.Join(four, ", "), rule.Protocol, rule.Port)) + b.WriteString(fmt.Sprintf("\t\tip saddr { %s } %s dport %d accept%s\n", + strings.Join(four, ", "), rule.Protocol, rule.Port, derivedComment(rule))) } if len(six) > 0 { - b.WriteString(fmt.Sprintf("\t\tip6 saddr { %s } %s dport %d accept\n", - strings.Join(six, ", "), rule.Protocol, rule.Port)) + b.WriteString(fmt.Sprintf("\t\tip6 saddr { %s } %s dport %d accept%s\n", + strings.Join(six, ", "), rule.Protocol, rule.Port, derivedComment(rule))) } case mcat.FromLocalNetwork: // The private network and the machine's own local networks, on this port alone (novox/hq @@ -399,15 +416,15 @@ func AsNftablesAllowing(rules []mcat.Rule, mesh []string, outward bool, foundati break } if len(four) > 0 { - b.WriteString(fmt.Sprintf("\t\tip saddr { %s } %s dport %d accept\n", - strings.Join(four, ", "), rule.Protocol, rule.Port)) + b.WriteString(fmt.Sprintf("\t\tip saddr { %s } %s dport %d accept%s\n", + strings.Join(four, ", "), rule.Protocol, rule.Port, derivedComment(rule))) } if len(six) > 0 { - b.WriteString(fmt.Sprintf("\t\tip6 saddr { %s } %s dport %d accept\n", - strings.Join(six, ", "), rule.Protocol, rule.Port)) + b.WriteString(fmt.Sprintf("\t\tip6 saddr { %s } %s dport %d accept%s\n", + strings.Join(six, ", "), rule.Protocol, rule.Port, derivedComment(rule))) } case mcat.FromEverywhere: - b.WriteString(fmt.Sprintf("\t\t%s dport %d accept\n", rule.Protocol, rule.Port)) + b.WriteString(fmt.Sprintf("\t\t%s dport %d accept%s\n", rule.Protocol, rule.Port, derivedComment(rule))) } } @@ -521,27 +538,27 @@ func AsNftablesAllowing(rules []mcat.Rule, mesh []string, outward bool, foundati case mcat.FromMesh: four, six := byFamily(mesh) if len(four) > 0 { - b.WriteString(fmt.Sprintf("\t\tip saddr { %s } ct original proto-dst %d accept\n", - strings.Join(four, ", "), rule.Port)) + b.WriteString(fmt.Sprintf("\t\tip saddr { %s } ct original proto-dst %d accept%s\n", + strings.Join(four, ", "), rule.Port, derivedComment(rule))) } if len(six) > 0 { - b.WriteString(fmt.Sprintf("\t\tip6 saddr { %s } ct original proto-dst %d accept\n", - strings.Join(six, ", "), rule.Port)) + b.WriteString(fmt.Sprintf("\t\tip6 saddr { %s } ct original proto-dst %d accept%s\n", + strings.Join(six, ", "), rule.Port, derivedComment(rule))) } case mcat.FromLocalNetwork: four, six := byFamily(mesh) lfour, lsix := byFamily(local) four, six = append(four, lfour...), append(six, lsix...) if len(four) > 0 { - b.WriteString(fmt.Sprintf("\t\tip saddr { %s } ct original proto-dst %d accept\n", - strings.Join(four, ", "), rule.Port)) + b.WriteString(fmt.Sprintf("\t\tip saddr { %s } ct original proto-dst %d accept%s\n", + strings.Join(four, ", "), rule.Port, derivedComment(rule))) } if len(six) > 0 { - b.WriteString(fmt.Sprintf("\t\tip6 saddr { %s } ct original proto-dst %d accept\n", - strings.Join(six, ", "), rule.Port)) + b.WriteString(fmt.Sprintf("\t\tip6 saddr { %s } ct original proto-dst %d accept%s\n", + strings.Join(six, ", "), rule.Port, derivedComment(rule))) } case mcat.FromEverywhere: - b.WriteString(fmt.Sprintf("\t\tct original proto-dst %d accept\n", rule.Port)) + b.WriteString(fmt.Sprintf("\t\tct original proto-dst %d accept%s\n", rule.Port, derivedComment(rule))) } } diff --git a/internal/compose/resolution/localnetwork_test.go b/internal/compose/resolution/localnetwork_test.go index fb604172..caaa61db 100644 --- a/internal/compose/resolution/localnetwork_test.go +++ b/internal/compose/resolution/localnetwork_test.go @@ -69,6 +69,7 @@ func TestALocalNetworkPortIsAdmittedFromItsNetworksAndTheMesh(t *testing.T) { rules := []mcat.Rule{ {Port: 1883, Protocol: "tcp", From: mcat.FromLocalNetwork, Because: []string{"mosquitto"}}, {Port: 5432, Protocol: "tcp", From: mcat.FromMesh, Because: []string{"postgres"}}, + {Port: 443, Protocol: "tcp", From: mcat.FromEverywhere, Because: []string{"caddy", "forge"}}, } local := CountedLocalNetworks(judged(t)) out := AsNftablesWithLocal(rules, []string{"10.10.0.1", "10.10.0.4"}, false, nil, @@ -80,6 +81,13 @@ func TestALocalNetworkPortIsAdmittedFromItsNetworksAndTheMesh(t *testing.T) { "ip saddr { 10.10.0.1, 10.10.0.4, 192.168.1.0/24 } ct original proto-dst 1883 accept", // The mesh-only port is not widened by the machine having a local network. "ip saddr { 10.10.0.1, 10.10.0.4 } tcp dport 5432 accept", + // Each derived accept rule names its modules, its listen and its scope in one comment (design 47 H2 §813 (4), + // H-A-357), in both chains. + `tcp dport 1883 accept comment "derived mosquitto tcp/1883 scope local-network"`, + `ct original proto-dst 1883 accept comment "derived mosquitto tcp/1883 scope local-network"`, + `tcp dport 5432 accept comment "derived postgres tcp/5432 scope private-network"`, + "\t\ttcp dport 443 accept comment \"derived caddy,forge tcp/443 scope any\"\n", + "\t\tct original proto-dst 443 accept comment \"derived caddy,forge tcp/443 scope any\"\n", } { if !strings.Contains(out, want) { t.Errorf("the rule set lacks %q:\n%s", want, out) @@ -92,7 +100,7 @@ func TestALocalNetworkPortIsAdmittedFromItsNetworksAndTheMesh(t *testing.T) { t.Errorf("1883 is open to anywhere:\n%s", out) } for _, line := range strings.Split(out, "\n") { - if strings.TrimSpace(line) == "tcp dport 1883 accept" { + if strings.HasPrefix(strings.TrimSpace(line), "tcp dport 1883 accept") { t.Fatalf("1883 is open to anywhere:\n%s", out) } }