Repository navigation
Read the chat pane Content-Security-Policy from the installation being served - #18833
Merged
Merged
Conversation
loadCspDirectives() cached dist/csp.json in a function-local static, so the directives were read once and kept for the session. An in-session Posit Assistant update stops the backend, installs, and restarts it, which re-reads nothing: the new client was served under the previous version's policy until RStudio itself was restarted, and a directive the new version needs was blocked by the browser. Drop the static. rebuildCspHeaderCache() already runs on every backend start via setChatBackendPort(), and the installation is pinned before the port is published, so the rebuild reads csp.json from the installation the restart resolved. The header string stays cached, so this is one small read per backend start rather than per request. Note that keying the cache on the installation path -- the option the issue prefers -- would not fix this today: installPackage() extracts over the same pai/bin, so the path a restart pins is unchanged. Tying the read to the backend start is correct for both that and the versioned layout of #18658, where the path does change. Addresses #18831.
rebuildCspHeaderCache() read the served installation and the backend port outside s_cspMutex and took it only to store the result, so the order in which two overlapping rebuilds committed did not have to match the order in which they read. The reachable pair is the lazy rebuild on an HTTP handler thread racing the first backend start -- later rebuilds all come from setChatBackendPort() on the main thread -- and the loser committing last would leave the header built from the installation resolved before the start pinned one, with s_cspHeaderBuilt set so nothing rebuilds it until the next restart. Hold the lock across the read as well, so the rebuild that commits last is also the one that read last. Lock ordering is unchanged: s_cspMutex -> s_installationMutex is the only direction taken, and setInstallationPath() takes s_installationMutex alone. Not covered by a test: making the inversion deterministic needs a hook inside the rebuild to park one thread mid-read, which is more seam than the window is worth. Addresses #18831.
setInstallationPath() changed which installation assets are served from without rebuilding the policy served with them, leaving correctness resting on every caller pairing the change with a setChatBackendPort() call. Uninstall does not pair them: it stops the backend first, which rebuilds the header while the pin is still set, and clears the pin afterwards. A user-level install removed while a system-wide or bundled copy remained therefore served that copy's assets under the removed installation's policy for the rest of the session, since no backend start follows an uninstall. Rebuild from inside setInstallationPath() instead, so the policy follows the installation structurally. The rebuild runs after releasing s_installationMutex, because it reads the pin back through servedInstallationPath() and would otherwise take the two locks in the opposite order from every other path. Both setters now rebuild, so the start sequence in startChatBackend() reaches the same end state in either order; the installation still goes first so no request landing in between is served under the previous policy, and the comment there no longer claims more than that. Addresses #18831.
gtritchie
had a problem deploying
to
e2e-playwright
September 15, 2026 22:05 — with
GitHub Actions
Failure
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 |
kevinushey
approved these changes
Sep 16, 2026
kevinushey
left a comment
Collaborator
There was a problem hiding this comment.
No actionable findings. The cache refreshes cover in-place updates, installation changes, and uninstall. Lock ordering is consistent, and the three new regression tests exercise the intended behavior.
CI reports passing C++ tests and cross-platform E2E checks. I did not rerun tests locally. Concurrent rebuilds remain untested by the new tests.
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
loadCspDirectives()cacheddist/csp.jsonin a function-local static, so thedirectives were read once and kept for the session. Updating Posit Assistant
from the chat pane stops the backend, installs, then restarts it and reloads
the UI, which re-reads nothing: the new client was served under the previous
version's Content-Security-Policy until RStudio itself was restarted. A version
needing a directive the old one did not -- another
connect-srcorigin, animg-src, a worker or font source -- has it blocked by the browser, and thechat pane is broken with nothing connecting it to the update the user just
accepted.
What the code does now
The directives are read on every header rebuild, and a rebuild happens whenever
RStudio changes what it serves:
setChatBackendPort()rebuilds, as it already did, so every backend startre-reads
csp.jsonfrom the installation that start resolved.setInstallationPath()now rebuilds too, so the policy follows theinstallation rather than depending on callers pairing the two. Uninstall did
not pair them -- it stops the backend, which rebuilds while the pin is still
set, and clears the pin afterwards -- so a user-level install removed while a
system-wide or bundled copy remained served that copy's assets under the
removed installation's policy for the rest of the session.
s_cspMutexacross the read as well as the store, so therebuild that commits last is also the one that read last. Without it, a lazy
rebuild on an HTTP handler thread racing the first backend start could commit
the older installation's directives last, with
s_cspHeaderBuiltset sonothing rebuilt it until the next restart.
The header string itself is still cached, so this is one small file read per
backend start rather than per request. Lock ordering is unchanged:
s_cspMutex->s_installationMutexis the only direction taken, andsetInstallationPath()releases the installation lock before rebuilding.Why the read is tied to the backend start
Keying the cache on the installation path -- the option #18831 suggests --
would not fix this.
installPackage()movespai/bintoai.prevandextracts into
pai/bin, so the path a restart pins is unchanged and apath-keyed cache stays stale. Tying the read to the events that change what is
served is correct both for that and for the versioned layout of #18658, where
the path does change.
What remains outside this fix is a third party replacing the pinned directory
in place, which triggers neither event -- an administrator upgrading a
system-wide install under live sessions. That is recorded in #18658, which also
carries the follow-up to derive the policy from the same per-request resolution
as the asset root.
Testing
rstudio-tests --scope rsession: 583 pass. Three new tests on the existingChatStaticFilesPinharness, each confirmed to fail without the change:csp.jsonoverwritten in place under an unchanged pin, which is what anin-session update does today. A test that swapped directories instead would
pass even with a path-keyed cache.
will do.
The fixture now also clears the backend port, and its comment records that both
setters rebuild from whatever is resolvable on the machine running the tests,
so a test asserting on the header has to pin an installation of its own.
Fixes #18831.