Skip to content

Derive RS_SERVER_URL and RS_SESSION_URL from the session's base URL - #18647

Merged
skyeturriff merged 2 commits into
mainfrom
fix/rs-server-url-blank
Aug 29, 2026
Merged

skyeturriff merged 2 commits into
mainfrom
fix/rs-server-url-blank

Conversation

@skyeturriff

@skyeturriff skyeturriff commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

Intent

Addresses https://github.com/rstudio/rstudio-pro/issues/12127
OS companion to https://github.com/rstudio/rstudio-pro/pull/12457

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. This derives both RS_SERVER_URL and RS_SESSION_URL
from 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 the
open-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 -l defect that is observable in RStudio
Server open source.

Approach

  • Adds parseSessionUrlEnvVars() to core/r_util, which splits the URL at the session segment:
    everything preceding it becomes RS_SERVER_URL, and the segment itself becomes
    RS_SESSION_URL.
  • A reverse-proxy sub-path stays on RS_SERVER_URL (https://host/rstudio/) rather than moving
    to the session side, so RS_SESSION_URL keeps exactly the value it has today. An earlier
    revision of this PR moved it, which broke SessionChat.cpp's buildWebSocketUrl(): that
    function 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-token
    cookie 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.
  • The env var derivation is a no-op for RStudio Server open source. 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 with an empty session URL, which is bit-for-bit what
    the sessionUrl == "" && serverUrl == "" fallback it replaces produced.
  • The rserver-url -l fix is not a no-op here. It dropped RS_SERVER_URL's trailing slash
    unconditionally, relying on RS_SESSION_URL to supply a leading one. Since RS_SESSION_URL is
    empty exactly when there is no session segment, open source printed
    http://localhost:8787p/8050/, with the separator between authority and path missing. It also
    read the first character of RS_SERVER_URL with no emptiness check, which was undefined
    whenever the variable was unset. Both are pre-existing and both are fixed.
  • The join is extracted next to parseArguments() in url-ports/UrlPorts.hpp so it can be
    tested, following that function's existing precedent.

Automated Tests

In this repo, UrlPortsMainTests.cpp gains five cases for the extracted join: domain root, proxy
sub-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-tests runs 11/11 green.

RSessionContextTests.cpp is added here with the coverage reachable in this repo: the /s and
/n prefixes, an unknown prefix, and the two no-session-segment shapes that pin the no-op claim
above. It is byte-identical to the copy in rstudio-pro, so pull-upstream has nothing to
reconcile 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
/p preview 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-tests 636 ran / 0 failed, rserver-tests 1635 ran / 0 failed, rserver-url-tests
9 ran / 0 failed, rstudio-server-core-tests 62 ran / 0 failed. The six files here are
byte-identical to the pro versions apart from three pre-existing divergences unrelated to this
change (the Positron path defines, the /([snp])/ regex, and a handleClientInit block).

I have not built this branch in this repo; please treat CI here as the first build signal for it.

QA Notes

  • rserver-url -l was exercised directly against the built binary, with these observed
    outputs (RS_PORT_TOKEN=91c63048efb0, port 8050):

    RS_SERVER_URL RS_SESSION_URL output
    http://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/ (empty) http://localhost:8787/p/64966e32/
    (unset) /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-jupyterlab extension invokes the tool) reads neither variable and is
    unchanged.

  • 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 Positron
    session 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 /rstudio with www-root-path=/rstudio:

    RS_SERVER_URL=https://<host>/rstudio/
    RS_SESSION_URL=/s/5d24acfc78a315054a768/
    rserver-url -l 8060 -> https://<host>/rstudio/s/5d24acfc78a315054a768/p/88c98843/
    

    One sub-path, one session segment, and no sub-path inside RS_SESSION_URL. Fetching that
    rserver-url -l output returned 200, while the doubled-prefix variant returned 500. A
    Positron session on the same instance reported https://<host>/ plus
    /rstudio/s/<its own scope>/, so both session types agree on the deployment root while
    splitting 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.ts asserts the root-path prefix only inside
    if (pathSegments.length > 0 && !['s','p'].includes(pathSegments[0])), which never fires on
    Desktop or a root-level server, and it uses toContain, which a doubled prefix would satisfy
    anyway. 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.md entry yet for the rserver-url -l fix. Every entry in that file references an issue
number 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

  • If this PR adds a new feature, or fixes a bug in a previously released version, it includes an entry in NEWS.md
  • If this PR adds or changes UI, the updated UI meets accessibility standards
  • A reviewer is assigned to this PR (if unsure who to assign, check Area Owners list)
  • This PR passes all local unit tests

@github-actions

github-actions Bot commented Aug 26, 2026 •

Copy link
Copy Markdown

RStudio E2E - all platforms

All tests passed!

#################### 100% pass rate

Passed Failed Skipped Flaky
2532 0 184 3
Platform ✅ Passed ❌ Failed ⚠️ Skipped 🔁 Flaky
✅ auth-teardown 2 0 10 0
✅ desktop-linux 634 0 38 1
✅ desktop-macos 640 0 33 1
✅ desktop-windows 639 0 39 1
✅ server-linux 589 0 44 0
✅ setup 28 0 20 0
View / download the full report

View report online -- opens in the browser, no download needed. Available for 14 days.

Download playwright-report.zip -- extract and open index.html

Includes screenshots, traces, and step-by-step details for every test. Expires in 15 days.


All platforms - commit 3a5e57048e4038537ff82ab2f8f5f4dc6ea21b90 - Run #750

Comment thread src/cpp/core/r_util/RSessionContext.cpp Outdated
if (pServerUrl)
*pServerUrl = prefixUrl.protocol() + "://" + prefixUrl.host() + "/";
if (pSessionUrl)
*pSessionUrl = prefixUrl.path() + urlPrefix;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

opted to stick with keeping *pSessionUrl empty when there is no session segment, and fixing the join in rserver-url

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Comment thread src/cpp/core/r_util/RSessionContext.cpp Outdated
// 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();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

agreed, will fold this in

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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 gtritchie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Claude thinks this will break Posit Assistant in Workbench with a proxy subpath (plus a couple minor non-blocking findings).

@skyeturriff
skyeturriff force-pushed the fix/rs-server-url-blank branch 2 times, most recently from a374c2e to eca6d1e Compare August 28, 2026 15:40
@skyeturriff
skyeturriff requested review from cm421 and gtritchie August 28, 2026 15:53
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.
@skyeturriff
skyeturriff force-pushed the fix/rs-server-url-blank branch from 2753515 to 3a5e570 Compare August 29, 2026 01:04
@skyeturriff
skyeturriff merged commit 6b8f24e into main Aug 29, 2026
29 checks passed
@skyeturriff
skyeturriff deleted the fix/rs-server-url-blank branch August 29, 2026 02:28
@gtritchie gtritchie mentioned this pull request Aug 31, 2026
3 of 4 tasks

This branch was previously deployed

1 inactive deployment
e2e-playwright — 3a5e5704 Deployed Aug 29, 2026 by skyeturriff via windows / e2e-desktop-windows (4) #750
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