From df63fd1568a4771710ba0bab12d096846d7929ff Mon Sep 17 00:00:00 2001 From: AdamMagued Date: Wed, 11 Nov 2026 11:22:11 +0200 Subject: [PATCH] httpcaddyfile: order automation policies by subject specificity Fixes #8300 Consolidate automation policies in Caddyfile adaptation by evaluating subject specificity rather than subject counts: - Exact domain matches take precedence over overlapping wildcards - Policies with subjects take precedence over catch-all policies - Insertion order is preserved for disjoint or equally specific sets - Conflicting bidirectional specificity requirements are rejected with an error --- caddyconfig/httpcaddyfile/tlsapp.go | 176 ++++++++++++++++++--- caddyconfig/httpcaddyfile/tlsapp_test.go | 187 +++++++++++++++++++++++ 2 files changed, 342 insertions(+), 21 deletions(-) diff --git a/caddyconfig/httpcaddyfile/tlsapp.go b/caddyconfig/httpcaddyfile/tlsapp.go index 95aa9983b..543033962 100644 --- a/caddyconfig/httpcaddyfile/tlsapp.go +++ b/caddyconfig/httpcaddyfile/tlsapp.go @@ -538,7 +538,11 @@ func (st ServerType) buildTLSApp( } // consolidate automation policies that are the exact same - tlsApp.Automation.Policies = consolidateAutomationPolicies(tlsApp.Automation.Policies) + var err error + tlsApp.Automation.Policies, err = consolidateAutomationPolicies(tlsApp.Automation.Policies) + if err != nil { + return nil, warnings, err + } // ensure automation policies don't overlap subjects (this should be // an error at provision-time as well, but catch it in the adapt phase @@ -1064,17 +1068,38 @@ func newBaseAutomationPolicy( // consolidateAutomationPolicies combines automation policies that are the same, // for a cleaner overall output. -func consolidateAutomationPolicies(aps []*caddytls.AutomationPolicy) []*caddytls.AutomationPolicy { - // sort from most specific to least specific; we depend on this ordering - sort.SliceStable(aps, func(i, j int) bool { - if automationPolicyIsSubset(aps[i], aps[j]) { - return true +func consolidateAutomationPolicies(aps []*caddytls.AutomationPolicy) ([]*caddytls.AutomationPolicy, error) { + // Consolidate merge-equivalent policies that can be combined without shadowing issues + // before validating topological ordering. This ensures merge-equivalent crossed policies + // are merged rather than falsely rejected as specificity cycles. +outerPreMerge: + for i := 0; i < len(aps); i++ { + for j := i + 1; j < len(aps); j++ { + if reflect.DeepEqual(aps[i], aps[j]) { + aps = slices.Delete(aps, j, j+1) + i-- + continue outerPreMerge + } + if len(aps[i].SubjectsRaw) > 0 && len(aps[j].SubjectsRaw) > 0 && + automationPoliciesAreMergeEquivalent(aps[i], aps[j]) && + !automationPolicyMergeShadows(i, j, aps) { + for _, subj := range aps[j].SubjectsRaw { + if !slices.Contains(aps[i].SubjectsRaw, subj) { + aps[i].SubjectsRaw = append(aps[i].SubjectsRaw, subj) + } + } + aps = slices.Delete(aps, j, j+1) + i-- + continue outerPreMerge + } } - if automationPolicyIsSubset(aps[j], aps[i]) { - return false - } - return len(aps[i].SubjectsRaw) > len(aps[j].SubjectsRaw) - }) + } + + var err error + aps, err = sortAutomationPoliciesBySpecificity(aps) + if err != nil { + return nil, err + } emptyAPCount := 0 origLenAPs := len(aps) @@ -1111,7 +1136,7 @@ func consolidateAutomationPolicies(aps []*caddytls.AutomationPolicy) []*caddytls } // If all policies are empty, we can return nil, as there is no need to set any policy if emptyAPCount == origLenAPs { - return nil + return nil, nil } // remove or combine duplicate policies @@ -1134,14 +1159,7 @@ outer: // otherwise the one without any subjects (a catch-all) would be // eaten up by the one with subjects; and if both have subjects, we // need to combine their lists - if automationPoliciesHaveSameIssuers(aps[i], aps[j]) && - reflect.DeepEqual(aps[i].ManagersRaw, aps[j].ManagersRaw) && - bytes.Equal(aps[i].StorageRaw, aps[j].StorageRaw) && - aps[i].MustStaple == aps[j].MustStaple && - aps[i].KeyType == aps[j].KeyType && - aps[i].OnDemand == aps[j].OnDemand && - aps[i].ReusePrivateKeys == aps[j].ReusePrivateKeys && - aps[i].RenewalWindowRatio == aps[j].RenewalWindowRatio { + if automationPoliciesAreMergeEquivalent(aps[i], aps[j]) { if len(aps[i].SubjectsRaw) > 0 && len(aps[j].SubjectsRaw) == 0 { // later policy (at j) has no subjects ("catch-all"), so we can // remove the identical-but-more-specific policy that comes first @@ -1170,7 +1188,112 @@ outer: } } - return aps + return aps, nil +} + +// sortAutomationPoliciesBySpecificity orders automation policies from most specific +// to least specific using a stable topological sort over the partial order defined +// by pairwise specificity constraints. Disjoint or incomparable policies preserve +// their original declaration order. +func sortAutomationPoliciesBySpecificity(aps []*caddytls.AutomationPolicy) ([]*caddytls.AutomationPolicy, error) { + n := len(aps) + if n <= 1 { + return aps, nil + } + + adj := make([][]int, n) + inDegree := make([]int, n) + + for i := 0; i < n; i++ { + for j := i + 1; j < n; j++ { + cmp, err := compareAutomationPolicies(aps[i], aps[j]) + if err != nil { + return nil, err + } + switch { + case cmp < 0: + // aps[i] is more specific than aps[j]; aps[i] must precede aps[j] + adj[i] = append(adj[i], j) + inDegree[j]++ + case cmp > 0: + // aps[j] is more specific than aps[i]; aps[j] must precede aps[i] + adj[j] = append(adj[j], i) + inDegree[i]++ + } + } + } + + sorted := make([]*caddytls.AutomationPolicy, 0, n) + visited := make([]bool, n) + + for step := 0; step < n; step++ { + next := -1 + for i := 0; i < n; i++ { + if !visited[i] && inDegree[i] == 0 { + next = i + break + } + } + if next == -1 { + return nil, fmt.Errorf("ambiguous automation policies: overlapping subjects require conflicting ordering") + } + + visited[next] = true + sorted = append(sorted, aps[next]) + + for _, neighbor := range adj[next] { + inDegree[neighbor]-- + } + } + + return sorted, nil +} + +// compareAutomationPolicies compares the specificity of two automation policies. +// It returns -1 if a is strictly more specific than b, 1 if b is strictly more +// specific than a, 0 if they are disjoint or equally specific, and an error if +// their overlapping subjects have conflicting specificity. +func compareAutomationPolicies(a, b *caddytls.AutomationPolicy) (int, error) { + aSubjects := a.SubjectsRaw + bSubjects := b.SubjectsRaw + + // A policy without subjects acts as a catch-all/default policy and matches + // any subject. A policy with subjects is strictly more specific than a catch-all. + if len(aSubjects) == 0 && len(bSubjects) == 0 { + return 0, nil + } + if len(aSubjects) > 0 && len(bSubjects) == 0 { + return -1, nil + } + if len(aSubjects) == 0 && len(bSubjects) > 0 { + return 1, nil + } + + var needABeforeB, needBBeforeA bool + for _, aSubj := range aSubjects { + for _, bSubj := range bSubjects { + if strings.EqualFold(aSubj, bSubj) { + continue + } + if certmagic.MatchWildcard(aSubj, bSubj) { + needABeforeB = true + } + if certmagic.MatchWildcard(bSubj, aSubj) { + needBBeforeA = true + } + } + } + + if needABeforeB && needBBeforeA { + return 0, fmt.Errorf("ambiguous automation policies: overlapping subjects require conflicting ordering") + } + if needABeforeB { + return -1, nil + } + if needBBeforeA { + return 1, nil + } + return 0, nil } // automationPolicyIsSubset returns true if a's subjects are a subset @@ -1257,6 +1380,17 @@ func subjectQualifiesForPublicCert(ap *caddytls.AutomationPolicy, subj string) b (strings.Count(subj, "*.") < 2 || ap.OnDemand) } +func automationPoliciesAreMergeEquivalent(a, b *caddytls.AutomationPolicy) bool { + return automationPoliciesHaveSameIssuers(a, b) && + reflect.DeepEqual(a.ManagersRaw, b.ManagersRaw) && + bytes.Equal(a.StorageRaw, b.StorageRaw) && + a.MustStaple == b.MustStaple && + a.KeyType == b.KeyType && + a.OnDemand == b.OnDemand && + a.ReusePrivateKeys == b.ReusePrivateKeys && + a.RenewalWindowRatio == b.RenewalWindowRatio +} + func automationPoliciesHaveSameIssuers(a, b *caddytls.AutomationPolicy) bool { if reflect.DeepEqual(a.IssuersRaw, b.IssuersRaw) { return automationPoliciesHaveCompatibleImplicitIssuers(a, b) diff --git a/caddyconfig/httpcaddyfile/tlsapp_test.go b/caddyconfig/httpcaddyfile/tlsapp_test.go index 8426a3986..4402fd3d6 100644 --- a/caddyconfig/httpcaddyfile/tlsapp_test.go +++ b/caddyconfig/httpcaddyfile/tlsapp_test.go @@ -1,6 +1,8 @@ package httpcaddyfile import ( + "encoding/json" + "slices" "testing" "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" @@ -72,3 +74,188 @@ https://example.net localhost:8080 { t.Fatalf("adapting Caddyfile: %v", err) } } + +func TestConsolidateAutomationPoliciesSpecificity(t *testing.T) { + t.Run("overlapping wildcard and exact subject sets in both insertion orders", func(t *testing.T) { + pWildcard := &caddytls.AutomationPolicy{ + SubjectsRaw: []string{"*.example.com", "other.com", "third.com"}, + KeyType: "rsa2048", + } + pSpecific := &caddytls.AutomationPolicy{ + SubjectsRaw: []string{"foo.example.com", "e.com"}, + KeyType: "ed25519", + } + + res1, err := consolidateAutomationPolicies([]*caddytls.AutomationPolicy{pWildcard, pSpecific}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if len(res1) != 2 || res1[0] != pSpecific || res1[1] != pWildcard { + t.Fatalf("expected [pSpecific, pWildcard], got %v", res1) + } + + res2, err := consolidateAutomationPolicies([]*caddytls.AutomationPolicy{pSpecific, pWildcard}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if len(res2) != 2 || res2[0] != pSpecific || res2[1] != pWildcard { + t.Fatalf("expected [pSpecific, pWildcard], got %v", res2) + } + }) + + t.Run("disjoint unequal-size sets preserve insertion order", func(t *testing.T) { + p1 := &caddytls.AutomationPolicy{ + SubjectsRaw: []string{"alpha.example.com", "beta.example.com", "gamma.example.com"}, + KeyType: "ed25519", + } + p2 := &caddytls.AutomationPolicy{ + SubjectsRaw: []string{"delta.example.org"}, + KeyType: "rsa2048", + } + + res1, err := consolidateAutomationPolicies([]*caddytls.AutomationPolicy{p1, p2}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if len(res1) != 2 || res1[0] != p1 || res1[1] != p2 { + t.Fatalf("expected [p1, p2], got %v", res1) + } + + res2, err := consolidateAutomationPolicies([]*caddytls.AutomationPolicy{p2, p1}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if len(res2) != 2 || res2[0] != p2 || res2[1] != p1 { + t.Fatalf("expected [p2, p1], got %v", res2) + } + }) + + t.Run("catch-all policy sorted after specific policies", func(t *testing.T) { + pCatchAll := &caddytls.AutomationPolicy{SubjectsRaw: nil, KeyType: "p256"} + pWildcard := &caddytls.AutomationPolicy{SubjectsRaw: []string{"*.example.com"}, KeyType: "rsa2048"} + pExact := &caddytls.AutomationPolicy{SubjectsRaw: []string{"foo.example.com"}, KeyType: "ed25519"} + + res1, err := consolidateAutomationPolicies([]*caddytls.AutomationPolicy{pCatchAll, pWildcard, pExact}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if len(res1) != 3 || res1[0] != pExact || res1[1] != pWildcard || res1[2] != pCatchAll { + t.Fatalf("expected [pExact, pWildcard, pCatchAll], got %v", res1) + } + + res2, err := consolidateAutomationPolicies([]*caddytls.AutomationPolicy{pExact, pWildcard, pCatchAll}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if len(res2) != 3 || res2[0] != pExact || res2[1] != pWildcard || res2[2] != pCatchAll { + t.Fatalf("expected [pExact, pWildcard, pCatchAll], got %v", res2) + } + }) + + t.Run("bidirectional conflict with different configurations returns error", func(t *testing.T) { + p1 := &caddytls.AutomationPolicy{SubjectsRaw: []string{"foo.example.com", "*.other.com"}, KeyType: "rsa2048"} + p2 := &caddytls.AutomationPolicy{SubjectsRaw: []string{"*.example.com", "bar.other.com"}, KeyType: "ed25519"} + + _, err := consolidateAutomationPolicies([]*caddytls.AutomationPolicy{p1, p2}) + if err == nil { + t.Fatalf("expected error for conflicting policies with different configurations, got nil") + } + }) + + t.Run("merge-equivalent crossed policies consolidate without error", func(t *testing.T) { + p1 := &caddytls.AutomationPolicy{SubjectsRaw: []string{"foo.example.com", "*.other.com"}, KeyType: "rsa2048"} + p2 := &caddytls.AutomationPolicy{SubjectsRaw: []string{"*.example.com", "bar.other.com"}, KeyType: "rsa2048"} + + res, err := consolidateAutomationPolicies([]*caddytls.AutomationPolicy{p1, p2}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if len(res) != 1 { + t.Fatalf("expected 1 consolidated policy, got %d", len(res)) + } + expectedSubjs := []string{"foo.example.com", "*.other.com", "*.example.com", "bar.other.com"} + for _, exp := range expectedSubjs { + if !slices.Contains(res[0].SubjectsRaw, exp) { + t.Errorf("expected subject %s in consolidated policy, got %v", exp, res[0].SubjectsRaw) + } + } + }) + + t.Run("merge-equivalent crossed policies that shadow intermediate policy returns error", func(t *testing.T) { + p1 := &caddytls.AutomationPolicy{SubjectsRaw: []string{"foo.example.com", "*.other.com"}, KeyType: "rsa2048"} + p2 := &caddytls.AutomationPolicy{SubjectsRaw: []string{"baz.example.com"}, KeyType: "ed25519"} + p3 := &caddytls.AutomationPolicy{SubjectsRaw: []string{"*.example.com", "bar.other.com"}, KeyType: "rsa2048"} + + _, err := consolidateAutomationPolicies([]*caddytls.AutomationPolicy{p1, p2, p3}) + if err == nil { + t.Fatalf("expected error when merge would shadow intermediate policy, got nil") + } + }) +} + +func TestConsolidateAutomationPoliciesCaddyfileAdapt(t *testing.T) { + t.Run("issue 8300 site blocks adapt with specific policy first", func(t *testing.T) { + input := `*.example.com, other.com, third.com { + tls internal +} + +foo.example.com, e.com { + tls { + issuer acme + } +} +` + adapter := caddyfile.Adapter{ServerType: ServerType{}} + cfgJSON, _, err := adapter.Adapt([]byte(input), nil) + if err != nil { + t.Fatalf("unexpected error adapting Caddyfile: %v", err) + } + + var parsedCfg struct { + Apps struct { + TLS struct { + Automation struct { + Policies []struct { + Subjects []string `json:"subjects"` + } `json:"policies"` + } `json:"automation"` + } `json:"tls"` + } `json:"apps"` + } + if err := json.Unmarshal(cfgJSON, &parsedCfg); err != nil { + t.Fatalf("unmarshaling config: %v", err) + } + + policies := parsedCfg.Apps.TLS.Automation.Policies + if len(policies) < 2 { + t.Fatalf("expected at least 2 automation policies, got %d", len(policies)) + } + if !slices.Contains(policies[0].Subjects, "foo.example.com") { + t.Errorf("expected policy 0 to contain foo.example.com, got %v", policies[0].Subjects) + } + if !slices.Contains(policies[1].Subjects, "*.example.com") { + t.Errorf("expected policy 1 to contain *.example.com, got %v", policies[1].Subjects) + } + }) + + t.Run("conflicting crossed site blocks fail adapt", func(t *testing.T) { + input := `foo.example.com, *.other.com { + tls { + key_type rsa2048 + } +} + +*.example.com, bar.other.com { + tls { + key_type ed25519 + } +} +` + adapter := caddyfile.Adapter{ServerType: ServerType{}} + _, _, err := adapter.Adapt([]byte(input), nil) + if err == nil { + t.Fatalf("expected error adapting conflicting crossed site blocks, got nil") + } + }) +} +