Skip to content

Fix crash when downloading a file with no read permission (#18864) - #18866

Merged
gtritchie merged 4 commits into
rstudio:mainfrom
samcofer:fix/18864-stream-response-cleanup
Sep 17, 2026
Merged

gtritchie merged 4 commits into
rstudio:mainfrom
samcofer:fix/18864-stream-response-cleanup

Conversation

@samcofer

Copy link
Copy Markdown
Contributor

Summary

Fixes #18864 — downloading/exporting a file the user has no read permission on (e.g. chmod 000) from the Files pane crashes RStudio Server with a segfault (the browser shows a 502 and the session enters a failed state).

Root cause

In Response::setStreamFile(), when streamResponse_->initialize() fails (opening the file returns EACCES), setError() is called but streamResponse_ is left set. Response::isStreamResponse() (static_cast<bool>(streamResponse_)) therefore still returns true, so AsyncConnectionImpl routes the response to StreamWriter, which calls FileStreamResponse::nextBuffer(). That dereferences fileStream_ at Response.cpp:89 — but fileStream_ was never opened, so it is null → segfault.

Fix

  1. Clear streamResponse_ on initialize() failure — the general fix. It stops the null deref and makes isStreamResponse() return false so the HTTP error body is actually sent. Covers every caller of setStreamFile().
  2. Defensive null-stream guard in FileStreamResponse::nextBuffer().

Platform scope

Affects the Unix/Linux gzip streaming path. Windows uses a different code path.

Testing

Manual repro: create a chmod 000 file, export it from the Files pane. Before: session segfaults (502 / failed). After: the download fails with an HTTP error and the session stays up.

Closes #18864

)

When Response::setStreamFile()'s streamResponse_->initialize() fails
(e.g. opening a file with no read permission returns EACCES), setError()
was called but streamResponse_ was left set. isStreamResponse() then still
returned true, so the async HTTP server drove the StreamWriter, which
called nextBuffer() on a response whose underlying fileStream_ was never
opened -- a null pointer dereference at Response.cpp:89 that segfaulted the
session.

Clear streamResponse_ on initialize() failure so the error body is sent
instead, and add a defensive null-stream guard in FileStreamResponse::
nextBuffer().

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@gtritchie gtritchie added this to the 2026.10 (Blue Mistflower) milestone Sep 17, 2026

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

1 blocking, 1 follow-up

🔴 must be resolved before merge. 🟠 is real but not merge-blocking — either address it here or open an issue to do so later.

  • 🔴 src/cpp/core/http/Response.cpp:862 — the error response still says Transfer-Encoding: chunked, so clients can't parse it
  • 🟠 src/cpp/core/http/Response.cpp:862 — no regression test for the unreadable-file path

The crash itself is fixed — resetting streamResponse_ does stop nextBuffer() from being called on a null stream. The remaining issue is that the error that replaces it doesn't reach the user.

// the error body is sent instead of the HTTP server attempting to stream
// from a response whose underlying file stream never opened (which would
// dereference a null pointer in nextBuffer() and crash the process)
streamResponse_.reset();

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.

🔴 The 500 still carries Transfer-Encoding: chunked from line 836, so the error body goes out unframed and the client can't parse it.

Suggested change
streamResponse_.reset();
streamResponse_.reset();
removeHeader(kTransferEncoding);
Why

setStreamFile sets Transfer-Encoding: chunked at line 836, before initialize() runs. When openForRead fails, setError does setStatusCode, removeCachingHeaders, setContentType("text/html") and setBodyUnencoded — that last one drops Content-Encoding and sets Content-Length, but nothing drops Transfer-Encoding.

isStreamResponse() is now false, so AsyncConnectionImpl.hpp:624 takes the plain-write branch and calls response_.toBuffers(). Message::headerBuffers (Message.cpp:204) emits every stored header verbatim, so the wire response has both Transfer-Encoding: chunked and Content-Length: N with a plain, un-chunked body.

