forked from coder/coder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy_test.go
More file actions
184 lines (152 loc) · 5.87 KB
/
Copy pathproxy_test.go
File metadata and controls
184 lines (152 loc) · 5.87 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
package workspaceapps_test
// App tests can be found in the apptest package.
import (
"context"
"net"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/coderd/httpapi"
"github.com/coder/coder/v2/coderd/httpmw"
"github.com/coder/coder/v2/coderd/jwtutils"
"github.com/coder/coder/v2/coderd/workspaceapps"
"github.com/coder/coder/v2/coderd/workspaceapps/appurl"
"github.com/coder/coder/v2/testutil"
)
type fakeSignedTokenProvider struct {
fromRequestCalls int
issueCalls int
}
func (s *fakeSignedTokenProvider) FromRequest(_ *http.Request) (*workspaceapps.SignedToken, bool) {
s.fromRequestCalls++
return nil, false
}
func (s *fakeSignedTokenProvider) Issue(_ context.Context, _ http.ResponseWriter, _ *http.Request, _ workspaceapps.IssueTokenRequest) (*workspaceapps.SignedToken, string, bool) {
s.issueCalls++
return nil, "", false
}
func TestHandleSubdomain(t *testing.T) {
t.Parallel()
t.Run("IgnoresUntrustedForwardedHost", func(t *testing.T) {
t.Parallel()
hostnamePattern := "*--apps.test.coder.com"
hostnameRegex, err := appurl.CompileHostnamePattern(hostnamePattern)
require.NoError(t, err)
dashboardURL, err := url.Parse("https://dashboard.test.coder.com")
require.NoError(t, err)
provider := &fakeSignedTokenProvider{}
srv := workspaceapps.NewServer(workspaceapps.ServerOptions{
Logger: testutil.Logger(t),
DashboardURL: dashboardURL,
AccessURL: dashboardURL,
Hostname: hostnamePattern,
HostnameRegex: hostnameRegex,
RealIPConfig: &httpmw.RealIPConfig{
TrustedOrigins: []*net.IPNet{{
IP: net.ParseIP("10.0.0.1"),
Mask: net.CIDRMask(32, 32),
}},
},
SignedTokenProvider: provider,
})
forgedHost := appurl.ApplicationURL{
AppSlugOrPort: "app",
WorkspaceName: "workspace",
Username: "victim",
}.String() + "--apps.test.coder.com"
nextCalled := false
next := http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) {
nextCalled = true
})
// Given: a request with a forged X-Forwarded-Host set to a valid
// app hostname, and an immediate peer outside the trusted proxy
// config.
req := httptest.NewRequest(http.MethodGet, "https://dashboard.test.coder.com/", nil)
req.Header.Set(httpapi.XForwardedHostHeader, forgedHost)
req.RemoteAddr = "17.18.19.20:1234"
// When: HandleSubdomain runs.
srv.HandleSubdomain()(next).ServeHTTP(httptest.NewRecorder(), req)
// Then: it ignores untrusted X-Forwarded-Host, so the received
// dashboard host is used, the request falls through to the next
// handler, and the signed app token provider is never called.
require.True(t, nextCalled)
require.Zero(t, provider.fromRequestCalls)
require.Zero(t, provider.issueCalls)
})
// After consuming a smuggled API key, the handler redirects to strip the key.
// A path that smuggles a separate host (e.g. "//evil.com") must redirect back
// to the current origin, not off-site. The full path-sanitization matrix lives
// in Test_originLocalURL.
t.Run("APIKeySmugglingStaysOnOrigin", func(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitShort)
hostnamePattern := "*--apps.test.coder.com"
hostnameRegex, err := appurl.CompileHostnamePattern(hostnamePattern)
require.NoError(t, err)
dashboardURL, err := url.Parse("https://dashboard.test.coder.com")
require.NoError(t, err)
// StaticKey lets us mint a smuggled key the handler can decrypt.
// A256GCMKW needs a 32-byte key.
keycache := jwtutils.StaticKey{ID: "test", Key: generateSecret(t, 32)}
payload := workspaceapps.EncryptedAPIKeyPayload{APIKey: "fake-api-key"}
payload.Fill(time.Now())
encryptedAPIKey, err := jwtutils.Encrypt(ctx, keycache, payload)
require.NoError(t, err)
srv := workspaceapps.NewServer(workspaceapps.ServerOptions{
Logger: testutil.Logger(t),
DashboardURL: dashboardURL,
AccessURL: dashboardURL,
Hostname: hostnamePattern,
HostnameRegex: hostnameRegex,
RealIPConfig: &httpmw.RealIPConfig{
TrustedOrigins: []*net.IPNet{{
IP: net.ParseIP("10.0.0.1"),
Mask: net.CIDRMask(32, 32),
}},
},
SignedTokenProvider: &fakeSignedTokenProvider{},
APIKeyEncryptionKeycache: keycache,
})
host := appurl.ApplicationURL{
AppSlugOrPort: "app",
WorkspaceName: "workspace",
Username: "user",
}.String() + "--apps.test.coder.com"
// Set r.URL.Path directly: the slash-collapsing middleware writes to chi's
// RoutePath, not r.URL.Path, so the raw "//evil.com" survives to the handler.
// "x=1" rides along to confirm unrelated params are preserved.
req := httptest.NewRequest(http.MethodGet, "https://"+host+"/", nil)
req.Host = host
req.RemoteAddr = "10.0.0.1:1234"
req.URL.Path = "//evil.com/phish"
req.URL.RawQuery = url.Values{
workspaceapps.SubdomainProxyAPIKeyParam: {encryptedAPIKey},
"x": {"1"},
}.Encode()
rec := httptest.NewRecorder()
nextCalled := false
next := http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
nextCalled = true
})
srv.HandleSubdomain()(next).ServeHTTP(rec, req)
res := rec.Result()
defer res.Body.Close()
// The key is consumed and a redirect is issued instead of proxying.
require.Equal(t, http.StatusSeeOther, res.StatusCode)
require.False(t, nextCalled)
loc := res.Header.Get("Location")
require.NotEmpty(t, loc)
require.False(t, strings.HasPrefix(loc, "//"), "redirect %q must stay same-origin", loc)
parsed, err := url.Parse(loc)
require.NoError(t, err)
require.Empty(t, parsed.Scheme, "redirect %q must not carry a scheme", loc)
require.Empty(t, parsed.Host, "redirect %q must not carry a host", loc)
// The smuggled key is stripped and unrelated query params survive.
require.Empty(t, parsed.Query().Get(workspaceapps.SubdomainProxyAPIKeyParam))
require.Equal(t, "1", parsed.Query().Get("x"))
})
}