-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy_test.go
More file actions
221 lines (198 loc) · 7.17 KB
/
Copy pathproxy_test.go
File metadata and controls
221 lines (198 loc) · 7.17 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
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
"time"
)
func TestHandleRe(t *testing.T) {
valid := []string{"bsky.app", "alice.bsky.social", "x.co", "a1-b2.example.com"}
invalid := []string{"", ".", "-leading.example", "trailing.example-", "spaces in.it", "a/b.c", strings.Repeat("a", 260) + ".com"}
for _, h := range valid {
if !handleRe.MatchString(h) {
t.Errorf("handleRe rejected %q", h)
}
}
for _, h := range invalid {
if handleRe.MatchString(h) {
t.Errorf("handleRe accepted %q", h)
}
}
}
func TestPostURIRe(t *testing.T) {
valid := []string{
"at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post/3l6oveex3ii2l",
"at://alice.bsky.social/app.bsky.feed.post/3k",
"at://did:web:example.com/app.bsky.feed.post/abc.def~ghi",
}
invalid := []string{
"",
"https://bsky.app/profile/x/post/y",
"at://did:plc:x/app.bsky.feed.like/3k",
"at://did:plc:x/app.bsky.feed.post/",
"at://did:plc:x/app.bsky.feed.post/3k/extra",
"at://did:plc:x/app.bsky.feed.post/3k?x=1",
}
for _, u := range valid {
if !postURIRe.MatchString(u) {
t.Errorf("postURIRe rejected %q", u)
}
}
for _, u := range invalid {
if postURIRe.MatchString(u) {
t.Errorf("postURIRe accepted %q", u)
}
}
}
// newTestServer wires a server against fake upstream and CDN, returning the
// thread-endpoint call counter for cache assertions.
func newTestServer(t *testing.T, actors string) (*server, *atomic.Int32, string) {
t.Helper()
var threadCalls atomic.Int32
var cdnBase string
upstreamSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.URL.Path {
case "/xrpc/com.atproto.identity.resolveHandle":
if r.URL.Query().Get("handle") == "missing.example.com" {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, `{"error":"InvalidRequest","message":"Unable to resolve handle"}`)
return
}
fmt.Fprint(w, `{"did":"did:plc:abc"}`)
case "/xrpc/app.bsky.feed.getPostThread":
threadCalls.Add(1)
fmt.Fprintf(w, `{"thread":{"post":{"author":{"handle":"alice.test","avatar":"%s/img/avatar/plain/did:plc:abc/bafkexample"}},"replies":[]}}`, cdnBase)
default:
w.WriteHeader(http.StatusNotFound)
}
}))
t.Cleanup(upstreamSrv.Close)
cdnSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/img/avatar/plain/did:plc:abc/bafkexample":
w.Header().Set("Content-Type", "image/webp")
w.Write([]byte("img-bytes"))
case "/img/avatar/plain/did:plc:abc/notimage":
w.Header().Set("Content-Type", "text/html")
w.Write([]byte("<html>"))
default:
w.WriteHeader(http.StatusNotFound)
}
}))
t.Cleanup(cdnSrv.Close)
cdnBase = cdnSrv.URL
s := &server{
up: newUpstream(upstreamSrv.URL),
cdn: newUpstream(cdnSrv.URL),
threads: newCache(45*time.Second, 16),
avatars: newCache(45*time.Second, 16),
actors: splitList(actors),
}
return s, &threadCalls, cdnSrv.URL
}
func doGet(s http.HandlerFunc, target string) *httptest.ResponseRecorder {
w := httptest.NewRecorder()
s(w, httptest.NewRequest(http.MethodGet, target, nil))
return w
}
func TestResolveHandle(t *testing.T) {
s, _, _ := newTestServer(t, "")
w := doGet(s.resolveHandle, "http://proxy.test/xrpc/com.atproto.identity.resolveHandle?handle=alice.test")
if w.Code != http.StatusOK {
t.Fatalf("status = %d, want 200", w.Code)
}
if !strings.Contains(w.Body.String(), "did:plc:abc") {
t.Fatalf("body = %q", w.Body.String())
}
if cc := w.Header().Get("Cache-Control"); cc != "public, max-age=45" {
t.Fatalf("Cache-Control = %q", cc)
}
w = doGet(s.resolveHandle, "http://proxy.test/xrpc/com.atproto.identity.resolveHandle?handle=..bad..")
if w.Code != http.StatusBadRequest {
t.Fatalf("invalid handle: status = %d, want 400", w.Code)
}
w = doGet(s.resolveHandle, "http://proxy.test/xrpc/com.atproto.identity.resolveHandle?handle=missing.example.com")
if w.Code != http.StatusBadRequest {
t.Fatalf("upstream error: status = %d, want 400 passed through", w.Code)
}
if cc := w.Header().Get("Cache-Control"); cc != "no-store" {
t.Fatalf("upstream error Cache-Control = %q, want no-store", cc)
}
if !strings.Contains(w.Body.String(), "Unable to resolve handle") {
t.Fatalf("upstream error body not passed through: %q", w.Body.String())
}
}
func TestGetPostThread(t *testing.T) {
s, threadCalls, _ := newTestServer(t, "")
uri := "at://did:plc:abc/app.bsky.feed.post/3k"
w := doGet(s.getPostThread, "http://proxy.test/xrpc/app.bsky.feed.getPostThread?uri="+uri+"&depth=3")
if w.Code != http.StatusOK {
t.Fatalf("status = %d, want 200: %s", w.Code, w.Body.String())
}
if !strings.Contains(w.Body.String(), `"avatar":"http://proxy.test/avatar/img/avatar/plain/did:plc:abc/bafkexample"`) {
t.Fatalf("avatar not rewritten to proxy: %s", w.Body.String())
}
doGet(s.getPostThread, "http://proxy.test/xrpc/app.bsky.feed.getPostThread?uri="+uri+"&depth=3")
if n := threadCalls.Load(); n != 1 {
t.Fatalf("upstream called %d times for cached thread, want 1", n)
}
for _, q := range []string{
"uri=https://bsky.app/profile/x/post/y",
"uri=" + uri + "&depth=-1",
"uri=" + uri + "&depth=1001",
"uri=" + uri + "&depth=x",
} {
w = doGet(s.getPostThread, "http://proxy.test/xrpc/app.bsky.feed.getPostThread?"+q)
if w.Code != http.StatusBadRequest {
t.Errorf("query %q: status = %d, want 400", q, w.Code)
}
}
}
func TestActorAllowlist(t *testing.T) {
s, _, _ := newTestServer(t, "alice.test,did:plc:abc")
w := doGet(s.getPostThread, "http://proxy.test/xrpc/app.bsky.feed.getPostThread?uri=at://did:plc:abc/app.bsky.feed.post/3k")
if w.Code != http.StatusOK {
t.Fatalf("allowlisted DID: status = %d, want 200", w.Code)
}
w = doGet(s.getPostThread, "http://proxy.test/xrpc/app.bsky.feed.getPostThread?uri=at://did:plc:other/app.bsky.feed.post/3k")
if w.Code != http.StatusForbidden {
t.Fatalf("other DID: status = %d, want 403", w.Code)
}
w = doGet(s.resolveHandle, "http://proxy.test/xrpc/com.atproto.identity.resolveHandle?handle=alice.test")
if w.Code != http.StatusOK {
t.Fatalf("allowlisted handle: status = %d, want 200", w.Code)
}
w = doGet(s.resolveHandle, "http://proxy.test/xrpc/com.atproto.identity.resolveHandle?handle=other.test")
if w.Code != http.StatusForbidden {
t.Fatalf("other handle: status = %d, want 403", w.Code)
}
}
func TestUpstreamBodyCap(t *testing.T) {
big := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Write(make([]byte, maxUpstreamBody+1024))
}))
t.Cleanup(big.Close)
body, _, err := newUpstream(big.URL).get("/x", nil, "application/json")
if err != nil {
t.Fatalf("get: %v", err)
}
if len(body) > maxUpstreamBody {
t.Fatalf("body length %d exceeds cap %d", len(body), maxUpstreamBody)
}
}
func TestErrorBodiesAreGenericJSON(t *testing.T) {
w := httptest.NewRecorder()
writeJSONError(w, http.StatusBadRequest, "InvalidRequest")
var parsed map[string]string
if err := json.Unmarshal(w.Body.Bytes(), &parsed); err != nil {
t.Fatalf("error body is not JSON: %v", err)
}
if len(parsed) != 1 || parsed["error"] != "InvalidRequest" {
t.Fatalf("error body = %v, want only {error: InvalidRequest}", parsed)
}
}