RFC 7230 §3.3.3 says a recipient seeing both must ignore Content-Length and read the body as chunked. So the client tries to parse Permission denied as a hex chunk-size line, fails, and drops the connection — the user gets a network error rather than the HTTP error described in the NEWS entry. Worth updating that entry too if the wording no longer matches.

The one-line removeHeader above is the smallest fix. Moving initialize() above the setContentType / setContentEncoding / setHeader calls is tidier, since then none of the streaming headers get set on a response that won't stream.

One more, in the caller rather than here: setAttachmentResponse (src/cpp/session/modules/SessionFiles.cpp:1410) sets Content-Disposition: attachment; filename*=... before calling setStreamFile, and setError doesn't clear that either. Even once the framing is fixed, the browser will save the error page to disk under the requested filename instead of showing it.

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.

🔴 Still open at 942404e — Response.cpp is unchanged in this push, so the 500 still goes out with both Transfer-Encoding: chunked and Content-Length and an unframed body. The one-line removeHeader(kTransferEncoding) suggestion above still applies.

// the error body is sent instead of the HTTP server attempting to stream
// from a response whose underlying file stream never opened (which would
// dereference a null pointer in nextBuffer() and crash the process)
streamResponse_.reset();

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.

🟠 Nothing pins this fix in place — a later reorder of setStreamFile could reintroduce the null dereference without failing a test.

Add a test that points setStreamFile at an unreadable file and asserts isStreamResponse() is false, statusCode() is 500, and no Transfer-Encoding header survives.

Why

Neither hunk is covered today. src/cpp/core/http/ has established suites (HeaderTests.cpp, TransferEncodingTests.cpp, AsyncConnectionImplTests.cpp) but no ResponseTests.cpp, and nothing constructs a Response over a FilePath that can't be opened.

A test that chmod 000s a temp file and calls setStreamFile covers both the crash fix and the header issue above in the same three assertions, so it's cheap relative to the segfault it guards against. Skip it on Windows, where the permission model won't reproduce the open failure the same way.

@samcofer
samcofer deployed to e2e-playwright September 17, 2026 18:34 — with GitHub Actions Active
@samcofer
samcofer deployed to e2e-playwright September 17, 2026 18:34 — with GitHub Actions Active
@samcofer
samcofer deployed to e2e-playwright September 17, 2026 18:34 — with GitHub Actions Active
@samcofer
samcofer deployed to e2e-playwright September 17, 2026 18:34 — with GitHub Actions Active
@samcofer
samcofer deployed to e2e-playwright September 17, 2026 18:34 — with GitHub Actions Active
@samcofer
samcofer deployed to e2e-playwright September 17, 2026 18:34 — with GitHub Actions Active
@samcofer
samcofer deployed to e2e-playwright September 17, 2026 18:52 — with GitHub Actions Active
@samcofer
samcofer deployed to e2e-playwright September 17, 2026 18:52 — with GitHub Actions Active
@samcofer
samcofer deployed to e2e-playwright September 17, 2026 18:53 — with GitHub Actions Active
@samcofer
samcofer deployed to e2e-playwright September 17, 2026 18:58 — with GitHub Actions Active
@samcofer
samcofer deployed to e2e-playwright September 17, 2026 18:58 — with GitHub Actions Active
…#18864)

Covers Response::setStreamFile() when the file stream cannot be opened:
one test for a missing file (deterministic, root-safe) and one for a
chmod 000 file (the reported scenario, skipped when running as root).

Both assert that isStreamResponse() is false and the status is 500 after
the failure, so the HTTP server does not drive nextBuffer() on a null file
stream. Verified: the tests fail on the unfixed code and pass with the fix.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@samcofer

Copy link
Copy Markdown
Contributor Author

Added ResponseTests.cpp and verified locally by building rstudio-core-tests (jammy build image, boost 1.91):

  • With the fix: both tests pass, including the chmod 000 case run as a non-root user (the reported scenario).
  • Negative control (fix reverted): both tests fail — isStreamResponse() and getStreamResponse() return true after the initialize() failure, i.e. the dangling stream response that leads the HTTP server into nextBuffer() on a null file stream.

