Repository navigation
Expand file tree
/
Copy pathdebug_server_test.go
More file actions
127 lines (99 loc) · 3.25 KB
/
Copy pathdebug_server_test.go
File metadata and controls
127 lines (99 loc) · 3.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
//go:build forge_debug
package forge
import (
"context"
"encoding/binary"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/gobwas/ws"
"github.com/gobwas/ws/wsutil"
)
func newDebugTestServer(t *testing.T) *httptest.Server {
t.Helper()
t.Setenv("FORGE_DEBUG_PORT", "")
cfg := DefaultAppConfig()
cfg.EnableConfigAutoDiscovery = false
cfg.Logger = NewNoopLogger()
ds := &debugState{a: New(WithConfig(cfg)).(*app), hub: newDebugHub(), stopCh: make(chan struct{})}
srv := httptest.NewServer(ds.handler())
t.Cleanup(srv.Close)
return srv
}
func wsURL(srv *httptest.Server) string {
return "ws" + strings.TrimPrefix(srv.URL, "http") + "/ws"
}
// Any web page could open a WebSocket to the debug port (browsers do not
// apply CORS to upgrades) and read the app's routes, config and services.
func TestDebugServer_RejectsForeignOrigin(t *testing.T) {
srv := newDebugTestServer(t)
d := ws.Dialer{Header: ws.HandshakeHeaderHTTP(http.Header{"Origin": {"https://evil.example"}})}
if conn, _, _, err := d.Dial(context.Background(), wsURL(srv)); err == nil {
conn.Close()
t.Fatal("a cross-origin upgrade was accepted")
}
for _, origin := range []string{"", "http://localhost:3000", "http://127.0.0.1:9080"} {
h := http.Header{}
if origin != "" {
h.Set("Origin", origin)
}
d := ws.Dialer{Header: ws.HandshakeHeaderHTTP(h)}
conn, _, _, err := d.Dial(context.Background(), wsURL(srv))
if err != nil {
t.Fatalf("origin %q rejected: %v", origin, err)
}
if _, err := wsutil.ReadServerText(conn); err != nil {
t.Fatalf("origin %q got no snapshot: %v", origin, err)
}
conn.Close()
}
}
// DNS rebinding reaches /state through a foreign Host header.
func TestDebugServer_RejectsForeignHost(t *testing.T) {
srv := newDebugTestServer(t)
for host, want := range map[string]int{
"attacker-rebind.example:9080": http.StatusForbidden,
"localhost:9080": http.StatusOK,
"127.0.0.1": http.StatusOK,
"[::1]:9080": http.StatusOK,
} {
req, _ := http.NewRequestWithContext(t.Context(), http.MethodGet, srv.URL+"/state", nil)
req.Host = host
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
if resp.StatusCode != want {
t.Errorf("Host %q -> %d, want %d", host, resp.StatusCode, want)
}
}
}
// A frame header may claim any length; it used to size an allocation.
func TestDebugServer_OversizedFrameClosesConnection(t *testing.T) {
srv := newDebugTestServer(t)
conn, _, _, err := ws.Dial(context.Background(), wsURL(srv))
if err != nil {
t.Fatal(err)
}
defer conn.Close()
if _, err := wsutil.ReadServerText(conn); err != nil {
t.Fatal(err)
}
// A masked binary frame header claiming 1 GiB, with no payload.
hdr := []byte{0x82, 0xff}
hdr = binary.BigEndian.AppendUint64(hdr, 1<<30)
hdr = append(hdr, 1, 2, 3, 4)
if _, err := conn.Write(hdr); err != nil {
t.Fatal(err)
}
_ = conn.SetReadDeadline(time.Now().Add(5 * time.Second))
buf := make([]byte, 1)
if _, err := conn.Read(buf); err == nil {
t.Fatal("the server kept the connection open after an oversized frame")
} else if ne, ok := err.(interface{ Timeout() bool }); ok && ne.Timeout() {
t.Fatal("the server is still waiting for the 1 GiB payload")
}
}