mirror of
https://github.com/coder/coder.git
synced 2026-06-03 13:08:25 +00:00
ffbfaf2a6f
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>
22 lines
594 B
Go
22 lines
594 B
Go
package cors
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/coder/coder/v2/codersdk"
|
|
)
|
|
|
|
type contextKeyBehavior struct{}
|
|
|
|
// WithBehavior sets the CORS behavior for the given context.
|
|
func WithBehavior(ctx context.Context, behavior codersdk.CORSBehavior) context.Context {
|
|
return context.WithValue(ctx, contextKeyBehavior{}, behavior)
|
|
}
|
|
|
|
// HasBehavior returns true if the given context has the specified CORS behavior.
|
|
func HasBehavior(ctx context.Context, behavior codersdk.CORSBehavior) bool {
|
|
val := ctx.Value(contextKeyBehavior{})
|
|
b, ok := val.(codersdk.CORSBehavior)
|
|
return ok && b == behavior
|
|
}
|