Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IsDomainName: check for escape as last character #1532

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading