Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(agent): reject malformed file_read offsets, saturate artifact page arithmetic (#6284)

M3gA-Mind2a9dafd70533
diff --git a/crates/openhuman-core/src/agent/harness/tool_result_artifacts/mod.rs b/crates/openhuman-core/src/agent/harness/tool_result_artifacts/mod.rs
index f5e5d44621..39cd57ce88 100644
--- a/crates/openhuman-core/src/agent/harness/tool_result_artifacts/mod.rs
+++ b/crates/openhuman-core/src/agent/harness/tool_result_artifacts/mod.rs
@@ -84,7 +84,8 @@ pub(crate) fn page_artifact_read(
return content;
}
let start = read.offset;
- let total = start + content.len();
+ // Saturating: the offset comes from the model's arguments.
+ let total = start.saturating_add(content.len());
let with_path = |next: usize| {
format!(
"\n\n[artifact page: bytes {start}..{next} of {total}. Continue with file_read {{\"path\":\"{}\",\"offset\":{next}}}]",
@@ -109,9 +110,9 @@ pub(crate) fn page_artifact_read(
};
let cut = crate::util::floor_char_boundary(&content, budget_bytes - longest);
let trailer = if use_path {
- with_path(start + cut)
+ with_path(start.saturating_add(cut))
} else {
- without_path(start + cut)
+ without_path(start.saturating_add(cut))
};
format!("{}{trailer}", &content[..cut])
}
diff --git a/crates/openhuman-core/src/agent/harness/tool_result_artifacts/mod_tests.rs b/crates/openhuman-core/src/agent/harness/tool_result_artifacts/mod_tests.rs
index 1e8c7e55b8..b4e4bc57b8 100644
--- a/crates/openhuman-core/src/agent/harness/tool_result_artifacts/mod_tests.rs
+++ b/crates/openhuman-core/src/agent/harness/tool_result_artifacts/mod_tests.rs
@@ -350,3 +350,18 @@ fn artifact_read_target_follows_only_use_skill_into_a_wrapped_tool() {
);
}
}
+
+#[test]
+fn a_page_near_the_maximum_offset_does_not_overflow() {
+ let read = ArtifactRead {
+ path: "artifacts/tool-results/s/shell/c.txt".to_string(),
+ offset: usize::MAX - 10,
+ };
+ // The offset comes straight from the model's arguments, so the page
+ // arithmetic must not overflow on an absurd one.
+ let page = std::panic::catch_unwind(|| page_artifact_read("q".repeat(5_000), &read, 1_000));
+ assert!(
+ page.is_ok(),
+ "an offset near usize::MAX must not overflow the page arithmetic"
+ );
+}
diff --git a/crates/openhuman-core/src/tools/impl/filesystem/file_read.rs b/crates/openhuman-core/src/tools/impl/filesystem/file_read.rs
index 7647f1a153..2aa3c8c264 100644
--- a/crates/openhuman-core/src/tools/impl/filesystem/file_read.rs
+++ b/crates/openhuman-core/src/tools/impl/filesystem/file_read.rs
@@ -133,11 +133,21 @@ impl FileReadTool {
.unwrap_or(std::time::SystemTime::UNIX_EPOCH);
file_state::record_read(&agent_id, resolved_path, mtime, false);
}
- let offset = args
- .get("offset")
- .and_then(serde_json::Value::as_u64)
- .and_then(|o| usize::try_from(o).ok())
- .unwrap_or(0);
+ // An absent or null offset reads from the start; anything else
+ // that is not a non-negative integer is rejected, so a
+ // malformed continuation never silently re-reads the file from
+ // byte 0.
+ let offset = match args.get("offset") {
+ None | Some(serde_json::Value::Null) => 0,
+ Some(value) => match value.as_u64().and_then(|o| usize::try_from(o).ok()) {
+ Some(offset) => offset,
+ None => {
+ return Ok(ToolResult::error(format!(
+ "offset must be a non-negative integer byte offset, got {value}"
+ )))
+ }
+ },
+ };
if offset == 0 {
return Ok(ToolResult::success(contents));
}
diff --git a/crates/openhuman-core/src/tools/impl/filesystem/file_read_tests.rs b/crates/openhuman-core/src/tools/impl/filesystem/file_read_tests.rs
index 7b72f66961..6efc482de2 100644
--- a/crates/openhuman-core/src/tools/impl/filesystem/file_read_tests.rs
+++ b/crates/openhuman-core/src/tools/impl/filesystem/file_read_tests.rs
@@ -349,3 +349,34 @@ async fn file_read_rejects_an_offset_inside_a_multibyte_character() {
.unwrap();
assert_eq!(boundary.output(), " world");
}
+
+#[tokio::test]
+async fn file_read_rejects_an_offset_that_is_not_a_non_negative_integer() {
+ let dir = tempfile::tempdir().unwrap();
+ tokio::fs::write(dir.path().join("offsets.txt"), "hello world")
+ .await
+ .unwrap();
+ let tool = FileReadTool::new(test_security(dir.path().to_path_buf()));
+
+ for bad in [json!(-5), json!("6"), json!(1.5)] {
+ let result = tool
+ .execute(json!({"path": "offsets.txt", "offset": bad.clone()}))
+ .await
+ .unwrap();
+ assert!(
+ result.is_error,
+ "offset {bad} must be rejected, not treated as 0: got {:?}",
+ result.output()
+ );
+ }
+
+ let null_offset = tool
+ .execute(json!({"path": "offsets.txt", "offset": null}))
+ .await
+ .unwrap();
+ assert_eq!(
+ null_offset.output(),
+ "hello world",
+ "a null offset reads from the start"
+ );
+}