diff --git a/agentscope-core/src/main/java/io/agentscope/core/state/JsonFileAgentStateStore.java b/agentscope-core/src/main/java/io/agentscope/core/state/JsonFileAgentStateStore.java index 3fcfd2662c..cfcb07e94e 100644 --- a/agentscope-core/src/main/java/io/agentscope/core/state/JsonFileAgentStateStore.java +++ b/agentscope-core/src/main/java/io/agentscope/core/state/JsonFileAgentStateStore.java @@ -382,15 +382,15 @@ protected Path getSessionDir(String userId, String sessionId) { } private static Path getStatePath(Path sessionDir, String key) { - return sessionDir.resolve(key + ".json"); + return sessionDir.resolve(safeStateKey(key) + ".json"); } private static Path getListPath(Path sessionDir, String key) { - return sessionDir.resolve(key + ".jsonl"); + return sessionDir.resolve(safeStateKey(key) + ".jsonl"); } private static Path getHashPath(Path sessionDir, String key) { - return sessionDir.resolve(key + ".hash"); + return sessionDir.resolve(safeStateKey(key) + ".hash"); } private String readHashFile(Path hashFile) { @@ -533,6 +533,27 @@ private static String safeSegment(String value) { .encodeToString(value.getBytes(StandardCharsets.UTF_8)); } + /** + * Encode a state key as one file-name segment. + * + *

The marker keeps an encoded key distinct from a caller-provided safe key whose value + * happens to be the same Base64-URL string. This also prevents separators and absolute paths + * from being interpreted by {@link Path#resolve(Path)}. + */ + private static String safeStateKey(String key) { + Objects.requireNonNull(key, "state key must not be null"); + if (key.isBlank()) { + throw new IllegalArgumentException("state key must not be blank"); + } + if (SAFE_FILENAME_PATTERN.matcher(key).matches()) { + return key; + } + return "~" + + Base64.getUrlEncoder() + .withoutPadding() + .encodeToString(key.getBytes(StandardCharsets.UTF_8)); + } + private static String decodeSegment(String segment) { if (SAFE_FILENAME_PATTERN.matcher(segment).matches()) { return segment; diff --git a/agentscope-core/src/test/java/io/agentscope/core/state/JsonFileAgentStateStoreTest.java b/agentscope-core/src/test/java/io/agentscope/core/state/JsonFileAgentStateStoreTest.java index e4c62b6856..ecc119624d 100644 --- a/agentscope-core/src/test/java/io/agentscope/core/state/JsonFileAgentStateStoreTest.java +++ b/agentscope-core/src/test/java/io/agentscope/core/state/JsonFileAgentStateStoreTest.java @@ -26,10 +26,12 @@ import io.agentscope.core.message.MsgRole; import io.agentscope.core.message.TextBlock; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.attribute.FileTime; import java.util.ArrayList; +import java.util.Base64; import java.util.List; import java.util.Optional; import java.util.Set; @@ -177,6 +179,59 @@ void saveListRewritesWhenARebuiltMessageDiffers(@TempDir Path tempDir) throws IO assertNotEquals(untouched, Files.getLastModifiedTime(listFile)); } + @Test + @DisplayName("Should keep state keys inside the configured root") + void stateKeyCannotEscapeRoot(@TempDir Path tempDir) throws IOException { + JsonFileAgentStateStore store = new JsonFileAgentStateStore(tempDir); + String name = "escape-" + System.nanoTime(); + String key = "../../../" + name; + Path outside = tempDir.getParent().resolve(name + ".json"); + Path outsideList = tempDir.getParent().resolve(name + ".jsonl"); + Path outsideHash = tempDir.getParent().resolve(name + ".hash"); + + store.save("user", "session", key, new TestState("single", 1)); + store.save("user", "session", key, List.of(new TestState("list", 2))); + + assertFalse(Files.exists(outside)); + assertFalse(Files.exists(outsideList)); + assertFalse(Files.exists(outsideHash)); + assertEquals( + Optional.of(new TestState("single", 1)), + store.get("user", "session", key, TestState.class)); + assertEquals( + List.of(new TestState("list", 2)), + store.getList("user", "session", key, TestState.class)); + + Files.writeString(outside, "marker"); + Files.writeString(outsideList, "marker"); + Files.writeString(outsideHash, "marker"); + store.delete("user", "session", key); + + assertTrue(Files.exists(outside)); + assertTrue(Files.exists(outsideList)); + assertTrue(Files.exists(outsideHash)); + } + + @Test + @DisplayName("Should keep encoded state keys distinct from literal keys") + void encodedStateKeyDoesNotCollideWithLiteralKey(@TempDir Path tempDir) { + JsonFileAgentStateStore store = new JsonFileAgentStateStore(tempDir); + String encoded = + Base64.getUrlEncoder() + .withoutPadding() + .encodeToString("a/b".getBytes(StandardCharsets.UTF_8)); + + store.save(null, "session1", "a/b", new TestState("encoded", 1)); + store.save(null, "session1", encoded, new TestState("literal", 2)); + + assertEquals( + Optional.of(new TestState("encoded", 1)), + store.get(null, "session1", "a/b", TestState.class)); + assertEquals( + Optional.of(new TestState("literal", 2)), + store.get(null, "session1", encoded, TestState.class)); + } + @Test @DisplayName("Should tolerate concurrent saves and reads of one key across instances") void saveSurvivesConcurrentWritesToSameKey(@TempDir Path tempDir) throws Exception {