dnsforward: fix panic; take Host into account

This commit is contained in:
Ainar Garipov
2023-02-21 14:54:43 +03:00
parent 66b831072c
commit f543b47261
4 changed files with 108 additions and 36 deletions

View File

@@ -14,21 +14,41 @@ and this project adheres to
<!-- <!--
## [v0.108.0] - TBA ## [v0.108.0] - TBA
## [v0.107.25] - 2023-03-09 (APPROX.) ## [v0.107.26] - 2023-03-09 (APPROX.)
See also the [v0.107.25 GitHub milestone][ms-v0.107.25]. See also the [v0.107.26 GitHub milestone][ms-v0.107.26].
[ms-v0.107.25]: https://github.com/AdguardTeam/AdGuardHome/milestone/61?closed=1 [ms-v0.107.26]: https://github.com/AdguardTeam/AdGuardHome/milestone/62?closed=1
NOTE: Add new changes BELOW THIS COMMENT. NOTE: Add new changes BELOW THIS COMMENT.
--> -->
### Fixed
- Failing service installation via script on FreeBSD ([#5431]).
[#5431]: https://github.com/AdguardTeam/AdGuardHome/issues/5431
<!-- <!--
NOTE: Add new changes ABOVE THIS COMMENT. NOTE: Add new changes ABOVE THIS COMMENT.
--> -->
## [v0.107.25] - 2023-02-21
See also the [v0.107.25 GitHub milestone][ms-v0.107.25].
### Fixed
- Panic when using unencrypted DNS-over-HTTPS ([#5518]).
[#5518]: https://github.com/AdguardTeam/AdGuardHome/issues/5518
[ms-v0.107.25]: https://github.com/AdguardTeam/AdGuardHome/milestone/61?closed=1
## [v0.107.24] - 2023-02-15 ## [v0.107.24] - 2023-02-15
See also the [v0.107.24 GitHub milestone][ms-v0.107.24]. See also the [v0.107.24 GitHub milestone][ms-v0.107.24].
@@ -1669,11 +1689,12 @@ See also the [v0.104.2 GitHub milestone][ms-v0.104.2].
<!-- <!--
[Unreleased]: https://github.com/AdguardTeam/AdGuardHome/compare/v0.107.25...HEAD [Unreleased]: https://github.com/AdguardTeam/AdGuardHome/compare/v0.107.26...HEAD
[v0.107.25]: https://github.com/AdguardTeam/AdGuardHome/compare/v0.107.24...v0.107.25 [v0.107.26]: https://github.com/AdguardTeam/AdGuardHome/compare/v0.107.25...v0.107.26
--> -->
[Unreleased]: https://github.com/AdguardTeam/AdGuardHome/compare/v0.107.24...HEAD [Unreleased]: https://github.com/AdguardTeam/AdGuardHome/compare/v0.107.25...HEAD
[v0.107.25]: https://github.com/AdguardTeam/AdGuardHome/compare/v0.107.24...v0.107.25
[v0.107.24]: https://github.com/AdguardTeam/AdGuardHome/compare/v0.107.23...v0.107.24 [v0.107.24]: https://github.com/AdguardTeam/AdGuardHome/compare/v0.107.23...v0.107.24
[v0.107.23]: https://github.com/AdguardTeam/AdGuardHome/compare/v0.107.22...v0.107.23 [v0.107.23]: https://github.com/AdguardTeam/AdGuardHome/compare/v0.107.22...v0.107.23
[v0.107.22]: https://github.com/AdguardTeam/AdGuardHome/compare/v0.107.21...v0.107.22 [v0.107.22]: https://github.com/AdguardTeam/AdGuardHome/compare/v0.107.21...v0.107.22

View File

@@ -147,11 +147,24 @@ func (s *Server) clientIDFromDNSContext(pctx *proxy.DNSContext) (clientID string
return clientID, nil return clientID, nil
} }
// clientServerName returns the TLS server name based on the protocol. // clientServerName returns the TLS server name based on the protocol. For
// DNS-over-HTTPS requests, it will return the hostname part of the Host header
// if there is one.
func clientServerName(pctx *proxy.DNSContext, proto proxy.Proto) (srvName string, err error) { func clientServerName(pctx *proxy.DNSContext, proto proxy.Proto) (srvName string, err error) {
switch proto { switch proto {
case proxy.ProtoHTTPS: case proxy.ProtoHTTPS:
srvName = pctx.HTTPRequest.TLS.ServerName r := pctx.HTTPRequest
if connState := r.TLS; connState != nil {
srvName = connState.ServerName
} else if r.Host != "" {
var host string
host, err = netutil.SplitHost(r.Host)
if err != nil {
return "", fmt.Errorf("parsing host: %w", err)
}
srvName = host
}
case proxy.ProtoQUIC: case proxy.ProtoQUIC:
qConn := pctx.QUICConnection qConn := pctx.QUICConnection
conn, ok := qConn.(quicConnection) conn, ok := qConn.(quicConnection)

View File

@@ -50,136 +50,169 @@ func TestServer_clientIDFromDNSContext(t *testing.T) {
testCases := []struct { testCases := []struct {
name string name string
proto proxy.Proto proto proxy.Proto
hostSrvName string confSrvName string
cliSrvName string cliSrvName string
wantClientID string wantClientID string
wantErrMsg string wantErrMsg string
inclHTTPTLS bool
strictSNI bool strictSNI bool
}{{ }{{
name: "udp", name: "udp",
proto: proxy.ProtoUDP, proto: proxy.ProtoUDP,
hostSrvName: "", confSrvName: "",
cliSrvName: "", cliSrvName: "",
wantClientID: "", wantClientID: "",
wantErrMsg: "", wantErrMsg: "",
inclHTTPTLS: false,
strictSNI: false, strictSNI: false,
}, { }, {
name: "tls_no_clientid", name: "tls_no_clientid",
proto: proxy.ProtoTLS, proto: proxy.ProtoTLS,
hostSrvName: "example.com", confSrvName: "example.com",
cliSrvName: "example.com", cliSrvName: "example.com",
wantClientID: "", wantClientID: "",
wantErrMsg: "", wantErrMsg: "",
inclHTTPTLS: false,
strictSNI: true, strictSNI: true,
}, { }, {
name: "tls_no_client_server_name", name: "tls_no_client_server_name",
proto: proxy.ProtoTLS, proto: proxy.ProtoTLS,
hostSrvName: "example.com", confSrvName: "example.com",
cliSrvName: "", cliSrvName: "",
wantClientID: "", wantClientID: "",
wantErrMsg: `clientid check: client server name "" ` + wantErrMsg: `clientid check: client server name "" ` +
`doesn't match host server name "example.com"`, `doesn't match host server name "example.com"`,
inclHTTPTLS: false,
strictSNI: true, strictSNI: true,
}, { }, {
name: "tls_no_client_server_name_no_strict", name: "tls_no_client_server_name_no_strict",
proto: proxy.ProtoTLS, proto: proxy.ProtoTLS,
hostSrvName: "example.com", confSrvName: "example.com",
cliSrvName: "", cliSrvName: "",
wantClientID: "", wantClientID: "",
wantErrMsg: "", wantErrMsg: "",
inclHTTPTLS: false,
strictSNI: false, strictSNI: false,
}, { }, {
name: "tls_clientid", name: "tls_clientid",
proto: proxy.ProtoTLS, proto: proxy.ProtoTLS,
hostSrvName: "example.com", confSrvName: "example.com",
cliSrvName: "cli.example.com", cliSrvName: "cli.example.com",
wantClientID: "cli", wantClientID: "cli",
wantErrMsg: "", wantErrMsg: "",
inclHTTPTLS: false,
strictSNI: true, strictSNI: true,
}, { }, {
name: "tls_clientid_hostname_error", name: "tls_clientid_hostname_error",
proto: proxy.ProtoTLS, proto: proxy.ProtoTLS,
hostSrvName: "example.com", confSrvName: "example.com",
cliSrvName: "cli.example.net", cliSrvName: "cli.example.net",
wantClientID: "", wantClientID: "",
wantErrMsg: `clientid check: client server name "cli.example.net" ` + wantErrMsg: `clientid check: client server name "cli.example.net" ` +
`doesn't match host server name "example.com"`, `doesn't match host server name "example.com"`,
inclHTTPTLS: false,
strictSNI: true, strictSNI: true,
}, { }, {
name: "tls_invalid_clientid", name: "tls_invalid_clientid",
proto: proxy.ProtoTLS, proto: proxy.ProtoTLS,
hostSrvName: "example.com", confSrvName: "example.com",
cliSrvName: "!!!.example.com", cliSrvName: "!!!.example.com",
wantClientID: "", wantClientID: "",
wantErrMsg: `clientid check: invalid clientid "!!!": ` + wantErrMsg: `clientid check: invalid clientid "!!!": ` +
`bad domain name label rune '!'`, `bad domain name label rune '!'`,
inclHTTPTLS: false,
strictSNI: true, strictSNI: true,
}, { }, {
name: "tls_clientid_too_long", name: "tls_clientid_too_long",
proto: proxy.ProtoTLS, proto: proxy.ProtoTLS,
hostSrvName: "example.com", confSrvName: "example.com",
cliSrvName: `abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmno` + cliSrvName: `abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmno` +
`pqrstuvwxyz0123456789.example.com`, `pqrstuvwxyz0123456789.example.com`,
wantClientID: "", wantClientID: "",
wantErrMsg: `clientid check: invalid clientid "abcdefghijklmno` + wantErrMsg: `clientid check: invalid clientid "abcdefghijklmno` +
`pqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789": ` + `pqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789": ` +
`domain name label is too long: got 72, max 63`, `domain name label is too long: got 72, max 63`,
inclHTTPTLS: false,
strictSNI: true, strictSNI: true,
}, { }, {
name: "quic_clientid", name: "quic_clientid",
proto: proxy.ProtoQUIC, proto: proxy.ProtoQUIC,
hostSrvName: "example.com", confSrvName: "example.com",
cliSrvName: "cli.example.com", cliSrvName: "cli.example.com",
wantClientID: "cli", wantClientID: "cli",
wantErrMsg: "", wantErrMsg: "",
inclHTTPTLS: false,
strictSNI: true, strictSNI: true,
}, { }, {
name: "tls_clientid_issue3437", name: "tls_clientid_issue3437",
proto: proxy.ProtoTLS, proto: proxy.ProtoTLS,
hostSrvName: "example.com", confSrvName: "example.com",
cliSrvName: "cli.myexample.com", cliSrvName: "cli.myexample.com",
wantClientID: "", wantClientID: "",
wantErrMsg: `clientid check: client server name "cli.myexample.com" ` + wantErrMsg: `clientid check: client server name "cli.myexample.com" ` +
`doesn't match host server name "example.com"`, `doesn't match host server name "example.com"`,
inclHTTPTLS: false,
strictSNI: true, strictSNI: true,
}, { }, {
name: "tls_case", name: "tls_case",
proto: proxy.ProtoTLS, proto: proxy.ProtoTLS,
hostSrvName: "example.com", confSrvName: "example.com",
cliSrvName: "InSeNsItIvE.example.com", cliSrvName: "InSeNsItIvE.example.com",
wantClientID: "insensitive", wantClientID: "insensitive",
wantErrMsg: ``, wantErrMsg: ``,
inclHTTPTLS: false,
strictSNI: true, strictSNI: true,
}, { }, {
name: "quic_case", name: "quic_case",
proto: proxy.ProtoQUIC, proto: proxy.ProtoQUIC,
hostSrvName: "example.com", confSrvName: "example.com",
cliSrvName: "InSeNsItIvE.example.com", cliSrvName: "InSeNsItIvE.example.com",
wantClientID: "insensitive", wantClientID: "insensitive",
wantErrMsg: ``, wantErrMsg: ``,
inclHTTPTLS: false,
strictSNI: true, strictSNI: true,
}, { }, {
name: "https_no_clientid", name: "https_no_clientid",
proto: proxy.ProtoHTTPS, proto: proxy.ProtoHTTPS,
hostSrvName: "example.com", confSrvName: "example.com",
cliSrvName: "example.com", cliSrvName: "example.com",
wantClientID: "", wantClientID: "",
wantErrMsg: "", wantErrMsg: "",
inclHTTPTLS: true,
strictSNI: true, strictSNI: true,
}, { }, {
name: "https_clientid", name: "https_clientid",
proto: proxy.ProtoHTTPS, proto: proxy.ProtoHTTPS,
hostSrvName: "example.com", confSrvName: "example.com",
cliSrvName: "cli.example.com", cliSrvName: "cli.example.com",
wantClientID: "cli", wantClientID: "cli",
wantErrMsg: "", 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, strictSNI: true,
}} }}
for _, tc := range testCases { for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
tlsConf := TLSConfig{ tlsConf := TLSConfig{
ServerName: tc.hostSrvName, ServerName: tc.confSrvName,
StrictSNICheck: tc.strictSNI, StrictSNICheck: tc.strictSNI,
} }
@@ -195,7 +228,7 @@ func TestServer_clientIDFromDNSContext(t *testing.T) {
switch tc.proto { switch tc.proto {
case proxy.ProtoHTTPS: case proxy.ProtoHTTPS:
httpReq = newHTTPReq(tc.cliSrvName) httpReq = newHTTPReq(tc.cliSrvName, tc.inclHTTPTLS)
case proxy.ProtoQUIC: case proxy.ProtoQUIC:
qconn = testQUICConnection{ qconn = testQUICConnection{
serverName: tc.cliSrvName, serverName: tc.cliSrvName,
@@ -222,20 +255,25 @@ func TestServer_clientIDFromDNSContext(t *testing.T) {
} }
// newHTTPReq is a helper to create HTTP requests for tests. // newHTTPReq is a helper to create HTTP requests for tests.
func newHTTPReq(cliSrvName string) (r *http.Request) { func newHTTPReq(cliSrvName string, inclTLS bool) (r *http.Request) {
u := &url.URL{ u := &url.URL{
Path: "/dns-query", Path: "/dns-query",
} }
return &http.Request{ r = &http.Request{
ProtoMajor: 1, ProtoMajor: 1,
ProtoMinor: 1, ProtoMinor: 1,
URL: u, URL: u,
Host: cliSrvName, Host: cliSrvName,
TLS: &tls.ConnectionState{
ServerName: cliSrvName,
},
} }
if inclTLS {
r.TLS = &tls.ConnectionState{
ServerName: cliSrvName,
}
}
return r
} }
func TestClientIDFromDNSContextHTTPS(t *testing.T) { func TestClientIDFromDNSContextHTTPS(t *testing.T) {

View File

@@ -52,7 +52,7 @@ trap not_found EXIT
go_version="$( "${GO:-go}" version )" go_version="$( "${GO:-go}" version )"
readonly go_version readonly go_version
go_min_version='go1.18' go_min_version='go1.19.6'
go_version_msg=" go_version_msg="
warning: your go version (${go_version}) is different from the recommended minimal one (${go_min_version}). warning: your go version (${go_version}) is different from the recommended minimal one (${go_min_version}).
if you have the version installed, please set the GO environment variable. if you have the version installed, please set the GO environment variable.