-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathhandler_test.go
More file actions
183 lines (154 loc) · 5.46 KB
/
handler_test.go
File metadata and controls
183 lines (154 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package beyond
import (
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/gorilla/websocket"
"github.com/presbrey/beyond/internal/authn"
"github.com/presbrey/beyond/internal/authz"
"github.com/stretchr/testify/assert"
)
func init() {
// Load the allowlist for testing
cwd, _ := os.Getwd()
*authz.AllowlistURL = "file://" + cwd + "/example/allowlist.json"
authz.RefreshAllowlist()
}
func TestHandlerPing(t *testing.T) {
request := httptest.NewRequest("GET", *healthPath, nil)
w := httptest.NewRecorder()
testMux.ServeHTTP(w, request)
resp := w.Result()
body, _ := io.ReadAll(resp.Body)
assert.Equal(t, 200, resp.StatusCode)
assert.Equal(t, *healthReply, string(body))
}
func TestHandlerGo(t *testing.T) {
request := httptest.NewRequest("GET", "/test?a=1", nil)
request.Host = "github.com"
w := httptest.NewRecorder()
testMux.ServeHTTP(w, request)
resp := w.Result()
body, _ := io.ReadAll(resp.Body)
assert.Equal(t, *fouroOneCode, resp.StatusCode)
assert.Equal(t, "", resp.Header.Get("Set-Cookie"))
assert.Equal(t, "\n<script type=\"text/javascript\">\nwindow.location.replace(\"https://"+*host+"/launch?next=https%3A%2F%2Fgithub.com%2Ftest%3Fa%3D1\");\n</script>\n", string(body))
}
func TestHandlerLaunch(t *testing.T) {
request := httptest.NewRequest("GET", "/launch?next=https%3A%2F%2Falachart.colofoo.net%2Ftest%3Fa%3D1", nil)
request.Host = *host
w := httptest.NewRecorder()
testMux.ServeHTTP(w, request)
resp := w.Result()
assert.Equal(t, 200, resp.StatusCode)
assert.NotEqual(t, "", resp.Header.Get("Set-Cookie"))
}
func TestHandlerOidcNoCookie(t *testing.T) {
request := httptest.NewRequest("GET", "/oidc", nil)
request.Host = *host
w := httptest.NewRecorder()
testMux.ServeHTTP(w, request)
resp := w.Result()
// Returns 403 because GetSession creates a new session with empty state
// which doesn't match the query parameter state
assert.Equal(t, 403, resp.StatusCode)
}
func TestHandlerOidcStateInvalid(t *testing.T) {
session := authn.NewSession(*authn.CookieName)
recorder := httptest.NewRecorder()
assert.NoError(t, authn.GetStore().Save(recorder, session))
cookie := strings.Split(recorder.Header().Get("Set-Cookie"), ";")[0]
request := httptest.NewRequest("GET", "/oidc?state=test1", nil)
request.Host = *host
request.Header.Set("Cookie", cookie)
w := httptest.NewRecorder()
testMux.ServeHTTP(w, request)
resp := w.Result()
body, _ := io.ReadAll(resp.Body)
assert.Equal(t, 403, resp.StatusCode)
assert.Contains(t, string(body), "Invalid Browser State")
}
func TestHandlerOidcStateValid(t *testing.T) {
session := authn.NewSession(*authn.CookieName)
session.Values["state"] = "test1"
recorder := httptest.NewRecorder()
assert.NoError(t, authn.GetStore().Save(recorder, session))
cookie := strings.Split(recorder.Header().Get("Set-Cookie"), ";")[0]
request := httptest.NewRequest("GET", "/oidc?state=test1", nil)
request.Host = *host
request.Header.Set("Cookie", cookie)
w := httptest.NewRecorder()
testMux.ServeHTTP(w, request)
resp := w.Result()
body, _ := io.ReadAll(resp.Body)
assert.Equal(t, 401, resp.StatusCode)
assert.Contains(t, string(body), "oauth2:")
}
func TestHandlerWebsocket(t *testing.T) {
t.SkipNow()
server := httptest.NewServer(testMux)
x, y, err := websocket.DefaultDialer.Dial(strings.Replace(server.URL, "http://", "ws://", 1)+"/", http.Header{"Host": []string{"echo.websocket.org"}})
assert.NoError(t, err)
err = x.WriteMessage(websocket.TextMessage, []byte("BEYOND"))
assert.NoError(t, err)
typ, msg, err := x.ReadMessage()
assert.Equal(t, 101, y.StatusCode)
assert.Equal(t, websocket.TextMessage, typ)
assert.Equal(t, "BEYOND", string(msg))
assert.NoError(t, err)
server.Close()
}
func TestHandlerAllowlist(t *testing.T) {
// Test allowed host (1.1.1.1 - Cloudflare DNS)
request := httptest.NewRequest("GET", "/cdn-cgi/trace", nil)
request.Host = "1.1.1.1"
w := httptest.NewRecorder()
testMux.ServeHTTP(w, request)
resp := w.Result()
body, _ := io.ReadAll(resp.Body)
assert.Equal(t, 200, resp.StatusCode)
assert.Equal(t, "", resp.Header.Get("Set-Cookie"))
// Cloudflare trace returns plain text with key=value pairs
assert.Contains(t, string(body), "ip=")
// Test blocked host (github.com)
request = httptest.NewRequest("GET", "/.well-known/acme-challenge/test", nil)
request.Host = "github.com"
w = httptest.NewRecorder()
testMux.ServeHTTP(w, request)
resp = w.Result()
body, _ = io.ReadAll(resp.Body)
assert.Equal(t, 404, resp.StatusCode)
assert.NotEqual(t, "", resp.Header.Get("Set-Cookie"))
assert.Contains(t, string(body), "Page not found")
}
func TestHandlerXHR(t *testing.T) {
request := httptest.NewRequest("GET", "/test?a=1", nil)
request.Host = "github.com"
request.Header.Set("X-Requested-With", "XMLHttpRequest")
w := httptest.NewRecorder()
testMux.ServeHTTP(w, request)
resp := w.Result()
body, _ := io.ReadAll(resp.Body)
assert.Equal(t, *fouroOneCode, resp.StatusCode)
assert.Equal(t, "", resp.Header.Get("Set-Cookie"))
assert.Equal(t, "", string(body))
}
func TestNexthopInvalid(t *testing.T) {
request := httptest.NewRequest("GET", "/favicon.ico", nil)
request.Host = "nonexistent.example.test"
w := httptest.NewRecorder()
testMux.ServeHTTP(w, request)
resp := w.Result()
body, _ := io.ReadAll(resp.Body)
assert.Equal(t, 404, resp.StatusCode)
assert.Equal(t, "", resp.Header.Get("Set-Cookie"))
assert.Contains(t, string(body), *fouroFourMessage)
}
func TestRandhex32(t *testing.T) {
h, err := authn.RandHex32()
assert.Len(t, h, 64)
assert.NoError(t, err)
}