Skip to content

Fix chat WebSocket URL for subpath reverse proxy deployments - #17250

Merged
gtritchie merged 4 commits into
mainfrom
bugfix/chat-websocket-root-path
Mar 19, 2026
Merged

gtritchie merged 4 commits into
mainfrom
bugfix/chat-websocket-root-path

Conversation

@gtritchie

Copy link
Copy Markdown
Member

Intent

Addresses #17249.

Summary

  • Prepend session-root-path to the chat WebSocket URL in buildWebSocketUrl() when RStudio Server is behind a subpath reverse proxy (e.g., /rstudio/)
  • Without this, the WebSocket URL is /p/{hash}/ai-chat/ws instead of /rstudio/p/{hash}/ai-chat/ws, which bypasses the proxy's location block and omits cookies scoped to the subpath

Details

buildWebSocketUrl() already prepends the RS_SESSION_URL env var for Workbench session prefixes, but did not account for the session-root-path option. In open-source RStudio Server, RS_SESSION_URL is always empty, so subpath deployments produced WebSocket URLs without the root path prefix.

Other port-mapped features (Shiny, Viewer, Plumber) are unaffected because they return relative URLs that the browser resolves against the current page. The chat code constructs an absolute path, so it needs the root path explicitly.

The fix reads options().rootPath() and prepends it when set to something other than the default /. The www-root-path server option passes this to the session automatically via --session-root-path.

Test plan

  • Root-level proxy: chat WebSocket connects at /p/{hash}/ai-chat/ws
  • Subpath proxy (/rstudio/): chat WebSocket connects at /rstudio/p/{hash}/ai-chat/ws
  • Desktop mode: unaffected (code is inside #ifdef RSTUDIO_SERVER)
  • Shiny/Viewer/Terminal: still work through subpath proxy
  • Session suspend/resume: chat reconnects

When RStudio Server is behind a reverse proxy at a subpath
(e.g., /rstudio/), buildWebSocketUrl() now prepends the
session-root-path to the port-mapped WebSocket path. Without this,
the browser sends the WebSocket upgrade to /p/{hash}/ai-chat/ws
which bypasses the proxy's location block and omits cookies scoped
to the subpath, causing authentication failures.

Addresses #17249

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@gtritchie gtritchie added this to the Globemaster Allium milestone Mar 19, 2026
gtritchie and others added 3 commits March 19, 2026 15:13
Move the path normalization and concatenation logic from
buildWebSocketUrl() into a pure function assembleWebSocketPath()
in ChatConstants. This makes the slash handling and root-path
prepending testable without session state.

Add ChatConstantsTests.cpp covering: no root path, root path
with/without trailing slash, session URL prefix, portmapped path
with/without leading slash, combined trailing slashes, IPv6 port
paths, and deep root paths.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
The length() > 1 guard kept "/" as-is, producing "//ai-chat".
Use the same unconditional trim as the old code so "/" collapses
to empty. Add a test case for this edge.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
- Restore cookie-scoping rationale in buildWebSocketUrl() explaining
  why all three path prefixes are needed
- Use session-root-path (the session option name) instead of
  www-root-path (the server option) in the inline comment
- Document the fixed /ai-chat suffix in the header doc comment

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@gtritchie
gtritchie requested a review from kevinushey March 19, 2026 21:31

@kevinushey kevinushey left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Clean, well-structured bugfix. The pure function extraction is the right call for testability, and the test coverage is thorough with good edge cases.

@posit-snyk-bot

posit-snyk-bot commented Mar 19, 2026 •

Copy link
Copy Markdown
Contributor

✅ Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
✅ Open Source Security 0 0 0 0 0 issues
✅ Licenses 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@gtritchie
gtritchie merged commit a96ef76 into main Mar 19, 2026
8 checks passed
@gtritchie
gtritchie deleted the bugfix/chat-websocket-root-path branch March 19, 2026 22:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants