Add app-server file preview read helpers (#61887)

## What changed

Add session helpers to resolve file paths against the source thread's workspace and read preview bytes through `fs/readFile`. Require a workspace on the app-server host, restrict reads to POSIX paths, and reject reads when the workspace changes.

Add text document validation that rejects invalid UTF-8 and control characters other than tabs and line endings.

## Testing

Add a mock WebSocket app-server test covering source-thread metadata requests, relative path resolution, large text reads, invalid UTF-8, binary content, and workspace changes during a read.

GitOrigin-RevId: b51cb227e478d72cba42309828e40bbffff6d847
This commit is contained in:
Felipe Coury
2026-11-16 21:46:24 +00:00
committed by copyberry
parent d5a478e31c
commit 59d8c79789
2 changed files with 203 additions and 0 deletions
+114
View File
@@ -16,7 +16,11 @@ use codex_app_server_protocol::FsWriteFileParams;
use codex_app_server_protocol::FsWriteFileResponse;
use codex_app_server_protocol::JSONRPCRequest;
use codex_app_server_protocol::RequestId;
use codex_protocol::ThreadId;
use codex_utils_absolute_path::AbsolutePathBuf;
use codex_utils_path_uri::LegacyAppPathString;
use codex_utils_path_uri::PathConvention;
use codex_utils_path_uri::PathUri;
use color_eyre::eyre::Result;
use color_eyre::eyre::WrapErr;
use serde::de::DeserializeOwned;
@@ -24,6 +28,102 @@ use serde_json::json;
use uuid::Uuid;
impl AppServerSession {
#[cfg_attr(
not(test),
expect(dead_code, reason = "used by the sidebar viewer in the next change")
)]
pub(crate) async fn resolve_thread_file(
&self,
source: ThreadId,
path: &AppServerPath,
) -> Result<PathUri> {
let cwd = self.preview_workspace(source).await?;
resolve_preview_path(path, &cwd)
}
async fn preview_workspace(&self, source: ThreadId) -> Result<PathUri> {
// Read only the environment metadata. Other paths in thread/read may be native
// to a different operating system from the TUI.
#[derive(serde::Deserialize)]
struct Workspace {
environments: Option<Vec<codex_app_server_protocol::ThreadEnvironment>>,
}
#[derive(serde::Deserialize)]
struct Response {
thread: Workspace,
}
let request_id = RequestId::String(Uuid::new_v4().to_string());
let response: Response = self
.request_handle()
.request_typed(ClientRequest::ThreadRead {
request_id,
params: codex_app_server_protocol::ThreadReadParams {
thread_id: source.to_string(),
include_turns: false,
},
})
.await?;
let environments = response.thread.environments.as_deref();
environments
.filter(|_| {
crate::windows_sandbox::host_from_environments(environments)
== crate::app::WindowsSandboxHost::Local
})
.and_then(|envs| envs.first())
.and_then(|primary| primary.cwd.to_inferred_path_uri())
.ok_or_else(|| {
color_eyre::eyre::eyre!("File previews require a workspace on the app-server host")
})
}
#[cfg_attr(
not(test),
expect(dead_code, reason = "used by the sidebar viewer in the next change")
)]
pub(crate) async fn read_document(
&self,
source: ThreadId,
original_path: &AppServerPath,
resolved_path: &PathUri,
) -> Result<String> {
let bytes = self
.read_preview_file(source, original_path, resolved_path)
.await?;
let text = String::from_utf8(bytes).wrap_err("Not a UTF-8 text file")?;
color_eyre::eyre::ensure!(
!text
.chars()
.any(|ch| ch.is_control() && !matches!(ch, '\t' | '\n' | '\r')),
"Not a text file"
);
Ok(text)
}
pub(crate) async fn read_preview_file(
&self,
source: ThreadId,
original_path: &AppServerPath,
resolved_path: &PathUri,
) -> Result<Vec<u8>> {
color_eyre::eyre::ensure!(
resolved_path.infer_path_convention() == Some(PathConvention::Posix),
"File previews on Windows hosts are unavailable"
);
let workspace = self.preview_workspace(source).await?;
color_eyre::eyre::ensure!(
resolve_preview_path(original_path, &workspace)? == *resolved_path,
"Workspace changed. Reopen the file to try again."
);
// fs/readFile follows symlinks. Only a server-side open can enforce no-follow.
let path = AppServerPath::from_app_server(resolved_path.inferred_native_path_string());
let bytes = self.file_system().fs_read_file_path(&path).await?;
color_eyre::eyre::ensure!(
self.preview_workspace(source).await? == workspace,
"Workspace changed. Reopen the file to try again."
);
Ok(bytes)
}
pub(crate) fn file_system(&self) -> AppServerFileSystem {
AppServerFileSystem {
request_handle: self.request_handle(),
@@ -31,6 +131,16 @@ impl AppServerSession {
}
}
fn resolve_preview_path(path: &AppServerPath, cwd: &PathUri) -> Result<PathUri> {
let path = LegacyAppPathString::from_string(path.as_str())
.resolve_against(cwd, /*user_home_dir*/ None)?;
color_eyre::eyre::ensure!(
!path.to_url().has_host(),
"File previews require a path on the app-server host"
);
Ok(path)
}
pub(crate) struct AppServerFileSystem {
pub(crate) request_handle: AppServerRequestHandle,
}
@@ -148,3 +258,7 @@ impl AppServerFileSystem {
}
}
}
#[cfg(test)]
#[path = "fs_preview_tests.rs"]
mod preview_tests;
@@ -0,0 +1,89 @@
#![cfg(test)]
use super::*;
use crate::app_server_session::ThreadParamsMode;
use crate::backend_id::BackendId;
use codex_app_server_protocol::JSONRPCMessage;
use futures::SinkExt;
use futures::StreamExt;
use pretty_assertions::assert_eq;
use tokio::net::TcpListener;
use tokio_tungstenite::tungstenite::Message;
#[tokio::test]
async fn remote_preview_requests_the_source_thread_and_validates_text() -> Result<()> {
let listener = TcpListener::bind("127.0.0.1:0").await?;
let endpoint = crate::resolve_remote_addr(&format!("ws://{}", listener.local_addr()?))?;
let source = ThreadId::new();
let text = "\tfirst\r\nsecond\n".repeat(80_000);
let encoded_text = STANDARD.encode(&text);
let server = tokio::spawn(async move {
let (stream, _) = listener.accept().await?;
let mut socket = tokio_tungstenite::accept_async(stream).await?;
let mut workspace = "/workspace";
let mut responses = [
encoded_text,
STANDARD.encode([0xff]),
STANDARD.encode(b"bad\0"),
STANDARD.encode("stale"),
]
.into_iter();
while let Some(Ok(Message::Text(wire))) = socket.next().await {
let JSONRPCMessage::Request(request) = serde_json::from_str(&wire)? else {
continue;
};
let mut reply = match request.method.as_str() {
"initialize" => json!({"result": {"userAgent": "reader-test/1"}}),
"thread/read" => {
let params: codex_app_server_protocol::ThreadReadParams =
serde_json::from_value(request.params.expect("thread params"))?;
assert_eq!(
params,
codex_app_server_protocol::ThreadReadParams {
thread_id: source.to_string(),
include_turns: false,
}
);
json!({"result": {"thread": {"environments": [
{"environmentId": "local", "cwd": workspace, "runtimeWorkspaceRoots": []}
]}}})
}
"fs/readFile" => {
assert_eq!(request.params, Some(json!({"path": "/workspace/notes.md"})));
let data_base64 = responses.next().expect("one preview request per case");
if responses.len() == 0 {
workspace = "/other";
}
json!({"result": {"dataBase64": data_base64}})
}
method => panic!("unexpected RPC: {method}"),
};
reply["id"] = json!(request.id);
socket.send(Message::Text(reply.to_string().into())).await?;
}
assert!(responses.next().is_none());
Ok::<_, color_eyre::Report>(())
});
let session = AppServerSession::new(
BackendId::new(),
crate::connect_remote_app_server(endpoint).await?,
ThreadParamsMode::Remote,
);
let path = AppServerPath::from_app_server("notes.md");
let resolved = session.resolve_thread_file(source, &path).await?;
assert_eq!(session.read_document(source, &path, &resolved).await?, text);
for expected in [
"Not a UTF-8 text file",
"Not a text file",
"Workspace changed",
] {
let error = session
.read_document(source, &path, &resolved)
.await
.unwrap_err();
assert!(format!("{error:#}").contains(expected), "{error:#}");
}
session.shutdown().await?;
server.await??;
Ok(())
}