-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhandler_test.go
More file actions
283 lines (249 loc) · 9.25 KB
/
Copy pathhandler_test.go
File metadata and controls
283 lines (249 loc) · 9.25 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package samesame
import (
"context"
"crypto"
"crypto/sha256"
"encoding/base64"
"io"
"maps"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
"time"
)
func TestDirectoryHandler(t *testing.T) {
t.Parallel()
k1, k2 := mustGenerate(t, "ed25519"), mustGenerate(t, "p256")
h, err := NewDirectoryHandler([]crypto.Signer{k1, k2}, DirectoryHandlerOptions{Authorities: []string{"bot.test"}})
if err != nil {
t.Fatalf("NewDirectoryHandler: %v", err)
}
get := httptest.NewRecorder()
h.ServeHTTP(get, httptest.NewRequest(http.MethodGet, "https://bot.test"+WellKnownPath, nil))
body := get.Body.Bytes()
t.Run("GET", func(t *testing.T) {
if get.Code != http.StatusOK {
t.Fatalf("status: want 200, got %d", get.Code)
}
if ct := get.Header().Get("Content-Type"); ct != MediaTypeDirectory {
t.Errorf("content type: got %s", ct)
}
if cc := get.Header().Get("Cache-Control"); cc != "public, max-age=3600" {
t.Errorf("cache control: got %s", cc)
}
sum := sha256.Sum256(body)
if got, want := get.Header().Get("Content-Digest"), "sha-256=:"+base64.StdEncoding.EncodeToString(sum[:])+":"; got != want {
t.Errorf("content digest: want %s, got %s", want, got)
}
if n := len(get.Header().Values("Signature")); n != 2 {
t.Errorf("want one signature per key (2), got %d", n)
}
for _, in := range get.Header().Values("Signature-Input") {
if !strings.Contains(in, `("@authority";req "content-digest")`) || !strings.Contains(in, `tag="http-message-signatures-directory"`) {
t.Errorf("unexpected Signature-Input %s", in)
}
}
if strings.Contains(string(body), `"d"`) {
t.Fatal("directory leaks private key material")
}
dir, err := ParseDirectory(body, DirectoryOptions{})
if err != nil {
t.Fatalf("ParseDirectory: %v", err)
}
if len(dir.Keys) != 2 || len(dir.Invalid) != 0 {
t.Fatalf("want 2 valid keys, got %d (invalid: %v)", len(dir.Keys), dir.Invalid)
}
for i, k := range []crypto.Signer{k1, k2} {
if want := mustThumbprint(t, k.Public()); dir.Keys[i].ID != want {
t.Errorf("key %d: want %s, got %s", i, want, dir.Keys[i].ID)
}
if kid, _ := dir.Keys[i].JWK.KeyID(); kid != dir.Keys[i].ID {
t.Errorf("key %d: kid %q is not the thumbprint", i, kid)
}
}
})
t.Run("HEAD", func(t *testing.T) {
w := httptest.NewRecorder()
h.ServeHTTP(w, httptest.NewRequest(http.MethodHead, "https://bot.test"+WellKnownPath, nil))
if w.Code != http.StatusOK || w.Body.Len() != 0 {
t.Errorf("want 200 with no body, got %d with %d bytes", w.Code, w.Body.Len())
}
})
t.Run("conditional GET", func(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "https://bot.test"+WellKnownPath, nil)
r.Header.Set("If-None-Match", get.Header().Get("ETag"))
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
if w.Code != http.StatusNotModified {
t.Errorf("want 304, got %d", w.Code)
}
})
t.Run("POST", func(t *testing.T) {
w := httptest.NewRecorder()
h.ServeHTTP(w, httptest.NewRequest(http.MethodPost, "https://bot.test"+WellKnownPath, nil))
if w.Code != http.StatusMethodNotAllowed {
t.Errorf("want 405, got %d", w.Code)
}
})
t.Run("signatures bind the request authority", func(t *testing.T) {
res := &http.Response{StatusCode: http.StatusOK, Header: get.Header(), Body: io.NopCloser(strings.NewReader(string(body)))}
dir, _ := ParseDirectory(body, DirectoryOptions{})
f := NewFetcher(FetcherOptions{})
for _, host := range []string{"bot.test", "evil.test"} {
d := &Directory{Keys: dir.Keys}
req := httptest.NewRequest(http.MethodGet, "https://"+host+WellKnownPath, nil)
f.dropUnboundKeys(d, res, req, body)
want := 2
if host == "evil.test" {
want = 0
}
if len(d.Keys) != want {
t.Errorf("%s: want %d bound keys, got %d: %v", host, want, len(d.Keys), d.Invalid)
}
}
})
t.Run("other hosts are refused", func(t *testing.T) {
// Otherwise anyone could obtain signatures binding the bot's
// keys to their own domain.
w := httptest.NewRecorder()
h.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "https://evil.test"+WellKnownPath, nil))
if w.Code != http.StatusMisdirectedRequest {
t.Errorf("want 421, got %d", w.Code)
}
if len(w.Header().Values("Signature")) != 0 {
t.Error("refused request carries signatures")
}
})
t.Run("host case is canonicalized", func(t *testing.T) {
// A fresh handler, so nothing is cached for bot.test yet.
fresh, err := NewDirectoryHandler([]crypto.Signer{k1, k2}, DirectoryHandlerOptions{Authorities: []string{"bot.test"}})
if err != nil {
t.Fatal(err)
}
r := httptest.NewRequest(http.MethodGet, "https://bot.test"+WellKnownPath, nil)
r.Host = "BOT.test"
w := httptest.NewRecorder()
fresh.ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Fatalf("want 200, got %d", w.Code)
}
res := &http.Response{StatusCode: http.StatusOK, Header: w.Header(), Body: io.NopCloser(strings.NewReader(w.Body.String()))}
d, _ := ParseDirectory(w.Body.Bytes(), DirectoryOptions{})
req := httptest.NewRequest(http.MethodGet, "https://bot.test"+WellKnownPath, nil)
NewFetcher(FetcherOptions{}).dropUnboundKeys(d, res, req, w.Body.Bytes())
if len(d.Keys) != 2 {
t.Errorf("signatures are not bound to the canonical host: %v", d.Invalid)
}
})
}
func TestNewDirectoryHandlerErrors(t *testing.T) {
t.Parallel()
key := mustGenerate(t, "ed25519")
for _, tt := range []struct {
name string
keys []crypto.Signer
opts DirectoryHandlerOptions
}{
{name: "no keys", opts: DirectoryHandlerOptions{Authorities: []string{"bot.test"}}},
{name: "P-521 key", keys: []crypto.Signer{mustGenerate(t, "p521")}, opts: DirectoryHandlerOptions{Authorities: []string{"bot.test"}}},
{name: "no authorities", keys: []crypto.Signer{key}},
{name: "empty authority", keys: []crypto.Signer{key}, opts: DirectoryHandlerOptions{Authorities: []string{""}}},
{name: "authority with a path", keys: []crypto.Signer{key}, opts: DirectoryHandlerOptions{Authorities: []string{"bot.test/keys"}}},
} {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if _, err := NewDirectoryHandler(tt.keys, tt.opts); err == nil {
t.Error("want an error, got none")
}
})
}
}
func TestDirectoryHandlerSignatureCache(t *testing.T) {
t.Parallel()
hh, err := NewDirectoryHandler([]crypto.Signer{mustGenerate(t, "ed25519")}, DirectoryHandlerOptions{Authorities: []string{"a.test", "b.test"}})
if err != nil {
t.Fatalf("NewDirectoryHandler: %v", err)
}
h := hh.(*directoryHandler)
now := time.Now()
h.now = func() time.Time { return now }
sig := func(host string) string {
t.Helper()
w := httptest.NewRecorder()
h.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "https://"+host+WellKnownPath, nil))
if w.Code != http.StatusOK {
t.Fatalf("%s: want 200, got %d", host, w.Code)
}
return w.Header().Get("Signature-Input") + " " + w.Header().Get("Signature")
}
first := sig("a.test")
// httpsign stamps created with the wall clock in whole seconds, and
// Ed25519 is deterministic, so without this pause a fresh signature
// would equal a cached one.
time.Sleep(1100 * time.Millisecond)
if again := sig("a.test"); again != first {
t.Error("signatures were not reused within the refresh window")
}
if other := sig("b.test"); other == first {
t.Error("two authorities share signatures")
}
now = now.Add(directorySignatureRefresh + time.Second)
if refreshed := sig("a.test"); refreshed == first {
t.Error("signatures were not refreshed after the refresh window")
}
}
// A statically served signed directory must pass the same checks as the
// handler, and only for the authority it was signed for.
func TestSignStaticDirectory(t *testing.T) {
t.Parallel()
key := mustGenerate(t, "ed25519")
// static serves a precomputed directory like a file server would.
var static atomic.Pointer[StaticDirectory]
serve := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sd := static.Load()
maps.Copy(w.Header(), sd.Header)
_, _ = w.Write(sd.Body)
})
srv := newTestDirectoryServer(t, serve)
other := newTestDirectoryServer(t, serve)
authority := strings.TrimPrefix(srv.srv.URL, "https://")
sd, err := SignStaticDirectory([]crypto.Signer{key}, authority, DirectoryHandlerOptions{})
if err != nil {
t.Fatalf("SignStaticDirectory: %v", err)
}
static.Store(sd)
if want := time.Now().Add(DefaultDirectorySignatureLifetime); sd.Expires.Sub(want).Abs() > time.Minute {
t.Errorf("Expires %s, want about %s", sd.Expires, want)
}
want, err := MarshalDirectory(key.Public())
if err != nil {
t.Fatal(err)
}
if string(sd.Body) != string(want) {
t.Errorf("body differs from MarshalDirectory:\n%s\n%s", sd.Body, want)
}
for _, tt := range []struct {
name string
srv *testDirectoryServer
wantKeys int
}{
{name: "signed authority", srv: srv, wantKeys: 1},
{name: "another authority", srv: other, wantKeys: 0},
} {
t.Run(tt.name, func(t *testing.T) {
f := fetcherFor(t, tt.srv, FetcherOptions{VerifyDirectorySignatures: true}, nil)
dir, err := f.Resolve(context.Background(), tt.srv.identifier(t))
if err != nil {
t.Fatalf("Resolve: %v", err)
}
if len(dir.Keys) != tt.wantKeys {
t.Errorf("want %d keys, got %d: %v", tt.wantKeys, len(dir.Keys), dir.Invalid)
}
})
}
if _, err := SignStaticDirectory([]crypto.Signer{key}, "Bot.Example", DirectoryHandlerOptions{}); err == nil {
t.Error("uppercase authority was accepted")
}
}