agentscope-java icon indicating copy to clipboard operation
agentscope-java copied to clipboard

[Bug] Enumeration surfaces: relative and ..-bearing paths bypass the namespace anchor (sibling-tenant access)

Open aiyili opened this issue 2 months ago • 2 comments

Summary

Follow-up from the #3254 review (non-blocking items), covering the ..-segment escape class on the enumeration surfaces that the shared AbstractFilesystem.denotesRootPath helper does not yet close:

  1. Relative root-equivalent forms — ./, .., and sub/.. are not classified as root spellings and reach backends raw. On a namespaced ROOTED LocalFilesystem, applyNamespacePrefix yields user-2/.. and cwd.resolve("user-2/..").normalize() lands on the workspace root — the sibling-tenant listing that the #3253 tests guard against for absolute spellings.
  2. Non-root absolute paths containing .. — ls("/alice/../bob") is forwarded raw; absolute paths bypass the namespace prefix, so the enumeration re-roots outside the caller's namespace.

denotesRootPath classifies but does not normalize, so fixing (1) alone is not enough for (2) — the enumeration surfaces (ls/grep/glob) need to normalize ./.. segments (or reject them) before delegation, consistently across CompositeFilesystem and the backends.

  1. Interface-level null-handling test — CompositeFilesystem.grep forwards null verbatim to the default backend (its documented working-directory form); every in-tree backend honours it via denotesRootPath, but containment depends on each new implementation remembering the convention. A contract test enumerating all AbstractFilesystem implementations would pin it.

Scope note

All of these are pre-existing behaviours, explicitly acknowledged as non-blocking in the #3254 review ("nothing here needs a redesign — the follow-ups can be a separate PR"). #3254's javadoc wording was adjusted in that PR to claim only root-spelling closure; this issue tracks closing the remaining ..-segment class.

Suggested direction

  • Extend denotesRootPath (or add normalizeEnumerationPath at the AbstractFilesystem level) to resolve ./.. for relative input and to strip/resolve .. segments for absolute non-root input before delegation;
  • or reject relative/..-bearing enumeration paths with an IllegalArgumentException, mirroring how delete/move already gate on validatePath;
  • plus the interface-level null contract test from (3).

aiyili avatar Oct 01 '26 00:10 aiyili

Issue Evaluation

Category: bug | Status: Partially confirmed (verified against main @ addf82b89, 2.0.4-SNAPSHOT)

We reproduced the reported behaviour with an executable probe (a workspace containing user-1/ and user-2/secret.txt; no repo files were modified). The escape class is real, but the affected configuration and the mechanism differ from the report.

Confirmed

Claim Evidence
Enumeration surfaces (ls/grep/glob) are unguarded, unlike delete/move validatePath (AbstractFilesystem.java:178-188) has 8 call sites, all on delete/move/upload (LocalFilesystem.java:530,559-560; CompositeFilesystem.java:465,476-477; RemoteFilesystem.java:597,615-616; BaseSandboxFilesystem.java:427,439-440) — none on the enumeration path
Relative .. escapes a per-user namespace probe: ls('..') → [user-1/, user-2/], glob('**','..') → [own.txt, user-2/secret.txt] — sibling namespaces become enumerable
applyNamespacePrefix does not touch ./.. segments LocalFilesystem.java:689-707: .. → user-1/..; then resolveUnrestricted (:681-687) does cwd.resolve(target).normalize() with no validation
CompositeFilesystem.grep forwards null verbatim to the default backend CompositeFilesystem.java:280-282; documented contract at AbstractFilesystem.java:98 (null searches the working directory). All in-tree backends currently behave, but nothing pins it — no contract test exists
The #3253 guards cover read/upload only, not enumeration LocalFilesystemModeTest.java:285-293, 314-325, 328-347, 350-369 all target read/upload; there is no ls/grep/glob traversal case anywhere under agentscope-harness/src/test/java/io/agentscope/harness/agent/filesystem/

