Repository navigation
Fix crash when downloading a file with no read permission (#18864) - #18866
Conversation
) 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
left a comment
There was a problem hiding this comment.
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 saysTransfer-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(); |
There was a problem hiding this comment.
🔴 The 500 still carries Transfer-Encoding: chunked from line 836, so the error body goes out unframed and the client can't parse it.
| 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.
There was a problem hiding this comment.
🔴 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(); |
There was a problem hiding this comment.
🟠 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.
…#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]>
|
Added
The missing-file test is deterministic and root-safe; the permission test is skipped when running as root (which bypasses the permission check). |
gtritchie
left a comment
There was a problem hiding this comment.
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 saysTransfer-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); |
There was a problem hiding this comment.
🟠 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:
| 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.
| // 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()); |
There was a problem hiding this comment.
🟡 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.
| // 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]>
|
Thanks @gtritchie — both addressed: 🔴 Transfer-Encoding on the error response — good catch. 🟠 Regression test — Verified locally against |
gtritchie
left a comment
There was a problem hiding this comment.
LGTM! No new findings. Thanks!
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]>
|
@gtritchie Merge whenever you want, I think it's ready now and I can't merge. |
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(), whenstreamResponse_->initialize()fails (opening the file returnsEACCES),setError()is called butstreamResponse_is left set.Response::isStreamResponse()(static_cast<bool>(streamResponse_)) therefore still returnstrue, soAsyncConnectionImplroutes the response toStreamWriter, which callsFileStreamResponse::nextBuffer(). That dereferencesfileStream_atResponse.cpp:89— butfileStream_was never opened, so it is null → segfault.Fix
streamResponse_oninitialize()failure — the general fix. It stops the null deref and makesisStreamResponse()returnfalseso the HTTP error body is actually sent. Covers every caller ofsetStreamFile().FileStreamResponse::nextBuffer().Platform scope
Affects the Unix/Linux gzip streaming path. Windows uses a different code path.
Testing
Manual repro: create a
chmod 000file, 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