Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,15 @@ stay at 1 and keep their refs, and no ref of a version-2 bundle predates
the rule, so nothing moved. `Bundle::required_version` decides it, and both
cases are pinned. Index entries name their first session's agent.

**A bundle's session ids are untrusted input.** Both adapters build the
restored file's name from the id, and `resumeCommand` puts it in a shell
line, so an id of `../../x` or `/x` chose where `restore` wrote and `;` what
the command ran. `agent::is_safe_session_id` (non-empty, `[A-Za-z0-9._-]`,
no leading `.`, at most 128 bytes) is checked when a bundle is unpacked, so a
bundle failing it is `bundle-unreadable` as a whole, and by both
`find_transcript`s, whose ids come from edit records in the same shared
store. Real ids are UUIDs. The bad id is never echoed back.

**Never the matched text.** Redaction is reported as `{"<rule>": <count>}`
plus a `redacted` total, exactly as much as the prose summary carries and no
more — `save`'s stdout lands in the next agent's transcript. The one field
Expand Down
14 changes: 14 additions & 0 deletions src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ pub struct SessionMeta {
pub other_cwds: Vec<String>,
}

/// Whether a session id can be put into a file name, and a resume command,
/// as it is. Every adapter files a transcript under a name built from its id,
/// and a bundle's or a store's id is someone else's input: `../x` or `/x`
/// would choose where `restore` writes, and `;` what a resume command runs.
/// Both agents' ids are UUIDs, so an allowlist costs nothing real.
pub fn is_safe_session_id(id: &str) -> bool {
!id.is_empty()
&& id.len() <= 128
&& !id.starts_with('.')
&& id
.bytes()
.all(|b| b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.'))
}

/// What the transcript says about a turn: the record that began it and the
/// last record written. Each id is whatever the agent's transcript uses to
/// name a position in itself — a record uuid for Claude Code, a turn id for
Expand Down
10 changes: 9 additions & 1 deletion src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,16 @@ pub fn unpack_bundle(data: &[u8]) -> Result<Bundle, String> {
.ok_or_else(|| format!("'{}'", key))
};
let optional = |key: &str| session.get(key).and_then(Value::as_str).map(str::to_string);
let session_id = required("sessionId")?;
// Not echoed: it is whatever the bundle's author chose to put there.
if !crate::agent::is_safe_session_id(&session_id) {
return Err(format!(
"session {} has an id that isn't a safe file name",
sessions.len() + 1
));
}
sessions.push(BundleSession {
session_id: required("sessionId")?,
session_id,
agent: optional("agent"),
agent_version: optional("agentVersion"),
cwd: required("cwd")?,
Expand Down
9 changes: 9 additions & 0 deletions src/claude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,10 @@ pub fn find_transcript(
session_id: &str,
store: Option<&dyn Store>,
) -> Result<Option<String>, StoreError> {
// The id comes from an edit record, which a shared store also supplies.
if !crate::agent::is_safe_session_id(session_id) {
return Ok(None);
}
let root = projects_dir(None);
if Path::new(&root).is_dir() {
for project in list_dir(&root) {
Expand Down Expand Up @@ -353,6 +357,11 @@ pub fn find_transcript(
.next()
.unwrap_or("")
.to_string();
// A listed key is the store's to choose too: `..` would step out
// of the cache.
if project.is_empty() || project.starts_with('.') || project.contains('\\') {
continue;
}
let data = match store.get(&key) {
Ok(data) => data,
Err(StoreError::Missing(_)) => return Ok(None),
Expand Down
4 changes: 4 additions & 0 deletions src/codex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,10 @@ pub fn find_transcript(
session_id: &str,
store: Option<&dyn Store>,
) -> Result<Option<String>, StoreError> {
// The id comes from an edit record, which a shared store also supplies.
if !crate::agent::is_safe_session_id(session_id) {
return Ok(None);
}
if let Some(path) = rollout_for(session_id) {
return Ok(Some(path));
}
Expand Down
31 changes: 31 additions & 0 deletions tests/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,34 @@ fn a_bundle_whose_agent_is_not_claude_is_version_2() {
bundle.agent = "codex".into();
assert_eq!(bundle.required_version(), 2);
}

#[test]
fn a_session_id_must_be_a_plain_file_name() {
use loch::agent::is_safe_session_id;
for good in [
"11111111-2222-4333-8444-555555555555",
"abc",
"old-id",
"a.b_c",
] {
assert!(is_safe_session_id(good), "{}", good);
}
for bad in [
"",
"..",
".hidden",
"../x",
"/etc/x",
"a/b",
"a\\b",
"x; rm -rf ~",
"a b",
"a\0b",
] {
assert!(!is_safe_session_id(bad), "{:?}", bad);
}
let mut bundle = sample_bundle(None);
bundle.sessions[0].session_id = "../../x".into();
let packed = pack_bundle(&bundle).unwrap();
assert!(unpack_bundle(&packed).is_err());
}
Loading
Loading