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:
Callum Styan
2025-07-30 13:42:39 -07:00
committed by GitHub
parent 96e32d60a2
commit ffbfaf2a6f
36 changed files with 1149 additions and 108 deletions
+8
View File
@@ -0,0 +1,8 @@
package codersdk
type CORSBehavior string
const (
CORSBehaviorSimple CORSBehavior = "simple"
CORSBehaviorPassthru CORSBehavior = "passthru"
)
+3
View File
@@ -206,6 +206,9 @@ type CreateTemplateRequest struct {
// true, and is why `*bool` is used here. When dynamic parameters becomes
// the default, this will default to false.
UseClassicParameterFlow *bool `json:"template_use_classic_parameter_flow,omitempty"`
// CORSBehavior allows optionally specifying the CORS behavior for all shared ports.
CORSBehavior *CORSBehavior `json:"cors_behavior"`
}
// CreateWorkspaceRequest provides options for creating a new workspace.
+2
View File
@@ -61,6 +61,7 @@ type Template struct {
// template version.
RequireActiveVersion bool `json:"require_active_version"`
MaxPortShareLevel WorkspaceAgentPortShareLevel `json:"max_port_share_level"`
CORSBehavior CORSBehavior `json:"cors_behavior"`
UseClassicParameterFlow bool `json:"use_classic_parameter_flow"`
}
@@ -252,6 +253,7 @@ type UpdateTemplateMeta struct {
// of the template.
DisableEveryoneGroupAccess bool `json:"disable_everyone_group_access"`
MaxPortShareLevel *WorkspaceAgentPortShareLevel `json:"max_port_share_level,omitempty"`
CORSBehavior *CORSBehavior `json:"cors_behavior,omitempty"`
// UseClassicParameterFlow is a flag that switches the default behavior to use the classic
// parameter flow when creating a workspace. This only affects deployments with the experiment
// "dynamic-parameters" enabled. This setting will live for a period after the experiment is