From 5547fd63a0ca32a7aa0db0b0cff51fb96fa4df6c Mon Sep 17 00:00:00 2001 From: Tom Thorogood Date: Mon, 26 Nov 2018 15:53:29 +1030 Subject: [PATCH] Fix garbage after name in compression map When packDomainName is called with an escaped domain name and compress being true, bs wasn't be truncated to the correct length and would include garbage that would be included in the compression map. --- msg.go | 2 +- msg_test.go | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/msg.go b/msg.go index 123becff..2d66bd7a 100644 --- a/msg.go +++ b/msg.go @@ -265,7 +265,7 @@ loop: } if compress && !bsFresh { - roBs = string(bs) + roBs = string(bs[:ls]) bsFresh = true } diff --git a/msg_test.go b/msg_test.go index 8a180018..7627d273 100644 --- a/msg_test.go +++ b/msg_test.go @@ -213,6 +213,28 @@ func TestUnpackDomainName(t *testing.T) { } } +func TestPackDomainNameCompressionMap(t *testing.T) { + msg := make([]byte, 256) + compression := make(map[string]int) + + _, err := PackDomainName(`www\.this.is.\131an.example.org.`, msg, 0, compression, true) + if err != nil { + t.Fatalf("PackDomainName failed: %v", err) + } + + for _, dname := range []string{ + `www.this.is.\131an.example.org.`, + `is.\131an.example.org.`, + "\x83an.example.org.", + `example.org.`, + `org.`, + } { + if _, ok := compression[dname]; !ok { + t.Errorf("expected to find %q in compression map", dname) + } + } +} + func TestPackDomainNameNSECTypeBitmap(t *testing.T) { ownername := "some-very-long-ownername.com." msg := &Msg{