Skip to content

Commit

Permalink
Pull request: 2582 invalid hostname
Browse files Browse the repository at this point in the history
Merge in DNS/adguard-home from 2582-invalid-hostname to master

Updates #2582.

Squashed commit of the following:

commit 909598d
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Thu Mar 4 17:32:58 2021 +0300

    all: imp code quality, log changes

commit b3b9708
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Thu Mar 4 16:56:28 2021 +0300

    home: add host processing logic
  • Loading branch information
EugeneOne1 committed Mar 4, 2021
1 parent 400b76d commit 90ebc4d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]).
Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions internal/home/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
26 changes: 26 additions & 0 deletions internal/home/clients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
})
}
}

0 comments on commit 90ebc4d

Please sign in to comment.