Skip to content

Commit a35141c

Browse files
committed
fix(harness): apply the namespace boundary to aliased absolute paths
An absolute path that reaches the workspace through a link is spelled outside the root, so the own-namespace check never ran: read, exists, downloadFiles and grep returned a sibling's file, edit rewrote it and move carried it out. Resolve the candidate and apply the same rule when it physically lands under the root, which is what the check already does for relative keys and for paths spelled inside the root.
1 parent d38bd66 commit a35141c

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

‎agentscope-harness/src/main/java/io/agentscope/harness/agent/filesystem/local/LocalFilesystem.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,15 @@ private Path resolveRooted(RuntimeContext rc, String effectiveKey) {
879879
requireOwnNamespace(rc, normalized);
880880
return normalized;
881881
}
882+
// A link can spell a workspace path from outside the root. The boundary applies to
883+
// whatever physically resolves under it, the same way it does for relative keys.
884+
if (namespaceBoundary && namespaceFactory != null) {
885+
Path physical = physicalPath(normalized);
886+
if (physical.isAbsolute() && physical.startsWith(physicalCwd())) {
887+
requireOwnNamespace(rc, normalized);
888+
return normalized;
889+
}
890+
}
882891
if (pathPolicy.isAllowed(normalized)) {
883892
return normalized;
884893
}

‎agentscope-harness/src/test/java/io/agentscope/harness/agent/filesystem/LocalFilesystemProjectEnumerationBoundaryTest.java‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,58 @@ void aliasedAncestor_cannotDeleteOrMoveTheHiddenSubtree() throws Exception {
229229
"unlinking the alias touched the namespaces");
230230
}
231231

232+
/**
233+
* The same alias also spells a file that is physically inside the workspace from outside
234+
* it. The boundary is about where a path lands, so the aliased spelling must reach no
235+
* further than the plain one: read-style calls come back empty and edits do not land.
236+
*/
237+
@Test
238+
void aliasedAbsolutePath_cannotReachAnotherNamespace() throws Exception {
239+
Path workspace = project.resolve("box/workspace");
240+
Files.createDirectories(workspace.resolve("alice"));
241+
Path memory = workspace.resolve("alice/MEMORY.md");
242+
Files.writeString(memory, "alice-secret-memory");
243+
Path alias = project.resolve("alias");
244+
try {
245+
Files.createSymbolicLink(alias, project);
246+
} catch (IOException | UnsupportedOperationException unsupported) {
247+
Assumptions.abort("symbolic links not supported here: " + unsupported.getMessage());
248+
}
249+
String aliasedFile = alias.resolve("box/workspace/alice/MEMORY.md").toString();
250+
String aliasedDir = alias.resolve("box/workspace/alice").toString();
251+
String stolen = workspace.resolve("bob/stolen.md").toString();
252+
RuntimeContext bob = caller("bob");
253+
254+
for (boolean projectWritable : new boolean[] {false, true}) {
255+
AbstractFilesystem fs =
256+
new LocalFilesystemSpec()
257+
.project(project)
258+
.projectWritable(projectWritable)
259+
.toFilesystem(workspace, rc -> List.of(rc.getUserId()));
260+
assertFalse(
261+
fs.read(bob, aliasedFile, 0, 100).isSuccess(),
262+
"read() followed an alias into a sibling namespace");
263+
assertFalse(
264+
fs.exists(bob, aliasedFile),
265+
"exists() followed an alias into a sibling namespace");
266+
assertFalse(
267+
fs.downloadFiles(bob, List.of(aliasedFile)).get(0).isSuccess(),
268+
"downloadFiles() followed an alias into a sibling namespace");
269+
assertTrue(
270+
fs.grep(bob, "alice-secret-memory", aliasedDir, null).matches().isEmpty(),
271+
"grep() followed an alias into a sibling namespace");
272+
assertFalse(
273+
fs.edit(bob, aliasedFile, "alice-secret-memory", "pwned", false).isSuccess(),
274+
"edit() rewrote a sibling namespace through an alias");
275+
assertEquals("alice-secret-memory", Files.readString(memory));
276+
assertFalse(
277+
fs.move(bob, aliasedFile, stolen).isSuccess(),
278+
"move() carried a sibling namespace file out through an alias");
279+
assertTrue(Files.isRegularFile(memory));
280+
assertFalse(Files.exists(workspace.resolve("bob/stolen.md")));
281+
}
282+
}
283+
232284
/**
233285
* The ripgrep path of {@code grep} aligns the process working directory with the search
234286
* root: {@code --glob} patterns match against paths relative to the working directory,

0 commit comments

Comments
 (0)