dns/generate_test.go
Tom Thorogood 274da7d3ef
Add new ZoneParser API (#794)
* Improve ParseZone tests

* Add new ZoneParser API

* Use the ZoneParser API directly in ReadRR

* Merge parseZoneHelper into ParseZone

* Make generate string building slightly more efficient

* Add SetDefaultTTL method to ZoneParser

This makes it possible for external consumers to implement ReadRR.

* Make $INCLUDE directive opt-in

The $INCLUDE directive opens a user controlled file and parses it as
a DNS zone file. The error messages may reveal portions of sensitive
files, such as:
	/etc/passwd: dns: not a TTL: "root0:0:root:/root:/bin/bash" at line: 1:31
	/etc/shadow: dns: not a TTL: "root:$6$<redacted>::0:99999:7:::" at line: 1:125

Both ParseZone and ReadRR are currently opt-in for backward
compatibility.

* Disable $INCLUDE support in ReadRR

ReadRR and NewRR are often passed untrusted input. At the same time,
$INCLUDE isn't really useful for ReadRR as it only ever returns the
first record.

This is a breaking change, but it currently represents a slight
security risk.

* Document the need to drain the ParseZone chan

* Cleanup the documentation of NewRR, ReadRR and ParseZone

* Document the ZoneParser API

* Deprecated the ParseZone function

* Add whitespace to ZoneParser.Next

* Remove prevName field from ZoneParser

This doesn't track anything meaningful as both zp.prevName and h.Name
are only ever set at the same point and to the same value.

* Use uint8 for ZoneParser.include field

It has a maximum value of 7 which easily fits within uint8.

This reduces the size of ZoneParser from 160 bytes to 152 bytes.

* Add setParseError helper to ZoneParser

* Surface $INCLUDE os.Open error in error message

* Rename ZoneParser.include field to includeDepth

* Make maximum $INCLUDE depth a const

* Add ParseZone and ZoneParser benchmarks

* Parse $GENERATE directive with a single ZoneParser

This should be more efficient than calling NewRR for each generated
record.

* Run go fmt on generate_test.go

* Add a benchmark for $GENERATE directives

* Use a custom reader for generate

This avoids the overhead and memory usage of building the zone string.

name         old time/op    new time/op    delta
Generate-12     165µs ± 4%     157µs ± 2%   -5.06%  (p=0.000 n=25+25)

name         old alloc/op   new alloc/op   delta
Generate-12    42.1kB ± 0%    31.8kB ± 0%  -24.42%  (p=0.000 n=20+23)

name         old allocs/op  new allocs/op  delta
Generate-12     1.56k ± 0%     1.55k ± 0%   -0.38%  (p=0.000 n=25+25)

* Return correct ParseError from generateReader

The last commit made these regular errors while they had been
ParseErrors before.

* Return error message as string from modToPrintf

This is slightly simpler and they don't need to be errors.

* Skip setting includeDepth in generate

This sub parser isn't allowed to use $INCLUDE directives anyway.

Note: If generate is ever changed to allow $INCLUDE directives, then
      this line must be added back. Without doing that, it would be
      be possible to exceed maxIncludeDepth.

* Make generateReader errors sticky

ReadByte should not be called after an error has been returned, but
this is cheap insurance.

* Move file and lex fields to end of generateReader

These are only used for creating a ParseError and so are unlikely to be
accessed.

* Don't return offset with error in modToPrintf

Along for the ride, are some whitespace and style changes.

* Add whitespace to generate and simplify step

* Use a for loop instead of goto in generate

* Support $INCLUDE directives inside $GENERATE directives

This was previously supported and may be useful. This is now more
rigorous as the maximum include depth is respected and relative
$INCLUDE directives are now supported from within $GENERATE.

* Don't return any lexer tokens after read error

Without this, read errors are likely to be lost and become parse errors
of the remaining str. The $GENERATE code relies on surfacing errors from
the reader.

* Support $INCLUDE in NewRR and ReadRR

Removing $INCLUDE support from these is a breaking change and should
not be included in this pull request.

* Add test to ensure $GENERATE respects $INCLUDE support

* Unify TestZoneParserIncludeDisallowed with other tests

* Remove stray whitespace from TestGenerateSurfacesErrors

* Move ZoneParser SetX methods above Err method

* $GENERATE should not accept step of 0

If step is allowed to be 0, then generateReader (and the code it
replaced) will get stuck in an infinite loop.

This is a potential DOS vulnerability.

* Fix ReadRR comment for file argument

I missed this previosuly. The file argument is also used to
resolve relative $INCLUDE directives.

* Prevent test panics on nil error

* Rework ZoneParser.subNext

This is slightly cleaner and will close the underlying *os.File even
if an error occurs.

* Make ZoneParser.generate call subNext

This also moves the calls to setParseError into generate.

* Report errors when parsing rest of $GENERATE directive

* Report proper error location in $GENERATE directive

This makes error messages much clearer.

* Simplify modToPrintf func

Note: When width is 0, the leading 0 of the fmt string is now excluded.
      This should not alter the formatting of numbers in anyway.

* Add comment explaining sub field

* Remove outdated error comment from generate
2018-10-20 11:47:56 +10:30

207 lines
5.9 KiB
Go

package dns
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
)
func TestGenerateRangeGuard(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "dns")
if err != nil {
t.Fatalf("could not create tmpdir for test: %v", err)
}
defer os.RemoveAll(tmpdir)
for i := 0; i <= 1; i++ {
path := filepath.Join(tmpdir, fmt.Sprintf("%04d.conf", i))
data := []byte(fmt.Sprintf("dhcp-%04d A 10.0.0.%d", i, i))
if err := ioutil.WriteFile(path, data, 0644); err != nil {
t.Fatalf("could not create tmpfile for test: %v", err)
}
}
var tests = [...]struct {
zone string
fail bool
}{
{`@ IN SOA ns.test. hostmaster.test. ( 1 8h 2h 7d 1d )
$GENERATE 0-1 dhcp-${0,4,d} A 10.0.0.$
`, false},
{`@ IN SOA ns.test. hostmaster.test. ( 1 8h 2h 7d 1d )
$GENERATE 0-1 dhcp-${0,0,x} A 10.0.0.$
`, false},
{`@ IN SOA ns.test. hostmaster.test. ( 1 8h 2h 7d 1d )
$GENERATE 128-129 dhcp-${-128,4,d} A 10.0.0.$
`, false},
{`@ IN SOA ns.test. hostmaster.test. ( 1 8h 2h 7d 1d )
$GENERATE 128-129 dhcp-${-129,4,d} A 10.0.0.$
`, true},
{`@ IN SOA ns.test. hostmaster.test. ( 1 8h 2h 7d 1d )
$GENERATE 0-2 dhcp-${2147483647,4,d} A 10.0.0.$
`, true},
{`@ IN SOA ns.test. hostmaster.test. ( 1 8h 2h 7d 1d )
$GENERATE 0-1 dhcp-${2147483646,4,d} A 10.0.0.$
`, false},
{`@ IN SOA ns.test. hostmaster.test. ( 1 8h 2h 7d 1d )
$GENERATE 0-1/step dhcp-${0,4,d} A 10.0.0.$
`, true},
{`@ IN SOA ns.test. hostmaster.test. ( 1 8h 2h 7d 1d )
$GENERATE 0-1/ dhcp-${0,4,d} A 10.0.0.$
`, true},
{`@ IN SOA ns.test. hostmaster.test. ( 1 8h 2h 7d 1d )
$GENERATE 0-10/2 dhcp-${0,4,d} A 10.0.0.$
`, false},
{`@ IN SOA ns.test. hostmaster.test. ( 1 8h 2h 7d 1d )
$GENERATE 0-1/0 dhcp-${0,4,d} A 10.0.0.$
`, true},
{`@ IN SOA ns.test. hostmaster.test. ( 1 8h 2h 7d 1d )
$GENERATE 0-1 $$INCLUDE ` + tmpdir + string(filepath.Separator) + `${0,4,d}.conf
`, false},
}
Outer:
for i := range tests {
for tok := range ParseZone(strings.NewReader(tests[i].zone), "test.", "test") {
if tok.Error != nil {
if !tests[i].fail {
t.Errorf("expected \n\n%s\nto be parsed, but got %v", tests[i].zone, tok.Error)
}
continue Outer
}
}
if tests[i].fail {
t.Errorf("expected \n\n%s\nto fail, but got no error", tests[i].zone)
}
}
}
func TestGenerateIncludeDepth(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "dns")
if err != nil {
t.Fatalf("could not create tmpfile for test: %v", err)
}
defer os.Remove(tmpfile.Name())
zone := `@ IN SOA ns.test. hostmaster.test. ( 1 8h 2h 7d 1d )
$GENERATE 0-1 $$INCLUDE ` + tmpfile.Name() + `
`
if _, err := tmpfile.WriteString(zone); err != nil {
t.Fatalf("could not write to tmpfile for test: %v", err)
}
if err := tmpfile.Close(); err != nil {
t.Fatalf("could not close tmpfile for test: %v", err)
}
zp := NewZoneParser(strings.NewReader(zone), ".", tmpfile.Name())
zp.SetIncludeAllowed(true)
for _, ok := zp.Next(); ok; _, ok = zp.Next() {
}
const expected = "too deeply nested $INCLUDE"
if err := zp.Err(); err == nil || !strings.Contains(err.Error(), expected) {
t.Errorf("expected error to include %q, got %v", expected, err)
}
}
func TestGenerateIncludeDisallowed(t *testing.T) {
const zone = `@ IN SOA ns.test. hostmaster.test. ( 1 8h 2h 7d 1d )
$GENERATE 0-1 $$INCLUDE test.conf
`
zp := NewZoneParser(strings.NewReader(zone), ".", "")
for _, ok := zp.Next(); ok; _, ok = zp.Next() {
}
const expected = "$INCLUDE directive not allowed"
if err := zp.Err(); err == nil || !strings.Contains(err.Error(), expected) {
t.Errorf("expected error to include %q, got %v", expected, err)
}
}
func TestGenerateSurfacesErrors(t *testing.T) {
const zone = `@ IN SOA ns.test. hostmaster.test. ( 1 8h 2h 7d 1d )
$GENERATE 0-1 dhcp-${0,4,dd} A 10.0.0.$
`
zp := NewZoneParser(strings.NewReader(zone), ".", "test")
for _, ok := zp.Next(); ok; _, ok = zp.Next() {
}
const expected = `test: dns: bad base in $GENERATE: "${0,4,dd}" at line: 2:20`
if err := zp.Err(); err == nil || err.Error() != expected {
t.Errorf("expected specific error, wanted %q, got %v", expected, err)
}
}
func TestGenerateSurfacesLexerErrors(t *testing.T) {
const zone = `@ IN SOA ns.test. hostmaster.test. ( 1 8h 2h 7d 1d )
$GENERATE 0-1 dhcp-${0,4,d} A 10.0.0.$ )
`
zp := NewZoneParser(strings.NewReader(zone), ".", "test")
for _, ok := zp.Next(); ok; _, ok = zp.Next() {
}
const expected = `test: dns: bad data in $GENERATE directive: "extra closing brace" at line: 2:40`
if err := zp.Err(); err == nil || err.Error() != expected {
t.Errorf("expected specific error, wanted %q, got %v", expected, err)
}
}
func TestGenerateModToPrintf(t *testing.T) {
tests := []struct {
mod string
wantFmt string
wantOffset int
wantErr bool
}{
{"0,0,d", "%d", 0, false},
{"0,0", "%d", 0, false},
{"0", "%d", 0, false},
{"3,2,d", "%02d", 3, false},
{"3,2", "%02d", 3, false},
{"3", "%d", 3, false},
{"0,0,o", "%o", 0, false},
{"0,0,x", "%x", 0, false},
{"0,0,X", "%X", 0, false},
{"0,0,z", "", 0, true},
{"0,0,0,d", "", 0, true},
{"-100,0,d", "%d", -100, false},
}
for _, test := range tests {
gotFmt, gotOffset, errMsg := modToPrintf(test.mod)
switch {
case errMsg != "" && !test.wantErr:
t.Errorf("modToPrintf(%q) - expected empty-error, but got %v", test.mod, errMsg)
case errMsg == "" && test.wantErr:
t.Errorf("modToPrintf(%q) - expected error, but got empty-error", test.mod)
case gotFmt != test.wantFmt:
t.Errorf("modToPrintf(%q) - expected format %q, but got %q", test.mod, test.wantFmt, gotFmt)
case gotOffset != test.wantOffset:
t.Errorf("modToPrintf(%q) - expected offset %d, but got %d", test.mod, test.wantOffset, gotOffset)
}
}
}
func BenchmarkGenerate(b *testing.B) {
const zone = `@ IN SOA ns.test. hostmaster.test. ( 1 8h 2h 7d 1d )
$GENERATE 32-158 dhcp-${-32,4,d} A 10.0.0.$
`
for n := 0; n < b.N; n++ {
zp := NewZoneParser(strings.NewReader(zone), ".", "")
for _, ok := zp.Next(); ok; _, ok = zp.Next() {
}
if err := zp.Err(); err != nil {
b.Fatal(err)
}
}
}