Fix URI and CAA parsing on quotes and backslashes. (#1101) (#1104)

Automatically submitted.
This commit is contained in:
Alex Fattouche 2020-05-13 20:24:22 +01:00 committed by GitHub
parent 8f63c2d20c
commit b28dcc1849
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 16 deletions

View File

@ -506,15 +506,10 @@ func sprintTxtOctet(s string) string {
} }
b, n := nextByte(s, i) b, n := nextByte(s, i)
switch { if n == 0 {
case n == 0:
i++ // dangling back slash i++ // dangling back slash
case b == '.': } else {
dst.WriteByte('.') writeTXTStringByte(&dst, b)
case b < ' ' || b > '~':
dst.WriteString(escapeByte(b))
default:
dst.WriteByte(b)
} }
i += n i += n
} }
@ -1121,6 +1116,7 @@ type URI struct {
Target string `dns:"octet"` Target string `dns:"octet"`
} }
// rr.Target to be parsed as a sequence of character encoded octets according to RFC 3986
func (rr *URI) String() string { func (rr *URI) String() string {
return rr.Hdr.String() + strconv.Itoa(int(rr.Priority)) + return rr.Hdr.String() + strconv.Itoa(int(rr.Priority)) +
" " + strconv.Itoa(int(rr.Weight)) + " " + sprintTxtOctet(rr.Target) " " + strconv.Itoa(int(rr.Weight)) + " " + sprintTxtOctet(rr.Target)
@ -1282,6 +1278,7 @@ type CAA struct {
Value string `dns:"octet"` Value string `dns:"octet"`
} }
// rr.Value Is the character-string encoding of the value field as specified in RFC 1035, Section 5.1.
func (rr *CAA) String() string { func (rr *CAA) String() string {
return rr.Hdr.String() + strconv.Itoa(int(rr.Flag)) + " " + rr.Tag + " " + sprintTxtOctet(rr.Value) return rr.Hdr.String() + strconv.Itoa(int(rr.Flag)) + " " + rr.Tag + " " + sprintTxtOctet(rr.Value)
} }

View File

@ -77,15 +77,15 @@ func TestSprintName(t *testing.T) {
got := sprintName("abc\\.def\007\"\127@\255\x05\xef\\") got := sprintName("abc\\.def\007\"\127@\255\x05\xef\\")
if want := "abc\\.def\\007\\\"W\\@\\173\\005\\239"; got != want { if want := "abc\\.def\\007\\\"W\\@\\173\\005\\239"; got != want {
t.Errorf("expected %q, got %q", got, want) t.Errorf("expected %q, got %q", want, got)
} }
} }
func TestSprintTxtOctet(t *testing.T) { func TestSprintTxtOctet(t *testing.T) {
got := sprintTxtOctet("abc\\.def\007\"\127@\255\x05\xef\\") got := sprintTxtOctet("abc\\.def\007\"\127@\255\x05\xef\\")
if want := "\"abc\\.def\\007\"W@\\173\\005\\239\""; got != want { if want := "\"abc\\.def\\007\\\"W@\\173\\005\\239\""; got != want {
t.Errorf("expected %q, got %q", got, want) t.Errorf("expected %q, got %q", want, got)
} }
} }
@ -96,7 +96,7 @@ func TestSprintTxt(t *testing.T) {
}) })
if want := "\"abc.def\\007\\\"W@\\173\\005\\239\" \"example.com\""; got != want { if want := "\"abc.def\\007\\\"W@\\173\\005\\239\" \"example.com\""; got != want {
t.Errorf("expected %q, got %q", got, want) t.Errorf("expected %q, got %q", want, got)
} }
} }
@ -128,7 +128,7 @@ func BenchmarkSprintName(b *testing.B) {
got := sprintName("abc\\.def\007\"\127@\255\x05\xef\\") got := sprintName("abc\\.def\007\"\127@\255\x05\xef\\")
if want := "abc\\.def\\007\\\"W\\@\\173\\005\\239"; got != want { if want := "abc\\.def\\007\\\"W\\@\\173\\005\\239"; got != want {
b.Fatalf("expected %q, got %q", got, want) b.Fatalf("expected %q, got %q", want, got)
} }
} }
} }
@ -138,7 +138,7 @@ func BenchmarkSprintName_NoEscape(b *testing.B) {
got := sprintName("large.example.com") got := sprintName("large.example.com")
if want := "large.example.com"; got != want { if want := "large.example.com"; got != want {
b.Fatalf("expected %q, got %q", got, want) b.Fatalf("expected %q, got %q", want, got)
} }
} }
} }
@ -147,8 +147,8 @@ func BenchmarkSprintTxtOctet(b *testing.B) {
for n := 0; n < b.N; n++ { for n := 0; n < b.N; n++ {
got := sprintTxtOctet("abc\\.def\007\"\127@\255\x05\xef\\") got := sprintTxtOctet("abc\\.def\007\"\127@\255\x05\xef\\")
if want := "\"abc\\.def\\007\"W@\\173\\005\\239\""; got != want { if want := "\"abc\\.def\\007\\\"W@\\173\\005\\239\""; got != want {
b.Fatalf("expected %q, got %q", got, want) b.Fatalf("expected %q, got %q", want, got)
} }
} }
} }