Skip to content
This repository was archived by the owner on Aug 12, 2021. It is now read-only.
Open
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
9 changes: 9 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ const (
IPv4AndIPv6 = (IPv4 | IPv6) //< Default option.
)

// DoS protection: we won't cache more than 1024 entries when receiving entries.
var maxSentEntries = 1024

type clientOpts struct {
listenOn IPType
ifaces []net.Interface
Expand Down Expand Up @@ -293,6 +296,12 @@ func (c *client) mainloop(ctx context.Context, params *lookupParams) {
// This is also a point to possibly stop probing actively for a
// service entry.
params.Entries <- e
// DoS protection: don't cache more than maxSentEntries entries
if len(sentEntries) >= maxSentEntries {
for key := range sentEntries {
delete(sentEntries, key)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also: we can just store a set, right? No need to store the values.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise, a small number of entries could still be large.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do that in a separate PR.

}
}
sentEntries[k] = e
if !params.isBrowsing {
params.disableProbing()
Expand Down
72 changes: 71 additions & 1 deletion service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package zeroconf

import (
"context"
"fmt"
"log"
"testing"
"time"

"github.com/pkg/errors"
)

var (
const (
mdnsName = "test--xxxxxxxxxxxx"
mdnsService = "_test--xxxx._tcp"
mdnsSubtype = "_test--xxxx._tcp,_fancy"
Expand Down Expand Up @@ -163,4 +164,73 @@ func TestSubtype(t *testing.T) {
t.Fatalf("Expected port is %d, but got %d", mdnsPort, result.Port)
}
})

t.Run("DoS protection", func(t *testing.T) {
origMaxSentEntries := maxSentEntries
maxSentEntries = 10
defer func() {
time.Sleep(100 * time.Millisecond) // give the mainloop some time to shut down
maxSentEntries = origMaxSentEntries
}()

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

const firstName = mdnsName

go startMDNS(ctx, mdnsPort, firstName, mdnsSubtype, mdnsDomain)
time.Sleep(time.Second)

resolver, err := NewResolver(nil)
if err != nil {
t.Fatalf("Expected create resolver success, but got %v", err)
}
entries := make(chan *ServiceEntry, maxSentEntries+1)
received := make(chan *ServiceEntry, 10)
go func() {
for {
select {
case entry, ok := <-entries:
if !ok {
return
}
if entry.Instance == firstName {
received <- entry
}
case <-ctx.Done():
return
}
}
}()
if err := resolver.Browse(ctx, mdnsService, mdnsDomain, entries); err != nil {
t.Fatalf("Expected browse success, but got %v", err)
}
select {
case <-received:
case <-time.NewTimer(time.Second).C:
t.Fatal("expected to discover service")
}

for i := 1; i < maxSentEntries; i++ {
go startMDNS(ctx, mdnsPort, fmt.Sprintf("%s-%d", mdnsName, i), mdnsSubtype, mdnsDomain)
}
time.Sleep(time.Second)

select {
case entry := <-entries:
t.Fatalf("didn't expect to receive an entry, got %v", entry)
default:
}

// Announcing this service will cause the map to overflow.
go startMDNS(ctx, mdnsPort, fmt.Sprintf("%s-%d", mdnsName, maxSentEntries), mdnsSubtype, mdnsDomain)

// wait for a re-announcement of the firstName service
select {
case <-received:
cancel()
case <-ctx.Done():
t.Fatal("expected to discover service")
}
})
}