Repository navigation
feat: scope browser-only connections to a template - #30578
Conversation
a073c05 to
c37b153
Compare
|
Hi @tarujg, thank you for creating this PR to add a per-template browser-only setting. The write-up is clear and the open questions you listed made it easy to review. Based on the current implementation, I think it breaks Coder Desktop connections. Coder Desktop uses one /api/v2/tailnet connection for all of a user's workspaces and asks for a tunnel to every agent in the workspace list by itself. With this change, the tunnel to a browser-only workspace is refused in AuthorizeTunnel, and the coordinator closes the whole connection on that error. The app then reconnects, gets the same list, and is dropped again. So a user with one running browser-only workspace would lose Coder Desktop for their other workspaces too. I found this by reading the code and have not reproduced it with a real client, so please correct me if I missed something. I think this needs to be addressed first. Two possible directions:
Two smaller things:
Happy to continue reviewing once the Coder Desktop issue is addressed, and happy to share pointers if you have any questions. |
BobbyHo
left a comment
There was a problem hiding this comment.
Nice change! I think there's one blocking issue we should address first, though: #30578 (comment)
nickvigilante
left a comment
There was a problem hiding this comment.
Two docs nits, but the docs LGTM otherwise!
| When only some workspaces handle data that requires it, enable browser-only on | ||
| those templates instead of the whole deployment. Set `browser_only` on the | ||
| template with `PATCH /api/v2/templates/{template}`. Coder then refuses SSH, port | ||
| forwarding and desktop IDE connections to workspaces built from that template, | ||
| and leaves the other templates in the deployment alone. | ||
|
|
||
| The deployment-wide setting still applies on top. While `CODER_BROWSER_ONLY` is | ||
| set, every template refuses those connections regardless of its own setting. |
There was a problem hiding this comment.
| When only some workspaces handle data that requires it, enable browser-only on | |
| those templates instead of the whole deployment. Set `browser_only` on the | |
| template with `PATCH /api/v2/templates/{template}`. Coder then refuses SSH, port | |
| forwarding and desktop IDE connections to workspaces built from that template, | |
| and leaves the other templates in the deployment alone. | |
| The deployment-wide setting still applies on top. While `CODER_BROWSER_ONLY` is | |
| set, every template refuses those connections regardless of its own setting. | |
| When only some workspaces handle data that requires it, enable browser-only on those templates instead of the whole deployment. | |
| Set `browser_only` on the template with `PATCH /api/v2/templates/{template}`. | |
| Coder then refuses SSH, port forwarding and desktop IDE connections to workspaces built from that template, and leaves the other templates in the deployment alone. | |
| The deployment-wide setting still applies on top. | |
| While `CODER_BROWSER_ONLY` is set, every template refuses those connections regardless of its own setting. |
|
|
||
| When only some workspaces handle data that requires it, enable browser-only on | ||
| those templates instead of the whole deployment. Set `browser_only` on the | ||
| template with `PATCH /api/v2/templates/{template}`. Coder then refuses SSH, port |
There was a problem hiding this comment.
Optional: link the API reference where PATCH /api/v2/templates/{template} first appears, so readers can find the request body.
The reference entry is Update template settings by ID.
This applies to the text as rewritten in the suggestion above.
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
…emplate with raw SQL
…tunnel is refused Coder Desktop shares one /api/v2/tailnet connection across all of a user's workspaces, so refusing a browser-only tunnel by closing the peer dropped every workspace and reconnected in a loop. - The AGPL and pgcoord coordinators now return a TunnelPolicyError on that request and keep the peer. - GetWorkspacesAndAgentsByOwnerID omits workspaces built from browser-only templates, so clients are not told to dial them. - Renumber the template browser_only migration to 000643 after rebasing onto main. - Apply docs review suggestions and regenerate the API reference.
c37b153 to
d6c17fb
Compare
|
Thanks for catching the Coder Desktop issue, @BobbyHo. I went with both directions:
Rebased onto current I haven't tested with a real Coder Desktop client yet. I'll add manual steps for |
|
Hi @tarujg, thank you for the quick update. The Coder Desktop issue looks fixed to me. I read both coordinator changes and the list filter, and the new A few more things to address before we could run a manual test:
Two smaller things that could be follow-ups:
On your open questions: I think the name Thanks again for working on this. Happy to help if you have any questions. |
|
Regarding manual tests, would you mind running the following when you have time? No rush at all. They cover the main paths and should be enough to show there is no regression. Setup: one user with two running workspaces, Turning the setting on needs a license with the browser-only feature. If you don't have one, just let me know and I'm happy to run the tests that need it on my end. curl -X PATCH "$CODER_URL/api/v2/templates/<template-id>" \
-H "Coder-Session-Token: $CODER_SESSION_TOKEN" \
-H "Content-Type: application/json" \
-d '{"browser_only": true}'
Whenever it is ready, it would be great if you could share the results here. A short note or a screenshot for each test is plenty. Thanks again for your patience and for all the work on this! |
|
Thanks for the detailed review, @BobbyHo. All five points and both follow-ups are addressed:
Follow-ups:
Thanks also for answering the open questions. I kept the Manual tests Run on a local
Dashboard screenshots
|
|
@BobbyHo, about tests 3 and 4: I couldn't run them locally. Coder Desktop signed in to my The same behavior is covered at the protocol level by |
Manual TestRun on 2026-11-18 against this branch at Three clients were used: the Coder CLI built from this head, the dashboard in Chrome, and a real Coder Desktop 0.8.4 on macOS. Scenarios 9 to 13 are tests 3 and 4 from the author's manual test comment, the two that could not be run without a signed build. Coder Desktop refuses a tunnel binary that Coder did not sign. For this run its debug setting All 14 scenarios passed. Three things from the run are worth a look, listed under the table.
Facts worth knowing from the run:
1. Environment# workspace that hosts the dev server
cd ~/coder && git fetch origin +pull/30578/head:pr-30578 && git checkout -q pr-30578
git log -1 --format='%h %s'
scripts/develop.sh --db-reset # in the background, log to a file
curl -s http://localhost:3000/api/v2/buildinfo | jq -r .version
curl -s -H "Coder-Session-Token: $TOKEN" http://localhost:3000/api/v2/entitlements | jq -c '{has_license, browser_only: .features.browser_only}'
curl -sI http://localhost:3000/bin/coder-darwin-arm64 | grep -iE '^HTTP|content-length'
psql -Atc "select column_name||' '||data_type||' default '||column_default from information_schema.columns where table_name='templates' and column_name='browser_only'"
psql -Atc "select version, dirty from schema_migrations"
coder templates pull docker ./tpl --yes
coder templates push docker-locked -d ./tpl --yes --variable docker_arch=amd64 --variable docker_host=unix:///var/run/docker.sock
coder create ws-normal -t docker --yes --parameter 'jetbrains_ides=[]'
coder create ws-locked -t docker-locked --yes --parameter 'jetbrains_ides=[]'
coder list
for w in ws-normal ws-locked; do coder ssh $w -- hostname; doneBoth workspaces accept a CLI connection before the setting is turned on. This is the "before" for scenario 4. In the blocks below, 2. Turning the setting on without a license is refusedcurl -s -o patch.json -w 'HTTP %{http_code}\n' -X PATCH "http://localhost:3000/api/v2/templates/$DOCKER_LOCKED_ID" \
-H "Coder-Session-Token: $TOKEN" -H 'Content-Type: application/json' -d '{"browser_only": true}'
cat patch.json
psql -Atc "select name, browser_only from templates order by name"Nothing was written. 3. With a license the same request succeedscoder licenses add -f license.txt
curl -s -H "Coder-Session-Token: $TOKEN" http://localhost:3000/api/v2/entitlements | jq -c '{has_license, browser_only: .features.browser_only}'
# the same PATCH as in scenario 2
psql -Atc "select name, browser_only from templates order by name"
curl -s -H "Coder-Session-Token: $TOKEN" 'http://localhost:3000/api/v2/audit?limit=1&q=resource_type:template%20action:write' \
| jq -c '.audit_logs[] | {action, status_code, resource_target, diff: .diff.browser_only}'The same request got 400 in scenario 2 and 200 here, and the license is the only change between them. The response body is cut to two fields above. 4. The CLI is refused for the browser-only workspace# a plain GET of the coordinate endpoint, then the same with WebSocket upgrade headers
curl -s -w '\nHTTP %{http_code}\n' -H "Coder-Session-Token: $TOKEN" "http://localhost:3000/api/v2/workspaceagents/$WS_LOCKED_AGENT/coordinate"
curl -s -w '\nHTTP %{http_code}\n' -H "Coder-Session-Token: $TOKEN" "http://localhost:3000/api/v2/workspaceagents/$WS_NORMAL_AGENT/coordinate"
coder ssh ws-locked -- hostname; echo "exit $?"
coder ssh ws-normal -- hostname; echo "exit $?"
coder port-forward ws-locked --tcp 18080:8080; echo "exit $?"The 400 for The connection log six seconds later, newest first. The refused requests added no row (fact 3): 5. The web terminal and code-server still workIn Chrome, signed in as code-server at 6. The workspace page hides the desktop buttonsEvery link and button in the main area of each workspace page, read from the page 64 seconds apart in the same tab: The two lists differ by exactly two entries, 7. The workspaces listThe controls of each row on Both rows are the same (fact 2). The Open VSCode button was not clicked. 8. Coder Desktop signs in to the dev server# laptop, with Coder Desktop signed out and quit
defaults write com.coder.Coder-Desktop DangerousDisableCoderSignatureValidation -bool true
open -a "Coder Desktop"
# sign in to http://127.0.0.1:3000 with a token for admin, turn Coder Connect on
scutil --nc list | grep -i coder
ps -axo pid,lstart,command | grep 'coder-darwin-arm64 vpn-daemon'The server log has a No signature error appeared. The binary Desktop keeps is under 9. Coder Desktop with the setting already on (author's test 3)# laptop
dscacheutil -q host -a name ws-normal.coder
dscacheutil -q host -a name ws-locked.coder
ssh -F /dev/null -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null [email protected] hostname; echo "exit $?"
ssh -F /dev/null -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null [email protected] hostname; echo "exit $?"
ping6 -c 3 ws-normal.coder | tail -2The second 10. Coder Desktop stays connected# laptop: an SSH session through the Desktop tunnel that prints the time every second
ssh -F /dev/null -o BatchMode=yes -o ServerAliveInterval=5 [email protected] 'while true; do date -u +%H:%M:%S; sleep 1; done' > hold-ws-normal.log &
# after two minutes
grep -cE '^[0-9]{2}:' hold-ws-normal.log; head -1 hold-ws-normal.log; tail -1 hold-ws-normal.log
# server
psql -c "select workspace_name, connection_method, code, count(*) from connection_logs where connect_time > now() - interval '15 minutes' group by 1,2,3"
psql -Atc "select count(*) from connection_logs where code = 403"
grep -c 'tunnel refused by policy' develop.log141 lines in 141 seconds, and the session did not end. Desktop had one tunnel row in 11 minutes and the tunnel process was still pid 2122. A reconnect loop would have broken the session within a second and written a 403 row on each cycle. 11. Turning the setting off# server, 20:41:34 UTC
# PATCH {"browser_only": false} on docker-locked
coder ssh ws-locked -- hostname; echo "exit $?"
# laptop, six reads over 64 s
dscacheutil -q host -a name ws-locked.coder
# server, 20:44:07 UTC
coder restart ws-locked --yes
# laptop, a loop reading the name once a second, then
ssh -F /dev/null -o BatchMode=yes [email protected] hostname; echo "exit $?"The CLI connected 14 seconds after the change. Coder Desktop did not list the workspace for 2 min 42 s, and a CLI session to the workspace in between did not bring it back. The restart did, in the same second it finished. This is what the docs in this PR describe. The Desktop menu then listed both 12. Turning the setting on while Coder Desktop is connected (author's test 4)Coder Desktop was connected and listed both workspaces. One SSH session was open to each workspace through the Desktop tunnel, printing the time every second. # server, 20:47:20 UTC
# PATCH {"browser_only": true} on docker-locked
# laptop, right after, and again 49 s later
ssh -F /dev/null -o BatchMode=yes [email protected] hostname; echo "exit $?"
tail -1 hold-ws-normal.log; tail -1 hold-ws-locked.log
# server, 20:48:09 UTC
coder ssh ws-locked -- hostname; echo "exit $?"
psql -Atc "select count(*) from connection_logs where code = 403"; grep -c 'tunnel refused by policy' develop.logThe other workspace was not disturbed, the open session to 13. Coder Desktop reconnects with a stale listThe laptop's port forward to the dev server was stopped at 20:50:27 UTC and started again at 20:50:37. This cut Coder Desktop off from coderd while it still listed # laptop, 30 s after the forward came back
scutil --nc list | grep -i coder
dscacheutil -q host -a name ws-locked.coder
ssh -F /dev/null -o BatchMode=yes [email protected] hostname; echo "exit $?"
ssh -F /dev/null -o BatchMode=yes [email protected] hostname; echo "exit $?"
log show --last 6m --predicate 'subsystem BEGINSWITH "com.coder.Coder-Desktop"' | grep 'refused by policy'
# server
grep 'tunnel refused by policy' develop.log
psql -c "select connect_time, workspace_name, connection_method, code, user_agent from connection_logs where code = 403"One refusal, one 403 row, one warning in Desktop's log, and no loop. The server log line is cut after the start of the error text. The tunnel process was still pid 2122 from scenario 8. A loop on the laptop that read the name once a second saw 14. Cleanupcoder tokens remove desktop-bo30578-2
coder delete ws-normal --yes; coder delete ws-locked --yes
docker ps --format '{{.Names}}' | grep coder-admin-ws- || echo "no test containers"
# stop develop.sh
# laptop
defaults delete com.coder.Coder-Desktop DangerousDisableCoderSignatureValidation |
|
Thanks @tarujg for addressing all of my comments. I ran the manual tests today in my local dev environment, including the two Coder Desktop cases you could not run. Everything passed. The details are in the Manual Test comment: #30578 (comment) I also applied a small docs change to your branch: #30578 (comment). It says that Coder Desktop users keep access until the workspace is stopped or restarted. The reason is that Coder Desktop keeps its tunnel to every workspace it already lists, and its list is only rebuilt on a workspace event. Changing the template setting does not publish one, so new SSH and IDE sessions through Coder Desktop still reach the workspace until then. The change also tells admins how to end open connections right away. The latest changes LGTM, so I am going to approve this PR. Publishing a workspace event when |
Main added 000645_external_auth_links_oauth_extra_key_id in coder#32344, so the number is taken again after the last merge of main.


CODER_BROWSER_ONLYrefuses 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 abrowser_onlysetting on the template, enforced in coderd, and leaves the deployment flag as a global override.Refs #29157
Supersedes #29204, which the stale bot closed before it was reviewed. This is the same change rebased onto current
main, with the migration renumbered to000643.TestMigration000617ConnectionLogsMethodseeded its template withdbgen.Template, which inserts every column the currentInsertTemplatequery names. At schema 616 that fails as soon as a later migration adds a column totemplates, so the test now inserts the template with raw SQL, the same way the sibling...InSingleTxntest already does.What it does
templates.browser_only(new column,falseby default) is surfaced asbrowser_onlyoncodersdk.Template,CreateTemplateRequestandUpdateTemplateMeta, modeled on theallow_workspace_renamessetting from #27558.Enabling it requires the
browser_onlyentitlement; 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 howAGPLTemplateAccessControlStorekeeps honoring an existingdeprecatedmessage that AGPL cannot set.FeatureBrowserOnly.Enabledtracks the deployment flag rather than the license, so the write gate usesEntitlement.Entitled().Enforcement
Every place the deployment flag already covers:
/api/v2/workspaceagents/{workspaceagent}/coordinatereturns 409 with the same shape as the deployment-wide handler./api/v2/tailnetis user-scoped and spans templates, so it cannot be refused as a whole. It is refused per tunnel inrbacAuthorizer.AuthorizeTunnel.tailnet.TunnelPolicyErrorcarries the reason past the generic "not found or you do not have permission" mask.CoordinateResponse.Errorand keep the peer, so other tunnels on the same connection stay up. Coder Desktop shares one connection across every workspace, and closing the peer would have dropped all of them in a reconnect loop.GetWorkspacesAndAgentsByOwnerIDomits workspaces built from browser-only templates, so the workspace updates on this endpoint never tell Desktop to dial them. Flipping a template does not publish a workspace event, so until the next update the non-fatal refusal covers the stale list.Things I would like direction on
browser_onlyfor parity withCODER_BROWSER_ONLY. On a template,require_browser_connectionsmight read better.AuthorizeTunnelruns whilecore.mutexis held, and this adds a second round trip (GetTemplateByID, which reads thetemplate_with_namesview). I have not measured it. If it matters, a narrowSELECT browser_only FROM templates WHERE id = $1would replace the join.enterprise/coderd/coderd.go.Not included
coder templates editflag.coder/terraform-provider-coderd.Verification
Not verified: a real Coder Desktop client, a multi-node deployment, workspace proxies, and any UI. The enterprise
pgcoordpath is covered byTestPGCoordinator_TunnelPolicyRefusal, not by an end-to-end test.Local runs after the rebase onto
8922988e34, on darwin/arm64 against Postgres in Docker, Go 1.26.10:TestBlockNonBrowserTemplate/AgentCoordinatedials the agent as the owner before and after flipping the template, so the refusal cannot be an RBAC denial. From the coderd log of the original run in #29204, fields trimmed for width:TestBlockNonBrowserTemplate/UserTailnetopens one/api/v2/tailnetconnection for a user with two workspaces, one browser-only. It asserts the browser-only workspace is missing from the workspace updates, itsAddTunnelgets the policy message back, a tunnel to the other workspace still receives peer updates on the same connection, and the 403 tunnel entry lands in the connection log.TunnelPolicyRefusalTestintailnet/testchecks the same non-fatal refusal against both the AGPL coordinator and pgcoord.UserTailnetfails with the AGPL coordinator change reverted, andTestPGCoordinator_TunnelPolicyRefusalfails with the pgcoord change reverted.make genwas run for the database,codersdktypescript, swagger and audit-log targets. The protobuf targets were skipped: nothing in this change touches.protofiles andprotoc-gen-gowas not available locally. After the rebase onto8922988e34, the database, API reference, audit-log and typescript targets were rerun.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. The rebase onto current
mainand the follow-up to review were also done with Claude Code (Opus 5.5).