interplane.Get Omega
Read in your language

aien-sovereign-core-mv

AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.

Brought by drakestapleton

branch main Branches Tags History

aien-sovereign-core-mv / crates / aien-drive / tests / path_jail.rs

367 lines · 11861 bytes Raw Blame History
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! File tools stay inside the command root.
17
18use aien_drive::{CommandPolicy, Config};
19use serde_json::json;
20use std::os::unix::fs::symlink;
21use std::path::{Path, PathBuf};
22use std::sync::atomic::{AtomicU64, Ordering};
23use std::time::{Duration, SystemTime, UNIX_EPOCH};
24
25static N: AtomicU64 = AtomicU64::new(0);
26
27struct Tmp(PathBuf);
28
29impl Tmp {
30 fn new() -> Self {
31 let n = N.fetch_add(1, Ordering::Relaxed);
32 let p = std::env::temp_dir().join(format!(
33 "aien-drive-paths-{}-{}-{n}",
34 std::process::id(),
35 SystemTime::now()
36 .duration_since(UNIX_EPOCH)
37 .unwrap()
38 .as_nanos()
39 ));
40 std::fs::create_dir_all(&p).unwrap();
41 Self(p)
42 }
43}
44
45impl Drop for Tmp {
46 fn drop(&mut self) {
47 let _ = std::fs::remove_dir_all(&self.0);
48 }
49}
50
51fn cfg(root: &Path) -> Config {
52 Config {
53 endpoint: String::new(),
54 model: String::new(),
55 cortex_endpoint: String::new(),
56 cortex_space: "aien-sources".into(),
57 request_timeout: Duration::from_secs(5),
58 command: CommandPolicy {
59 root: root.to_path_buf(),
60 ..CommandPolicy::default()
61 },
62 cortex_token: None,
63 }
64}
65
66fn tool(root: &Path, name: &str, args: serde_json::Value) -> serde_json::Value {
67 aien_drive::dispatch_tool(&cfg(root), name, &args)
68}
69
70#[test]
71fn parent_traversal_is_rejected() {
72 let tmp = Tmp::new();
73 let outside = tmp
74 .0
75 .parent()
76 .unwrap()
77 .join(format!("outside-{}", std::process::id()));
78 std::fs::write(&outside, "secret-outside").unwrap();
79 let rel = format!("../{}", outside.file_name().unwrap().to_string_lossy());
80 let res = tool(tmp.0.as_path(), "view_file", json!({"path": rel}));
81 assert!(res.get("error").is_some(), "{res}");
82 assert!(
83 !res.to_string().contains("secret-outside"),
84 "escaped read returned the outside file: {res}"
85 );
86 let _ = std::fs::remove_file(&outside);
87}
88
89#[test]
90fn symlink_that_leaves_the_root_is_rejected() {
91 let tmp = Tmp::new();
92 let outside = tmp
93 .0
94 .parent()
95 .unwrap()
96 .join(format!("sym-out-{}", std::process::id()));
97 std::fs::write(&outside, "secret-symlink").unwrap();
98 symlink(&outside, tmp.0.join("escape")).unwrap();
99 let res = tool(tmp.0.as_path(), "view_file", json!({"path": "escape"}));
100 assert!(res.get("error").is_some(), "{res}");
101 assert!(
102 !res.to_string().contains("secret-symlink"),
103 "symlink read left the root: {res}"
104 );
105 let _ = std::fs::remove_file(&outside);
106}
107
108#[test]
109fn absolute_path_inside_the_root_is_readable() {
110 let tmp = Tmp::new();
111 let file = tmp.0.join("inside.txt");
112 std::fs::write(&file, "inside-ok\n").unwrap();
113 let res = tool(
114 tmp.0.as_path(),
115 "view_file",
116 json!({"path": file.display().to_string()}),
117 );
118 assert_eq!(res["lines"], "inside-ok\n", "{res}");
119}
120
121#[test]
122fn git_hooks_write_is_refused() {
123 let tmp = Tmp::new();
124 let hook = tmp.0.join(".git/hooks/pre-commit");
125 let res = tool(
126 tmp.0.as_path(),
127 "write_to_file",
128 json!({"path": ".git/hooks/pre-commit", "content": "echo hooked\n"}),
129 );
130 assert!(res.get("error").is_some(), "{res}");
131 assert!(!hook.exists(), "hook file was written");
132}
133
134#[test]
135fn overwrite_defaults_to_false() {
136 let tmp = Tmp::new();
137 std::fs::write(tmp.0.join("keep.txt"), "original").unwrap();
138 let res = tool(
139 tmp.0.as_path(),
140 "write_to_file",
141 json!({"path": "keep.txt", "content": "replaced"}),
142 );
143 assert!(res.get("error").is_some(), "{res}");
144 assert_eq!(
145 std::fs::read_to_string(tmp.0.join("keep.txt")).unwrap(),
146 "original"
147 );
148}
149
150#[test]
151fn protected_write_names_are_refused() {
152 let tmp = Tmp::new();
153 for path in [
154 ".ssh/config",
155 "build.rs",
156 ".bashrc",
157 ".profile",
158 ".zshrc",
159 ".GIT/hooks/pre-commit",
160 ".envrc",
161 "Makefile",
162 ".gitattributes",
163 ".cargo/config.toml",
164 ".github/workflows/ci.yml",
165 ] {
166 let res = tool(
167 tmp.0.as_path(),
168 "write_to_file",
169 json!({"path": path, "content": "x", "overwrite": true}),
170 );
171 assert!(res.get("error").is_some(), "{path}: {res}");
172 assert!(!tmp.0.join(path).exists(), "{path} was written");
173 }
174}
175
176#[test]
177fn write_through_symlink_into_git_is_refused() {
178 let tmp = Tmp::new();
179 std::fs::create_dir_all(tmp.0.join(".git")).unwrap();
180 std::fs::write(tmp.0.join(".git/config"), "safe").unwrap();
181 symlink(tmp.0.join(".git/config"), tmp.0.join("alias")).unwrap();
182 let res = tool(
183 tmp.0.as_path(),
184 "write_to_file",
185 json!({"path": "alias", "content": "pwned", "overwrite": true}),
186 );
187 assert!(res.get("error").is_some(), "{res}");
188 assert_eq!(
189 std::fs::read_to_string(tmp.0.join(".git/config")).unwrap(),
190 "safe"
191 );
192}
193
194#[test]
195fn hardlink_to_git_config_is_refused() {
196 let tmp = Tmp::new();
197 std::fs::create_dir_all(tmp.0.join(".git")).unwrap();
198 std::fs::write(tmp.0.join(".git/config"), "safe").unwrap();
199 std::fs::hard_link(tmp.0.join(".git/config"), tmp.0.join("notes.txt")).unwrap();
200 let res = tool(
201 tmp.0.as_path(),
202 "write_to_file",
203 json!({"path": "notes.txt", "content": "pwned", "overwrite": true}),
204 );
205 assert!(res.get("error").is_some(), "{res}");
206 assert_eq!(
207 std::fs::read_to_string(tmp.0.join(".git/config")).unwrap(),
208 "safe"
209 );
210}
211
212#[test]
213fn any_hardlinked_file_is_not_written() {
214 // Hooks, rc files, worktree configs and files outside the root are not
215 // on any fixed list. A second link to the inode is enough to refuse.
216 let tmp = Tmp::new();
217 std::fs::create_dir_all(tmp.0.join(".git/hooks")).unwrap();
218 std::fs::write(tmp.0.join(".git/hooks/pre-commit"), "#!/bin/sh\n").unwrap();
219 std::fs::hard_link(tmp.0.join(".git/hooks/pre-commit"), tmp.0.join("hook.txt")).unwrap();
220 let outside = tmp
221 .0
222 .parent()
223 .unwrap()
224 .join(format!("link-out-{}", std::process::id()));
225 std::fs::write(&outside, "outside").unwrap();
226 let linked = std::fs::hard_link(&outside, tmp.0.join("out.txt")).is_ok();
227 let mut targets = vec!["hook.txt"];
228 if linked {
229 targets.push("out.txt");
230 }
231 for name in targets {
232 let res = tool(
233 tmp.0.as_path(),
234 "write_to_file",
235 json!({"path": name, "content": "pwned", "overwrite": true}),
236 );
237 assert!(res.get("error").is_some(), "{name}: {res}");
238 }
239 assert_eq!(
240 std::fs::read_to_string(tmp.0.join(".git/hooks/pre-commit")).unwrap(),
241 "#!/bin/sh\n"
242 );
243 assert_eq!(std::fs::read_to_string(&outside).unwrap(), "outside");
244 let _ = std::fs::remove_file(&outside);
245 // Control: a file with one link is still overwritten.
246 std::fs::write(tmp.0.join("plain.txt"), "old").unwrap();
247 let res = tool(
248 tmp.0.as_path(),
249 "write_to_file",
250 json!({"path": "plain.txt", "content": "new", "overwrite": true}),
251 );
252 assert!(res.get("error").is_none(), "{res}");
253 assert_eq!(
254 std::fs::read_to_string(tmp.0.join("plain.txt")).unwrap(),
255 "new"
256 );
257}
258
259#[test]
260fn intermediate_symlink_is_refused() {
261 let tmp = Tmp::new();
262 let outside = tmp
263 .0
264 .parent()
265 .unwrap()
266 .join(format!("mid-out-{}", std::process::id()));
267 std::fs::create_dir(&outside).unwrap();
268 std::fs::write(outside.join("secret.txt"), "secret-mid").unwrap();
269 symlink(&outside, tmp.0.join("mid")).unwrap();
270 let res = tool(
271 tmp.0.as_path(),
272 "view_file",
273 json!({"path": "mid/secret.txt"}),
274 );
275 assert!(res.get("error").is_some(), "{res}");
276 assert!(!res.to_string().contains("secret-mid"), "{res}");
277 let _ = std::fs::remove_dir_all(&outside);
278}
279
280#[test]
281fn absolute_path_outside_the_root_is_rejected() {
282 let tmp = Tmp::new();
283 let outside = tmp
284 .0
285 .parent()
286 .unwrap()
287 .join(format!("abs-out-{}", std::process::id()));
288 std::fs::write(&outside, "secret-abs").unwrap();
289 let res = tool(
290 tmp.0.as_path(),
291 "view_file",
292 json!({"path": outside.display().to_string()}),
293 );
294 assert!(res.get("error").is_some(), "{res}");
295 assert!(!res.to_string().contains("secret-abs"), "{res}");
296 let _ = std::fs::remove_file(&outside);
297}
298
299#[test]
300fn create_dir_into_git_is_refused() {
301 let tmp = Tmp::new();
302 let res = tool(tmp.0.as_path(), "create_dir", json!({"path": ".git/hooks"}));
303 assert!(res.get("error").is_some(), "{res}");
304 assert!(!tmp.0.join(".git/hooks").exists());
305}
306
307#[test]
308fn list_dir_and_grep_cannot_leave_the_root() {
309 let tmp = Tmp::new();
310 std::fs::write(tmp.0.join("notes.txt"), "needle\n").unwrap();
311 let listed = tool(tmp.0.as_path(), "list_dir", json!({"path": ".."}));
312 assert!(listed.get("error").is_some(), "{listed}");
313 let searched = tool(
314 tmp.0.as_path(),
315 "grep_search",
316 json!({"query": "root", "path": ".."}),
317 );
318 assert!(searched.get("error").is_some(), "{searched}");
319 let ok = tool(
320 tmp.0.as_path(),
321 "grep_search",
322 json!({"query": "needle", "path": "notes.txt"}),
323 );
324 assert!(
325 ok["matches"].as_str().unwrap_or("").contains("needle"),
326 "{ok}"
327 );
328}
329
330#[test]
331fn think_blocks_are_not_a_tool_channel() {
332 let hidden = "<think><tool_call>{\"name\":\"goal\",\"arguments\":{\"action\":\"done\"}}</tool_call></think>";
333 assert!(aien_drive::extract_tool_calls(hidden).is_empty());
334 let mixed = "<think>no</think>\n<tool_call>{\"name\":\"goal\",\"arguments\":{\"action\":\"done\"}}</tool_call>";
335 let calls = aien_drive::extract_tool_calls(mixed);
336 assert_eq!(calls.len(), 1, "{calls:?}");
337 assert_eq!(calls[0]["name"], "goal");
338}
339
340#[test]
341fn unclosed_or_orphan_think_blocks_are_not_a_tool_channel() {
342 let call = "<tool_call>{\"name\":\"goal\",\"arguments\":{\"action\":\"done\"}}</tool_call>";
343 // Truncated reasoning: the opener never closes.
344 assert!(aien_drive::extract_tool_calls(&format!("<think>plan {call}")).is_empty());
345 assert!(aien_drive::extract_tool_calls(&format!("<THINKING>plan {call}")).is_empty());
346 // A chat template that opens the block in the prompt leaves only the
347 // closer in the content.
348 assert!(aien_drive::extract_tool_calls(&format!("plan {call}</think>\nanswer")).is_empty());
349 let after = aien_drive::extract_tool_calls(&format!("plan</think>\n{call}"));
350 assert_eq!(after.len(), 1, "{after:?}");
351}
352
353#[test]
354fn grep_pattern_is_not_a_flag() {
355 let tmp = Tmp::new();
356 std::fs::write(tmp.0.join("notes.txt"), "keep -n here\n").unwrap();
357 let res = tool(
358 tmp.0.as_path(),
359 "grep_search",
360 json!({"query": "-n", "path": "notes.txt"}),
361 );
362 let matches = res["matches"].as_str().unwrap_or("");
363 assert!(
364 matches.contains("-n"),
365 "pattern was treated as a grep flag: {res}"
366 );
367}