From cf0e49e34d2a5178906c732a3e1f376fbe5d8f60 Mon Sep 17 00:00:00 2001 From: piexel Date: Fri, 6 Nov 2026 11:29:26 +0100 Subject: [PATCH] Reject saved ids whose namespace or path could leave the world's storage The id names the stored file, so a namespace of .. must be rejected as well as a path segment. --- README.md | 2 +- api/src/main/java/com/volmit/world/SavedKey.java | 10 +++++----- api/src/test/java/com/volmit/world/SavedKeyTest.java | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index d2cc52653..d0bf9d731 100644 --- a/README.md +++ b/README.md @@ -368,7 +368,7 @@ waves.record(now); // changed in pl one wrote. - `Codec.write` may run while module threads change the value, so it must read it safely. A stored payload the codec rejects is logged and replaced with `initial`. -- The id names the stored file, so its path has no empty, `.` or `..` segment. +- The id names the stored file, so none of its segments is empty, `.` or `..`. A module that wants to act when the host loads a pack or starts or stops a world registers a `ModuleLifecycle`. Every hook does nothing by default, and a module class may implement it and register itself: diff --git a/api/src/main/java/com/volmit/world/SavedKey.java b/api/src/main/java/com/volmit/world/SavedKey.java index 5d680965d..9d2a36991 100644 --- a/api/src/main/java/com/volmit/world/SavedKey.java +++ b/api/src/main/java/com/volmit/world/SavedKey.java @@ -5,9 +5,9 @@ import java.util.Objects; import java.util.function.Supplier; /** The identity and form of one value a module keeps with a running world across restarts - * ({@link RunningWorld#saved}). The id is namespaced like a mantle key and starts with the owning module's id or its - * namespace; the host names its storage after it, so each {@code /} separated segment of the path must be a name, never - * empty, {@code .} or {@code ..}. + * ({@link RunningWorld#saved}). The id is namespaced like a mantle key and should start with the owning module's id; the host + * names its storage after it, so each segment of the namespace and of the {@code /} separated path must be a name, + * never empty, {@code .} or {@code ..}. * * @param id the stable storage identity, such as {@code example:moss/patches} * @param type the value class @@ -26,9 +26,9 @@ public record SavedKey(String id, Class type, Codec codec, Supplier if (type.isPrimitive()) { throw new IllegalArgumentException("Saved values require a reference type"); } - for (String segment : id.substring(id.indexOf(':') + 1).split("/", -1)) { + for (String segment : id.replace(':', '/').split("/", -1)) { if (segment.isEmpty() || segment.equals(".") || segment.equals("..")) { - throw new IllegalArgumentException("A saved ID names files, so its path has no empty, . or .. segment: " + id); + throw new IllegalArgumentException("A saved ID names files, so it has no empty, . or .. segment: " + id); } } } diff --git a/api/src/test/java/com/volmit/world/SavedKeyTest.java b/api/src/test/java/com/volmit/world/SavedKeyTest.java index 6f17fba2e..3b287fac8 100644 --- a/api/src/test/java/com/volmit/world/SavedKeyTest.java +++ b/api/src/test/java/com/volmit/world/SavedKeyTest.java @@ -43,7 +43,7 @@ class SavedKeyTest { @Test void rejectsIdsThatCouldLeaveTheWorldsStorage() { - for (String id : new String[]{"example:../book", "example:a/../b", "example:./book", "example:/book", "example:book/", "example:a//b"}) { + for (String id : new String[]{"example:../book", "example:a/../b", "example:./book", "example:/book", "example:book/", "example:a//b", "..:book", ".:book"}) { assertThrows(IllegalArgumentException.class, () -> key(id), id); } }