The missing-file test is deterministic and root-safe; the permission test is skipped when running as root (which bypasses the permission check).

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

1 blocking (unresolved from the last round), 1 follow-up, 1 nit

🔴 must be resolved before merge. 🟠 is real but not merge-blocking — either address it here or open an issue to do so later. 🟡 is optional.

  • 🔴 src/cpp/core/http/Response.cpp:862 — unchanged: the error response still says Transfer-Encoding: chunked (existing thread)
  • 🟠 src/cpp/core/http/ResponseTests.cpp:63 — tests don't assert header state, so the malformed-response half stays unguarded
  • 🟡 src/cpp/core/http/ResponseTests.cpp:45 — comment says it creates a temp file; tempFilePath() only generates a path

Resolved since the last round: the missing regression test. ResponseTests.cpp matches the conventions in HeaderTests.cpp, and src/cpp/core/CMakeLists.txt:371 globs *Tests.cpp, so it's picked up by --scope core with no build change needed. The root-skip guard and the permission restore before cleanup are both right.

I read the new test rather than running it — this checkout has no configured build directory, so I haven't executed the suite.

// otherwise the HTTP server calls nextBuffer() on a null file stream
EXPECT_FALSE(response.isStreamResponse());
EXPECT_FALSE(response.getStreamResponse());
EXPECT_EQ(response.statusCode(), status::InternalServerError);

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.

🟠 Both tests pass against the current code, including the Transfer-Encoding bug, so they lock in the crash fix but not the validity of the error response.

Add the header assertion alongside the existing three, in both tests:

Suggested change
EXPECT_EQ(response.statusCode(), status::InternalServerError);
EXPECT_EQ(response.statusCode(), status::InternalServerError);
EXPECT_TRUE(response.headerValue(kTransferEncoding).empty());
Why

isStreamResponse(), getStreamResponse() and statusCode() all already hold today, which is what you want from a regression test for the segfault. But the response those three describe is still unparseable on the wire for the reason in the other thread, and nothing here would notice.

Once setStreamFile stops leaving Transfer-Encoding behind, this assertion is what keeps it that way — the failure mode is a later reorder of the header setup, which no other test in src/cpp/core/http/ would catch.

Comment thread src/cpp/core/http/ResponseTests.cpp Outdated
Comment on lines +45 to +49
// a path that is guaranteed not to exist: create a temp file, then remove it
FilePath missingFile;
ASSERT_FALSE(FilePath::tempFilePath(missingFile));
ASSERT_FALSE(missingFile.remove());
ASSERT_FALSE(missingFile.exists());

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.

🟡 The comment describes creating and removing a temp file, but tempFilePath() only generates a unique path — nothing is created, so the remove() on the next line is a no-op.

Suggested change
// a path that is guaranteed not to exist: create a temp file, then remove it
FilePath missingFile;
ASSERT_FALSE(FilePath::tempFilePath(missingFile));
ASSERT_FALSE(missingFile.remove());
ASSERT_FALSE(missingFile.exists());
// tempFilePath() only generates a unique path, so this never exists
FilePath missingFile;
ASSERT_FALSE(FilePath::tempFilePath(missingFile));
ASSERT_FALSE(missingFile.exists());
Why

FilePath::tempFilePath delegates to uniqueFilePath (src/cpp/shared_core/FilePath.cpp:634), which builds a path from boost::filesystem::unique_path() and returns it — it never touches the filesystem.

The test still passes as written: boost::filesystem::remove on a path that doesn't exist returns false without throwing, so FilePath::remove() hands back Success() and the ASSERT_FALSE is satisfied. So this is cosmetic — the line just reads as though it does something.

…#18864)

When setStreamFile()'s initialize() failed, the response kept the
Transfer-Encoding: chunked header set up for streaming, but setError()
replaces the body with a fixed-length error body. A response advertising
chunked encoding with an unframed body is unparseable, so the error never
reached the client. Remove the Transfer-Encoding header on failure, and
extend the regression tests to assert it is gone.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@samcofer

Copy link
Copy Markdown
Contributor Author

