Fix mapped IPv4 filtering and IP rule error line numbers
This commit is contained in:
parent
d5b64670e3
commit
9fd8201016
4 changed files with 65 additions and 7 deletions
|
|
@ -281,7 +281,7 @@ func ParseIPRule(line string, lineNo int) (cleanLine string, trailingStar bool,
|
|||
trailingStar = strings.HasSuffix(line, "*")
|
||||
|
||||
if len(line) < 2 || (ip != nil && trailingStar) {
|
||||
return "", false, fmt.Errorf("suspicious IP rule [%s] at line %d", line, lineNo)
|
||||
return "", false, fmt.Errorf("suspicious IP rule [%s] at line %d", line, 1+lineNo)
|
||||
}
|
||||
|
||||
cleanLine = line
|
||||
|
|
@ -292,10 +292,10 @@ func ParseIPRule(line string, lineNo int) (cleanLine string, trailingStar bool,
|
|||
cleanLine = cleanLine[:len(cleanLine)-1]
|
||||
}
|
||||
if len(cleanLine) == 0 {
|
||||
return "", false, fmt.Errorf("empty IP rule at line %d", lineNo)
|
||||
return "", false, fmt.Errorf("empty IP rule at line %d", 1+lineNo)
|
||||
}
|
||||
if strings.Contains(cleanLine, "*") {
|
||||
return "", false, fmt.Errorf("invalid rule: [%s] - wildcards can only be used as a suffix at line %d", line, lineNo)
|
||||
return "", false, fmt.Errorf("invalid rule: [%s] - wildcards can only be used as a suffix at line %d", line, 1+lineNo)
|
||||
}
|
||||
|
||||
return strings.ToLower(cleanLine), trailingStar, nil
|
||||
|
|
@ -323,11 +323,11 @@ func LoadIPRules(lines string, prefixes *iradix.Tree, ips map[string]any, networ
|
|||
err := ProcessConfigLines(lines, func(line string, lineNo int) error {
|
||||
if strings.Contains(line, "/") {
|
||||
if networks == nil {
|
||||
dlog.Errorf("CIDR rule [%s] at line %d but no network table provided", line, lineNo)
|
||||
dlog.Errorf("CIDR rule [%s] at line %d but no network table provided", line, 1+lineNo)
|
||||
return nil
|
||||
}
|
||||
if err := networks.AddCIDR(line, true); err != nil {
|
||||
dlog.Errorf("Invalid CIDR rule [%s] at line %d: %v", line, lineNo, err)
|
||||
dlog.Errorf("Invalid CIDR rule [%s] at line %d: %v", line, 1+lineNo, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,9 +2,67 @@
|
|||
|
||||
import (
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
iradix "github.com/hashicorp/go-immutable-radix"
|
||||
"github.com/jedisct1/dlog"
|
||||
"github.com/k-sone/critbitgo"
|
||||
)
|
||||
|
||||
func TestLoadIPRulesErrorLineNumbers(t *testing.T) {
|
||||
logFile, err := os.Create(filepath.Join(t.TempDir(), "errors.log"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
oldFD, oldLevel := dlog.GetFileDescriptor(), dlog.LogLevel()
|
||||
dlog.UseSyslog(false)
|
||||
dlog.SetFileDescriptor(logFile)
|
||||
dlog.SetLogLevel(dlog.SeverityError)
|
||||
t.Cleanup(func() {
|
||||
dlog.SetFileDescriptor(oldFD)
|
||||
dlog.SetLogLevel(oldLevel)
|
||||
logFile.Close()
|
||||
})
|
||||
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
rule string
|
||||
networks *critbitgo.Net
|
||||
}{
|
||||
{"short rule", "*", critbitgo.NewNet()},
|
||||
{"empty prefix", ".*", critbitgo.NewNet()},
|
||||
{"embedded wildcard", "192.*.2", critbitgo.NewNet()},
|
||||
{"invalid CIDR", "192.0.2.0/33", critbitgo.NewNet()},
|
||||
{"missing network table", "192.0.2.0/24", nil},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if err := logFile.Truncate(0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := logFile.Seek(0, 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ips := make(map[string]any)
|
||||
if _, err := LoadIPRules("# Rules\n\n"+tt.rule+"\n192.0.2.1\n", iradix.New(), ips, tt.networks); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ips["192.0.2.1"] != true {
|
||||
t.Error("valid rule after the error was not loaded")
|
||||
}
|
||||
log, err := os.ReadFile(logFile.Name())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(log), "line 3") {
|
||||
t.Errorf("error log should identify line 3: %s", log)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractClientIPStr(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
|
|
|||
|
|
@ -156,7 +156,7 @@ func (plugin *PluginAllowedIP) Eval(pluginsState *PluginsState, msg *dns.Msg) er
|
|||
if rrtype == dns.TypeA {
|
||||
ipStr = answer.(*dns.A).A.Addr.String()
|
||||
} else if rrtype == dns.TypeAAAA {
|
||||
ipStr = answer.(*dns.AAAA).AAAA.Addr.String() // IPv4-mapped IPv6 addresses are converted to IPv4
|
||||
ipStr = answer.(*dns.AAAA).AAAA.Addr.Unmap().String() // IPv4-mapped IPv6 addresses are converted to IPv4
|
||||
}
|
||||
if _, found := plugin.allowedIPs[ipStr]; found {
|
||||
allowed, reason = true, ipStr
|
||||
|
|
|
|||
|
|
@ -160,7 +160,7 @@ func (plugin *PluginBlockIP) Eval(pluginsState *PluginsState, msg *dns.Msg) erro
|
|||
if rrtype == dns.TypeA {
|
||||
ipStr = answer.(*dns.A).A.Addr.String()
|
||||
} else if rrtype == dns.TypeAAAA {
|
||||
ipStr = answer.(*dns.AAAA).AAAA.Addr.String() // IPv4-mapped IPv6 addresses are converted to IPv4
|
||||
ipStr = answer.(*dns.AAAA).AAAA.Addr.Unmap().String() // IPv4-mapped IPv6 addresses are converted to IPv4
|
||||
}
|
||||
if _, found := plugin.blockedIPs[ipStr]; found {
|
||||
reject, reason = true, ipStr
|
||||
|
|
|
|||
Loading…
Reference in a new issue