Repository navigation
Derive RS_SERVER_URL and RS_SESSION_URL from the session's base URL - #18647
Conversation
RStudio E2E - all platformsAll tests passed!
View / download the full reportView report online -- opens in the browser, no download needed. Available for 14 days. Download playwright-report.zip -- extract and open
All platforms - commit |
| if (pServerUrl) | ||
| *pServerUrl = prefixUrl.protocol() + "://" + prefixUrl.host() + "/"; | ||
| if (pSessionUrl) | ||
| *pSessionUrl = prefixUrl.path() + urlPrefix; |
There was a problem hiding this comment.
Folding the proxy sub-path into RS_SESSION_URL changes that variable's contract, and there is a second consumer in this repo that still assumes the old one.
SessionChat.cpp:4963-4966 builds the chat WebSocket path from options().rootPath() plus RS_SESSION_URL:
std::string rootPath = options().rootPath();
std::string sessionUrl = core::system::getenv(kSessionUrlEnvVar);
std::string wsPath = chat_constants::assembleWebSocketPath(rootPath, sessionUrl, portmappedPath);and assembleWebSocketPath() is a plain root + session + mapped + "/ai-chat" concatenation (ChatConstants.cpp:87). rootPath is the same reverse-proxy sub-path this line now prepends to the session URL (session-root-path, set from www-root-path).
Scenario: Workbench behind a proxy sub-path, www-root-path=/rstudio, host page URL https://host/rstudio/s/<scope>/.
- before:
RS_SESSION_URL=/s/<scope>/-> ws path/rstudio/s/<scope>/p/58fab3e4/ai-chat(correct) - after:
RS_SESSION_URL=/rstudio/s/<scope>/-> ws path/rstudio/rstudio/s/<scope>/p/58fab3e4/ai-chat
The doubled sub-path 404s and, because the port-token cookie is scoped to the session path, the browser also omits the cookie, so the chat backend connection fails. This is the sub-path deployment the PR description notes was not verified end to end.
Also worth updating the now-stale comment at SessionChat.cpp:4957 ("sessionUrl: Workbench session prefix (e.g., "/s/{id}"), empty in OSS") whichever way this is resolved.
There was a problem hiding this comment.
So really to get this to work we'd just need to move the /rstudio from RS_SESSION_URL and add it to RS_SERVER_URL for RStudio sessions. This is different from what we do in other sessions, but we could file an issue to bring everything to parity
There was a problem hiding this comment.
I like that fix. It narrows the change so RS_SESSION_URL stays what it is currently (just the session prefix and no proxy subpath i.e. /s/<sessionid>/), rserver-url -l still reconstructs correctly, and we don't have to touch this existing assistant chat path
There was a problem hiding this comment.
Confirmed and fixed by keeping the sub-path on RS_SERVER_URL instead, so RS_SESSION_URL is now unchanged by this PR and assembleWebSocketPath() sees exactly what it sees today. Force-pushed as a374c2e.
Two follow-on notes:
The comment at SessionChat.cpp:4957 turns out to stay accurate under this shape (RS_SESSION_URL really is just /s/{id}, empty in OSS), so I have left it alone rather than reword it.
The trade is that RStudio Pro and launcher-fed sessions now split the pair differently on sub-path deployments (RStudio Pro https://host/rstudio/ + /s/<scope>/; Jupyter/VS Code/Positron https://host/ + /rstudio/s/<scope>/). Both concatenate to the same URL and the domain root agrees exactly, so rserver-url -l is unaffected either way. Filing that divergence separately per @cm421 rather than resolving it here, since closing it means changing either the launcher's three session types or the Assistant WebSocket path.
Worth recording that nothing would have caught this: chat-websocket-url.test.ts guards the prefix only inside if (pathSegments.length > 0 && !['s','p'].includes(pathSegments[0])), which never fires on Desktop or a root-level server, and it asserts with toContain, which a doubled prefix satisfies anyway.
| // RStudio Server (open source) has no per-session URL scoping, so the | ||
| // whole url is the server url. | ||
| if (pServerUrl) | ||
| *pServerUrl = url; |
There was a problem hiding this comment.
This branch does not satisfy the invariant the new header comment states ("Trimming one trailing slash from *pServerUrl and appending *pSessionUrl reproduces the session URL"), and the sole consumer produces a malformed URL because of it.
OSS scenario, server at http://localhost:8787/: the client sends GWT.getHostPageBaseURL(), which always ends in /, so RS_SERVER_URL="http://localhost:8787/" and RS_SESSION_URL="". UrlPortsMain.cpp:67-72 pops the one trailing slash and prints serverPath + sessionPath + "p/" + token + "/":
$ rserver-url -l 8050
http://localhost:8787p/8050/
The slash between authority and path is gone. Same for a sub-path deployment: https://host/rstudio/ -> https://host/rstudiop/8050/.
Agreed this is pre-existing (the old sessionUrl == "" && serverUrl == "" fallback did the same), so it is not a regression -- but this PR is where the pair's contract is now written down, and the contract as written is not what this branch returns. Returning "/" rather than "" for *pSessionUrl here makes the header comment true and fixes rserver-url -l in open source, with no effect on assembleWebSocketPath() (it strips the trailing slash from the session component).
There was a problem hiding this comment.
opted to stick with keeping *pSessionUrl empty when there is no session segment, and fixing the join in rserver-url
There was a problem hiding this comment.
Done in a374c2e. The join now drops the trailing slash only when the session path supplies a leading one, so with RS_SESSION_URL empty the output goes from http://localhost:8787p/8050/ to http://localhost:8787/p/8050/. Verified against the built binary, not just the unit tests.
I also pulled the join out into buildProxiedUrl() next to parseArguments() in url-ports/UrlPorts.hpp, since that function had already established the seam, and added five cases to UrlPortsMainTests.cpp: domain root, sub-path, no session segment, unset server url, and a server url with no trailing slash. rserver-url-tests is 9/9.
| // No server to report, so the whole prefix stays on the session side and | ||
| // the two values still concatenate back to the input. | ||
| if (pServerUrl) | ||
| pServerUrl->clear(); |
There was a problem hiding this comment.
Minor / defensive: an empty *pServerUrl is now a documented output of this public function, and the consumer is not guarded against it. UrlPortsMain.cpp:67 does
if (serverPath.back() == '/')with no emptiness check, so RS_SERVER_URL="" means back() on an empty string -- undefined behavior, in practice garbage or a crash from rserver-url -l <port>.
Not reachable from makePortTokenCookie() today, since its base URL comes from GWT.getHostPageBaseURL() and is always absolute, so this branch is unreachable through the only caller in this repo. Since the function is core API and this PR documents the empty-server-url case as supported, an if (!serverPath.empty() && serverPath.back() == '/') in UrlPortsMain.cpp would close it off.
There was a problem hiding this comment.
agreed, will fold this in
There was a problem hiding this comment.
Folded in, same commit. The emptiness check moved into buildProxiedUrl() along with the rest of the join, so RS_SERVER_URL unset now yields /s/<scope>/p/8050/ and exit 0 rather than reading past the end of an empty string. Covered by BuildProxiedUrlWithoutServerUrl.
Still reachable in practice even after the reshape, incidentally: the variable can simply be unset, in which case getenv returns NULL and the code path sees an empty string regardless of what parseSessionUrlEnvVars() would have produced.
gtritchie
left a comment
There was a problem hiding this comment.
Claude thinks this will break Posit Assistant in Workbench with a proxy subpath (plus a couple minor non-blocking findings).
a374c2e to
eca6d1e
Compare
makePortTokenCookie() took RS_SERVER_URL from parseSessionUrl()'s pBaseUrl out-param, which holds only a URL's path component. For a server at the domain root that path is empty, so the variable was set to the empty string. Add parseSessionUrlEnvVars(), which splits the URL at the session segment: everything preceding it becomes RS_SERVER_URL, including any reverse-proxy sub-path, and the segment itself becomes RS_SESSION_URL. The pair still concatenates back to the session URL, which is the contract rserver-url -l relies on, and RS_SESSION_URL keeps the value it has today. This is a no-op for RStudio Server: its URLs carry no /s/<scope>/ segment, so parseSessionUrl() takes its no-match path and the new function returns the whole URL as the server URL, matching the fallback it replaces. The blank value is observable in Posit Workbench, tracked as rstudio-pro#12127. RSessionContextTests.cpp covers the behaviour reachable here: the /s and /n prefixes, an unknown prefix, and the two no-session-segment shapes that pin the no-op above. Workbench-only shapes are tested in rstudio-pro.
The long output dropped RS_SERVER_URL's trailing slash unconditionally, relying on RS_SESSION_URL to supply a leading one. RS_SESSION_URL is empty when the URL carries no session segment, so RStudio Server printed http://localhost:8787p/8050/. It also read the first character of RS_SERVER_URL without checking it was set, which was undefined whenever the variable was empty. Join the two and then normalize the result to end in a slash, so the port path stays rooted even when neither variable is set, which is reachable before the session's first client_init. Extract the join next to parseArguments so it can be tested, following that function's precedent, and cover the domain-root, sub-path, no-session-segment, unset-server-url, no-trailing-slash and neither-set cases.
2753515 to
3a5e570
Compare
Intent
Addresses https://github.com/rstudio/rstudio-pro/issues/12127
OS companion to https://github.com/rstudio/rstudio-pro/pull/12457
makePortTokenCookie()tookRS_SERVER_URLfromparseSessionUrl()'spBaseUrlout-param,which holds only a URL's path component. For a server at the domain root that path is empty, so
the variable was set to the empty string. This derives both
RS_SERVER_URLandRS_SESSION_URLfrom the base URL so the pair is always populated and always reconstructs the session URL.
The blank value is only observable in Posit Workbench, where session URLs carry a
/s/<scope>/segment. It is tracked internally as rstudio-pro#12127. This PR is theopen-source half of that fix, landing here first so it can be pulled into rstudio-pro rather
than diverging in shared files.
The second commit fixes a related
rserver-url -ldefect that is observable in RStudioServer open source.
Approach
parseSessionUrlEnvVars()tocore/r_util, which splits the URL at the session segment:everything preceding it becomes
RS_SERVER_URL, and the segment itself becomesRS_SESSION_URL.RS_SERVER_URL(https://host/rstudio/) rather than movingto the session side, so
RS_SESSION_URLkeeps exactly the value it has today. An earlierrevision of this PR moved it, which broke
SessionChat.cpp'sbuildWebSocketUrl(): thatfunction adds the sub-path itself from the root-path option, so the sub-path was applied twice,
giving
/rstudio/rstudio/s/<scope>/p/<token>/ai-chat. That 404s, and because the port-tokencookie is scoped to the session path the browser also omits it, so the chat backend connection
fails. Thanks to @gtritchie for catching it. This is the same failure class as Chat WebSocket URL missing root path in subpath proxy deployments #17249.
/s/<scope>/segment, soparseSessionUrl()takes its no-match path and the new functionreturns the whole URL as the server URL with an empty session URL, which is bit-for-bit what
the
sessionUrl == "" && serverUrl == ""fallback it replaces produced.rserver-url -lfix is not a no-op here. It droppedRS_SERVER_URL's trailing slashunconditionally, relying on
RS_SESSION_URLto supply a leading one. SinceRS_SESSION_URLisempty exactly when there is no session segment, open source printed
http://localhost:8787p/8050/, with the separator between authority and path missing. It alsoread the first character of
RS_SERVER_URLwith no emptiness check, which was undefinedwhenever the variable was unset. Both are pre-existing and both are fixed.
parseArguments()inurl-ports/UrlPorts.hppso it can betested, following that function's existing precedent.
Automated Tests
In this repo,
UrlPortsMainTests.cppgains five cases for the extracted join: domain root, proxysub-path, no session segment (the open-source shape, asserting the separator survives), unset
server URL, a server URL with no trailing slash, and neither variable set.
rserver-url-testsruns 11/11 green.RSessionContextTests.cppis added here with the coverage reachable in this repo: the/sand/nprefixes, an unknown prefix, and the two no-session-segment shapes that pin the no-op claimabove. It is byte-identical to the copy in rstudio-pro, so
pull-upstreamhas nothing toreconcile at that path.
Workbench-only shapes (domain-root and sub-path session scoping, trailing routes and query
strings, the doubled-segment regression guard, port preservation, and a host name containing the
proxy sub-path text) are tested in rstudio-pro's
RSessionContextOverlayTests.cpp, along with the/ppreview prefix, which cannot pass here because this repo's regex is/([sn])/.Results, run against an identical copy of this code in the rstudio-pro checkout:
rstudio-core-tests636 ran / 0 failed,rserver-tests1635 ran / 0 failed,rserver-url-tests9 ran / 0 failed,
rstudio-server-core-tests62 ran / 0 failed. The six files here arebyte-identical to the pro versions apart from three pre-existing divergences unrelated to this
change (the Positron path defines, the
/([snp])/regex, and ahandleClientInitblock).I have not built this branch in this repo; please treat CI here as the first build signal for it.
QA Notes
rserver-url -lwas exercised directly against the built binary, with these observedoutputs (
RS_PORT_TOKEN=91c63048efb0, port 8050):RS_SERVER_URLRS_SESSION_URLhttp://localhost:8787//s/abc1234567/http://localhost:8787/s/abc1234567/p/64966e32/https://host/rstudio//s/abc1234567/https://host/rstudio/s/abc1234567/p/64966e32/http://localhost:8787/http://localhost:8787/p/64966e32//s/abc1234567//s/abc1234567/p/64966e32/The third row is the open-source case that previously printed
http://localhost:8787p/64966e32/; the fourth was previously undefined. Short mode (no-l,which is how the
pwb-jupyterlabextension invokes the tool) reads neither variable and isunchanged.
The Workbench acceptance criterion was verified by hand on a domain-root deployment:
Sys.getenv("RS_SERVER_URL")returns the same value in an RStudio Pro session and a Positronsession on the same instance. That is also asserted by an automated Workbench spec, which now
passes.
Verified on a live reverse-proxy sub-path deployment (2026-08-28), Workbench behind Apache
at
/rstudiowithwww-root-path=/rstudio:One sub-path, one session segment, and no sub-path inside
RS_SESSION_URL. Fetching thatrserver-url -loutput returned200, while the doubled-prefix variant returned500. APositron session on the same instance reported
https://<host>/plus/rstudio/s/<its own scope>/, so both session types agree on the deployment root whilesplitting it differently, which is the divergence noted above.
Known gap: the Assistant chat WebSocket has no end-to-end coverage under a sub-path
deployment.
chat-websocket-url.test.tsasserts the root-path prefix only insideif (pathSegments.length > 0 && !['s','p'].includes(pathSegments[0])), which never fires onDesktop or a root-level server, and it uses
toContain, which a doubled prefix would satisfyanyway. Since this PR's first revision broke exactly that path, it is worth knowing that
nothing here would have caught it. Related: Extend Playwright base-prefs mechanism to Server mode #17520 excludes the Server engine from the
Assistant suite. The live sub-path verification above could not cover this either, because
Assistant was not installed on that environment.
Documentation
No documentation change needed for the env var derivation; no user-facing behavior changes in
RStudio Server open source for that half.
No
NEWS.mdentry yet for therserver-url -lfix. Every entry in that file references an issuenumber and there is no open-source issue for this defect, since it was found in review here. Happy
to file one and add the entry if you would like it in the release notes.
Checklist
NEWS.md