From 0430c2dd122f8c58835e39cdc36e40d9fe045af1 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Wed, 10 Mar 2021 07:31:11 -0800 Subject: [PATCH] wgengine/tsdns: truncate Map.PrettyDiffFrom string at 1KB Hello's were painful. Signed-off-by: Brad Fitzpatrick --- wgengine/tsdns/map.go | 15 +++++++++++++++ wgengine/tsdns/map_test.go | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/wgengine/tsdns/map.go b/wgengine/tsdns/map.go index 5cc774555..c51dbf59b 100644 --- a/wgengine/tsdns/map.go +++ b/wgengine/tsdns/map.go @@ -92,6 +92,9 @@ func (m *Map) PrettyDiffFrom(old *Map) string { } buf := new(strings.Builder) + space := func() bool { + return buf.Len() < (1 << 10) + } for len(oldNames) > 0 && len(newNames) > 0 { var name string @@ -109,6 +112,9 @@ func (m *Map) PrettyDiffFrom(old *Map) string { oldNames = oldNames[1:] newNames = newNames[1:] } + if !space() { + continue + } ipOld, inOld := oldNameToIP[name] ipNew, inNew := newNameToIP[name] @@ -128,6 +134,9 @@ func (m *Map) PrettyDiffFrom(old *Map) string { } for _, name := range oldNames { + if !space() { + break + } if _, ok := newNameToIP[name]; !ok { buf.WriteByte('-') printSingleNameIP(buf, name, oldNameToIP[name]) @@ -135,11 +144,17 @@ func (m *Map) PrettyDiffFrom(old *Map) string { } for _, name := range newNames { + if !space() { + break + } if _, ok := oldNameToIP[name]; !ok { buf.WriteByte('+') printSingleNameIP(buf, name, newNameToIP[name]) } } + if !space() { + buf.WriteString("... [truncated]\n") + } return buf.String() } diff --git a/wgengine/tsdns/map_test.go b/wgengine/tsdns/map_test.go index 5d01f8d77..dba9bb586 100644 --- a/wgengine/tsdns/map_test.go +++ b/wgengine/tsdns/map_test.go @@ -5,6 +5,8 @@ package tsdns import ( + "fmt" + "strings" "testing" "inet.af/netaddr" @@ -135,4 +137,20 @@ func TestPrettyDiffFrom(t *testing.T) { } }) } + + t.Run("truncated", func(t *testing.T) { + small := NewMap(nil, nil) + m := map[string]netaddr.IP{} + for i := 0; i < 5000; i++ { + m[fmt.Sprintf("host%d.ipn.dev.", i)] = netaddr.IPv4(100, 64, 1, 1) + } + veryBig := NewMap(m, nil) + diff := veryBig.PrettyDiffFrom(small) + if len(diff) > 3<<10 { + t.Errorf("pretty diff too large: %d bytes", len(diff)) + } + if !strings.Contains(diff, "truncated") { + t.Errorf("big diff not truncated") + } + }) }