Skip to content

Commit fa07ff0

Browse files
rayjanokaRay Janoka
andauthored
Adding double-quotes to TXT record content (#24)
* wrapping TXT record content in double-quotes * cleanup * cleanup * cleanup * adding backward compatibility for existing unquoted content * cleanup * cleanup * moving to rr to cover make it more flexible --------- Co-authored-by: Ray Janoka <[email protected]>
1 parent 6116e5a commit fa07ff0

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

client.go

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"net/http"
99
"net/url"
10+
"strings"
1011

1112
"github.com/libdns/libdns"
1213
)
@@ -59,13 +60,24 @@ func (p *Provider) updateRecord(ctx context.Context, oldRec, newRec cfDNSRecord)
5960
}
6061

6162
func (p *Provider) getDNSRecords(ctx context.Context, zoneInfo cfZone, rec libdns.Record, matchContent bool) ([]cfDNSRecord, error) {
62-
rr := rec.RR()
63+
rr, err := cloudflareRecord(rec)
64+
if err != nil {
65+
return nil, err
66+
}
6367

6468
qs := make(url.Values)
6569
qs.Set("type", rr.Type)
6670
qs.Set("name", libdns.AbsoluteName(rr.Name, zoneInfo.Name))
71+
72+
var unwrappedContent string
6773
if matchContent {
68-
qs.Set("content", rr.Data)
74+
if rr.Type == "TXT" {
75+
unwrappedContent = unwrapContent(rr.Content)
76+
// Use the contains (wildcard) search with unquoted content to return both quoted and unquoted content
77+
qs.Set("content.contains", unwrappedContent)
78+
} else {
79+
qs.Set("content.exact", rr.Content)
80+
}
6981
}
7082

7183
reqURL := fmt.Sprintf("%s/zones/%s/dns_records?%s", baseURL, zoneInfo.ID, qs.Encode())
@@ -76,6 +88,26 @@ func (p *Provider) getDNSRecords(ctx context.Context, zoneInfo cfZone, rec libdn
7688

7789
var results []cfDNSRecord
7890
_, err = p.doAPIRequest(req, &results)
91+
92+
// Since the TXT search used contains (wildcard), check for exact matches
93+
if matchContent && rr.Type == "TXT" {
94+
for i := 0; i < len(results); i++ {
95+
// Prefer exact quoted content
96+
if results[i].Content == rr.Content {
97+
return []cfDNSRecord{results[i]}, nil
98+
}
99+
}
100+
101+
for i := 0; i < len(results); i++ {
102+
// Using exact unquoted content is acceptable
103+
if results[i].Content == unwrappedContent {
104+
return []cfDNSRecord{results[i]}, nil
105+
}
106+
}
107+
108+
return []cfDNSRecord{}, nil
109+
}
110+
79111
return results, err
80112
}
81113

@@ -167,3 +199,17 @@ func (p *Provider) doAPIRequest(req *http.Request, result any) (cfResponse, erro
167199
}
168200

169201
const baseURL = "https://api.cloudflare.com/client/v4"
202+
203+
func unwrapContent(content string) string {
204+
if strings.HasPrefix(content, `"`) && strings.HasSuffix(content, `"`) {
205+
content = strings.TrimPrefix(strings.TrimSuffix(content, `"`), `"`)
206+
}
207+
return content
208+
}
209+
210+
func wrapContent(content string) string {
211+
if !strings.HasPrefix(content, `"`) && !strings.HasSuffix(content, `"`) {
212+
content = fmt.Sprintf("%q", content)
213+
}
214+
return content
215+
}

models.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,12 @@ func (r cfDNSRecord) libdnsRecord(zone string) (libdns.Record, error) {
181181
Target: r.Data.Target,
182182
}, nil
183183
case "TXT":
184+
// unwrap the quotes from the content
185+
unwrappedContent := unwrapContent(r.Content)
184186
return libdns.TXT{
185187
Name: name,
186188
TTL: ttl,
187-
Text: r.Content,
189+
Text: unwrappedContent,
188190
}, nil
189191
// NOTE: HTTPS records from Cloudflare have a `r.Content` that can be
190192
// parsed by [libdns.RR.Parse] so that is what we do here. While we are
@@ -242,6 +244,10 @@ func cloudflareRecord(r libdns.Record) (cfDNSRecord, error) {
242244
if rr.Type == "CNAME" && strings.HasSuffix(cfRec.Content, ".cfargotunnel.com") {
243245
cfRec.Proxied = true
244246
}
247+
if rr.Type == "TXT" {
248+
// wrap the content in quotes
249+
cfRec.Content = wrapContent(cfRec.Content)
250+
}
245251
return cfRec, nil
246252
}
247253

0 commit comments

Comments
 (0)