Skip to content

[Bug]: JsonFileAgentStateStore allows state keys to escape the configured root directory #4290

Open

Description

@lishibin

Describe the bug

JsonFileAgentStateStore uses the caller-provided state key directly when constructing the .json, .jsonl, and .hash paths. User and session identifiers are encoded through safeSegment(...), but state keys are not.

A key containing .. segments or an absolute path can therefore escape the configured rootDirectory. This affects both writes and deletes. If an application or extension passes untrusted input as a state key, the process can write or delete files outside the configured state directory, subject to the process filesystem permissions.

To Reproduce

Path root = Files.createTempDirectory("as-state-root-");
String name = "escape-" + UUID.randomUUID();
Path outside = root.getParent().resolve(name + ".json");

JsonFileAgentStateStore store = new JsonFileAgentStateStore(root);

// Create the normal session directory first.
store.save("user", "session", "safe", new TestState("seed"));

String key = "../../../" + name;
store.save("user", "session", key, new TestState("escaped"));

System.out.println(Files.exists(outside)); // true

Files.writeString(outside, "marker");
store.delete("user", "session", key);
System.out.println(Files.exists(outside)); // false

TestState can be any simple State implementation, for example record TestState(String value) implements State {}.

The same path construction is used by getListPath(...) and getHashPath(...), so list state and list metadata are affected too.

Expected behavior

All state files should remain below the configured rootDirectory, regardless of the state key. A key containing path separators, .., or an absolute path should either be rejected with a clear validation error or encoded as a single safe filename segment.

Error messages

No exception is thrown. The operation succeeds and writes or deletes the path outside the configured root.

Environment

  • AgentScope-Java Version: 2.1.0-SNAPSHOT, main at cd8a21c4a
  • Java Version: 25
  • OS: macOS

Additional context

The issue is in JsonFileAgentStateStore.getStatePath(...), getListPath(...), and getHashPath(...), where sessionDir.resolve(key + suffix) is used directly. The existing session-ID encoding does not protect the state-key path.

A central state-key validation or encoding helper, plus regression tests for single-state save, list-state save, and delete, should cover the fix.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions