2021-01-27 15:32:13 +00:00
|
|
|
package dnsforward
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
2021-10-22 08:58:18 +00:00
|
|
|
"github.com/AdguardTeam/golibs/testutil"
|
2021-01-27 15:32:13 +00:00
|
|
|
"github.com/lucas-clemente/quic-go"
|
|
|
|
"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
|
2021-04-13 13:23:36 +00:00
|
|
|
// actually implementing all methods.
|
2021-01-27 15:32:13 +00:00
|
|
|
net.Conn
|
|
|
|
|
|
|
|
serverName string
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConnectionState implements the tlsConn interface for testTLSConn.
|
|
|
|
func (c testTLSConn) ConnectionState() (cs tls.ConnectionState) {
|
|
|
|
cs.ServerName = c.serverName
|
|
|
|
|
|
|
|
return cs
|
|
|
|
}
|
|
|
|
|
2022-05-24 16:47:09 +00:00
|
|
|
// testQUICConnection is a quicConnection for tests.
|
|
|
|
type testQUICConnection struct {
|
|
|
|
// Connection is embedded here simply to make testQUICConnection a
|
|
|
|
// quic.Connection without actually implementing all methods.
|
|
|
|
quic.Connection
|
2021-01-27 15:32:13 +00:00
|
|
|
|
|
|
|
serverName string
|
|
|
|
}
|
|
|
|
|
2022-05-24 16:47:09 +00:00
|
|
|
// ConnectionState implements the quicConnection interface for
|
|
|
|
// testQUICConnection.
|
|
|
|
func (c testQUICConnection) ConnectionState() (cs quic.ConnectionState) {
|
2021-04-21 09:49:41 +00:00
|
|
|
cs.TLS.ServerName = c.serverName
|
2021-01-27 15:32:13 +00:00
|
|
|
|
|
|
|
return cs
|
|
|
|
}
|
|
|
|
|
2021-06-29 12:53:28 +00:00
|
|
|
func TestServer_clientIDFromDNSContext(t *testing.T) {
|
2021-08-09 13:03:37 +00:00
|
|
|
// TODO(a.garipov): Consider moving away from the text-based error
|
|
|
|
// checks and onto a more structured approach.
|
2021-01-27 15:32:13 +00:00
|
|
|
testCases := []struct {
|
|
|
|
name string
|
2021-06-29 12:53:28 +00:00
|
|
|
proto proxy.Proto
|
2021-01-27 15:32:13 +00:00
|
|
|
hostSrvName string
|
|
|
|
cliSrvName string
|
|
|
|
wantClientID string
|
|
|
|
wantErrMsg string
|
2021-02-11 12:20:30 +00:00
|
|
|
strictSNI bool
|
2021-01-27 15:32:13 +00:00
|
|
|
}{{
|
|
|
|
name: "udp",
|
|
|
|
proto: proxy.ProtoUDP,
|
|
|
|
hostSrvName: "",
|
|
|
|
cliSrvName: "",
|
|
|
|
wantClientID: "",
|
|
|
|
wantErrMsg: "",
|
2021-02-11 12:20:30 +00:00
|
|
|
strictSNI: false,
|
2021-01-27 15:32:13 +00:00
|
|
|
}, {
|
2022-02-10 12:42:59 +00:00
|
|
|
name: "tls_no_clientid",
|
2021-01-27 15:32:13 +00:00
|
|
|
proto: proxy.ProtoTLS,
|
|
|
|
hostSrvName: "example.com",
|
|
|
|
cliSrvName: "example.com",
|
|
|
|
wantClientID: "",
|
|
|
|
wantErrMsg: "",
|
2021-02-11 12:20:30 +00:00
|
|
|
strictSNI: true,
|
|
|
|
}, {
|
|
|
|
name: "tls_no_client_server_name",
|
|
|
|
proto: proxy.ProtoTLS,
|
|
|
|
hostSrvName: "example.com",
|
|
|
|
cliSrvName: "",
|
|
|
|
wantClientID: "",
|
2022-02-10 12:42:59 +00:00
|
|
|
wantErrMsg: `clientid check: client server name "" ` +
|
2021-02-11 12:20:30 +00:00
|
|
|
`doesn't match host server name "example.com"`,
|
|
|
|
strictSNI: true,
|
|
|
|
}, {
|
|
|
|
name: "tls_no_client_server_name_no_strict",
|
|
|
|
proto: proxy.ProtoTLS,
|
|
|
|
hostSrvName: "example.com",
|
|
|
|
cliSrvName: "",
|
|
|
|
wantClientID: "",
|
|
|
|
wantErrMsg: "",
|
|
|
|
strictSNI: false,
|
2021-01-27 15:32:13 +00:00
|
|
|
}, {
|
2022-02-10 12:42:59 +00:00
|
|
|
name: "tls_clientid",
|
2021-01-27 15:32:13 +00:00
|
|
|
proto: proxy.ProtoTLS,
|
|
|
|
hostSrvName: "example.com",
|
|
|
|
cliSrvName: "cli.example.com",
|
|
|
|
wantClientID: "cli",
|
|
|
|
wantErrMsg: "",
|
2021-02-11 12:20:30 +00:00
|
|
|
strictSNI: true,
|
2021-01-27 15:32:13 +00:00
|
|
|
}, {
|
2022-02-10 12:42:59 +00:00
|
|
|
name: "tls_clientid_hostname_error",
|
2021-01-27 15:32:13 +00:00
|
|
|
proto: proxy.ProtoTLS,
|
|
|
|
hostSrvName: "example.com",
|
|
|
|
cliSrvName: "cli.example.net",
|
|
|
|
wantClientID: "",
|
2022-02-10 12:42:59 +00:00
|
|
|
wantErrMsg: `clientid check: client server name "cli.example.net" ` +
|
2021-02-11 12:20:30 +00:00
|
|
|
`doesn't match host server name "example.com"`,
|
|
|
|
strictSNI: true,
|
2021-01-27 15:32:13 +00:00
|
|
|
}, {
|
2022-02-10 12:42:59 +00:00
|
|
|
name: "tls_invalid_clientid",
|
2021-01-27 15:32:13 +00:00
|
|
|
proto: proxy.ProtoTLS,
|
|
|
|
hostSrvName: "example.com",
|
|
|
|
cliSrvName: "!!!.example.com",
|
|
|
|
wantClientID: "",
|
2022-02-10 12:42:59 +00:00
|
|
|
wantErrMsg: `clientid check: invalid clientid "!!!": ` +
|
2021-08-09 13:03:37 +00:00
|
|
|
`bad domain name label rune '!'`,
|
2021-02-11 12:20:30 +00:00
|
|
|
strictSNI: true,
|
2021-01-27 15:32:13 +00:00
|
|
|
}, {
|
2022-02-10 12:42:59 +00:00
|
|
|
name: "tls_clientid_too_long",
|
2021-02-11 12:20:30 +00:00
|
|
|
proto: proxy.ProtoTLS,
|
|
|
|
hostSrvName: "example.com",
|
|
|
|
cliSrvName: `abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmno` +
|
|
|
|
`pqrstuvwxyz0123456789.example.com`,
|
2021-01-27 15:32:13 +00:00
|
|
|
wantClientID: "",
|
2022-02-10 12:42:59 +00:00
|
|
|
wantErrMsg: `clientid check: invalid clientid "abcdefghijklmno` +
|
2021-05-06 10:02:48 +00:00
|
|
|
`pqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789": ` +
|
2021-08-09 13:03:37 +00:00
|
|
|
`domain name label is too long: got 72, max 63`,
|
2021-02-11 12:20:30 +00:00
|
|
|
strictSNI: true,
|
2021-01-27 15:32:13 +00:00
|
|
|
}, {
|
2022-02-10 12:42:59 +00:00
|
|
|
name: "quic_clientid",
|
2021-01-27 15:32:13 +00:00
|
|
|
proto: proxy.ProtoQUIC,
|
|
|
|
hostSrvName: "example.com",
|
|
|
|
cliSrvName: "cli.example.com",
|
|
|
|
wantClientID: "cli",
|
|
|
|
wantErrMsg: "",
|
2021-02-11 12:20:30 +00:00
|
|
|
strictSNI: true,
|
2021-08-12 15:35:30 +00:00
|
|
|
}, {
|
2022-02-10 12:42:59 +00:00
|
|
|
name: "tls_clientid_issue3437",
|
2021-08-12 15:35:30 +00:00
|
|
|
proto: proxy.ProtoTLS,
|
|
|
|
hostSrvName: "example.com",
|
|
|
|
cliSrvName: "cli.myexample.com",
|
|
|
|
wantClientID: "",
|
2022-02-10 12:42:59 +00:00
|
|
|
wantErrMsg: `clientid check: client server name "cli.myexample.com" ` +
|
2021-08-12 15:35:30 +00:00
|
|
|
`doesn't match host server name "example.com"`,
|
|
|
|
strictSNI: true,
|
2022-05-04 18:01:41 +00:00
|
|
|
}, {
|
|
|
|
name: "tls_case",
|
|
|
|
proto: proxy.ProtoTLS,
|
|
|
|
hostSrvName: "example.com",
|
|
|
|
cliSrvName: "InSeNsItIvE.example.com",
|
|
|
|
wantClientID: "insensitive",
|
|
|
|
wantErrMsg: ``,
|
|
|
|
strictSNI: true,
|
|
|
|
}, {
|
|
|
|
name: "quic_case",
|
|
|
|
proto: proxy.ProtoQUIC,
|
|
|
|
hostSrvName: "example.com",
|
|
|
|
cliSrvName: "InSeNsItIvE.example.com",
|
|
|
|
wantClientID: "insensitive",
|
|
|
|
wantErrMsg: ``,
|
|
|
|
strictSNI: true,
|
2021-01-27 15:32:13 +00:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2021-02-11 12:20:30 +00:00
|
|
|
tlsConf := TLSConfig{
|
|
|
|
ServerName: tc.hostSrvName,
|
|
|
|
StrictSNICheck: tc.strictSNI,
|
|
|
|
}
|
2021-06-29 12:53:28 +00:00
|
|
|
|
2021-01-27 15:32:13 +00:00
|
|
|
srv := &Server{
|
2021-02-11 12:20:30 +00:00
|
|
|
conf: ServerConfig{TLSConfig: tlsConf},
|
2021-01-27 15:32:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var conn net.Conn
|
|
|
|
if tc.proto == proxy.ProtoTLS {
|
|
|
|
conn = testTLSConn{
|
|
|
|
serverName: tc.cliSrvName,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-24 16:47:09 +00:00
|
|
|
var qconn quic.Connection
|
2021-01-27 15:32:13 +00:00
|
|
|
if tc.proto == proxy.ProtoQUIC {
|
2022-05-24 16:47:09 +00:00
|
|
|
qconn = testQUICConnection{
|
2021-01-27 15:32:13 +00:00
|
|
|
serverName: tc.cliSrvName,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-29 12:53:28 +00:00
|
|
|
pctx := &proxy.DNSContext{
|
2022-05-24 16:47:09 +00:00
|
|
|
Proto: tc.proto,
|
|
|
|
Conn: conn,
|
|
|
|
QUICConnection: qconn,
|
2021-01-27 15:32:13 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 12:53:28 +00:00
|
|
|
clientID, err := srv.clientIDFromDNSContext(pctx)
|
|
|
|
assert.Equal(t, tc.wantClientID, clientID)
|
2021-01-27 15:32:13 +00:00
|
|
|
|
2021-10-22 08:58:18 +00:00
|
|
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
2021-01-27 15:32:13 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-29 12:53:28 +00:00
|
|
|
func TestClientIDFromDNSContextHTTPS(t *testing.T) {
|
2021-01-27 15:32:13 +00:00
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
path string
|
|
|
|
wantClientID string
|
|
|
|
wantErrMsg string
|
|
|
|
}{{
|
2022-02-10 12:42:59 +00:00
|
|
|
name: "no_clientid",
|
2021-01-27 15:32:13 +00:00
|
|
|
path: "/dns-query",
|
|
|
|
wantClientID: "",
|
|
|
|
wantErrMsg: "",
|
|
|
|
}, {
|
2022-02-10 12:42:59 +00:00
|
|
|
name: "no_clientid_slash",
|
2021-01-27 15:32:13 +00:00
|
|
|
path: "/dns-query/",
|
|
|
|
wantClientID: "",
|
|
|
|
wantErrMsg: "",
|
|
|
|
}, {
|
2022-02-10 12:42:59 +00:00
|
|
|
name: "clientid",
|
2021-01-27 15:32:13 +00:00
|
|
|
path: "/dns-query/cli",
|
|
|
|
wantClientID: "cli",
|
|
|
|
wantErrMsg: "",
|
|
|
|
}, {
|
2022-02-10 12:42:59 +00:00
|
|
|
name: "clientid_slash",
|
2021-01-27 15:32:13 +00:00
|
|
|
path: "/dns-query/cli/",
|
|
|
|
wantClientID: "cli",
|
|
|
|
wantErrMsg: "",
|
2022-05-04 18:01:41 +00:00
|
|
|
}, {
|
|
|
|
name: "clientid_case",
|
|
|
|
path: "/dns-query/InSeNsItIvE",
|
|
|
|
wantClientID: "insensitive",
|
|
|
|
wantErrMsg: ``,
|
2021-01-27 15:32:13 +00:00
|
|
|
}, {
|
|
|
|
name: "bad_url",
|
|
|
|
path: "/foo",
|
|
|
|
wantClientID: "",
|
2022-02-10 12:42:59 +00:00
|
|
|
wantErrMsg: `clientid check: invalid path "/foo"`,
|
2021-01-27 15:32:13 +00:00
|
|
|
}, {
|
|
|
|
name: "extra",
|
|
|
|
path: "/dns-query/cli/foo",
|
|
|
|
wantClientID: "",
|
2022-02-10 12:42:59 +00:00
|
|
|
wantErrMsg: `clientid check: invalid path "/dns-query/cli/foo": extra parts`,
|
2021-01-27 15:32:13 +00:00
|
|
|
}, {
|
2022-02-10 12:42:59 +00:00
|
|
|
name: "invalid_clientid",
|
2021-01-27 15:32:13 +00:00
|
|
|
path: "/dns-query/!!!",
|
|
|
|
wantClientID: "",
|
2022-02-10 12:42:59 +00:00
|
|
|
wantErrMsg: `clientid check: invalid clientid "!!!": bad domain name label rune '!'`,
|
2021-01-27 15:32:13 +00:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
r := &http.Request{
|
|
|
|
URL: &url.URL{
|
|
|
|
Path: tc.path,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-06-29 12:53:28 +00:00
|
|
|
pctx := &proxy.DNSContext{
|
|
|
|
Proto: proxy.ProtoHTTPS,
|
|
|
|
HTTPRequest: r,
|
2021-01-27 15:32:13 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 12:53:28 +00:00
|
|
|
clientID, err := clientIDFromDNSContextHTTPS(pctx)
|
|
|
|
assert.Equal(t, tc.wantClientID, clientID)
|
2021-01-27 15:32:13 +00:00
|
|
|
|
2021-10-22 08:58:18 +00:00
|
|
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
2021-01-27 15:32:13 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|