Skip to content

Commit d38bd66

Browse files
committed
fix(harness): refuse an aliased ancestor of the hidden workspace
The ancestor guard compared normalized spellings, so a link to a project directory spelled the same physical path differently and the walk underneath it ran. Compare the resolved physical path as well. The move route had the same hole without a link: per-project moves were dispatched to the workspace layer, which never hides the workspace, so the guard in the project layer was never consulted. Route project-to-project moves through the project layer and keep workspace paths on the overlay. Refs #4069
1 parent f30dc70 commit d38bd66

3 files changed

Lines changed: 70 additions & 5 deletions

File tree

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,26 @@ public GlobResult glob(RuntimeContext runtimeContext, String pattern, String pat
234234

235235
@Override
236236
public WriteResult move(RuntimeContext runtimeContext, String fromPath, String toPath) {
237-
return super.move(
238-
runtimeContext,
239-
toWorkspaceRelativePath(runtimeContext, fromPath),
240-
toWorkspaceRelativePath(runtimeContext, toPath));
237+
String from = toWorkspaceRelativePath(runtimeContext, fromPath);
238+
String to = toWorkspaceRelativePath(runtimeContext, toPath);
239+
if (underWorkspace(runtimeContext, fromPath) || underWorkspace(runtimeContext, toPath)) {
240+
return super.move(runtimeContext, from, to);
241+
}
242+
// Both ends are project paths. Route them through the project layer, which is the one
243+
// that knows the workspace subtree is hidden; an aliased ancestor is refused there.
244+
return projectFs.move(runtimeContext, from, to);
245+
}
246+
247+
/**
248+
* True for workspace paths whatever their name: the prefix list only classifies relative
249+
* keys, so an absolute path under the workspace root has to be compared by position.
250+
*/
251+
private boolean underWorkspace(RuntimeContext runtimeContext, String filePath) {
252+
if (isWorkspacePath(runtimeContext, filePath)) {
253+
return true;
254+
}
255+
Path path = Path.of(normalizeSeparators(filePath));
256+
return path.isAbsolute() && path.normalize().startsWith(workspaceRoot);
241257
}
242258

243259
// ==================== Path classification ====================

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,13 @@ private boolean containsExcludedSubtree(Path path) {
819819
return false;
820820
}
821821
Path absolute = path.toAbsolutePath().normalize();
822-
return excludedSubtrees.stream().anyMatch(excluded -> excluded.startsWith(absolute));
822+
if (excludedSubtrees.stream().anyMatch(excluded -> excluded.startsWith(absolute))) {
823+
return true;
824+
}
825+
// An alias to an ancestor spells the path differently, so compare positions physically.
826+
Path real = realPath(path);
827+
return real != null
828+
&& excludedRealSubtrees.stream().anyMatch(excluded -> excluded.startsWith(real));
823829
}
824830

825831
protected Path resolvePath(RuntimeContext rc, String key) {

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,49 @@ void excludedSubtree_staysAbsentForDeleteAndMove() throws Exception {
186186
"move() carried the excluded subtree out of its hidden location");
187187
}
188188

189+
/**
190+
* An alias spells the ancestor of the hidden subtree differently. Comparing normalized
191+
* spellings alone would let delete and move walk through it into the namespaces.
192+
*/
193+
@Test
194+
void aliasedAncestor_cannotDeleteOrMoveTheHiddenSubtree() throws Exception {
195+
Path workspace = project.resolve("box/workspace");
196+
Files.createDirectories(workspace.resolve("alice"));
197+
Files.writeString(workspace.resolve("alice/MEMORY.md"), "alice-secret-memory");
198+
Path alias = project.resolve("alias");
199+
try {
200+
Files.createSymbolicLink(alias, project);
201+
} catch (IOException | UnsupportedOperationException unsupported) {
202+
Assumptions.abort("symbolic links not supported here: " + unsupported.getMessage());
203+
}
204+
AbstractFilesystem fs =
205+
new LocalFilesystemSpec()
206+
.project(project)
207+
.projectWritable(true)
208+
.toFilesystem(workspace, rc -> List.of(rc.getUserId()));
209+
210+
RuntimeContext bob = caller("bob");
211+
String aliasedAncestor = alias.resolve("box").toString();
212+
assertFalse(
213+
fs.delete(bob, aliasedAncestor).isSuccess(),
214+
"delete() walked into the namespaces through an aliased ancestor");
215+
assertTrue(
216+
Files.isRegularFile(workspace.resolve("alice/MEMORY.md")),
217+
"delete() removed a sibling namespace through an aliased ancestor");
218+
assertFalse(
219+
fs.move(bob, aliasedAncestor, project.resolve("moved").toString()).isSuccess(),
220+
"move() carried the namespaces out through an aliased ancestor");
221+
assertTrue(
222+
Files.isRegularFile(workspace.resolve("alice/MEMORY.md")),
223+
"move() relocated a sibling namespace through an aliased ancestor");
224+
assertFalse(Files.exists(project.resolve("moved")));
225+
// The alias entry itself stays deletable: unlinking it reaches no target.
226+
assertTrue(fs.delete(bob, alias.toString()).isSuccess());
227+
assertTrue(
228+
Files.isRegularFile(workspace.resolve("alice/MEMORY.md")),
229+
"unlinking the alias touched the namespaces");
230+
}
231+
189232
/**
190233
* The ripgrep path of {@code grep} aligns the process working directory with the search
191234
* root: {@code --glob} patterns match against paths relative to the working directory,

0 commit comments

Comments
 (0)