diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 4cf80d341..afaddc99c 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -93,7 +93,7 @@ jobs:
strategy:
matrix:
os: [ 'ubuntu-latest', 'windows-latest', 'macos-latest' ]
- java: [ '17', '21' ]
+ java: [ '17', '21', '25' ]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v7
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2f9fa0376..5005730df 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,11 @@
+## Unreleased
+
+### Issue Fixes
+
+- **Kotlin stdlib collections are readable again after upgrading to 5.4.0** ([#1330](https://github.com/nitrite/nitrite-java/issues/1330))
+ - The MVStore deserialization allowlist added in 5.4.0 covered only `org.dizitart.no2.**;java.**`, so a document written by a Kotlin app holding `emptyList()`, `emptySet()` or `emptyMap()` - stored as `kotlin.collections.EmptyList`, `EmptySet` and `EmptyMap` - could no longer be read. `kotlin.collections.**` is now allowed by default, in the store filter and in the v1 migration filter.
+ - **A class refused by the filter is no longer treated as file corruption.** `NitriteMVStore.openMVMap` used to roll the store back one version per failed attempt, discarding committed data and ending in `Chunk N not found`, which hid the real cause. It now fails at once with a `NitriteSecurityException` that names the rejected class and points to `MVStoreModuleBuilder.allowedClasses(...)`, and leaves the file untouched.
+
## Release 5.4.0 - Oct 5, 2026
Eleven changes from [@brettwooldridge](https://github.com/brettwooldridge), most of them found on a production system, and one H2 workaround found while testing them. Four are data-integrity fixes, three of which can end with a store that will not reopen or a query that quietly returns the wrong rows.
diff --git a/nitrite-mvstore-adapter/src/main/java/org/dizitart/no2/mvstore/FilteredObjectDataType.java b/nitrite-mvstore-adapter/src/main/java/org/dizitart/no2/mvstore/FilteredObjectDataType.java
index c60acb126..5a402d244 100644
--- a/nitrite-mvstore-adapter/src/main/java/org/dizitart/no2/mvstore/FilteredObjectDataType.java
+++ b/nitrite-mvstore-adapter/src/main/java/org/dizitart/no2/mvstore/FilteredObjectDataType.java
@@ -40,7 +40,9 @@ class FilteredObjectDataType extends ObjectDataType {
private static final int TYPE_SERIALIZED_OBJECT = 19;
private static final int LAST_PRIMITIVE_CLASS_ID = 7;
- private static final String DEFAULT_ALLOWED = "org.dizitart.no2.**;java.**";
+ // kotlin.collections covers the serializable kotlin stdlib collections (EmptyList,
+ // EmptySet, EmptyMap, builders) stored by kotlin apps through emptyList() and friends
+ private static final String DEFAULT_ALLOWED = "org.dizitart.no2.**;java.**;kotlin.collections.**";
private final ObjectInputFilter filter;
@@ -49,7 +51,7 @@ class FilteredObjectDataType extends ObjectDataType {
}
/**
- * Builds the filter allowing nitrite's own and JDK types, plus the given
+ * Builds the filter allowing nitrite's own, JDK and kotlin collection types, plus the given
* JEP 290 patterns; a process-wide filter, if set, can still reject.
*/
static ObjectInputFilter createFilter(String allowedClasses) {
@@ -78,19 +80,61 @@ public Object read(ByteBuffer buff) {
// ObjectDataType; nitrite never stores one, so refuse it
int classId = buff.get(buff.position() + 1);
if (classId < 0 || classId > LAST_PRIMITIVE_CLASS_ID) {
- throw DataUtils.newMVStoreException(ERROR_SERIALIZATION,
- "Object arrays are not allowed in a nitrite map");
+ throw new SerialFilterRejectedException("Object arrays are not allowed in a nitrite map");
}
}
return super.read(buff);
}
private Object readFiltered(byte[] data) {
+ RejectionRecorder recorder = new RejectionRecorder(filter);
try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(data))) {
- in.setObjectInputFilter(filter);
+ in.setObjectInputFilter(recorder);
return in.readObject();
} catch (Exception e) {
+ if (recorder.rejected) {
+ throw new SerialFilterRejectedException(rejectionMessage(recorder.rejectedClass), e);
+ }
throw DataUtils.newMVStoreException(ERROR_SERIALIZATION, "Could not deserialize {0}", e, e);
}
}
+
+ private static String rejectionMessage(Class> rejectedClass) {
+ if (rejectedClass == null) {
+ return "A stored object exceeds the limits of the deserialization filter";
+ }
+ while (rejectedClass.isArray()) {
+ rejectedClass = rejectedClass.getComponentType();
+ }
+ String className = rejectedClass.getName();
+ String packageName = rejectedClass.getPackage() == null ? "" : rejectedClass.getPackage().getName();
+ String example = packageName.isEmpty() ? className : packageName + ".**";
+ return "Class " + className + " is not allowed to be deserialized from the database. "
+ + "If it is trusted, allow it with MVStoreModuleBuilder.allowedClasses(...), "
+ + "e.g. allowedClasses(\"" + example + "\")";
+ }
+
+ /**
+ * Delegates to the configured filter and remembers what it rejected, so the
+ * resulting error can name the class instead of a bare "filter status: REJECTED".
+ */
+ private static class RejectionRecorder implements ObjectInputFilter {
+ private final ObjectInputFilter delegate;
+ private boolean rejected;
+ private Class> rejectedClass;
+
+ RejectionRecorder(ObjectInputFilter delegate) {
+ this.delegate = delegate;
+ }
+
+ @Override
+ public Status checkInput(FilterInfo info) {
+ Status status = delegate.checkInput(info);
+ if (status == Status.REJECTED && !rejected) {
+ rejected = true;
+ rejectedClass = info.serialClass();
+ }
+ return status;
+ }
+ }
}
diff --git a/nitrite-mvstore-adapter/src/main/java/org/dizitart/no2/mvstore/MVStoreModuleBuilder.java b/nitrite-mvstore-adapter/src/main/java/org/dizitart/no2/mvstore/MVStoreModuleBuilder.java
index afd48aea4..64f6f4899 100644
--- a/nitrite-mvstore-adapter/src/main/java/org/dizitart/no2/mvstore/MVStoreModuleBuilder.java
+++ b/nitrite-mvstore-adapter/src/main/java/org/dizitart/no2/mvstore/MVStoreModuleBuilder.java
@@ -124,8 +124,9 @@ public class MVStoreModuleBuilder {
/**
* Extra JEP 290 patterns (for example {@code com.example.model.**}) of
- * classes allowed to be deserialized from the store, besides nitrite's own
- * and JDK types. Needed only when documents hold values of other
+ * classes allowed to be deserialized from the store, besides nitrite's own,
+ * JDK and kotlin collection ({@code kotlin.collections.**}) types. Needed
+ * only when documents hold values of other
* {@link java.io.Serializable} classes.
*/
private String allowedClasses;
diff --git a/nitrite-mvstore-adapter/src/main/java/org/dizitart/no2/mvstore/NitriteMVStore.java b/nitrite-mvstore-adapter/src/main/java/org/dizitart/no2/mvstore/NitriteMVStore.java
index c601a4834..3c886051c 100644
--- a/nitrite-mvstore-adapter/src/main/java/org/dizitart/no2/mvstore/NitriteMVStore.java
+++ b/nitrite-mvstore-adapter/src/main/java/org/dizitart/no2/mvstore/NitriteMVStore.java
@@ -30,6 +30,7 @@
import org.dizitart.no2.common.util.StringUtils;
import org.dizitart.no2.exceptions.NitriteIOException;
+import org.dizitart.no2.exceptions.NitriteSecurityException;
import org.dizitart.no2.index.BoundingBox;
import org.dizitart.no2.store.AbstractNitriteStore;
import org.dizitart.no2.store.NitriteMap;
@@ -318,6 +319,13 @@ private MVMap openMVMap(String mapName, MVMap.MapBuilder builder) {
enableAutoCommitIfPending();
return map;
} catch (MVStoreException me) {
+ SerialFilterRejectedException rejection = findRejection(me);
+ if (rejection != null) {
+ // the data is intact, only not allowed to be read; rolling back to
+ // an earlier version would discard committed data and hide the cause
+ throw new NitriteSecurityException("Unable to open map " + mapName + ": "
+ + rejection.getMessage(), rejection);
+ }
if (version == 0) {
throw me;
}
@@ -335,10 +343,21 @@ private MVMap openMVMap(String mapName, MVMap.MapBuilder builder) {
exception = me;
}
}
+ } catch (NitriteSecurityException e) {
+ throw e;
} catch (Exception e) {
exception = e;
}
throw new NitriteIOException("Unable to open map " + mapName, exception);
}
+
+ private static SerialFilterRejectedException findRejection(Throwable error) {
+ for (Throwable t = error; t != null; t = t.getCause()) {
+ if (t instanceof SerialFilterRejectedException) {
+ return (SerialFilterRejectedException) t;
+ }
+ }
+ return null;
+ }
}
diff --git a/nitrite-mvstore-adapter/src/main/java/org/dizitart/no2/mvstore/SerialFilterRejectedException.java b/nitrite-mvstore-adapter/src/main/java/org/dizitart/no2/mvstore/SerialFilterRejectedException.java
new file mode 100644
index 000000000..dae2c46fb
--- /dev/null
+++ b/nitrite-mvstore-adapter/src/main/java/org/dizitart/no2/mvstore/SerialFilterRejectedException.java
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2017-2021 Nitrite author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.dizitart.no2.mvstore;
+
+import org.h2.mvstore.MVStoreException;
+
+import static org.h2.mvstore.DataUtils.ERROR_SERIALIZATION;
+
+/**
+ * Thrown when a stored object is refused by the deserialization allowlist of
+ * {@link FilteredObjectDataType}. Unlike other serialization errors this is not
+ * a sign of a corrupt file, so the store must not roll back on it. It extends
+ * {@link MVStoreException} so that h2 passes it through instead of reporting
+ * the page as corrupt.
+ *
+ * @since 5.4.1
+ */
+class SerialFilterRejectedException extends MVStoreException {
+ private static final long serialVersionUID = 1762953123L;
+
+ SerialFilterRejectedException(String message) {
+ super(ERROR_SERIALIZATION, message);
+ }
+
+ SerialFilterRejectedException(String message, Throwable cause) {
+ this(message);
+ initCause(cause);
+ }
+}
diff --git a/nitrite-mvstore-adapter/src/main/java/org/dizitart/no2/mvstore/compat/v1/NitriteObjectInputStream.java b/nitrite-mvstore-adapter/src/main/java/org/dizitart/no2/mvstore/compat/v1/NitriteObjectInputStream.java
index d52caab26..cd6d3dfac 100644
--- a/nitrite-mvstore-adapter/src/main/java/org/dizitart/no2/mvstore/compat/v1/NitriteObjectInputStream.java
+++ b/nitrite-mvstore-adapter/src/main/java/org/dizitart/no2/mvstore/compat/v1/NitriteObjectInputStream.java
@@ -36,12 +36,12 @@ public class NitriteObjectInputStream extends ObjectInputStream {
/**
* Allowlist filter for the legacy v1 migration path (CWE-502). Only Nitrite's
- * own (compat) types and standard JDK types may be deserialized; any other
- * class - e.g. a third-party gadget on the classpath - is rejected. See
- * GHSA-9297-g93h-86gg.
+ * own (compat) types, standard JDK types and kotlin stdlib collections may be
+ * deserialized; any other class - e.g. a third-party gadget on the classpath -
+ * is rejected. See GHSA-9297-g93h-86gg.
*/
static final ObjectInputFilter SERIAL_FILTER = ObjectInputFilter.Config.createFilter(
- "maxdepth=32;org.dizitart.no2.**;java.**;!*");
+ "maxdepth=32;org.dizitart.no2.**;java.**;kotlin.collections.**;!*");
static {
migrationMap.put("org.dizitart.no2.Security$UserCredential", Compat.UserCredential.class);
diff --git a/nitrite-mvstore-adapter/src/test/java/org/dizitart/no2/mvstore/SerialFilterTest.java b/nitrite-mvstore-adapter/src/test/java/org/dizitart/no2/mvstore/SerialFilterTest.java
index 7c82edad7..dda7cee9b 100644
--- a/nitrite-mvstore-adapter/src/test/java/org/dizitart/no2/mvstore/SerialFilterTest.java
+++ b/nitrite-mvstore-adapter/src/test/java/org/dizitart/no2/mvstore/SerialFilterTest.java
@@ -64,6 +64,37 @@ public void rejectsGadgetInObjectArray() {
assertFalse("gadget readObject() must not run", EvilGadget.readObjectCalled);
}
+ @Test
+ public void rejectionNamesClassAndKeepsData() {
+ try (Nitrite db = open(null)) {
+ db.getCollection("notes").insert(Document.createDocument("payload", new EvilGadget()));
+ }
+
+ try (Nitrite db = open(null)) {
+ db.getCollection("notes").find().toList();
+ fail("expected the gadget to be rejected");
+ } catch (Exception expected) {
+ String message = messages(expected);
+ assertTrue(message, message.contains(EvilGadget.class.getName()));
+ assertTrue(message, message.contains("allowedClasses"));
+ }
+
+ // a rejected class is not corruption: the store must not have been rolled back
+ try (Nitrite db = open("com.evil.*")) {
+ NitriteCollection notes = db.getCollection("notes");
+ assertEquals(1, notes.size());
+ assertTrue(notes.find().firstOrNull().get("payload") instanceof EvilGadget);
+ }
+ }
+
+ private static String messages(Throwable error) {
+ StringBuilder builder = new StringBuilder();
+ for (Throwable t = error; t != null; t = t.getCause()) {
+ builder.append(t.getMessage()).append('\n');
+ }
+ return builder.toString();
+ }
+
@Test
public void allowsConfiguredClasses() {
try (Nitrite db = open(null)) {
diff --git a/nitrite-native-tests/pom.xml b/nitrite-native-tests/pom.xml
index 5daa7cbb1..d0cac1b1c 100644
--- a/nitrite-native-tests/pom.xml
+++ b/nitrite-native-tests/pom.xml
@@ -122,6 +122,9 @@
true${project.build.outputDirectory}
+
--initialize-at-run-time=org.junit.jupiter.engine.discovery.MethodSegmentResolver
diff --git a/nitrite-native-tests/src/test/java/org/dizitart/nitrite/test/DatabaseTestUtils.java b/nitrite-native-tests/src/test/java/org/dizitart/nitrite/test/DatabaseTestUtils.java
index 9faa3b0ba..2246b460d 100644
--- a/nitrite-native-tests/src/test/java/org/dizitart/nitrite/test/DatabaseTestUtils.java
+++ b/nitrite-native-tests/src/test/java/org/dizitart/nitrite/test/DatabaseTestUtils.java
@@ -3,6 +3,7 @@
import org.dizitart.nitrite.test.repository.PersonEntityConverter;
import org.dizitart.no2.Nitrite;
import org.dizitart.no2.mvstore.MVStoreModule;
+import org.jspecify.annotations.Nullable;
import java.io.IOException;
import java.io.InputStream;
@@ -26,7 +27,7 @@ public static Nitrite setupDatabase() throws IOException {
return setupDatabase(null);
}
- public static Nitrite setupDatabase(final URL template) throws IOException {
+ public static Nitrite setupDatabase(@Nullable final URL template) throws IOException {
final Path databasePath = getRandomDatabasePath();
@@ -37,8 +38,14 @@ public static Nitrite setupDatabase(final URL template) throws IOException {
}
}
+ return openDatabase(databasePath, null);
+ }
+
+ public static Nitrite openDatabase(final Path databasePath, @Nullable final String allowedClasses) {
+
final MVStoreModule mvStoreModule = MVStoreModule.withConfig()
.filePath(databasePath.toFile())
+ .allowedClasses(allowedClasses)
.build();
return Nitrite.builder()
diff --git a/nitrite-native-tests/src/test/java/org/dizitart/nitrite/test/serialization/SerialFilterTest.java b/nitrite-native-tests/src/test/java/org/dizitart/nitrite/test/serialization/SerialFilterTest.java
new file mode 100644
index 000000000..1da8ac979
--- /dev/null
+++ b/nitrite-native-tests/src/test/java/org/dizitart/nitrite/test/serialization/SerialFilterTest.java
@@ -0,0 +1,84 @@
+package org.dizitart.nitrite.test.serialization;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import org.dizitart.nitrite.test.DatabaseTestUtils;
+import org.dizitart.nitrite.test.serialization.model.UntrustedPayload;
+import org.dizitart.no2.Nitrite;
+import org.dizitart.no2.collection.Document;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
+
+public class SerialFilterTest {
+
+ private static final String ALLOWED_CLASSES = "org.dizitart.nitrite.test.serialization.model.*";
+
+ @Test
+ void rejectedClassShouldBeReportedWithoutRollback() throws IOException {
+
+ final Path databasePath = DatabaseTestUtils.getRandomDatabasePath();
+
+ try (final Nitrite database = DatabaseTestUtils.openDatabase(databasePath, null)) {
+ // requires additional serialization hints for UntrustedPayload
+ // see: src/test/resources/META-INF/native-image/org.dizitart/nitrite-native-tests/serialization-config.json
+ database.getCollection("test").insert(Document.createDocument()
+ .put("payload", new UntrustedPayload("test")));
+ }
+
+ // the error message is built from the rejected class (name and package),
+ // which has to work in native image as well
+ try (final Nitrite database = DatabaseTestUtils.openDatabase(databasePath, null)) {
+ assertThatThrownBy(() -> database.getCollection("test").find().toList())
+ .hasStackTraceContaining("Class " + UntrustedPayload.class.getName() + " is not allowed")
+ .hasStackTraceContaining("allowedClasses(\"org.dizitart.nitrite.test.serialization.model.**\")");
+ }
+
+ // a rejected class is no corruption, so the committed data must still be there
+ try (final Nitrite database = DatabaseTestUtils.openDatabase(databasePath, ALLOWED_CLASSES)) {
+
+ final var testCollection = database.getCollection("test");
+
+ assertThat(testCollection.size()).isEqualTo(1);
+ final Document document = testCollection.find().firstOrNull();
+ assertThat(document).isNotNull();
+ assertThat(document.get("payload", UntrustedPayload.class).getValue()).isEqualTo("test");
+ }
+ }
+
+ @Test
+ void rejectedArrayShouldBeReportedByComponentType() throws IOException {
+
+ final Path databasePath = DatabaseTestUtils.getRandomDatabasePath();
+
+ try (final Nitrite database = DatabaseTestUtils.openDatabase(databasePath, null)) {
+ // object arrays are only stored through java serialization when nested, e.g. in a list
+ final List