Skip to content

feat: bypass built-in CORS handling for workspace apps #15669

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
Closed

Conversation

dannykopping
Copy link
Contributor

@dannykopping dannykopping commented Nov 27, 2024

NOTE: this PR is blocked; we want to first introduce this feature to port shares, as it has transpired that that use-case is far more common. It would be awkward to get this PR into the next release but not for port shares, so we're keeping this one on hold.


This PR introduces a partial fix for #15096; it allows operators to specify the CORS behavior on a per-coder_app level.

This change requires cors_behavior to be set on a given coder_app; coder/terraform-provider-coder#309 introduces that new attribute.

coderd currently handles CORS automatically by handling preflight requests and stripping CORS headers from upstream coder_app responses.

Two CORS behaviors are defined in this PR:

  • simple: the current behavior of handling CORS within coderd
  • passthru: new behavior which transparently bypasses our CORS handling in coderd so the coder_app service is then fully responsible for handling CORS

We plan to further add this behavior to port shares.

The sharing level (owner, authenticated, public) is still respected, regardless of CORS behavior.

@@ -395,41 +397,73 @@ func (s *Server) HandleSubdomain(middlewares ...func(http.Handler) http.Handler)
return
}

// Use the passed in app middlewares before checking authentication and
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to reviewers: this is the meat of the change.
I had to move some things around here since the token was only being created after CORS was handled, and the defined behavior is stored in the token.

I moved the CORS middleware invocation into determineCORSBehavior because we needed to conditionally invoke it if behavior != passthru, only.

Signed-off-by: Danny Kopping <danny@coder.com>
Signed-off-by: Danny Kopping <danny@coder.com>
Signed-off-by: Danny Kopping <danny@coder.com>
Signed-off-by: Danny Kopping <danny@coder.com>
Signed-off-by: Danny Kopping <danny@coder.com>
Signed-off-by: Danny Kopping <danny@coder.com>
Signed-off-by: Danny Kopping <danny@coder.com>
Signed-off-by: Danny Kopping <danny@coder.com>
Signed-off-by: Danny Kopping <danny@coder.com>
@dannykopping dannykopping changed the title feat: bypass built-in CORS handling feat: bypass built-in CORS handling for workspace apps Nov 28, 2024
@github-actions github-actions bot added the stale This issue is like stale bread. label Dec 6, 2024
@github-actions github-actions bot closed this Dec 9, 2024
@dannykopping
Copy link
Contributor Author

Reopening; this has been deprioritized slightly but not forgotten.

@dannykopping dannykopping reopened this Dec 9, 2024
@github-actions github-actions bot removed the stale This issue is like stale bread. label Dec 10, 2024
@github-actions github-actions bot added the stale This issue is like stale bread. label Dec 19, 2024
@github-actions github-actions bot closed this Dec 22, 2024
@github-actions github-actions bot deleted the dk/cors branch May 30, 2025 00:07
cstyan added a commit that referenced this pull request Jul 30, 2025
…8706)

Solves #15096

This is a slight rework/refactor of the earlier PRs from @dannykopping
and @Emyrk:
- #15669
- #15684
- #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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale This issue is like stale bread.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy