2018-05-01 23:45:06 +00:00
|
|
|
package validation
|
|
|
|
|
|
|
|
import (
|
2018-12-07 17:35:05 +00:00
|
|
|
"bytes"
|
2018-05-01 23:45:06 +00:00
|
|
|
"fmt"
|
2018-12-07 17:35:05 +00:00
|
|
|
"io/ioutil"
|
2018-05-01 23:45:06 +00:00
|
|
|
"testing"
|
2019-02-27 22:47:00 +00:00
|
|
|
"time"
|
2018-05-01 23:45:06 +00:00
|
|
|
|
2018-10-08 19:20:28 +00:00
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
2018-10-29 16:57:58 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-05-01 23:45:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestValidateHostname(t *testing.T) {
|
|
|
|
var inputHostname string
|
|
|
|
hostname, err := ValidateHostname(inputHostname)
|
2018-10-08 19:20:28 +00:00
|
|
|
assert.Equal(t, err, nil)
|
2018-05-01 23:45:06 +00:00
|
|
|
assert.Empty(t, hostname)
|
|
|
|
|
|
|
|
inputHostname = "hello.example.com"
|
|
|
|
hostname, err = ValidateHostname(inputHostname)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "hello.example.com", hostname)
|
|
|
|
|
|
|
|
inputHostname = "http://hello.example.com"
|
|
|
|
hostname, err = ValidateHostname(inputHostname)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "hello.example.com", hostname)
|
|
|
|
|
|
|
|
inputHostname = "bücher.example.com"
|
|
|
|
hostname, err = ValidateHostname(inputHostname)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "xn--bcher-kva.example.com", hostname)
|
|
|
|
|
|
|
|
inputHostname = "http://bücher.example.com"
|
|
|
|
hostname, err = ValidateHostname(inputHostname)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "xn--bcher-kva.example.com", hostname)
|
|
|
|
|
|
|
|
inputHostname = "http%3A%2F%2Fhello.example.com"
|
|
|
|
hostname, err = ValidateHostname(inputHostname)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "hello.example.com", hostname)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateUrl(t *testing.T) {
|
|
|
|
validUrl, err := ValidateUrl("")
|
2018-10-08 19:20:28 +00:00
|
|
|
assert.Equal(t, fmt.Errorf("URL should not be empty"), err)
|
2018-05-01 23:45:06 +00:00
|
|
|
assert.Empty(t, validUrl)
|
|
|
|
|
|
|
|
validUrl, err = ValidateUrl("https://localhost:8080")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "https://localhost:8080", validUrl)
|
|
|
|
|
|
|
|
validUrl, err = ValidateUrl("localhost:8080")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "http://localhost:8080", validUrl)
|
|
|
|
|
|
|
|
validUrl, err = ValidateUrl("http://localhost")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "http://localhost", validUrl)
|
|
|
|
|
|
|
|
validUrl, err = ValidateUrl("http://127.0.0.1:8080")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "http://127.0.0.1:8080", validUrl)
|
|
|
|
|
|
|
|
validUrl, err = ValidateUrl("127.0.0.1:8080")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "http://127.0.0.1:8080", validUrl)
|
|
|
|
|
|
|
|
validUrl, err = ValidateUrl("127.0.0.1")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "http://127.0.0.1", validUrl)
|
|
|
|
|
|
|
|
validUrl, err = ValidateUrl("https://127.0.0.1:8080")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "https://127.0.0.1:8080", validUrl)
|
|
|
|
|
|
|
|
validUrl, err = ValidateUrl("[::1]:8080")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "http://[::1]:8080", validUrl)
|
|
|
|
|
|
|
|
validUrl, err = ValidateUrl("http://[::1]")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "http://[::1]", validUrl)
|
|
|
|
|
|
|
|
validUrl, err = ValidateUrl("http://[::1]:8080")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "http://[::1]:8080", validUrl)
|
|
|
|
|
|
|
|
validUrl, err = ValidateUrl("[::1]")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "http://[::1]", validUrl)
|
|
|
|
|
|
|
|
validUrl, err = ValidateUrl("https://example.com")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "https://example.com", validUrl)
|
|
|
|
|
|
|
|
validUrl, err = ValidateUrl("example.com")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "http://example.com", validUrl)
|
|
|
|
|
|
|
|
validUrl, err = ValidateUrl("http://hello.example.com")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "http://hello.example.com", validUrl)
|
|
|
|
|
|
|
|
validUrl, err = ValidateUrl("hello.example.com")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "http://hello.example.com", validUrl)
|
|
|
|
|
|
|
|
validUrl, err = ValidateUrl("hello.example.com:8080")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "http://hello.example.com:8080", validUrl)
|
|
|
|
|
|
|
|
validUrl, err = ValidateUrl("https://hello.example.com:8080")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "https://hello.example.com:8080", validUrl)
|
|
|
|
|
|
|
|
validUrl, err = ValidateUrl("https://bücher.example.com")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "https://xn--bcher-kva.example.com", validUrl)
|
|
|
|
|
|
|
|
validUrl, err = ValidateUrl("bücher.example.com")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "http://xn--bcher-kva.example.com", validUrl)
|
|
|
|
|
|
|
|
validUrl, err = ValidateUrl("https%3A%2F%2Fhello.example.com")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "https://hello.example.com", validUrl)
|
|
|
|
|
|
|
|
validUrl, err = ValidateUrl("ftp://alex:12345@hello.example.com:8080/robot.txt")
|
|
|
|
assert.Equal(t, "Currently Argo Tunnel does not support ftp protocol.", err.Error())
|
|
|
|
assert.Empty(t, validUrl)
|
|
|
|
|
|
|
|
validUrl, err = ValidateUrl("https://alex:12345@hello.example.com:8080")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "https://hello.example.com:8080", validUrl)
|
|
|
|
|
|
|
|
}
|
2018-10-08 19:20:28 +00:00
|
|
|
|
|
|
|
func TestToggleProtocol(t *testing.T) {
|
|
|
|
assert.Equal(t, "https", toggleProtocol("http"))
|
|
|
|
assert.Equal(t, "http", toggleProtocol("https"))
|
|
|
|
assert.Equal(t, "random", toggleProtocol("random"))
|
|
|
|
assert.Equal(t, "", toggleProtocol(""))
|
|
|
|
}
|
|
|
|
|
2018-12-07 17:35:05 +00:00
|
|
|
// Happy path 1: originURL is HTTP, and HTTP connections work
|
2018-10-08 19:20:28 +00:00
|
|
|
func TestValidateHTTPService_HTTP2HTTP(t *testing.T) {
|
2018-10-29 16:57:58 +00:00
|
|
|
originURL := "http://127.0.0.1/"
|
|
|
|
hostname := "example.com"
|
2018-12-07 17:35:05 +00:00
|
|
|
|
|
|
|
assert.Nil(t, ValidateHTTPService(originURL, hostname, testRoundTripper(func(req *http.Request) (*http.Response, error) {
|
|
|
|
assert.Equal(t, req.Host, hostname)
|
|
|
|
if req.URL.Scheme == "http" {
|
|
|
|
return emptyResponse(200), nil
|
|
|
|
}
|
|
|
|
if req.URL.Scheme == "https" {
|
|
|
|
t.Fatal("http works, shouldn't have tried with https")
|
|
|
|
}
|
|
|
|
panic("Shouldn't reach here")
|
|
|
|
})))
|
|
|
|
|
|
|
|
assert.Nil(t, ValidateHTTPService(originURL, hostname, testRoundTripper(func(req *http.Request) (*http.Response, error) {
|
|
|
|
assert.Equal(t, req.Host, hostname)
|
|
|
|
if req.URL.Scheme == "http" {
|
|
|
|
return emptyResponse(503), nil
|
|
|
|
}
|
|
|
|
if req.URL.Scheme == "https" {
|
|
|
|
t.Fatal("http works, shouldn't have tried with https")
|
|
|
|
}
|
|
|
|
panic("Shouldn't reach here")
|
|
|
|
})))
|
2018-10-08 19:20:28 +00:00
|
|
|
}
|
|
|
|
|
2018-12-07 17:35:05 +00:00
|
|
|
// Happy path 2: originURL is HTTPS, and HTTPS connections work
|
|
|
|
func TestValidateHTTPService_HTTPS2HTTPS(t *testing.T) {
|
|
|
|
originURL := "https://127.0.0.1/"
|
2018-10-29 16:57:58 +00:00
|
|
|
hostname := "example.com"
|
2018-12-07 17:35:05 +00:00
|
|
|
|
|
|
|
assert.Nil(t, ValidateHTTPService(originURL, hostname, testRoundTripper(func(req *http.Request) (*http.Response, error) {
|
|
|
|
assert.Equal(t, req.Host, hostname)
|
|
|
|
if req.URL.Scheme == "http" {
|
|
|
|
t.Fatal("https works, shouldn't have tried with http")
|
|
|
|
}
|
|
|
|
if req.URL.Scheme == "https" {
|
|
|
|
return emptyResponse(200), nil
|
|
|
|
}
|
|
|
|
panic("Shouldn't reach here")
|
|
|
|
})))
|
|
|
|
|
|
|
|
assert.Nil(t, ValidateHTTPService(originURL, hostname, testRoundTripper(func(req *http.Request) (*http.Response, error) {
|
|
|
|
assert.Equal(t, req.Host, hostname)
|
|
|
|
if req.URL.Scheme == "http" {
|
|
|
|
t.Fatal("https works, shouldn't have tried with http")
|
|
|
|
}
|
|
|
|
if req.URL.Scheme == "https" {
|
|
|
|
return emptyResponse(503), nil
|
|
|
|
}
|
|
|
|
panic("Shouldn't reach here")
|
|
|
|
})))
|
2018-10-08 19:20:28 +00:00
|
|
|
}
|
|
|
|
|
2018-12-07 17:35:05 +00:00
|
|
|
// Error path 1: originURL is HTTPS, but HTTP connections work
|
2018-10-08 19:20:28 +00:00
|
|
|
func TestValidateHTTPService_HTTPS2HTTP(t *testing.T) {
|
2018-10-29 16:57:58 +00:00
|
|
|
originURL := "https://127.0.0.1:1234/"
|
|
|
|
hostname := "example.com"
|
2018-12-07 17:35:05 +00:00
|
|
|
|
|
|
|
assert.Error(t, ValidateHTTPService(originURL, hostname, testRoundTripper(func(req *http.Request) (*http.Response, error) {
|
|
|
|
assert.Equal(t, req.Host, hostname)
|
|
|
|
if req.URL.Scheme == "http" {
|
|
|
|
return emptyResponse(200), nil
|
|
|
|
}
|
|
|
|
if req.URL.Scheme == "https" {
|
|
|
|
return nil, assert.AnError
|
|
|
|
}
|
|
|
|
panic("Shouldn't reach here")
|
|
|
|
})))
|
|
|
|
|
|
|
|
assert.Error(t, ValidateHTTPService(originURL, hostname, testRoundTripper(func(req *http.Request) (*http.Response, error) {
|
|
|
|
assert.Equal(t, req.Host, hostname)
|
|
|
|
if req.URL.Scheme == "http" {
|
|
|
|
return emptyResponse(503), nil
|
|
|
|
}
|
|
|
|
if req.URL.Scheme == "https" {
|
|
|
|
return nil, assert.AnError
|
|
|
|
}
|
|
|
|
panic("Shouldn't reach here")
|
|
|
|
})))
|
2018-10-08 19:20:28 +00:00
|
|
|
}
|
|
|
|
|
2018-12-07 17:35:05 +00:00
|
|
|
// Error path 2: originURL is HTTP, but HTTPS connections work
|
2018-10-08 19:20:28 +00:00
|
|
|
func TestValidateHTTPService_HTTP2HTTPS(t *testing.T) {
|
2018-10-29 16:57:58 +00:00
|
|
|
originURL := "http://127.0.0.1:1234/"
|
|
|
|
hostname := "example.com"
|
2018-12-07 17:35:05 +00:00
|
|
|
|
|
|
|
assert.Error(t, ValidateHTTPService(originURL, hostname, testRoundTripper(func(req *http.Request) (*http.Response, error) {
|
|
|
|
assert.Equal(t, req.Host, hostname)
|
|
|
|
if req.URL.Scheme == "http" {
|
|
|
|
return nil, assert.AnError
|
|
|
|
}
|
|
|
|
if req.URL.Scheme == "https" {
|
|
|
|
return emptyResponse(200), nil
|
|
|
|
}
|
|
|
|
panic("Shouldn't reach here")
|
|
|
|
})))
|
|
|
|
|
|
|
|
assert.Error(t, ValidateHTTPService(originURL, hostname, testRoundTripper(func(req *http.Request) (*http.Response, error) {
|
|
|
|
assert.Equal(t, req.Host, hostname)
|
|
|
|
if req.URL.Scheme == "http" {
|
|
|
|
return nil, assert.AnError
|
|
|
|
}
|
|
|
|
if req.URL.Scheme == "https" {
|
|
|
|
return emptyResponse(503), nil
|
|
|
|
}
|
|
|
|
panic("Shouldn't reach here")
|
|
|
|
})))
|
2019-07-11 18:34:31 +00:00
|
|
|
}
|
2018-12-07 17:35:05 +00:00
|
|
|
|
2019-07-11 18:34:31 +00:00
|
|
|
// Ensure the client does not follow 302 responses
|
|
|
|
func TestValidateHTTPService_NoFollowRedirects(t *testing.T) {
|
|
|
|
hostname := "example.com"
|
2018-12-07 17:35:05 +00:00
|
|
|
redirectServer, redirectClient, err := createSecureMockServerAndClient(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.Path == "/followedRedirect" {
|
|
|
|
t.Fatal("shouldn't have followed the 302")
|
|
|
|
}
|
|
|
|
if r.Method == "CONNECT" {
|
|
|
|
assert.Equal(t, "127.0.0.1:443", r.Host)
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, hostname, r.Host)
|
|
|
|
}
|
|
|
|
w.Header().Set("Location", "/followedRedirect")
|
|
|
|
w.WriteHeader(302)
|
|
|
|
}))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer redirectServer.Close()
|
2019-07-11 18:34:31 +00:00
|
|
|
assert.NoError(t, ValidateHTTPService(redirectServer.URL, hostname, redirectClient.Transport))
|
2018-12-07 17:35:05 +00:00
|
|
|
}
|
|
|
|
|
2019-07-11 18:34:31 +00:00
|
|
|
// Ensure validation times out when origin URL is nonresponsive
|
2019-02-27 22:47:00 +00:00
|
|
|
func TestValidateHTTPService_NonResponsiveOrigin(t *testing.T) {
|
2019-07-11 18:34:31 +00:00
|
|
|
originURL := "http://127.0.0.1/"
|
2019-02-27 22:47:00 +00:00
|
|
|
hostname := "example.com"
|
|
|
|
oldValidationTimeout := validationTimeout
|
|
|
|
defer func() {
|
|
|
|
validationTimeout = oldValidationTimeout
|
|
|
|
}()
|
|
|
|
validationTimeout = 500 * time.Millisecond
|
|
|
|
|
2019-07-11 18:34:31 +00:00
|
|
|
// Use createMockServerAndClient, not createSecureMockServerAndClient.
|
|
|
|
// The latter will bail with HTTP 400 immediately on an http:// request,
|
|
|
|
// which defeats the purpose of a 'nonresponsive origin' test.
|
|
|
|
server, client, err := createMockServerAndClient(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2019-02-27 22:47:00 +00:00
|
|
|
if r.Method == "CONNECT" {
|
|
|
|
assert.Equal(t, "127.0.0.1:443", r.Host)
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, hostname, r.Host)
|
|
|
|
}
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
w.WriteHeader(200)
|
|
|
|
}))
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
err = ValidateHTTPService(originURL, hostname, client.Transport)
|
2019-07-11 18:34:31 +00:00
|
|
|
fmt.Println(err)
|
2019-02-27 22:47:00 +00:00
|
|
|
if err, ok := err.(net.Error); assert.True(t, ok) {
|
|
|
|
assert.True(t, err.Timeout())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-20 18:13:29 +00:00
|
|
|
func TestNewAccessValidatorOk(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
url := "test.cloudflareaccess.com"
|
|
|
|
access, err := NewAccessValidator(ctx, url, url, "")
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, access)
|
|
|
|
|
|
|
|
assert.Error(t, access.Validate(ctx, ""))
|
|
|
|
assert.Error(t, access.Validate(ctx, "invalid"))
|
|
|
|
|
|
|
|
req := httptest.NewRequest("GET", "https://test.cloudflareaccess.com", nil)
|
|
|
|
req.Header.Set(accessJwtHeader, "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c")
|
|
|
|
assert.Error(t, access.ValidateRequest(ctx, req))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewAccessValidatorErr(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
urls := []string{
|
|
|
|
"",
|
|
|
|
"tcp://test.cloudflareaccess.com",
|
|
|
|
"wss://cloudflarenone.com",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, url := range urls {
|
|
|
|
access, err := NewAccessValidator(ctx, url, url, "")
|
|
|
|
|
|
|
|
assert.Error(t, err, url)
|
|
|
|
assert.Nil(t, access)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-07 17:35:05 +00:00
|
|
|
type testRoundTripper func(req *http.Request) (*http.Response, error)
|
|
|
|
|
|
|
|
func (f testRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
|
|
return f(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
func emptyResponse(statusCode int) *http.Response {
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: statusCode,
|
|
|
|
Body: ioutil.NopCloser(bytes.NewReader(nil)),
|
|
|
|
Header: make(http.Header),
|
|
|
|
}
|
2018-10-08 19:20:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func createMockServerAndClient(handler http.Handler) (*httptest.Server, *http.Client, error) {
|
|
|
|
client := http.DefaultClient
|
|
|
|
server := httptest.NewServer(handler)
|
|
|
|
|
|
|
|
client.Transport = &http.Transport{
|
|
|
|
Proxy: func(req *http.Request) (*url.URL, error) {
|
|
|
|
return url.Parse(server.URL)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return server, client, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createSecureMockServerAndClient(handler http.Handler) (*httptest.Server, *http.Client, error) {
|
|
|
|
client := http.DefaultClient
|
|
|
|
server := httptest.NewTLSServer(handler)
|
|
|
|
|
|
|
|
cert, err := x509.ParseCertificate(server.TLS.Certificates[0].Certificate[0])
|
|
|
|
if err != nil {
|
|
|
|
server.Close()
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
certpool := x509.NewCertPool()
|
|
|
|
certpool.AddCert(cert)
|
|
|
|
|
|
|
|
client.Transport = &http.Transport{
|
|
|
|
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
|
|
return net.Dial("tcp", server.URL[strings.LastIndex(server.URL, "/")+1:])
|
|
|
|
},
|
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
RootCAs: certpool,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return server, client, nil
|
|
|
|
}
|