- Notifications
You must be signed in to change notification settings - Fork928
feat: allow cross-origin requests between users' own apps#7688
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
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
7 commits Select commitHold shift + click to select a range
1a690f0
Allow cross-origin requests between users' own apps
code-asherd8a0ba9
Simplify CORS handler with AllowOriginFunc
code-asher45d3565
Break out workspace app cors handler
code-asherda1298a
Add tests for workspace app cors
code-asher1cb650a
Test options requests
code-ashere733304
Move test app into test cases
code-ashera0f2eb8
Recover accidentally removed newline
code-asherFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
6 changes: 3 additions & 3 deletionscoderd/coderd.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletionscoderd/httpmw/cors.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletionscoderd/httpmw/cors_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package httpmw_test | ||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"github.com/stretchr/testify/require" | ||
"github.com/coder/coder/coderd/httpapi" | ||
"github.com/coder/coder/coderd/httpmw" | ||
) | ||
func TestWorkspaceAppCors(t *testing.T) { | ||
code-asher marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
t.Parallel() | ||
regex, err := httpapi.CompileHostnamePattern("*--apps.dev.coder.com") | ||
require.NoError(t, err) | ||
methods := []string{ | ||
http.MethodOptions, | ||
http.MethodHead, | ||
http.MethodGet, | ||
http.MethodPost, | ||
http.MethodPut, | ||
http.MethodPatch, | ||
http.MethodDelete, | ||
} | ||
tests := []struct { | ||
name string | ||
origin string | ||
app httpapi.ApplicationURL | ||
allowed bool | ||
}{ | ||
{ | ||
name: "Self", | ||
origin: "https://3000--agent--ws--user--apps.dev.coder.com", | ||
app: httpapi.ApplicationURL{ | ||
AppSlugOrPort: "3000", | ||
AgentName: "agent", | ||
WorkspaceName: "ws", | ||
Username: "user", | ||
}, | ||
allowed: true, | ||
}, | ||
{ | ||
name: "SameWorkspace", | ||
origin: "https://8000--agent--ws--user--apps.dev.coder.com", | ||
app: httpapi.ApplicationURL{ | ||
AppSlugOrPort: "3000", | ||
AgentName: "agent", | ||
WorkspaceName: "ws", | ||
Username: "user", | ||
}, | ||
allowed: true, | ||
}, | ||
{ | ||
name: "SameUser", | ||
origin: "https://8000--agent2--ws2--user--apps.dev.coder.com", | ||
app: httpapi.ApplicationURL{ | ||
AppSlugOrPort: "3000", | ||
AgentName: "agent", | ||
WorkspaceName: "ws", | ||
Username: "user", | ||
}, | ||
allowed: true, | ||
}, | ||
{ | ||
name: "DifferentOriginOwner", | ||
origin: "https://3000--agent--ws--user2--apps.dev.coder.com", | ||
app: httpapi.ApplicationURL{ | ||
AppSlugOrPort: "3000", | ||
AgentName: "agent", | ||
WorkspaceName: "ws", | ||
Username: "user", | ||
}, | ||
allowed: false, | ||
}, | ||
{ | ||
name: "DifferentHostOwner", | ||
origin: "https://3000--agent--ws--user--apps.dev.coder.com", | ||
app: httpapi.ApplicationURL{ | ||
AppSlugOrPort: "3000", | ||
AgentName: "agent", | ||
WorkspaceName: "ws", | ||
Username: "user2", | ||
}, | ||
allowed: false, | ||
}, | ||
} | ||
for _, test := range tests { | ||
test := test | ||
t.Run(test.name, func(t *testing.T) { | ||
t.Parallel() | ||
for _, method := range methods { | ||
r := httptest.NewRequest(method, "http://localhost", nil) | ||
r.Header.Set("Origin", test.origin) | ||
rw := httptest.NewRecorder() | ||
// Preflight requests need to know what method will be requested. | ||
if method == http.MethodOptions { | ||
r.Header.Set("Access-Control-Request-Method", method) | ||
} | ||
handler := httpmw.WorkspaceAppCors(regex, test.app)(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { | ||
rw.WriteHeader(http.StatusNoContent) | ||
})) | ||
handler.ServeHTTP(rw, r) | ||
if test.allowed { | ||
require.Equal(t, test.origin, rw.Header().Get("Access-Control-Allow-Origin")) | ||
} else { | ||
require.Equal(t, "", rw.Header().Get("Access-Control-Allow-Origin")) | ||
} | ||
// For options we should never get to our handler as the middleware | ||
// short-circuits with a 200. | ||
if method == http.MethodOptions { | ||
require.Equal(t, http.StatusOK, rw.Code) | ||
} else { | ||
require.Equal(t, http.StatusNoContent, rw.Code) | ||
} | ||
} | ||
}) | ||
} | ||
} |
55 changes: 27 additions & 28 deletionscoderd/workspaceapps/proxy.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletioncodersdk/deployment.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.