mirror of
https://git.vectorsigma.ru/public/AdGuardHome.git
synced 2026-07-28 20:59:08 +00:00
Merge in DNS/adguard-home from AGDNS-3061-config-modifier to master Squashed commit of the following: commit a0068547bd0209d12e8dbf98ddd5e4ed2545cdd0 Merge: 97b798af6451255675Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Aug 5 17:39:31 2025 +0300 Merge branch 'master' into AGDNS-3061-config-modifier commit 97b798af6a50ee27ee5ed2bcf1c4c3670f5afc5d Merge: 96d21efc9b8043e4f0Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Jul 30 20:27:41 2025 +0300 Merge branch 'master' into AGDNS-3061-config-modifier commit 96d21efc984073adef5026de8a03b5bf94542648 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Jul 30 20:18:43 2025 +0300 all: imp code commit 67c5608b4be3bd712a0ab5980f25ecea1ed21d65 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Jul 29 20:31:19 2025 +0300 all: imp code commit 52f45a9f70f57d8e7f7fc0e9e8291ff0dde74343 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Jul 28 20:32:13 2025 +0300 all: use config modifier commit d389ffd286460d8ff1964bd2ee8dabdafb832b9b Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Jul 25 14:20:31 2025 +0300 bamboo-specs: fix ci commit 3f303ac9131a07e4af5783696b237436fd93d65c Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Jul 25 14:18:42 2025 +0300 home: config modifier
349 lines
8.4 KiB
Go
349 lines
8.4 KiB
Go
package dnsforward
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"net"
|
|
"net/http"
|
|
"net/url"
|
|
"testing"
|
|
|
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
|
"github.com/AdguardTeam/golibs/testutil"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
// testTLSConn is a tlsConn for tests.
|
|
type testTLSConn struct {
|
|
// Conn is embedded here simply to make testTLSConn a net.Conn without
|
|
// actually implementing all methods.
|
|
net.Conn
|
|
|
|
serverName string
|
|
}
|
|
|
|
// ConnectionState implements the tlsConn interface for testTLSConn.
|
|
func (c testTLSConn) ConnectionState() (cs tls.ConnectionState) {
|
|
cs.ServerName = c.serverName
|
|
|
|
return cs
|
|
}
|
|
|
|
func TestServer_clientIDFromDNSContext(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
proto proxy.Proto
|
|
confSrvName string
|
|
cliSrvName string
|
|
wantClientID string
|
|
wantErrMsg string
|
|
inclHTTPTLS bool
|
|
strictSNI bool
|
|
}{{
|
|
name: "udp",
|
|
proto: proxy.ProtoUDP,
|
|
confSrvName: "",
|
|
cliSrvName: "",
|
|
wantClientID: "",
|
|
wantErrMsg: "",
|
|
inclHTTPTLS: false,
|
|
strictSNI: false,
|
|
}, {
|
|
name: "tls_no_clientid",
|
|
proto: proxy.ProtoTLS,
|
|
confSrvName: "example.com",
|
|
cliSrvName: "example.com",
|
|
wantClientID: "",
|
|
wantErrMsg: "",
|
|
inclHTTPTLS: false,
|
|
strictSNI: true,
|
|
}, {
|
|
name: "tls_no_client_server_name",
|
|
proto: proxy.ProtoTLS,
|
|
confSrvName: "example.com",
|
|
cliSrvName: "",
|
|
wantClientID: "",
|
|
wantErrMsg: `clientid check: client server name "" ` +
|
|
`doesn't match host server name "example.com"`,
|
|
inclHTTPTLS: false,
|
|
strictSNI: true,
|
|
}, {
|
|
name: "tls_no_client_server_name_no_strict",
|
|
proto: proxy.ProtoTLS,
|
|
confSrvName: "example.com",
|
|
cliSrvName: "",
|
|
wantClientID: "",
|
|
wantErrMsg: "",
|
|
inclHTTPTLS: false,
|
|
strictSNI: false,
|
|
}, {
|
|
name: "tls_clientid",
|
|
proto: proxy.ProtoTLS,
|
|
confSrvName: "example.com",
|
|
cliSrvName: "cli.example.com",
|
|
wantClientID: "cli",
|
|
wantErrMsg: "",
|
|
inclHTTPTLS: false,
|
|
strictSNI: true,
|
|
}, {
|
|
name: "tls_clientid_hostname_error",
|
|
proto: proxy.ProtoTLS,
|
|
confSrvName: "example.com",
|
|
cliSrvName: "cli.example.net",
|
|
wantClientID: "",
|
|
wantErrMsg: `clientid check: client server name "cli.example.net" ` +
|
|
`doesn't match host server name "example.com"`,
|
|
inclHTTPTLS: false,
|
|
strictSNI: true,
|
|
}, {
|
|
name: "tls_invalid_clientid",
|
|
proto: proxy.ProtoTLS,
|
|
confSrvName: "example.com",
|
|
cliSrvName: "!!!.example.com",
|
|
wantClientID: "",
|
|
wantErrMsg: `clientid check: invalid clientid "!!!": ` +
|
|
`bad hostname label rune '!'`,
|
|
inclHTTPTLS: false,
|
|
strictSNI: true,
|
|
}, {
|
|
name: "tls_clientid_too_long",
|
|
proto: proxy.ProtoTLS,
|
|
confSrvName: "example.com",
|
|
cliSrvName: `abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmno` +
|
|
`pqrstuvwxyz0123456789.example.com`,
|
|
wantClientID: "",
|
|
wantErrMsg: `clientid check: invalid clientid "abcdefghijklmno` +
|
|
`pqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789": ` +
|
|
`hostname label is too long: got 72, max 63`,
|
|
inclHTTPTLS: false,
|
|
strictSNI: true,
|
|
}, {
|
|
name: "quic_clientid",
|
|
proto: proxy.ProtoQUIC,
|
|
confSrvName: "example.com",
|
|
cliSrvName: "cli.example.com",
|
|
wantClientID: "cli",
|
|
wantErrMsg: "",
|
|
inclHTTPTLS: false,
|
|
strictSNI: true,
|
|
}, {
|
|
name: "tls_clientid_issue3437",
|
|
proto: proxy.ProtoTLS,
|
|
confSrvName: "example.com",
|
|
cliSrvName: "cli.myexample.com",
|
|
wantClientID: "",
|
|
wantErrMsg: `clientid check: client server name "cli.myexample.com" ` +
|
|
`doesn't match host server name "example.com"`,
|
|
inclHTTPTLS: false,
|
|
strictSNI: true,
|
|
}, {
|
|
name: "tls_case",
|
|
proto: proxy.ProtoTLS,
|
|
confSrvName: "example.com",
|
|
cliSrvName: "InSeNsItIvE.example.com",
|
|
wantClientID: "insensitive",
|
|
wantErrMsg: ``,
|
|
inclHTTPTLS: false,
|
|
strictSNI: true,
|
|
}, {
|
|
name: "quic_case",
|
|
proto: proxy.ProtoQUIC,
|
|
confSrvName: "example.com",
|
|
cliSrvName: "InSeNsItIvE.example.com",
|
|
wantClientID: "insensitive",
|
|
wantErrMsg: ``,
|
|
inclHTTPTLS: false,
|
|
strictSNI: true,
|
|
}, {
|
|
name: "https_no_clientid",
|
|
proto: proxy.ProtoHTTPS,
|
|
confSrvName: "example.com",
|
|
cliSrvName: "example.com",
|
|
wantClientID: "",
|
|
wantErrMsg: "",
|
|
inclHTTPTLS: true,
|
|
strictSNI: true,
|
|
}, {
|
|
name: "https_clientid",
|
|
proto: proxy.ProtoHTTPS,
|
|
confSrvName: "example.com",
|
|
cliSrvName: "cli.example.com",
|
|
wantClientID: "cli",
|
|
wantErrMsg: "",
|
|
inclHTTPTLS: true,
|
|
strictSNI: true,
|
|
}, {
|
|
name: "https_issue5518",
|
|
proto: proxy.ProtoHTTPS,
|
|
confSrvName: "example.com",
|
|
cliSrvName: "cli.example.com",
|
|
wantClientID: "cli",
|
|
wantErrMsg: "",
|
|
inclHTTPTLS: false,
|
|
strictSNI: true,
|
|
}, {
|
|
name: "https_no_host",
|
|
proto: proxy.ProtoHTTPS,
|
|
confSrvName: "example.com",
|
|
cliSrvName: "example.com",
|
|
wantClientID: "",
|
|
wantErrMsg: "",
|
|
inclHTTPTLS: false,
|
|
strictSNI: true,
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
tlsConf := &TLSConfig{
|
|
ServerName: tc.confSrvName,
|
|
StrictSNICheck: tc.strictSNI,
|
|
}
|
|
|
|
srv := &Server{
|
|
conf: ServerConfig{TLSConf: tlsConf},
|
|
baseLogger: testLogger,
|
|
logger: testLogger,
|
|
}
|
|
|
|
var (
|
|
conn net.Conn
|
|
httpReq *http.Request
|
|
)
|
|
|
|
switch tc.proto {
|
|
case proxy.ProtoHTTPS:
|
|
httpReq = newHTTPReq(tc.cliSrvName, tc.inclHTTPTLS)
|
|
case proxy.ProtoQUIC:
|
|
// TODO(a.garipov): Find ways of testing this with the new
|
|
// quic-go API.
|
|
t.Skipf("skipped during the quic-go api update")
|
|
case proxy.ProtoTLS:
|
|
conn = testTLSConn{
|
|
serverName: tc.cliSrvName,
|
|
}
|
|
}
|
|
|
|
pctx := &proxy.DNSContext{
|
|
Proto: tc.proto,
|
|
Conn: conn,
|
|
HTTPRequest: httpReq,
|
|
}
|
|
|
|
clientID, err := srv.clientIDFromDNSContext(pctx)
|
|
assert.Equal(t, tc.wantClientID, clientID)
|
|
|
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
|
})
|
|
}
|
|
}
|
|
|
|
// newHTTPReq is a helper to create HTTP requests for tests.
|
|
func newHTTPReq(cliSrvName string, inclTLS bool) (r *http.Request) {
|
|
u := &url.URL{
|
|
Path: "/dns-query",
|
|
}
|
|
|
|
r = &http.Request{
|
|
ProtoMajor: 1,
|
|
ProtoMinor: 1,
|
|
URL: u,
|
|
Host: cliSrvName,
|
|
}
|
|
|
|
if inclTLS {
|
|
r.TLS = &tls.ConnectionState{
|
|
ServerName: cliSrvName,
|
|
}
|
|
}
|
|
|
|
return r
|
|
}
|
|
|
|
func TestClientIDFromDNSContextHTTPS(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
path string
|
|
cliSrvName string
|
|
wantClientID string
|
|
wantErrMsg string
|
|
}{{
|
|
name: "no_clientid",
|
|
path: "/dns-query",
|
|
cliSrvName: "example.com",
|
|
wantClientID: "",
|
|
wantErrMsg: "",
|
|
}, {
|
|
name: "no_clientid_slash",
|
|
path: "/dns-query/",
|
|
cliSrvName: "example.com",
|
|
wantClientID: "",
|
|
wantErrMsg: "",
|
|
}, {
|
|
name: "clientid",
|
|
path: "/dns-query/cli",
|
|
cliSrvName: "example.com",
|
|
wantClientID: "cli",
|
|
wantErrMsg: "",
|
|
}, {
|
|
name: "clientid_slash",
|
|
path: "/dns-query/cli/",
|
|
cliSrvName: "example.com",
|
|
wantClientID: "cli",
|
|
wantErrMsg: "",
|
|
}, {
|
|
name: "clientid_case",
|
|
path: "/dns-query/InSeNsItIvE",
|
|
cliSrvName: "example.com",
|
|
wantClientID: "insensitive",
|
|
wantErrMsg: ``,
|
|
}, {
|
|
name: "bad_url",
|
|
path: "/foo",
|
|
cliSrvName: "example.com",
|
|
wantClientID: "",
|
|
wantErrMsg: `clientid check: invalid path "/foo"`,
|
|
}, {
|
|
name: "extra",
|
|
path: "/dns-query/cli/foo",
|
|
cliSrvName: "example.com",
|
|
wantClientID: "",
|
|
wantErrMsg: `clientid check: invalid path "/dns-query/cli/foo": extra parts`,
|
|
}, {
|
|
name: "invalid_clientid",
|
|
path: "/dns-query/!!!",
|
|
cliSrvName: "example.com",
|
|
wantClientID: "",
|
|
wantErrMsg: `clientid check: invalid clientid "!!!": bad hostname label rune '!'`,
|
|
}, {
|
|
name: "both_ids",
|
|
path: "/dns-query/right",
|
|
cliSrvName: "wrong.example.com",
|
|
wantClientID: "right",
|
|
wantErrMsg: "",
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
connState := &tls.ConnectionState{
|
|
ServerName: tc.cliSrvName,
|
|
}
|
|
|
|
r := &http.Request{
|
|
URL: &url.URL{
|
|
Path: tc.path,
|
|
},
|
|
TLS: connState,
|
|
}
|
|
|
|
pctx := &proxy.DNSContext{
|
|
Proto: proxy.ProtoHTTPS,
|
|
HTTPRequest: r,
|
|
}
|
|
|
|
clientID, err := clientIDFromDNSContextHTTPS(pctx)
|
|
assert.Equal(t, tc.wantClientID, clientID)
|
|
|
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
|
})
|
|
}
|
|
}
|