mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: allow bypassing current CORS magic based on template config (#18706)
Solves https://github.com/coder/coder/issues/15096 This is a slight rework/refactor of the earlier PRs from @dannykopping and @Emyrk: - https://github.com/coder/coder/pull/15669 - https://github.com/coder/coder/pull/15684 - https://github.com/coder/coder/pull/17596 Rather than having a per-app CORS behaviour setting and additionally a template level setting for ports, this PR adds a single template level CORS behaviour setting that is then used by all apps/ports for workspaces created from that template. The main changes are in `proxy.go` and `request.go` to: a) get the CORS behaviour setting from the template b) have `HandleSubdomain` bypass the CORS middleware handler if the selected behaviour is `passthru` c) in `proxyWorkspaceApp`, do not modify the response if the selected behaviour is `passthru` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added support for configuring CORS behavior ("simple" or "passthru") at the template level for all shared ports. * Introduced a new "CORS Behavior" setting in the template creation and settings forms. * API endpoints and responses now include the optional `cors_behavior` property for templates. * Workspace apps and proxy now honor the specified CORS behavior, enabling conditional CORS middleware application. * Enhanced workspace app tests with comprehensive scenarios covering CORS behaviors and authentication states. * **Bug Fixes** * None. * **Documentation** * Updated API and admin documentation to describe the new `cors_behavior` property and its usage. * Added examples and schema references for CORS behavior in relevant API docs. * **Tests** * Extended automated tests to cover different CORS behavior scenarios for templates and workspace apps. * **Chores** * Updated audit logging to track changes to the `cors_behavior` field on templates. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Signed-off-by: Callum Styan <callumstyan@gmail.com>
This commit is contained in:
@@ -204,6 +204,9 @@ type databaseRequest struct {
|
||||
// AppSharingLevel is the sharing level of the app. This is forced to be set
|
||||
// to AppSharingLevelOwner if the access method is terminal.
|
||||
AppSharingLevel database.AppSharingLevel
|
||||
// CorsBehavior is set at the template level for all apps/ports in a workspace, and can
|
||||
// either be the current CORS middleware 'simple' or bypass the cors middleware with 'passthru'.
|
||||
CorsBehavior database.CorsBehavior
|
||||
}
|
||||
|
||||
// getDatabase does queries to get the owner user, workspace and agent
|
||||
@@ -296,7 +299,14 @@ func (r Request) getDatabase(ctx context.Context, db database.Store) (*databaseR
|
||||
// First check if it's a port-based URL with an optional "s" suffix for HTTPS.
|
||||
potentialPortStr = strings.TrimSuffix(r.AppSlugOrPort, "s")
|
||||
portUint, portUintErr = strconv.ParseUint(potentialPortStr, 10, 16)
|
||||
corsBehavior database.CorsBehavior
|
||||
)
|
||||
|
||||
tmpl, err := db.GetTemplateByID(ctx, workspace.TemplateID)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("get template %q: %w", workspace.TemplateID, err)
|
||||
}
|
||||
corsBehavior = tmpl.CorsBehavior
|
||||
//nolint:nestif
|
||||
if portUintErr == nil {
|
||||
protocol := "http"
|
||||
@@ -417,6 +427,7 @@ func (r Request) getDatabase(ctx context.Context, db database.Store) (*databaseR
|
||||
App: app,
|
||||
AppURL: appURLParsed,
|
||||
AppSharingLevel: appSharingLevel,
|
||||
CorsBehavior: corsBehavior,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user