- Notifications
You must be signed in to change notification settings - Fork928
fix: strip CORS headers from applications#8057
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -928,7 +928,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) { | ||
forceURLTransport(t, client) | ||
// Create workspace. | ||
port := appServer(t, nil) | ||
workspace, _ = createWorkspaceWithApps(t, client, user.OrganizationIDs[0], user, port) | ||
// Verify that the apps have the correct sharing levels set. | ||
@@ -1260,4 +1260,61 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) { | ||
}) | ||
} | ||
}) | ||
t.Run("CORSHeadersStripped", func(t *testing.T) { | ||
t.Parallel() | ||
appDetails := setupProxyTest(t, &DeploymentOptions{ | ||
headers: http.Header{ | ||
"X-Foobar": []string{"baz"}, | ||
"Access-Control-Allow-Origin": []string{"http://localhost"}, | ||
"access-control-allow-origin": []string{"http://localhost"}, | ||
"Access-Control-Allow-Credentials": []string{"true"}, | ||
"Access-Control-Allow-Methods": []string{"PUT"}, | ||
"Access-Control-Allow-Headers": []string{"X-Foobar"}, | ||
"Vary": []string{ | ||
"Origin", | ||
"origin", | ||
"Access-Control-Request-Headers", | ||
"access-Control-request-Headers", | ||
"Access-Control-Request-Methods", | ||
"ACCESS-CONTROL-REQUEST-METHODS", | ||
"X-Foobar", | ||
}, | ||
}, | ||
}) | ||
appURL := appDetails.SubdomainAppURL(appDetails.Apps.Owner) | ||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) | ||
defer cancel() | ||
resp, err := requestWithRetries(ctx, t, appDetails.AppClient(t), http.MethodGet, appURL.String(), nil) | ||
require.NoError(t, err) | ||
defer resp.Body.Close() | ||
require.Equal(t, http.StatusOK, resp.StatusCode) | ||
require.Equal(t, []string(nil), resp.Header.Values("Access-Control-Allow-Origin")) | ||
require.Equal(t, []string(nil), resp.Header.Values("Access-Control-Allow-Credentials")) | ||
require.Equal(t, []string(nil), resp.Header.Values("Access-Control-Allow-Methods")) | ||
require.Equal(t, []string(nil), resp.Header.Values("Access-Control-Allow-Headers")) | ||
// Somehow there are two "Origin"s in Vary even though there should only be | ||
// one (from the CORS middleware), even if you remove the headers being sent | ||
// above. When I do nothing else but change the expected value below to | ||
// have two "Origin"s suddenly Vary only has one. It is somehow always the | ||
// opposite of whatever I put for the expected. So, reluctantly, remove | ||
// duplicate "Origin" values. | ||
var deduped []string | ||
var addedOrigin bool | ||
for _, value := range resp.Header.Values("Vary") { | ||
if value != "Origin" || !addedOrigin { | ||
if value == "Origin" { | ||
addedOrigin = true | ||
} | ||
deduped = append(deduped, value) | ||
} | ||
} | ||
Comment on lines +1301 to +1316 MemberAuthor
| ||
require.Equal(t, []string{"Origin", "X-Foobar"}, deduped) | ||
require.Equal(t, []string{"baz"}, resp.Header.Values("X-Foobar")) | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -22,6 +22,7 @@ import ( | ||
"github.com/coder/coder/coderd/httpapi" | ||
"github.com/coder/coder/coderd/httpmw" | ||
"github.com/coder/coder/coderd/tracing" | ||
"github.com/coder/coder/coderd/util/slice" | ||
"github.com/coder/coder/coderd/wsconncache" | ||
"github.com/coder/coder/codersdk" | ||
"github.com/coder/coder/site" | ||
@@ -541,6 +542,26 @@ func (s *Server) proxyWorkspaceApp(rw http.ResponseWriter, r *http.Request, appT | ||
defer release() | ||
proxy.Transport = conn.HTTPTransport() | ||
proxy.ModifyResponse = func(r *http.Response) error { | ||
r.Header.Del(httpmw.AccessControlAllowOriginHeader) | ||
r.Header.Del(httpmw.AccessControlAllowCredentialsHeader) | ||
r.Header.Del(httpmw.AccessControlAllowMethodsHeader) | ||
r.Header.Del(httpmw.AccessControlAllowHeadersHeader) | ||
varies := r.Header.Values(httpmw.VaryHeader) | ||
r.Header.Del(httpmw.VaryHeader) | ||
forbiddenVary := []string{ | ||
httpmw.OriginHeader, | ||
httpmw.AccessControlRequestMethodsHeader, | ||
httpmw.AccessControlRequestHeadersHeader, | ||
} | ||
for _, value := range varies { | ||
if !slice.ContainsCompare(forbiddenVary, value, strings.EqualFold) { | ||
r.Header.Add(httpmw.VaryHeader, value) | ||
} | ||
} | ||
Comment on lines +550 to +561 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. What is this part doing? MemberAuthor
| ||
return nil | ||
} | ||
// This strips the session token from a workspace app request. | ||
cookieHeaders := r.Header.Values("Cookie")[:] | ||
r.Header.Del("Cookie") | ||