fix(rewrite): fix cname target rewrite for CNAME chains (#7853)

* fix(rewrite): fix cname target rewrite for CNAME chains

This fix corrects the cname target rewrite to handle CNAME chains:
- Preserves only the CNAME records before matching the rule
- Rewrites only the CNAME target that matches the rule
- Includes all records from the re-resolved upstream response

Signed-off-by: hide <hide@hide.net.eu.org>

* docs(rewrite): document how answer records are handled in CNAME target rewrite

Signed-off-by: hide <hide@hide.net.eu.org>

* fix(rewrite): simplify slice append per staticcheck S1011

Signed-off-by: hide <hide@hide.net.eu.org>

* docs(rewrite): add extra line between code and paragraph

Signed-off-by: hide <hide@hide.net.eu.org>

---------

Signed-off-by: hide <hide@hide.net.eu.org>
Co-authored-by: hide <hide@hide.net.eu.org>
This commit is contained in:
hide
2026-02-22 06:10:35 +09:00
committed by GitHub
parent 191a783e46
commit 78524a7921
3 changed files with 38 additions and 13 deletions

View File

@@ -61,6 +61,12 @@ func (u *MockedUpstream) Lookup(ctx context.Context, state request.Request, name
}
m.Truncated = true
return m, nil
case "intermediate-2.staging.":
m.Answer = []dns.RR{
test.CNAME("intermediate-2.staging. 200 IN CNAME final.staging.net."),
test.A("final.staging.net. 120 IN A 5.6.7.8"),
}
return m, nil
}
return &dns.Msg{}, nil
}
@@ -76,6 +82,7 @@ func TestCNameTargetRewrite(t *testing.T) {
{[]string{"continue", "cname", "substring", "efgh", "zzzz.www"}, reflect.TypeFor[*cnameTargetRule]()},
{[]string{"continue", "cname", "regex", `(.*)\.web\.(.*)\.site\.`, `{1}.webapp.{2}.org.`}, reflect.TypeFor[*cnameTargetRule]()},
{[]string{"continue", "cname", "exact", "music.truncated.spotify.com.", "music.truncated.spotify.com."}, reflect.TypeFor[*cnameTargetRule]()},
{[]string{"continue", "cname", "suffix", "prod.", "staging."}, reflect.TypeFor[*cnameTargetRule]()},
}
rules := make([]Rule, 0, len(ruleset))
for i, r := range ruleset {
@@ -180,6 +187,21 @@ func doTestCNameTargetTests(t *testing.T, rules []Rule) {
},
true,
},
{"cname-chain.org.", dns.TypeA,
[]dns.RR{
test.CNAME("cname-chain.org. 200 IN CNAME intermediate-1.com"),
test.CNAME("intermediate-1.com 200 IN CNAME intermediate-2.prod."),
test.CNAME("intermediate-2.prod. 200 IN CNAME final.prod.net."),
test.A("final.prod.net. 120 IN A 1.2.3.4"),
},
[]dns.RR{
test.CNAME("cname-chain.org. 200 IN CNAME intermediate-1.com"),
test.CNAME("intermediate-1.com 200 IN CNAME intermediate-2.staging."),
test.CNAME("intermediate-2.staging. 200 IN CNAME final.staging.net."),
test.A("final.staging.net. 120 IN A 5.6.7.8"),
},
false,
},
}
ctx := context.TODO()
for i, tc := range tests {