From 90ebc4d8c9f9657c826fd0d39e708aa6cbdc12ad Mon Sep 17 00:00:00 2001 From: Eugene Burkov Date: Thu, 4 Mar 2021 17:49:34 +0300 Subject: [PATCH] Pull request: 2582 invalid hostname Merge in DNS/adguard-home from 2582-invalid-hostname to master Updates #2582. Squashed commit of the following: commit 909598dae00588792b092f89c272c4487ba55dd1 Author: Eugene Burkov Date: Thu Mar 4 17:32:58 2021 +0300 all: imp code quality, log changes commit b3b970803709030c48cfe343b88d73524a043de3 Author: Eugene Burkov Date: Thu Mar 4 16:56:28 2021 +0300 home: add host processing logic --- CHANGELOG.md | 2 ++ internal/home/clients.go | 13 +++++++++++++ internal/home/clients_test.go | 26 ++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 12b8d1a7..a409cd8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ and this project adheres to ### Fixed +- Incomplete hostnames with trailing zero-bytes handling ([#2582]). - Wrong DNS-over-TLS ALPN configuration ([#2681]). - Inconsistent responses for messages with EDNS0 and AD when DNS caching is enabled ([#2600]). @@ -27,6 +28,7 @@ and this project adheres to - Incomplete DNS upstreams validation ([#2674]). - Wrong parsing of DHCP options of the `ip` type ([#2688]). +[#2582]: https://github.com/AdguardTeam/AdGuardHome/issues/2582 [#2600]: https://github.com/AdguardTeam/AdGuardHome/issues/2600 [#2674]: https://github.com/AdguardTeam/AdGuardHome/issues/2674 [#2681]: https://github.com/AdguardTeam/AdGuardHome/issues/2681 diff --git a/internal/home/clients.go b/internal/home/clients.go index 5a9e9eac..6c314874 100644 --- a/internal/home/clients.go +++ b/internal/home/clients.go @@ -586,9 +586,22 @@ func (clients *clientsContainer) SetWhoisInfo(ip string, info [][]string) { log.Debug("clients: set whois info for auto-client with IP %s: %q", ip, info) } +// sanitizeHost proccesses a host to remove some special symbols causing errors. +// Logic may be expanded in the future. +func sanitizeHost(host string) (processed string) { + // cutset brings together all the deprecated sets. + // + // See https://github.com/AdguardTeam/AdGuardHome/issues/2582. + const cutset = "\x00" + + return strings.TrimRight(host, cutset) +} + // AddHost adds a new IP-hostname pairing. The priorities of the sources is // taken into account. ok is true if the pairing was added. func (clients *clientsContainer) AddHost(ip, host string, src clientSource) (ok bool, err error) { + host = sanitizeHost(host) + clients.lock.Lock() ok = clients.addHostLocked(ip, host, src) clients.lock.Unlock() diff --git a/internal/home/clients_test.go b/internal/home/clients_test.go index a098bf4c..e6200ddd 100644 --- a/internal/home/clients_test.go +++ b/internal/home/clients_test.go @@ -290,3 +290,29 @@ func TestClientsCustomUpstream(t *testing.T) { assert.Equal(t, 1, len(config.Upstreams)) assert.Equal(t, 1, len(config.DomainReservedUpstreams)) } + +func TestProcessHost(t *testing.T) { + const ( + name int = iota + host + want + + fieldsNum + ) + + testCases := [][fieldsNum]string{{ + name: "valid", + host: "abc", + want: "abc", + }, { + name: "with_trailing_zero_byte", + host: "abc\x00", + want: "abc", + }} + + for _, tc := range testCases { + t.Run(tc[name], func(t *testing.T) { + assert.Equal(t, tc[want], sanitizeHost(tc[host])) + }) + } +}