Skip to content

Commit

Permalink
dns/dnsmessage: allow name compression for SRV resource parsing
Browse files Browse the repository at this point in the history
As per RFC 3597:

Receiving servers MUST decompress domain names in RRs of well-known
   type, and SHOULD also decompress RRs of type RP, AFSDB, RT, SIG, PX,
   NXT, NAPTR, and SRV (although the current specification of the SRV RR
   in RFC2782 prohibits compression, RFC2052 mandated it, and some
   servers following that earlier specification are still in use).

This change allows SRV resource decompression.

Updates golang/go#36718
Updates golang/go#37362

Change-Id: I473c0d3803758e5b12886f378d2ed54bd5392144
GitHub-Last-Rev: 88d2e06
GitHub-Pull-Request: #199
Reviewed-on: https://go-review.googlesource.com/c/net/+/540375
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
Auto-Submit: Damien Neil <dneil@google.com>
Reviewed-by: Damien Neil <dneil@google.com>
  • Loading branch information
mateusz834 authored and gopherbot committed Feb 3, 2024
1 parent b2208d0 commit 73e4b50
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 31 deletions.
10 changes: 1 addition & 9 deletions dns/dnsmessage/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@ var (
errTooManyAdditionals = errors.New("too many Additionals to pack (>65535)")
errNonCanonicalName = errors.New("name is not in canonical format (it must end with a .)")
errStringTooLong = errors.New("character string exceeds maximum length (255)")
errCompressedSRV = errors.New("compressed name in SRV resource data")
)

// Internal constants.
Expand Down Expand Up @@ -2028,10 +2027,6 @@ func (n *Name) pack(msg []byte, compression map[string]uint16, compressionOff in

// unpack unpacks a domain name.
func (n *Name) unpack(msg []byte, off int) (int, error) {
return n.unpackCompressed(msg, off, true /* allowCompression */)
}

func (n *Name) unpackCompressed(msg []byte, off int, allowCompression bool) (int, error) {
// currOff is the current working offset.
currOff := off

Expand Down Expand Up @@ -2076,9 +2071,6 @@ Loop:
name = append(name, '.')
currOff = endOff
case 0xC0: // Pointer
if !allowCompression {
return off, errCompressedSRV
}
if currOff >= len(msg) {
return off, errInvalidPtr
}
Expand Down Expand Up @@ -2549,7 +2541,7 @@ func unpackSRVResource(msg []byte, off int) (SRVResource, error) {
return SRVResource{}, &nestedError{"Port", err}
}
var target Name
if _, err := target.unpackCompressed(msg, off, false /* allowCompression */); err != nil {
if _, err := target.unpack(msg, off); err != nil {
return SRVResource{}, &nestedError{"Target", err}
}
return SRVResource{priority, weight, port, target}, nil
Expand Down
22 changes: 0 additions & 22 deletions dns/dnsmessage/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,28 +303,6 @@ func TestNameUnpackTooLongName(t *testing.T) {
}
}

func TestIncompressibleName(t *testing.T) {
name := MustNewName("example.com.")
compression := map[string]uint16{}
buf, err := name.pack(make([]byte, 0, 100), compression, 0)
if err != nil {
t.Fatal("first Name.pack() =", err)
}
buf, err = name.pack(buf, compression, 0)
if err != nil {
t.Fatal("second Name.pack() =", err)
}
var n1 Name
off, err := n1.unpackCompressed(buf, 0, false /* allowCompression */)
if err != nil {
t.Fatal("unpacking incompressible name without pointers failed:", err)
}
var n2 Name
if _, err := n2.unpackCompressed(buf, off, false /* allowCompression */); err != errCompressedSRV {
t.Errorf("unpacking compressed incompressible name with pointers: got %v, want = %v", err, errCompressedSRV)
}
}

func checkErrorPrefix(err error, prefix string) bool {
e, ok := err.(*nestedError)
return ok && e.s == prefix
Expand Down

0 comments on commit 73e4b50

Please sign in to comment.