Skip to content

Commit

Permalink
IsDomainName: check for escape as last character (#1532)
Browse files Browse the repository at this point in the history
Keep track if the escape, if still true when returning isDomainName
should return false.

TODO:
- Should still be done in packDomainName as well.
- And that should be tested
- Some tests now fail

There are multiple other places that supposedly also check for this, but
they are not called in the parsing.

Fixes: #1528

Signed-off-by: Miek Gieben <miek@miek.nl>
  • Loading branch information
miekg committed Feb 15, 2024
1 parent 57dcd27 commit 2230854
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
8 changes: 7 additions & 1 deletion defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,12 @@ func IsDomainName(s string) (labels int, ok bool) {
off int
begin int
wasDot bool
escape bool
)
for i := 0; i < len(s); i++ {
switch s[i] {
case '\\':
escape = !escape
if off+1 > lenmsg {
return labels, false
}
Expand All @@ -217,6 +219,7 @@ func IsDomainName(s string) (labels int, ok bool) {

wasDot = false
case '.':
escape = false
if i == 0 && len(s) > 1 {
// leading dots are not legal except for the root zone
return labels, false
Expand All @@ -243,10 +246,13 @@ func IsDomainName(s string) (labels int, ok bool) {
labels++
begin = i + 1
default:
escape = false
wasDot = false
}
}

if escape {
return labels, false
}
return labels, true
}

Expand Down
9 changes: 9 additions & 0 deletions scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,15 @@ func TestParseKnownRRAsRFC3597(t *testing.T) {
})
}

func TestParseOpenEscape(t *testing.T) {
if _, err := NewRR("example.net IN CNAME example.net."); err != nil {
t.Fatalf("expected no error, but got: %s", err)
}
if _, err := NewRR("example.net IN CNAME example.org\\"); err == nil {
t.Fatalf("expected an error, but got none")
}
}

func BenchmarkNewRR(b *testing.B) {
const name1 = "12345678901234567890123456789012345.12345678.123."
const s = name1 + " 3600 IN MX 10 " + name1
Expand Down

0 comments on commit 2230854

Please sign in to comment.