Skip to content

fix(coderd/workspaceapps): prefer app session cookie over Authorization#22041

Merged
deansheather merged 1 commit intocoder:mainfrom
cryptoluks:fix/app-session-cookie-precedence
Feb 11, 2026
Merged

fix(coderd/workspaceapps): prefer app session cookie over Authorization#22041
deansheather merged 1 commit intocoder:mainfrom
cryptoluks:fix/app-session-cookie-precedence

Conversation

@cryptoluks
Copy link
Contributor

This PR fixes a workspace app authentication bug where requests that include an Authorization header (intended for the upstream app) can cause Coder to ignore the workspace app session cookie (coder_subdomain_app_session_token_* / coder_path_app_session_token). When that happens, Coder fails to mint or renew coder_signed_app_token and redirects to /api/v2/applications/auth-redirect instead of proxying the request to the workspace.

This commonly shows up when users run a frontend and backend in the same workspace and the backend requires Authorization (for example, curl -H "Authorization: bearer ..." or browser fetch() calls).

Related issues / context:

Root Cause

In coderd/workspaceapps/cookies.go, AppCookies.TokenFromRequest checked httpmw.APITokenFromRequest(r) first. That helper returns a token from several places, including Authorization: Bearer ....

As a result, when a request included an upstream Authorization header, that header value was returned as the “session token” for the app proxy, and coder_subdomain_app_session_token_* was never read. Authentication then failed and the request was treated as signed out.

Fix

Change the precedence in AppCookies.TokenFromRequest:

  1. First check the access-method-specific cookie:

    • subdomain apps: coder_subdomain_app_session_token_{hash}
    • path apps: coder_path_app_session_token
  2. If not present, fall back to httpmw.APITokenFromRequest(r) (so non-browser clients can still authenticate via query, header, or bearer tokens if they really want to).

This ensures that:

  • Backend requests that require Authorization still reach the workspace.
  • coder_signed_app_token can be renewed from the app session cookie even when Authorization is present.
  • Authorization is still forwarded to the upstream app (the reverse proxy code does not strip it).

Initially, I attempted workarounds (#20667 (comment), #19728 (comment)), but adding /auth-redirect to the permissive CORS paths and extending the validity of workspace app auth tokens from 1 minute to 1 hour only partially masked the issue. After workspace restarts and token expiry, I no longer saw CORS errors, but the tokens were still not renewed.

After patching my local Nix-based setup on Coder v1.30.0 with this change, I can no longer observe this behavior.

@cdr-bot cdr-bot bot added the community Pull Requests and issues created by the community. label Feb 10, 2026
@github-actions
Copy link

github-actions bot commented Feb 10, 2026

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

@cryptoluks
Copy link
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 Feb 10, 2026
@cryptoluks cryptoluks changed the title Fix workspace app token renewal when Authorization header is present fix(workspaceapps): prefer app session cookie over Authorization Feb 10, 2026
@cryptoluks cryptoluks changed the title fix(workspaceapps): prefer app session cookie over Authorization fix(coderd/workspaceapps): prefer app session cookie over Authorization Feb 10, 2026
Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

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

Nice work!

@deansheather
Copy link
Member

Thank you for your contribution!

@deansheather deansheather merged commit fcf431c into coder:main Feb 11, 2026
35 of 37 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Feb 11, 2026
@coder coder unlocked this conversation Feb 11, 2026
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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants