mirror of
https://github.com/StackExchange/dnscontrol.git
synced 2024-05-11 05:55:12 +00:00
CLOUDFLAREAPI: Fix TXT quoting bug (#1543)
* Cleanup * wip! * Fix cloudflare quote bug * wip! * cleanup * go generate
This commit is contained in:
@@ -20,6 +20,7 @@ func IsQuoted(s string) bool {
|
||||
}
|
||||
|
||||
// StripQuotes returns the string with the starting and ending quotes removed.
|
||||
// If it is not quoted, the original string is returned.
|
||||
func StripQuotes(s string) string {
|
||||
if IsQuoted(s) {
|
||||
return s[1 : len(s)-1]
|
||||
@@ -36,12 +37,20 @@ func ParseQuotedTxt(s string) []string {
|
||||
if !IsQuoted(s) {
|
||||
return []string{s}
|
||||
}
|
||||
|
||||
// TODO(tlim): Consider using r, err := ParseQuotedFields(s)
|
||||
return strings.Split(StripQuotes(s), `" "`)
|
||||
}
|
||||
|
||||
// ParseQuotedFields is like strings.Fields except individual fields
|
||||
// might be quoted using `"`.
|
||||
func ParseQuotedFields(s string) ([]string, error) {
|
||||
// Parse according to RFC1035 zonefile specifications.
|
||||
// "foo" -> one string: `foo``
|
||||
// "foo" "bar" -> two strings: `foo` and `bar`
|
||||
// Quotes are escaped with \"
|
||||
|
||||
// Implementation note:
|
||||
// Fields are space-separated but a field might be quoted. This is,
|
||||
// essentially, a CSV where spaces are the field separator (not
|
||||
// commas). Therefore, we use the CSV parser. See https://stackoverflow.com/a/47489846/71978
|
||||
|
@@ -43,5 +43,5 @@ func (rc *RecordConfig) SetTargetCAAString(s string) error {
|
||||
if len(part) != 3 {
|
||||
return fmt.Errorf("CAA value does not contain 3 fields: (%#v)", s)
|
||||
}
|
||||
return rc.SetTargetCAAStrings(part[0], part[1], StripQuotes(part[2]))
|
||||
return rc.SetTargetCAAStrings(part[0], part[1], part[2])
|
||||
}
|
||||
|
@@ -1,5 +1,7 @@
|
||||
package models
|
||||
|
||||
import "strings"
|
||||
|
||||
/*
|
||||
Sadly many providers handle TXT records in strange and non-compliant ways.
|
||||
|
||||
@@ -71,11 +73,30 @@ func (rc *RecordConfig) SetTargetTXTs(s []string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetTargetTXTJoined returns the TXT target as one string. If it was stored as multiple strings, concatenate them.
|
||||
func (rc *RecordConfig) GetTargetTXTJoined() string {
|
||||
return strings.Join(rc.TxtStrings, "")
|
||||
}
|
||||
|
||||
// SetTargetTXTString is like SetTargetTXT but accepts one big string,
|
||||
// which must be parsed into one or more strings based on how it is quoted.
|
||||
// Ex: foo << 1 string
|
||||
// foo bar << 1 string
|
||||
// "foo bar" << 1 string
|
||||
// "foo" "bar" << 2 strings
|
||||
// FIXME(tlim): This function is badly named. It obscures the fact
|
||||
// that the string is parsed for quotes and should only be used for returns TXTMulti.
|
||||
// Deprecated: Use SetTargetTXTfromRFC1035Quoted instead.
|
||||
func (rc *RecordConfig) SetTargetTXTString(s string) error {
|
||||
return rc.SetTargetTXTs(ParseQuotedTxt(s))
|
||||
}
|
||||
|
||||
func (rc *RecordConfig) SetTargetTXTfromRFC1035Quoted(s string) error {
|
||||
many, err := ParseQuotedFields(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return rc.SetTargetTXTs(many)
|
||||
}
|
||||
|
||||
// There is no GetTargetTXTfromRFC1025Quoted(). Use GetTargetRFC1035Quoted()
|
||||
|
Reference in New Issue
Block a user