plugin/hosts: don't drop over-long fields silently (#8551)

#8496 fixed a silent truncation here and named the invariant in its own commit
message: entries were dropped and the hosts file simply looked shorter than it
is, with nothing in the log.

#8516 replaced that mechanism with a streaming parser bounded by maxFieldSize.
The error log #8496 added is still in parse(), but it can no longer report a
dropped entry: bufio.ErrBufferFull is consumed by the read loop, so only a real
I/O error reaches it. A field over maxFieldSize is discarded in lineParser with
no log at all, and when that field is the address the whole line goes with it.

Report both cases, once per dropped field, with the line number and the source
the entries came from.

Signed-off-by: Baltasar Blanco <baltasarblanco.dev@gmail.com>
This commit is contained in:
Baltasar Blanco
2026-09-15 23:24:23 -03:00
committed by GitHub
parent 8de7a8b89d
commit 14ed42bd1f
2 changed files with 150 additions and 4 deletions

View File

@@ -5,7 +5,10 @@
package hosts
import (
"bytes"
"fmt"
"io"
golog "log"
"net"
"os"
"reflect"
@@ -386,3 +389,129 @@ func TestParseLongLineWithComment(t *testing.T) {
t.Errorf("LookupStaticHostV4(after.example.org.) = %v, want [127.0.0.3]", addrs)
}
}
func TestParseLogsOversizedField(t *testing.T) {
// #8496 established that a hosts file entry must never be dropped without a
// trace: "the hosts file simply looked shorter than it is, with nothing in
// the log". A field over maxFieldSize is dropped, and when it is the address
// the whole line goes with it, so both cases have to be reported.
var logBuf bytes.Buffer
golog.SetOutput(&logBuf)
defer golog.SetOutput(io.Discard)
long := strings.Repeat("a", maxFieldSize+1)
h := &Hostsfile{
Origins: []string{"."},
hmap: newMap(),
inline: newMap(),
options: newOptions(),
path: "/tmp/hosts.test",
}
h.hmap = h.parse(strings.NewReader(
"127.0.0.1 before.example.org\n" +
"127.0.0.2 " + long + ".example.org\n" +
long + " orphan.example.org\n" +
"127.0.0.4 after.example.org\n"))
// Controls: the entries around the dropped fields are still parsed.
for _, tc := range []struct{ name, addr string }{
{"before.example.org.", "127.0.0.1"},
{"after.example.org.", "127.0.0.4"},
} {
if addrs := h.LookupStaticHostV4(tc.name); len(addrs) != 1 || addrs[0].String() != tc.addr {
t.Errorf("LookupStaticHostV4(%s) = %v, want [%s]", tc.name, addrs, tc.addr)
}
}
if addrs := h.LookupStaticHostV4("orphan.example.org."); len(addrs) != 0 {
t.Errorf("LookupStaticHostV4(orphan.example.org.) = %v, want []", addrs)
}
got := logBuf.String()
for _, want := range []string{
`[ERROR] plugin/hosts: Hosts file "/tmp/hosts.test", line 2:`,
`[ERROR] plugin/hosts: Hosts file "/tmp/hosts.test", line 3:`,
"dropping the line",
"dropping the name",
} {
if !strings.Contains(got, want) {
t.Errorf("Expected log to contain %q, got %q", want, got)
}
}
// One report per dropped field, not one per read: the field on line 3 is
// assembled across several calls to append.
if n := strings.Count(got, "[ERROR] plugin/hosts:"); n != 2 {
t.Errorf("Expected exactly 2 reports, got %d in %q", n, got)
}
}
func TestParseLogsOversizedFieldSpanningReads(t *testing.T) {
// The line number must survive a field that crosses the read buffer: feed
// runs once per chunk, but only the last chunk of a line ends it. A field
// this long is also reported once, not once per read.
var logBuf bytes.Buffer
golog.SetOutput(&logBuf)
defer golog.SetOutput(io.Discard)
long := strings.Repeat("a", 5<<20)
h := &Hostsfile{
Origins: []string{"."},
hmap: newMap(),
inline: newMap(),
options: newOptions(),
path: "/tmp/hosts.test",
}
h.hmap = h.parse(strings.NewReader(
"127.0.0.1 before.example.org\n" +
"127.0.0.2 " + long + ".example.org one.example.org\n" +
"127.0.0.3 after.example.org\n"))
for _, tc := range []struct{ name, addr string }{
{"one.example.org.", "127.0.0.2"},
{"after.example.org.", "127.0.0.3"},
} {
if addrs := h.LookupStaticHostV4(tc.name); len(addrs) != 1 || addrs[0].String() != tc.addr {
t.Errorf("LookupStaticHostV4(%s) = %v, want [%s]", tc.name, addrs, tc.addr)
}
}
got := logBuf.String()
if want := fmt.Sprintf("line 2: name longer than %d bytes", maxFieldSize); !strings.Contains(got, want) {
t.Errorf("Expected log to contain %q, got %q", want, got)
}
if n := strings.Count(got, "[ERROR] plugin/hosts:"); n != 1 {
t.Errorf("Expected exactly 1 report, got %d in %q", n, got)
}
}
func TestInitInlineReportsItsOwnSource(t *testing.T) {
// Inline entries are parsed with the same parser but do not come from the
// hosts file, so naming the file in a diagnostic would send the operator to
// the wrong place, at a line number that does not exist there.
var logBuf bytes.Buffer
golog.SetOutput(&logBuf)
defer golog.SetOutput(io.Discard)
h := &Hostsfile{
Origins: []string{"."},
hmap: newMap(),
inline: newMap(),
options: newOptions(),
path: "/tmp/hosts.test",
}
h.initInline([]string{
"127.0.0.1 first.example.org",
strings.Repeat("a", maxFieldSize+1) + " orphan.example.org",
})
if addrs := h.inline.name4["first.example.org."]; len(addrs) != 1 {
t.Errorf("inline name4[first.example.org.] = %v, want one address", addrs)
}
got := logBuf.String()
if want := "Inline hosts entries, line 2: address longer"; !strings.Contains(got, want) {
t.Errorf("Expected log to contain %q, got %q", want, got)
}
if strings.Contains(got, "/tmp/hosts.test") {
t.Errorf("Inline entries reported as coming from the hosts file: %q", got)
}
}