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.
This commit is contained in:
piexel committed 2026-11-06 11:29:26 +01:00
1 parent a0fcb74365
commit cf0e49e34d
3 files changed
+7 -7

No files matched your search

+1 -1
View File
@@ -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:
@@ -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<T>(String id, Class<T> type, Codec<T> codec, Supplier<T>
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);
}
}
}
@@ -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);
}
}