Skip to content

feat: scope browser-only connections to a template - #29204

Closed
tarujg wants to merge 4 commits into
coder:mainfrom
tarujg:taruj/template-browser-only
Closed

tarujg wants to merge 4 commits into
coder:mainfrom
tarujg:taruj/template-browser-only

Conversation

@tarujg

@tarujg tarujg commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

CODER_BROWSER_ONLY refuses SSH, port forwarding and desktop IDE connections for a whole deployment, so a deployment that hosts both regulated and general-purpose templates can only adopt it by taking those connections away from everyone. This adds a browser_only setting on the template, enforced in coderd, and leaves the deployment flag as a global override.

Refs #29157

What it does

templates.browser_only (new column, false by default) is surfaced as browser_only on codersdk.Template, CreateTemplateRequest and UpdateTemplateMeta, modeled on the allow_workspace_renames setting from #27558.

Enabling it requires the browser_only entitlement; disabling it does not, and enforcement never checks the entitlement. A lapsed license therefore neither silently reopens the connections nor strands a template with them refused. This mirrors how AGPLTemplateAccessControlStore keeps honoring an existing deprecated message that AGPL cannot set.

FeatureBrowserOnly.Enabled tracks the deployment flag rather than the license, so the write gate uses Entitlement.Entitled().

Enforcement

Every place the deployment flag already covers:

  • /api/v2/workspaceagents/{workspaceagent}/coordinate returns 409 with the same shape as the deployment-wide handler.
  • /api/v2/tailnet is user-scoped and spans templates, so it cannot be refused as a whole. It is refused per tunnel in rbacAuthorizer.AuthorizeTunnel. tailnet.TunnelPolicyError carries the reason past the generic "not found or you do not have permission" mask so the coordinator logs the real cause.
  • The MCP agent-connection tool, which already checked the deployment flag.

Things I would like direction on

  • Naming. I reused browser_only for parity with CODER_BROWSER_ONLY. On a template, require_browser_connections might read better.
  • Refusal on /api/v2/tailnet drops the whole peer. An AuthorizeTunnel error makes the coordinator tear down the connection, which is what already happens for an RBAC denial. Coder Desktop keeps one session for every workspace, so touching a browser-only workspace will drop and reconnect the session rather than just failing that tunnel. Pre-existing behavior, but this makes it much easier to hit.
  • An extra query under the coordinator mutex. AuthorizeTunnel runs while core.mutex is held, and this adds a second round trip (GetTemplateByID, which reads the template_with_names view). I have not measured it. If it matters, a narrow SELECT browser_only FROM templates WHERE id = $1 would replace the join.
  • Whether the feature should report as enabled when any template uses it. That would be a change in enterprise/coderd/coderd.go.

Not included

  • No dashboard UI. The setting is only reachable through the API today.
  • No coder templates edit flag.
  • The issue asks to express this in Terraform, which additionally needs a change in coder/terraform-provider-coderd.

Verification

Not verified: a multi-node deployment, the enterprise pgcoord path (covered only through the shared CoordinateeAuth code, not by a test), workspace proxies, and any UI.

Local runs on darwin/arm64 against Postgres in Docker, Go 1.26.5:

$ go test ./coderd/ -run 'TestResolveTemplateMetaUpdate|TestPatchTemplateMeta/BrowserOnlyNotEntitled' -count=1
ok  	github.com/coder/coder/v2/coderd	4.004s

$ go test ./enterprise/coderd/ -run 'TestBlockNonBrowser' -count=1
ok  	github.com/coder/coder/v2/enterprise/coderd	2.591s

$ go test ./coderd/database/migrations/ ./enterprise/audit/ ./tailnet/ -count=1
ok  	github.com/coder/coder/v2/coderd/database/migrations	26.739s
ok  	github.com/coder/coder/v2/enterprise/audit	9.736s
ok  	github.com/coder/coder/v2/tailnet	19.544s

$ golangci-lint run ./coderd/... ./codersdk/... ./enterprise/... ./tailnet/...
0 issues.

site$ pnpm run lint:types
> tsc -p .

TestBlockNonBrowserTemplate/AgentCoordinate dials the agent as the owner before and after flipping the template, so the refusal cannot be an RBAC denial. From that run's coderd log, fields trimmed for width:

