2019-03-06 18:08:40 +00:00
|
|
|
package keyfetch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2019-03-06 21:59:25 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2019-03-06 18:08:40 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestInvalidIssuer(t *testing.T) {
|
|
|
|
_, err := NewWhitelist([]string{"somethingorother"})
|
|
|
|
if nil == err {
|
|
|
|
t.Log("invalid http urls can get through, but that's okay")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = NewWhitelist([]string{"//example.com/foo"})
|
|
|
|
if nil == err {
|
|
|
|
t.Fatal(errors.New("semi-bad url got through"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIssuerMatches(t *testing.T) {
|
2019-04-17 23:08:53 +00:00
|
|
|
// because [""] = strings.Split(os.Getenv("DOESNTEXIST"), ",")
|
2019-03-06 18:08:40 +00:00
|
|
|
trusted := []string{
|
2019-04-17 23:08:53 +00:00
|
|
|
"",
|
2019-03-06 18:08:40 +00:00
|
|
|
"https://example.com/",
|
|
|
|
"foobar.net/def/",
|
|
|
|
"https://*.wild.org",
|
|
|
|
"https://*.west.mali/verde",
|
|
|
|
}
|
2019-04-15 17:09:34 +00:00
|
|
|
privates := []string{
|
|
|
|
"http://happy.xyz/abc",
|
|
|
|
}
|
2019-03-06 18:08:40 +00:00
|
|
|
|
2019-04-15 17:09:34 +00:00
|
|
|
_, err := NewWhitelist(append(trusted, privates...))
|
2019-03-06 18:08:40 +00:00
|
|
|
if nil == err {
|
|
|
|
t.Fatal(errors.New("An insecure domain got through!"))
|
|
|
|
}
|
|
|
|
|
2019-04-15 17:09:34 +00:00
|
|
|
// Empty list is allowed... I guess?
|
|
|
|
list, err := NewWhitelist(nil)
|
|
|
|
if nil != err {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// Combo list
|
2019-04-17 23:08:53 +00:00
|
|
|
list, err = NewWhitelist(trusted[1:], privates)
|
2019-03-06 18:08:40 +00:00
|
|
|
if nil != err {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var iss string
|
|
|
|
iss = "https://example.com"
|
2019-03-25 23:48:39 +00:00
|
|
|
if !list.IsTrustedIssuer(iss) {
|
2019-03-06 18:08:40 +00:00
|
|
|
t.Fatal("A good domain didn't make it:", iss)
|
|
|
|
}
|
|
|
|
|
|
|
|
iss = "https://example.com/"
|
2019-03-25 23:48:39 +00:00
|
|
|
if !list.IsTrustedIssuer(iss) {
|
2019-03-06 18:08:40 +00:00
|
|
|
t.Fatal("A good domain didn't make it:", iss)
|
|
|
|
}
|
|
|
|
|
|
|
|
iss = "http://example.com"
|
2019-03-25 23:48:39 +00:00
|
|
|
if list.IsTrustedIssuer(iss) {
|
2019-03-06 18:08:40 +00:00
|
|
|
t.Fatal("A bad URL slipped past", iss)
|
|
|
|
}
|
|
|
|
|
|
|
|
iss = "https://example.com/foo"
|
2019-03-25 23:48:39 +00:00
|
|
|
if list.IsTrustedIssuer(iss) {
|
2019-03-06 18:08:40 +00:00
|
|
|
t.Fatal("A bad URL slipped past", iss)
|
|
|
|
}
|
|
|
|
|
|
|
|
iss = "http://happy.xyz/abc"
|
2019-03-25 23:48:39 +00:00
|
|
|
if !list.IsTrustedIssuer(iss) {
|
2019-03-06 18:08:40 +00:00
|
|
|
t.Fatal("A good URL didn't make it:", iss)
|
|
|
|
}
|
|
|
|
|
|
|
|
iss = "http://happy.xyz/abc/"
|
2019-03-25 23:48:39 +00:00
|
|
|
if !list.IsTrustedIssuer(iss) {
|
2019-03-06 18:08:40 +00:00
|
|
|
t.Fatal("A good URL didn't make it:", iss)
|
|
|
|
}
|
|
|
|
|
|
|
|
iss = "http://happy.xyz/abc/d"
|
2019-03-25 23:48:39 +00:00
|
|
|
if list.IsTrustedIssuer(iss) {
|
2019-03-06 18:08:40 +00:00
|
|
|
t.Fatal("A bad URL slipped past", iss)
|
|
|
|
}
|
|
|
|
|
|
|
|
iss = "http://happy.xyz/abcd"
|
2019-03-25 23:48:39 +00:00
|
|
|
if list.IsTrustedIssuer(iss) {
|
2019-03-06 18:08:40 +00:00
|
|
|
t.Fatal("A bad URL slipped past", iss)
|
|
|
|
}
|
|
|
|
|
|
|
|
iss = "https://foobar.net/def"
|
2019-03-25 23:48:39 +00:00
|
|
|
if !list.IsTrustedIssuer(iss) {
|
2019-03-06 18:08:40 +00:00
|
|
|
t.Fatal("A good URL didn't make it:", iss)
|
|
|
|
}
|
|
|
|
|
|
|
|
iss = "https://foobar.net/def/"
|
2019-03-25 23:48:39 +00:00
|
|
|
if !list.IsTrustedIssuer(iss) {
|
2019-03-06 18:08:40 +00:00
|
|
|
t.Fatal("A good URL didn't make it:", iss)
|
|
|
|
}
|
|
|
|
|
|
|
|
iss = "http://foobar.net/def/"
|
2019-03-25 23:48:39 +00:00
|
|
|
if list.IsTrustedIssuer(iss) {
|
2019-03-06 18:08:40 +00:00
|
|
|
t.Fatal("A bad URL slipped past", iss)
|
|
|
|
}
|
|
|
|
|
|
|
|
iss = "https://foobar.net/def/e"
|
2019-03-25 23:48:39 +00:00
|
|
|
if list.IsTrustedIssuer(iss) {
|
2019-03-06 18:08:40 +00:00
|
|
|
t.Fatal("A bad URL slipped past", iss)
|
|
|
|
}
|
|
|
|
|
|
|
|
iss = "https://foobar.net/defe"
|
2019-03-25 23:48:39 +00:00
|
|
|
if list.IsTrustedIssuer(iss) {
|
2019-03-06 18:08:40 +00:00
|
|
|
t.Fatal("A bad URL slipped past", iss)
|
|
|
|
}
|
|
|
|
|
|
|
|
iss = "https://wild.org"
|
2019-03-25 23:48:39 +00:00
|
|
|
if list.IsTrustedIssuer(iss) {
|
2019-03-06 18:08:40 +00:00
|
|
|
t.Fatal("A bad URL slipped past", iss)
|
|
|
|
}
|
|
|
|
|
|
|
|
iss = "https://foo.wild.org"
|
2019-03-25 23:48:39 +00:00
|
|
|
if !list.IsTrustedIssuer(iss) {
|
2019-03-06 18:08:40 +00:00
|
|
|
t.Fatal("A good URL didn't make it:", iss)
|
|
|
|
}
|
|
|
|
|
|
|
|
iss = "https://sub.foo.wild.org"
|
2019-03-25 23:48:39 +00:00
|
|
|
if !list.IsTrustedIssuer(iss) {
|
2019-03-06 18:08:40 +00:00
|
|
|
t.Fatal("A good URL didn't make it:", iss)
|
|
|
|
}
|
|
|
|
|
|
|
|
iss = "https://foo.wild.org/cherries"
|
2019-03-25 23:48:39 +00:00
|
|
|
if list.IsTrustedIssuer(iss) {
|
2019-03-06 18:08:40 +00:00
|
|
|
t.Fatal("A bad URL slipped past", iss)
|
|
|
|
}
|
|
|
|
|
|
|
|
iss = "https://sub.west.mali/verde/"
|
2019-03-25 23:48:39 +00:00
|
|
|
if !list.IsTrustedIssuer(iss) {
|
2019-03-06 18:08:40 +00:00
|
|
|
t.Fatal("A good URL didn't make it:", iss)
|
|
|
|
}
|
|
|
|
|
|
|
|
iss = "https://sub.west.mali"
|
2019-03-25 23:48:39 +00:00
|
|
|
if list.IsTrustedIssuer(iss) {
|
2019-03-06 18:08:40 +00:00
|
|
|
t.Fatal("A bad URL slipped past", iss)
|
|
|
|
}
|
|
|
|
}
|
2019-03-06 21:59:25 +00:00
|
|
|
|
|
|
|
func TestImplicitIssuer(t *testing.T) {
|
|
|
|
var r *http.Request
|
|
|
|
var iss string
|
|
|
|
|
|
|
|
r = &http.Request{
|
|
|
|
Host: "example.com",
|
|
|
|
URL: &url.URL{Path: "/foo/bar/baz"},
|
|
|
|
Header: http.Header(map[string][]string{
|
|
|
|
"x-forwarded-host": []string{"example.com"},
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
iss = "https://example.com/foo"
|
2019-03-25 23:48:39 +00:00
|
|
|
if !isTrustedIssuer(iss, nil, r) {
|
2019-03-06 21:59:25 +00:00
|
|
|
t.Fatal("A good URL didn't make it:", iss)
|
|
|
|
}
|
|
|
|
|
|
|
|
r = &http.Request{
|
|
|
|
Host: "example.com",
|
|
|
|
URL: &url.URL{Path: "/"},
|
|
|
|
Header: http.Header(map[string][]string{
|
|
|
|
"x-forwarded-host": []string{"example.com"},
|
|
|
|
"x-forwarded-proto": []string{"http"},
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
iss = "http://example.com/foo"
|
2019-03-25 23:48:39 +00:00
|
|
|
if isTrustedIssuer(iss, nil, r) {
|
2019-03-06 21:59:25 +00:00
|
|
|
t.Fatal("A bad URL slipped past:", iss)
|
|
|
|
}
|
|
|
|
|
|
|
|
r = &http.Request{
|
|
|
|
Host: "example.com",
|
|
|
|
URL: &url.URL{Path: "/foo"},
|
|
|
|
Header: http.Header(map[string][]string{
|
|
|
|
"x-forwarded-host": []string{"example.com"},
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
iss = "https://example.com/foo/bar/baz"
|
2019-03-25 23:48:39 +00:00
|
|
|
if isTrustedIssuer(iss, nil, r) {
|
2019-03-06 21:59:25 +00:00
|
|
|
t.Fatal("A bad URL slipped past:", iss)
|
|
|
|
}
|
|
|
|
|
|
|
|
r = &http.Request{
|
|
|
|
Host: "example.com",
|
|
|
|
URL: &url.URL{Path: "/"},
|
|
|
|
Header: http.Header(map[string][]string{
|
|
|
|
"x-forwarded-proto": []string{"https"},
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
iss = "https://example.com/"
|
2019-03-25 23:48:39 +00:00
|
|
|
if !isTrustedIssuer(iss, nil, r) {
|
2019-03-06 21:59:25 +00:00
|
|
|
t.Fatal("A good URL didn't make it:", iss)
|
|
|
|
}
|
|
|
|
}
|