diff --git a/ads_idna_test.go b/ads_idna_test.go new file mode 100644 index 0000000..8f2524c --- /dev/null +++ b/ads_idna_test.go @@ -0,0 +1,24 @@ +package ads + +import ( + "github.com/stretchr/testify/assert" + "golang.org/x/net/idna" + "testing" +) + +var values = map[string]string{ + "ɢoogle.com": "xn--oogle-wmc.com", + "müller.c-mueller.de": "xn--mller-kva.c-mueller.de", + "mähl.c-mueller.de": "xn--mhl-qla.c-mueller.de", + "💩.krnl.eu": "xn--ls8h.krnl.eu", + "c-mueller.de": "c-mueller.de", +} + +func TestIDNADecode(t *testing.T) { + for k, v := range values { + result, err := idna.ToASCII(k) + assert.NoError(t, err) + t.Log(result) + assert.Equal(t, v, result) + } +} diff --git a/go.mod b/go.mod index c921640..99141c6 100644 --- a/go.mod +++ b/go.mod @@ -9,5 +9,5 @@ require ( github.com/miekg/dns v1.1.28 github.com/prometheus/client_golang v1.5.0 github.com/stretchr/testify v1.5.1 - golang.org/x/net v0.0.0-20200301022130-244492dfa37a + golang.org/x/net v0.0.0-20200320181208-1c781a10960a ) diff --git a/list_map_handler.go b/list_map_handler.go index d561b3e..3642504 100644 --- a/list_map_handler.go +++ b/list_map_handler.go @@ -17,6 +17,7 @@ package ads import ( + "golang.org/x/net/idna" "io/ioutil" "net/http" "os" @@ -114,6 +115,12 @@ func parseListFile(data []byte, blockageMap ListMap) { continue } + var err error + url, err = idna.ToASCII(url) + if err != nil { + continue + } + // Enable blocking for url if ValidateQName(url) && utf8.Valid([]byte(url)) { blockageMap[url] = true diff --git a/list_map_handler_test.go b/list_map_handler_test.go index 91868c4..ed3a393 100644 --- a/list_map_handler_test.go +++ b/list_map_handler_test.go @@ -52,6 +52,7 @@ func TestListFetch(t *testing.T) { expData, err := ioutil.ReadAll(expectedList) for _, url := range strings.Split(string(expData), "\n") { + t.Logf("Expected QName: %q Found: %v", url, list[url]) assert.True(t, list[url]) } assert.False(t, list["testme.com"]) diff --git a/setup_parse.go b/setup_parse.go index a963075..642b681 100644 --- a/setup_parse.go +++ b/setup_parse.go @@ -20,6 +20,7 @@ import ( "fmt" "github.com/caddyserver/caddy" "github.com/coredns/coredns/plugin" + "golang.org/x/net/idna" "net" "net/url" "time" @@ -143,7 +144,12 @@ func parsePluginConfiguration(c *caddy.Controller) (*adsPluginConfig, error) { if !c.NextArg() { return nil, plugin.Error("ads", c.Err("No name for blacklist (block) entry defined")) } - config.BlacklistRules = append(config.BlacklistRules, c.Val()) + v := c.Val() + encoded, err := idna.ToASCII(v) + if err != nil { + return nil, plugin.Error("ads", c.Err(fmt.Sprintf("Could not decode IDN of qname %q", v))) + } + config.BlacklistRules = append(config.BlacklistRules, encoded) break case "block-regex": if !c.NextArg() { @@ -155,7 +161,12 @@ func parsePluginConfiguration(c *caddy.Controller) (*adsPluginConfig, error) { if !c.NextArg() { return nil, plugin.Error("ads", c.Err("No name for whitelist (permit) entry defined")) } - config.WhitelistRules = append(config.WhitelistRules, c.Val()) + v := c.Val() + encoded, err := idna.ToASCII(v) + if err != nil { + return nil, plugin.Error("ads", c.Err(fmt.Sprintf("Could not decode IDN of qname %q", v))) + } + config.WhitelistRules = append(config.WhitelistRules, encoded) break case "permit-regex": if !c.NextArg() { diff --git a/setup_test.go b/setup_test.go index 63c4a55..5bb1d0e 100644 --- a/setup_test.go +++ b/setup_test.go @@ -92,6 +92,14 @@ const invalid_Blacklist_Multi = `ads { block }` +const valid_IDN_Block = `ads { + block mähl.c-mueller.de +}` + +const valid_IDN_Permit = `ads { + permit mähl.c-mueller.de +}` + const valid_Regex_Whitelist_Single = `ads { permit-regex (^|\.)local\.c-mueller\.de$ }` @@ -215,6 +223,8 @@ func TestSetup_ValidWhiteAndBlacklist(t *testing.T) { valid_Blacklist_Single, valid_Whitelist_Multi, valid_Whitelist_Single, + valid_IDN_Block, + valid_IDN_Permit, } for _, v := range cfs { diff --git a/testdata/test_blocklist b/testdata/test_blocklist index 8017aea..8618abd 100644 --- a/testdata/test_blocklist +++ b/testdata/test_blocklist @@ -2,4 +2,10 @@ 0.0.0.0 testdomain.de #0.0.0.0 123.de 0.0.0.0 test-123.de -0.0.0.0 test.com \ No newline at end of file +0.0.0.0 test.com +0.0.0.0 ɢoogle.com +0.0.0.0 müller.c-mueller.de +0.0.0.0 mähl.krnl.eu +0.0.0.0 mähl.c-mueller.de +0.0.0.0 💩.krnl.eu +0.0.0.0 c-mueller.de \ No newline at end of file diff --git a/testdata/test_blocklist_expected_domains b/testdata/test_blocklist_expected_domains index 14f35a9..b2aa353 100644 --- a/testdata/test_blocklist_expected_domains +++ b/testdata/test_blocklist_expected_domains @@ -1,4 +1,10 @@ test.de testdomain.de test-123.de -test.com \ No newline at end of file +test.com +xn--oogle-wmc.com +xn--mller-kva.c-mueller.de +xn--mhl-qla.c-mueller.de +xn--mhl-qla.krnl.eu +xn--ls8h.krnl.eu +c-mueller.de \ No newline at end of file