[debu]  coderd: GET  path=/api/v2/workspaceagents/a2ac4b4f-.../coordinate  workspace_name=purple-wilson3  agent_name=example  requestor_name=testuser  status_code=101
[debu]  coderd: PATCH  path=/api/v2/templates/d7306d85-...  requestor_name=testuser  status_code=200
[debu]  coderd: GET  path=/api/v2/workspaceagents/a2ac4b4f-.../coordinate  workspace_name=purple-wilson3  agent_name=example  requestor_name=testuser  status_code=409

TestBlockNonBrowserTemplate/UserTailnet sends an AddTunnel on /api/v2/tailnet and asserts the 403 tunnel entry in the connection log. The coordinator logs the reason:

[erro]  coord: peerReadLoop error handling request  peer_id=57e79494-c26d-4a9b-a6ca-087008146aeb  peer_name=client  error="authorization: Non-browser connections are disabled for this template."  request="add_tunnel:{id:\"...\"}"

make gen was run for the database, codersdk typescript, swagger and audit-log targets. The protobuf targets were skipped: nothing in this change touches .proto files and protoc-gen-go was not available locally.

AI disclosure

Most of this PR was written by Claude Code (Opus 5), so it falls under the AI contribution guidelines. The command output above is verbatim from those runs. Opening as a draft so I can review it before it reaches maintainers.

CODER_BROWSER_ONLY refuses SSH, port forwarding and desktop IDE
connections for a whole deployment, so a deployment that hosts both
regulated and general-purpose templates cannot adopt it without taking
those connections away from everyone.

Add a browser_only setting on the template, enforced in coderd at the
agent-scoped coordinate endpoint, per tunnel on the user-scoped tailnet
endpoint, and on the MCP agent connection. The deployment-wide flag
stays a global override.

Enabling the setting requires the browser_only entitlement. Disabling it
does not, and enforcement does not check the entitlement, so a lapsed
license neither reopens the connections nor strands the template.

Refs coder#29157
@github-actions

github-actions Bot commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@github-actions github-actions Bot added the community Pull Requests and issues created by the community. label Sep 11, 2026
@tarujg
tarujg marked this pull request as ready for review September 11, 2026 00:32
@tarujg

tarujg commented Sep 11, 2026

Copy link
Copy Markdown
Contributor Author

I have read the CLA Document and I hereby sign the CLA

cdrci2 added a commit to coder/cla that referenced this pull request Sep 11, 2026
@github-actions github-actions Bot added the stale This issue is like stale bread. label Oct 5, 2026
@github-actions github-actions Bot closed this Oct 9, 2026
BobbyHo pushed a commit that referenced this pull request Nov 18, 2026
Add per-template `browser_only` enforcement while preserving `CODER_BROWSER_ONLY` as a deployment-wide override.

Refs #29157. Supersedes #29204 (closed by stale bot). Rebased onto current `main`, with migration renumbered to `000643`.

### Changes

- Add `templates.browser_only` (default `false`) to the database, SDK, and template create/update APIs.
- Require the `browser_only` entitlement to enable the setting, but allow disabling and continue enforcement after license expiry.
- Reject agent coordinate connections with 409 and enforce per-template tunnel restrictions through `AuthorizeTunnel`.
- Return non-fatal tunnel policy errors in both AGPL coordinator and pgcoord, preserving other active tunnels on shared connections.
- Exclude browser-only workspaces from Desktop workspace updates and enforce the restriction in MCP agent connections.
- Fix `TestMigration000617ConnectionLogsMethod` to use raw SQL, avoiding schema compatibility issues.

### Open questions

- **Naming:** Keep `browser_only` or use `require_browser_connections`?
- **Performance:** `AuthorizeTunnel` adds a database query under the coordinator mutex. Consider a narrower query if needed.
- **Feature reporting:** Should the feature report as enabled when any template uses it?

### Out of scope

Dashboard UI, CLI flag, and Terraform provider support (requires a separate `coder/terraform-provider-coderd` change).

### Verification

- Relevant unit and integration tests passed, including coordinate rejection, tunnel isolation, workspace filtering, and non-fatal policy errors in both coordinators.
- `golangci-lint` passed with no issues.
- Regenerated database, SDK TypeScript, Swagger/API reference, and audit-log artifacts.
- Not manually verified with Coder Desktop, multi-node deployments, workspace proxies, or UI.

### AI disclosure

Most changes and the rebase were completed with Claude Code (Opus 5/5.5), following the [AI contribution guidelines](https://coder.com/docs/about/contributing/AI_CONTRIBUTING).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community Pull Requests and issues created by the community. stale This issue is like stale bread.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant