Repository navigation
Expand file tree
/
Copy pathsecurity_test.go
More file actions
209 lines (185 loc) · 7.84 KB
/
Copy pathsecurity_test.go
File metadata and controls
209 lines (185 loc) · 7.84 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package api
import (
"encoding/base64"
"net/http"
"net/http/httptest"
"testing"
)
func TestValidateCredentialsMatchesUsernameAndPassword(t *testing.T) {
previousUsername, previousPassword := Username, Password
Username, Password = "service-user", "service-password"
defer func() { Username, Password = previousUsername, previousPassword }()
tests := []struct {
name string
username string
password string
want bool
}{
{name: "both match", username: "service-user", password: "service-password", want: true},
{name: "wrong password", username: "service-user", password: "service-passwore", want: false},
{name: "wrong username", username: "service-usex", password: "service-password", want: false},
{name: "shorter password", username: "service-user", password: "service", want: false},
{name: "longer username", username: "service-user-x", password: "service-password", want: false},
{name: "empty values", username: "", password: "", want: false},
{name: "swapped values", username: "service-password", password: "service-user", want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := validateCredentials(tt.username, tt.password); got != tt.want {
t.Fatalf("validateCredentials(%q, %q) = %v, want %v", tt.username, tt.password, got, tt.want)
}
})
}
}
func TestBasicTokenAcceptsConfiguredCredentials(t *testing.T) {
previousUsername, previousPassword := Username, Password
Username, Password = "service-user", "service-password"
defer func() { Username, Password = previousUsername, previousPassword }()
tests := []struct {
name string
credential string
wantStatus int
}{
{name: "valid", credential: "service-user:service-password", wantStatus: http.StatusNoContent},
{name: "invalid password", credential: "service-user:other", wantStatus: http.StatusUnauthorized},
{name: "no separator", credential: "service-user", wantStatus: http.StatusUnauthorized},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
handler := BasicToken(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) })
req := httptest.NewRequest(http.MethodGet, "/private", nil)
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(tt.credential)))
res := httptest.NewRecorder()
handler(res, req)
if res.Code != tt.wantStatus {
t.Fatalf("expected status %d, got %d", tt.wantStatus, res.Code)
}
})
}
}
// Until Username and Password are both set to values other than "" and
// "default", the default validator rejects every credential pair.
func TestValidateCredentialsRejectsAllWhileNotConfigured(t *testing.T) {
configurations := []struct {
name string
username string
password string
}{
{name: "defaults", username: "default", password: "default"},
{name: "empty", username: "", password: ""},
{name: "default password", username: "service-user", password: "default"},
{name: "default username", username: "default", password: "service-password"},
{name: "empty password", username: "service-user", password: ""},
{name: "empty username", username: "", password: "service-password"},
}
for _, configuration := range configurations {
t.Run(configuration.name, func(t *testing.T) {
previousUsername, previousPassword := Username, Password
Username, Password = configuration.username, configuration.password
defer func() { Username, Password = previousUsername, previousPassword }()
credentials := [][2]string{
{configuration.username, configuration.password},
{"default", "default"},
{"", ""},
}
for _, credential := range credentials {
if validateCredentials(credential[0], credential[1]) {
t.Fatalf("validateCredentials(%q, %q) = true with Username %q and Password %q, want false", credential[0], credential[1], Username, Password)
}
}
})
}
}
func TestBasicTokenRejectsDefaultCredentialsWhileNotConfigured(t *testing.T) {
previousUsername, previousPassword := Username, Password
Username, Password = "default", "default"
defer func() { Username, Password = previousUsername, previousPassword }()
nextCalled := false
handler := BasicToken(func(w http.ResponseWriter, r *http.Request) {
nextCalled = true
w.WriteHeader(http.StatusNoContent)
})
req := httptest.NewRequest(http.MethodGet, "/private", nil)
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("default:default")))
res := httptest.NewRecorder()
handler(res, req)
if res.Code != http.StatusUnauthorized {
t.Fatalf("expected status %d, got %d", http.StatusUnauthorized, res.Code)
}
if nextCalled {
t.Fatal("expected next handler not to run")
}
}
// A replacement validator decides on its own; the configuration rule belongs
// to the default validator only.
func TestBasicTokenUsesCustomValidatorWhileNotConfigured(t *testing.T) {
previousUsername, previousPassword, previousValidator := Username, Password, ValidateBasicAuthCredentialsFunc
Username, Password = "default", "default"
ValidateBasicAuthCredentialsFunc = func(username, password string) bool {
return username == "custom-user" && password == "custom-password"
}
defer func() {
Username, Password, ValidateBasicAuthCredentialsFunc = previousUsername, previousPassword, previousValidator
}()
handler := BasicToken(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) })
req := httptest.NewRequest(http.MethodGet, "/private", nil)
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("custom-user:custom-password")))
res := httptest.NewRecorder()
handler(res, req)
if res.Code != http.StatusNoContent {
t.Fatalf("expected status %d, got %d", http.StatusNoContent, res.Code)
}
}
// serveBasicToken answers an authenticated request through BasicToken on
// the default instance and on an instance, and returns the Basic-Client and
// Basic-Secret request headers the handler saw on each.
func serveBasicToken(t *testing.T, requestHeaders map[string]string) map[string][2]string {
t.Helper()
previousUsername, previousPassword := Username, Password
Username, Password = "service-user", "service-password"
defer func() { Username, Password = previousUsername, previousPassword }()
instances := map[string]func(http.Handler) http.Handler{
"default instance": func(next http.Handler) http.Handler { return next },
"instance": New().Handler,
}
seen := map[string][2]string{}
for name, wrap := range instances {
handler := BasicToken(func(w http.ResponseWriter, r *http.Request) {
seen[name] = [2]string{r.Header.Get("Basic-Client"), r.Header.Get("Basic-Secret")}
w.WriteHeader(http.StatusNoContent)
})
req := httptest.NewRequest(http.MethodGet, "/private", nil)
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("service-user:service-password")))
for key, value := range requestHeaders {
req.Header.Set(key, value)
}
res := httptest.NewRecorder()
wrap(handler).ServeHTTP(res, req)
if res.Code != http.StatusNoContent {
t.Fatalf("%s: status = %d, want %d", name, res.Code, http.StatusNoContent)
}
}
return seen
}
func TestBasicTokenDoesNotCopyThePasswordIntoBasicSecret(t *testing.T) {
for name, headers := range serveBasicToken(t, nil) {
if headers[1] != "" {
t.Errorf("%s: Basic-Secret = %q, want no header", name, headers[1])
}
}
}
// v0 overwrote a client-sent Basic-Secret, so handlers may still trust it.
func TestBasicTokenRemovesAClientSentBasicSecret(t *testing.T) {
for name, headers := range serveBasicToken(t, map[string]string{"Basic-Secret": "forged"}) {
if headers[1] != "" {
t.Errorf("%s: Basic-Secret = %q, want no header", name, headers[1])
}
}
}
func TestBasicTokenSetsBasicClient(t *testing.T) {
for name, headers := range serveBasicToken(t, nil) {
if headers[0] != "service-user" {
t.Errorf("%s: Basic-Client = %q, want %q", name, headers[0], "service-user")
}
}
}