Thanks @gtritchie — both addressed:

🔴 Transfer-Encoding on the error response — good catch. setStreamFile() sets Transfer-Encoding: chunked for streaming, and on initialize() failure setError() replaces the body with a fixed-length one, leaving the response advertising chunked encoding with an unframed body (unparseable). Fixed by removing the Transfer-Encoding header in the failure branch before setError() (51431f0).

🟠 Regression test — src/cpp/core/http/ResponseTests.cpp was pushed just after your review (942404e), so it wasn't in the commit you looked at. It covers a missing file (deterministic, root-safe) and the chmod 000 case (skipped under root). I've now also extended both tests to assert the error response is no longer Transfer-Encoding: chunked.

Verified locally against rstudio-core-tests: both tests pass with the fix and fail with it reverted.

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

LGTM! No new findings. Thanks!

@samcofer
samcofer deployed to e2e-playwright September 17, 2026 20:28 — with GitHub Actions Active
@samcofer
samcofer deployed to e2e-playwright September 17, 2026 20:28 — with GitHub Actions Active
@samcofer
samcofer deployed to e2e-playwright September 17, 2026 20:28 — with GitHub Actions Active
@samcofer
samcofer deployed to e2e-playwright September 17, 2026 20:28 — with GitHub Actions Active
@samcofer
samcofer deployed to e2e-playwright September 17, 2026 20:28 — with GitHub Actions Active
@samcofer
samcofer deployed to e2e-playwright September 17, 2026 20:36 — with GitHub Actions Active
@samcofer
samcofer deployed to e2e-playwright September 17, 2026 20:36 — with GitHub Actions Active
@samcofer
samcofer deployed to e2e-playwright September 17, 2026 20:36 — with GitHub Actions Active
@samcofer
samcofer deployed to e2e-playwright September 17, 2026 20:36 — with GitHub Actions Active
@samcofer
samcofer deployed to e2e-playwright September 17, 2026 20:36 — with GitHub Actions Active
@samcofer
samcofer deployed to e2e-playwright September 17, 2026 20:36 — with GitHub Actions Active
tempFilePath() only generates a unique path; it does not create the file.
Fix the misleading comment and drop the no-op remove() call.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@samcofer

Copy link
Copy Markdown
Contributor Author

@gtritchie Merge whenever you want, I think it's ready now and I can't merge.

@gtritchie
gtritchie merged commit 00b29ec into rstudio:main Sep 17, 2026
4 checks passed
@samcofer
samcofer deployed to e2e-playwright September 17, 2026 21:43 — with GitHub Actions Active
@samcofer
samcofer deployed to e2e-playwright September 17, 2026 21:43 — with GitHub Actions Active
@samcofer
samcofer deployed to e2e-playwright September 17, 2026 21:44 — with GitHub Actions Active
@samcofer
samcofer deployed to e2e-playwright September 17, 2026 21:44 — with GitHub Actions Active
@samcofer
samcofer deployed to e2e-playwright September 17, 2026 21:44 — with GitHub Actions Active
@samcofer
samcofer deployed to e2e-playwright September 17, 2026 21:44 — with GitHub Actions Active
@samcofer
samcofer deployed to e2e-playwright September 17, 2026 21:49 — with GitHub Actions Active
@samcofer
samcofer deployed to e2e-playwright September 17, 2026 21:49 — with GitHub Actions Active
@samcofer
samcofer deployed to e2e-playwright September 17, 2026 21:49 — with GitHub Actions Active
@samcofer
samcofer deployed to e2e-playwright September 17, 2026 21:49 — with GitHub Actions Active
@samcofer
samcofer deployed to e2e-playwright September 17, 2026 21:49 — with GitHub Actions Active
@samcofer
samcofer deployed to e2e-playwright September 17, 2026 21:49 — with GitHub Actions Active

This branch was successfully deployed

1 active deployment
e2e-playwright — 304b11bf Deployed Sep 17, 2026 by samcofer via windows / e2e-desktop-windows (4) #1078
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.

rsession/server crashes (segfault) when downloading a file with no read permission

2 participants