Pull request: 2582 invalid hostname vol.2

Merge in DNS/adguard-home from 2582-invalid-hostname-2 to master

Updates #2582.

Squashed commit of the following:

commit 9d3ceb289e3869b2c3d12e91ec104fb25d7931ee
Merge: 91c68e46 90054974
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Fri Mar 5 19:11:49 2021 +0300

    Merge branch 'master' into 2582-invalid-hostname-2

commit 91c68e468c5f5b12a2fb509ff391133483c9d915
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Fri Mar 5 18:28:14 2021 +0300

    all: mv trimming from home to dhcpd

commit f51faf35288577b6f610f172b26e7ac13aa24f72
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Fri Mar 5 16:28:00 2021 +0300

    home: add more host sanitizings
This commit is contained in:
Eugene Burkov 2021-03-05 19:20:36 +03:00
parent 90054974bc
commit 5aa0ca9319
3 changed files with 11 additions and 40 deletions

View File

@ -7,6 +7,7 @@ import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"net" "net"
"strings"
"sync" "sync"
"time" "time"
@ -463,7 +464,16 @@ func (s *v4Server) processRequest(req, resp *dhcpv4.DHCPv4) (*Lease, bool) {
} }
if lease.Expiry.Unix() != leaseExpireStatic { if lease.Expiry.Unix() != leaseExpireStatic {
lease.Hostname = string(hostname) // The trimming is required since some devices include trailing
// zero-byte in DHCP option length calculation.
//
// See https://github.com/AdguardTeam/AdGuardHome/issues/2582.
//
// TODO(e.burkov): Remove after the trimming for hostname option
// will be added into github.com/insomniacslk/dhcp module.
hostnameStr := strings.TrimRight(string(hostname), "\x00")
lease.Hostname = hostnameStr
s.commitLease(lease) s.commitLease(lease)
} else if len(lease.Hostname) != 0 { } else if len(lease.Hostname) != 0 {
o := &optFQDN{ o := &optFQDN{

View File

@ -586,22 +586,9 @@ func (clients *clientsContainer) SetWhoisInfo(ip string, info [][]string) {
log.Debug("clients: set whois info for auto-client with IP %s: %q", ip, info) 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 // AddHost adds a new IP-hostname pairing. The priorities of the sources is
// taken into account. ok is true if the pairing was added. // taken into account. ok is true if the pairing was added.
func (clients *clientsContainer) AddHost(ip, host string, src clientSource) (ok bool, err error) { func (clients *clientsContainer) AddHost(ip, host string, src clientSource) (ok bool, err error) {
host = sanitizeHost(host)
clients.lock.Lock() clients.lock.Lock()
ok = clients.addHostLocked(ip, host, src) ok = clients.addHostLocked(ip, host, src)
clients.lock.Unlock() clients.lock.Unlock()

View File

@ -290,29 +290,3 @@ func TestClientsCustomUpstream(t *testing.T) {
assert.Equal(t, 1, len(config.Upstreams)) assert.Equal(t, 1, len(config.Upstreams))
assert.Equal(t, 1, len(config.DomainReservedUpstreams)) 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]))
})
}
}