From fea6836f9eb96e696c7f1d8ced080b8ce8bf1edd Mon Sep 17 00:00:00 2001 From: jochen Date: Sun, 22 Nov 2026 00:11:18 +0100 Subject: [PATCH] E6b: cut a derived rule's comment to nftables' 128 bytes with a count MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit nftables refuses a rule comment past 128 bytes, and with it the whole table, so a port shared by enough modules took every opening down. The comment is a label: past the bound its module list is cut with a count of the rest; the whole list stands in the table's lines above the rule. Builds: design 47 H2 §813 (4); H-A-357, H-A-384 (1) --- internal/compose/resolution/filtering.go | 17 ++++++++++++++--- .../compose/resolution/localnetwork_test.go | 10 ++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/internal/compose/resolution/filtering.go b/internal/compose/resolution/filtering.go index e3bdab86..864f4071 100644 --- a/internal/compose/resolution/filtering.go +++ b/internal/compose/resolution/filtering.go @@ -154,12 +154,23 @@ var scopeWords = map[string]string{ mcat.FromLocalNetwork: "local-network", } +// commentMost is the most bytes nftables takes in a rule's comment; a longer one refuses the whole table. +const commentMost = 128 + // 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. +// the table is loaded whole, never reconciled rule by rule. The comment is a label: past commentMost the module list is +// cut with a count of the rest, `derived a,b,+4 tcp/443 scope any`, and the whole list stands in the table's lines +// above the rule (H-A-384 (1)). 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])) + label := func(names []string) string { + return fmt.Sprintf("derived %s %s/%d scope %s", strings.Join(names, ","), rule.Protocol, rule.Port, scopeWords[rule.From]) + } + text := label(rule.Because) + for kept := len(rule.Because) - 1; len(text) > commentMost && kept >= 0; kept-- { + text = label(append(slices.Clone(rule.Because[:kept]), fmt.Sprintf("+%d", len(rule.Because)-kept))) + } + return fmt.Sprintf(" comment %q", text) } // widest drops a rule that another already covers. diff --git a/internal/compose/resolution/localnetwork_test.go b/internal/compose/resolution/localnetwork_test.go index a289eede..8fe35db7 100644 --- a/internal/compose/resolution/localnetwork_test.go +++ b/internal/compose/resolution/localnetwork_test.go @@ -84,6 +84,9 @@ func TestALocalNetworkPortIsAdmittedFromItsNetworksAndTheMesh(t *testing.T) { {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"}}, + // A shared port whose module list would pass nftables' 128 bytes: the label is cut with a count (H-A-384 (1)). + {Port: 8443, Protocol: "tcp", From: mcat.FromEverywhere, Because: []string{"mesh-delivery-pipeline", "mesh-delivery-builder", + "mesh-delivery-assigner", "mesh-delivery-indexer", "mesh-delivery-gate", "mesh-delivery-teller"}}, } local := CountedLocalNetworks(judged(t)) out := AsNftablesWithLocal(rules, []string{"10.10.0.1", "10.10.0.4"}, false, nil, @@ -102,11 +105,18 @@ func TestALocalNetworkPortIsAdmittedFromItsNetworksAndTheMesh(t *testing.T) { `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", + `tcp dport 8443 accept comment "derived mesh-delivery-pipeline,mesh-delivery-builder,mesh-delivery-assigner,mesh-delivery-indexer,+2 tcp/8443 scope any"`, + "\t\t# mesh-delivery-teller\n", } { if !strings.Contains(out, want) { t.Errorf("the rule set lacks %q:\n%s", want, out) } } + for _, line := range strings.Split(out, "\n") { + if _, comment, ok := strings.Cut(line, ` comment "`); ok && len(strings.TrimSuffix(comment, `"`)) > commentMost { + t.Errorf("a comment passes nftables' %d bytes: %s", commentMost, line) + } + } if strings.Contains(out, "203.0.113") { t.Errorf("the public subnet reached the rule set:\n%s", out) }