Corrections

  1. AbstractFilesystem.denotesRootPath does not exist in this tree. Root classification is inlined per call site: "/".equals(path) / ".".equals(path) at CompositeFilesystem.java:199,280,343 and LocalFilesystem.java:421, plus RemoteFilesystem.normalizePath (:720-723). So "extend denotesRootPath" is not an actionable option; "introduce a shared helper" is.
  2. A namespaced ROOTED LocalFilesystem does not leak. resolveRooted calls AbstractFilesystem.validatePath(effectiveKey) as its first statement (LocalFilesystem.java:633-634), so the cwd.resolve("user-2/..").normalize() path you describe is never reached — probe: [ROOTED] ls('..') threw IllegalArgumentException. The reachable escape is UNRESTRICTED + namespace, which is exactly the default backend of RemoteFilesystemSpec when sharedLocalWorkspace=false (RemoteFilesystemSpec.java:244-247, wired via HarnessAgent.java:2428,2438,2499). The report does not mention that configuration, and it is what makes this worth fixing.
  3. ./ and sub/.. are not escapes. Both normalize back inside the namespace (probe: [UNRES] ls('sub/..') → [own.txt], [ROOTED] ls('./') → [own.txt]). Rejecting every "relative / ..-bearing" path as the suggested direction implies would break legitimate relative usage; only .. segments that move the resolved result outside the anchor need handling.
  4. Absolute paths bypassing the namespace prefix is intentional and already pinned, not a defect: see the comment at LocalFilesystem.java:693-700 and the tests rooted_absolutePathNotCorruptedByNamespace (LocalFilesystemModeTest.java:149-163) / rooted_leadingSlashWithNamespace (:250-265). Under UNRESTRICTED any absolute path can enumerate the host — that is the documented escape hatch (workspace/LocalFsMode.java) — so ls("/alice/../bob") is one instance of a by-design property; normalizing .. inside it would not restore containment.
  5. "every in-tree backend honours null via denotesRootPath" — each backend handles null its own way (LocalFilesystem.java:397 → "."; RemoteFilesystem.java:335 with normalizePath :720-723 → "/"; BaseSandboxFilesystem.java:334,381; OverlayFilesystem.java:196-199 / ProjectAwareOverlay.java:224-228 / BakedContextFilesystem.java:77-79 pass it through). Notably the sandbox ls does not honour it: null reaches FilesystemUtils.shellQuote (:101-106) and becomes '', i.e. a degraded failure rather than a working-directory search. The contract test you propose should pin ls as well as grep.
  6. Side observation: in ROOTED a traversal attempt raises IllegalArgumentException (:634), while LocalFilesystem.grep only catches SecurityException (:396-400), so the exception propagates raw through CompositeFilesystem to the tool layer instead of surfacing as a typed tool error. Worth folding into the same cleanup.

Severity: high. Cross-tenant enumeration of sibling namespaces (directory names, and file paths via glob) under the default distributed wiring. Not critical: delete/move stay guarded, and local single-user setups default to ROOTED (LocalFilesystemSpec.java:61), which is already safe. Whether read/write also cross the boundary is a strong inference rather than a measured fact — they share resolvePath, but we did not execute that case.

Fix direction we would propose

  • Primary: tighten LocalFilesystem.resolveUnrestricted (:681-687) for relative input whose normalized result leaves the anchor, mirroring resolveSandboxed:611-613 in both condition and exception type. One call site covers ls/grep/glob/read/write/edit/upload/download/exists (all go through resolvePath), keeps absolute pass-through intact (changing it would break the contracts pinned in correction 4), and matches the documented UNRESTRICTED semantics "relative paths anchor to rootDir" — a relative .. already contradicts that wording today.
  • Secondary: add the interface-level contract test you suggest, parameterized over all in-tree AbstractFilesystem implementations, pinning (a) grep(null) / ls(null) search the working directory only and (b) ls/grep/glob with .. stay within the namespace. Candidate cases in LocalFilesystemModeTest: unrestricted_namespaced_lsDotDotCannotEscapeUserNamespace, unrestricted_namespaced_globDotDotCannotEscapeUserNamespace, unrestricted_namespaced_dotSlashAndSubDotDotStayInNamespace; plus RemoteFilesystemSpecTest.defaultBackend_namespaced_lsDotDotDoesNotListSiblingNamespaces to pin the production default end-to-end.

Next step

We are not opening a PR unconditionally here. @aiyili if you plan to send this yourself, say so and we will review it; otherwise tell us which fix location you prefer (mode-level in resolveUnrestricted vs a shared enumeration helper on AbstractFilesystem) and we will turn it into a concrete proposal. We would also narrow the scope note to the UNRESTRICTED + namespace wiring per corrections 2–4.


Automated evaluation by github-manager-bot

oss-maintainer avatar Oct 01 '26 01:10 oss-maintainer

Hi, this is the follow-up I filed from the #3254 review — taking it myself. Plan: resolve ./.. segments for relative input and ..-bearing absolute paths on the enumeration surfaces (shared helper), plus the interface-level null-handling contract test enumerating all AbstractFilesystem implementations. PR coming shortly.

aiyili avatar Oct 01 '26 15:10 aiyili