Skip to content

Read the chat pane Content-Security-Policy from the installation being served - #18833

Merged
gtritchie merged 4 commits into
mainfrom
bugfix/18831-stale-csp-after-update
Sep 16, 2026
Merged

gtritchie merged 4 commits into
mainfrom
bugfix/18831-stale-csp-after-update

Conversation

@gtritchie

Copy link
Copy Markdown
Member

loadCspDirectives() cached dist/csp.json in a function-local static, so the
directives 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-src origin, an
img-src, a worker or font source -- has it blocked by the browser, and the
chat 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 start
    re-reads csp.json from the installation that start resolved.
  • setInstallationPath() now rebuilds too, so the policy follows the
    installation 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.
  • The rebuild holds s_cspMutex across the read as well as the store, so the
    rebuild 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_cspHeaderBuilt set so
    nothing 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_installationMutex is the only direction taken, and
setInstallationPath() 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() moves pai/bin to ai.prev and
extracts into pai/bin, so the path a restart pins is unchanged and a
path-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 existing
ChatStaticFilesPin harness, each confirmed to fail without the change:

  • csp.json overwritten in place under an unchanged pin, which is what an
    in-session update does today. A test that swapped directories instead would
    pass even with a path-keyed cache.
  • A restart onto a different installation, which is what a versioned install
    will do.
  • A pin change with no port change, which is the uninstall case.

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.

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 gtritchie added this to the 2026.10 (Blue Mistflower) milestone Sep 15, 2026
@gtritchie
gtritchie deployed to e2e-playwright September 15, 2026 21:51 — with GitHub Actions Active
@gtritchie
gtritchie deployed to e2e-playwright September 15, 2026 21:51 — with GitHub Actions Active
@gtritchie
gtritchie deployed to e2e-playwright September 15, 2026 21:53 — with GitHub Actions Active
@gtritchie
gtritchie deployed to e2e-playwright September 15, 2026 21:53 — with GitHub Actions Active
@gtritchie
gtritchie deployed to e2e-playwright September 15, 2026 21:59 — with GitHub Actions Active
@gtritchie
gtritchie deployed to e2e-playwright September 15, 2026 21:59 — with GitHub Actions Active
@gtritchie
gtritchie deployed to e2e-playwright September 15, 2026 22:05 — with GitHub Actions Active
@gtritchie
gtritchie deployed to e2e-playwright September 15, 2026 22:05 — with GitHub Actions Active
@gtritchie
gtritchie deployed to e2e-playwright September 15, 2026 22:05 — with GitHub Actions Active
@gtritchie
gtritchie deployed to e2e-playwright September 15, 2026 22:05 — with GitHub Actions Active
@gtritchie
gtritchie deployed to e2e-playwright September 15, 2026 22:05 — with GitHub Actions Active
@github-actions

github-actions Bot commented Sep 15, 2026 •

Copy link
Copy Markdown

RStudio E2E - all platforms

All tests passed!

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

Passed Failed Skipped Flaky
2994 0 187 2
Platform ✅ Passed ❌ Failed ⚠️ Skipped 🔁 Flaky
✅ auth-teardown 2 0 10 0
✅ desktop-linux 752 0 38 1
✅ desktop-macos 758 0 33 1
✅ desktop-windows 755 0 42 0
✅ server-linux 699 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 014348322875901605ee9cd8aef816f7c3b92f86 - Run #1035

@gtritchie
gtritchie deployed to e2e-playwright September 16, 2026 15:31 — with GitHub Actions Active
@gtritchie
gtritchie deployed to e2e-playwright September 16, 2026 15:31 — with GitHub Actions Active
@gtritchie
gtritchie deployed to e2e-playwright September 16, 2026 15:31 — with GitHub Actions Active
@gtritchie
gtritchie deployed to e2e-playwright September 16, 2026 15:31 — with GitHub Actions Active
@gtritchie
gtritchie deployed to e2e-playwright September 16, 2026 15:32 — with GitHub Actions Active
@gtritchie
gtritchie deployed to e2e-playwright September 16, 2026 15:32 — with GitHub Actions Active
@gtritchie
gtritchie deployed to e2e-playwright September 16, 2026 15:37 — with GitHub Actions Active
@gtritchie
gtritchie deployed to e2e-playwright September 16, 2026 15:37 — with GitHub Actions Active
@gtritchie
gtritchie deployed to e2e-playwright September 16, 2026 15:37 — with GitHub Actions Active
@gtritchie
gtritchie deployed to e2e-playwright September 16, 2026 15:37 — with GitHub Actions Active
@gtritchie
gtritchie deployed to e2e-playwright September 16, 2026 15:37 — with GitHub Actions Active
@gtritchie
gtritchie deployed to e2e-playwright September 16, 2026 15:37 — with GitHub Actions Active

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

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.

@gtritchie
gtritchie merged commit 1ac4805 into main Sep 16, 2026
35 checks passed
@gtritchie
gtritchie deleted the bugfix/18831-stale-csp-after-update branch September 16, 2026 17:17

This branch was successfully deployed

1 active deployment
e2e-playwright — 01434832 Deployed Sep 16, 2026 by gtritchie via windows / e2e-desktop-windows (5) #1035
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.

Chat pane serves a stale Content-Security-Policy after Posit Assistant is updated in-session

2 participants