From 0f4bc6191350ff00437636bb1c8bea511c104c4b Mon Sep 17 00:00:00 2001 From: esrakartalOpt Date: Thu, 19 Nov 2026 14:47:01 -0600 Subject: [PATCH 1/2] [FSSDK-13470] Fix Arnica findings: allowlist JSON library reflection, bump quickstart Jackson - Add JsonLibraryDetector: Class.forName is only called for an allowlist of supported JSON library classes and without initializing the class. Use it in DefaultConfigParser, JsonParserProvider and DefaultJsonSerializer (Arnica CWE-470 unsafe reflection findings). - Bump java-quickstart Jackson to 2.23.0 (jackson-annotations 2.23) to fix jackson-core and jackson-databind SCA findings. Co-Authored-By: Claude Sonnet 5.5 --- .../ab/config/parser/DefaultConfigParser.java | 8 +-- .../serializer/DefaultJsonSerializer.java | 8 +-- .../ab/internal/JsonLibraryDetector.java | 61 +++++++++++++++++++ .../ab/internal/JsonParserProvider.java | 7 +-- .../ab/internal/JsonLibraryDetectorTest.java | 47 ++++++++++++++ java-quickstart/build.gradle | 7 ++- 6 files changed, 117 insertions(+), 21 deletions(-) create mode 100644 core-api/src/main/java/com/optimizely/ab/internal/JsonLibraryDetector.java create mode 100644 core-api/src/test/java/com/optimizely/ab/internal/JsonLibraryDetectorTest.java diff --git a/core-api/src/main/java/com/optimizely/ab/config/parser/DefaultConfigParser.java b/core-api/src/main/java/com/optimizely/ab/config/parser/DefaultConfigParser.java index ff1748cb0..bec1e40f4 100644 --- a/core-api/src/main/java/com/optimizely/ab/config/parser/DefaultConfigParser.java +++ b/core-api/src/main/java/com/optimizely/ab/config/parser/DefaultConfigParser.java @@ -16,6 +16,7 @@ */ package com.optimizely.ab.config.parser; +import com.optimizely.ab.internal.JsonLibraryDetector; import com.optimizely.ab.internal.PropertyUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -71,12 +72,7 @@ ConfigParser get() { } private boolean isPresent() { - try { - Class.forName(className); - return true; - } catch (ClassNotFoundException e) { - return false; - } + return JsonLibraryDetector.isPresent(className); } } /** diff --git a/core-api/src/main/java/com/optimizely/ab/event/internal/serializer/DefaultJsonSerializer.java b/core-api/src/main/java/com/optimizely/ab/event/internal/serializer/DefaultJsonSerializer.java index a914f3e90..d087b76d9 100644 --- a/core-api/src/main/java/com/optimizely/ab/event/internal/serializer/DefaultJsonSerializer.java +++ b/core-api/src/main/java/com/optimizely/ab/event/internal/serializer/DefaultJsonSerializer.java @@ -20,6 +20,7 @@ import org.slf4j.LoggerFactory; import com.optimizely.ab.config.parser.MissingJsonParserException; +import com.optimizely.ab.internal.JsonLibraryDetector; import javax.annotation.Nonnull; @@ -67,12 +68,7 @@ Serializer create() { } private static boolean isPresent(@Nonnull String className) { - try { - Class.forName(className); - return true; - } catch (ClassNotFoundException e) { - return false; - } + return JsonLibraryDetector.isPresent(className); } //======== Lazy-init Holder ========// diff --git a/core-api/src/main/java/com/optimizely/ab/internal/JsonLibraryDetector.java b/core-api/src/main/java/com/optimizely/ab/internal/JsonLibraryDetector.java new file mode 100644 index 000000000..2fad1f3cf --- /dev/null +++ b/core-api/src/main/java/com/optimizely/ab/internal/JsonLibraryDetector.java @@ -0,0 +1,61 @@ +/** + * Copyright 2026, Optimizely Inc. and contributors + * + * 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 com.optimizely.ab.internal; + +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +/** + * Detects whether a supported JSON library is available on the classpath. + *

+ * Class names are only ever looked up if they are in a fixed allowlist of known JSON library classes, so + * {@code Class.forName} can never be driven by an arbitrary (for example user-supplied) class name. + * Classes are also loaded without being initialized, so no static initializer of the probed class runs. + */ +public final class JsonLibraryDetector { + + private static final Set ALLOWED_CLASS_NAMES = Collections.unmodifiableSet(new HashSet(Arrays.asList( + "com.google.gson.Gson", + "com.fasterxml.jackson.databind.ObjectMapper", + "org.json.JSONObject", + "org.json.simple.JSONObject" + ))); + + private JsonLibraryDetector() { + } + + /** + * @param className fully qualified class name to look for + * @return true if the class is one of the supported JSON library classes and is present on the classpath, + * false if it is absent or is not in the allowlist + */ + public static boolean isPresent(String className) { + if (className == null || !ALLOWED_CLASS_NAMES.contains(className)) { + return false; + } + + try { + // nosemgrep: java.lang.security.audit.unsafe-reflection.unsafe-reflection + // className is validated against the fixed allowlist above and the class is not initialized. + Class.forName(className, false, JsonLibraryDetector.class.getClassLoader()); + return true; + } catch (ClassNotFoundException e) { + return false; + } + } +} diff --git a/core-api/src/main/java/com/optimizely/ab/internal/JsonParserProvider.java b/core-api/src/main/java/com/optimizely/ab/internal/JsonParserProvider.java index 8bde2ac66..14cf95b14 100644 --- a/core-api/src/main/java/com/optimizely/ab/internal/JsonParserProvider.java +++ b/core-api/src/main/java/com/optimizely/ab/internal/JsonParserProvider.java @@ -34,12 +34,7 @@ public enum JsonParserProvider { } private boolean isAvailable() { - try { - Class.forName(className); - return true; - } catch (ClassNotFoundException e) { - return false; - } + return JsonLibraryDetector.isPresent(className); } public static JsonParserProvider getDefaultParser() { diff --git a/core-api/src/test/java/com/optimizely/ab/internal/JsonLibraryDetectorTest.java b/core-api/src/test/java/com/optimizely/ab/internal/JsonLibraryDetectorTest.java new file mode 100644 index 000000000..d3b242324 --- /dev/null +++ b/core-api/src/test/java/com/optimizely/ab/internal/JsonLibraryDetectorTest.java @@ -0,0 +1,47 @@ +/** + * Copyright 2026, Optimizely Inc. and contributors + * + * 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 com.optimizely.ab.internal; + +import org.junit.Test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class JsonLibraryDetectorTest { + + @Test + public void allowlistedLibrariesOnTheClasspathAreDetected() { + assertTrue(JsonLibraryDetector.isPresent("com.google.gson.Gson")); + assertTrue(JsonLibraryDetector.isPresent("com.fasterxml.jackson.databind.ObjectMapper")); + assertTrue(JsonLibraryDetector.isPresent("org.json.JSONObject")); + assertTrue(JsonLibraryDetector.isPresent("org.json.simple.JSONObject")); + } + + @Test + public void classNamesOutsideTheAllowlistAreRejectedEvenWhenTheClassExists() { + assertFalse(JsonLibraryDetector.isPresent("java.lang.String")); + assertFalse(JsonLibraryDetector.isPresent("java.lang.Runtime")); + assertFalse(JsonLibraryDetector.isPresent("com.optimizely.ab.internal.JsonLibraryDetector")); + } + + @Test + public void unknownAndMalformedNamesAreRejected() { + assertFalse(JsonLibraryDetector.isPresent(null)); + assertFalse(JsonLibraryDetector.isPresent("")); + assertFalse(JsonLibraryDetector.isPresent("not.a.real.Clazz")); + assertFalse(JsonLibraryDetector.isPresent("com.google.gson.Gson ")); + } +} diff --git a/java-quickstart/build.gradle b/java-quickstart/build.gradle index ef86b3045..ea4f15a2c 100644 --- a/java-quickstart/build.gradle +++ b/java-quickstart/build.gradle @@ -5,9 +5,10 @@ dependencies { implementation project(':core-httpclient-impl') // implementation group: 'com.google.code.gson', name: 'gson', version: gsonVersion - implementation 'com.fasterxml.jackson.core:jackson-core:2.17.0' - implementation 'com.fasterxml.jackson.core:jackson-annotations:2.17.0' - implementation 'com.fasterxml.jackson.core:jackson-databind:2.17.0' + // NOTE: jackson-annotations drops the patch version since Jackson 2.20 (2.23, not 2.23.0) + implementation 'com.fasterxml.jackson.core:jackson-core:2.23.0' + implementation 'com.fasterxml.jackson.core:jackson-annotations:2.23' + implementation 'com.fasterxml.jackson.core:jackson-databind:2.23.0' implementation group: 'org.apache.httpcomponents', name: 'httpclient', version: httpClientVersion implementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: log4jVersion From 1b361e32198c6e088386c586553850eb6b4234ad Mon Sep 17 00:00:00 2001 From: esrakartalOpt Date: Thu, 19 Nov 2026 15:35:54 -0600 Subject: [PATCH 2/2] [FSSDK-13470] Address review: nosemgrep placement and allowlist drift guard - Put the nosemgrep comment directly above Class.forName so Semgrep honours it. - Add JsonLibraryDetector.isAllowed and tests that check every ConfigParserSupplier and JsonParserProvider class name is allowlisted. Co-Authored-By: Claude Sonnet 5.5 --- .../ab/config/parser/DefaultConfigParser.java | 4 +++ .../ab/internal/JsonLibraryDetector.java | 12 +++++-- .../ab/internal/JsonParserProvider.java | 4 +++ .../parser/ConfigParserSupplierTest.java | 33 +++++++++++++++++++ .../ab/internal/JsonLibraryDetectorTest.java | 9 +++++ 5 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 core-api/src/test/java/com/optimizely/ab/config/parser/ConfigParserSupplierTest.java diff --git a/core-api/src/main/java/com/optimizely/ab/config/parser/DefaultConfigParser.java b/core-api/src/main/java/com/optimizely/ab/config/parser/DefaultConfigParser.java index bec1e40f4..30a568ae1 100644 --- a/core-api/src/main/java/com/optimizely/ab/config/parser/DefaultConfigParser.java +++ b/core-api/src/main/java/com/optimizely/ab/config/parser/DefaultConfigParser.java @@ -71,6 +71,10 @@ ConfigParser get() { return supplier.get(); } + String getClassName() { + return className; + } + private boolean isPresent() { return JsonLibraryDetector.isPresent(className); } diff --git a/core-api/src/main/java/com/optimizely/ab/internal/JsonLibraryDetector.java b/core-api/src/main/java/com/optimizely/ab/internal/JsonLibraryDetector.java index 2fad1f3cf..4900527ac 100644 --- a/core-api/src/main/java/com/optimizely/ab/internal/JsonLibraryDetector.java +++ b/core-api/src/main/java/com/optimizely/ab/internal/JsonLibraryDetector.java @@ -39,19 +39,27 @@ public final class JsonLibraryDetector { private JsonLibraryDetector() { } + /** + * @param className fully qualified class name to check + * @return true if the class name is in the allowlist of supported JSON library classes + */ + public static boolean isAllowed(String className) { + return className != null && ALLOWED_CLASS_NAMES.contains(className); + } + /** * @param className fully qualified class name to look for * @return true if the class is one of the supported JSON library classes and is present on the classpath, * false if it is absent or is not in the allowlist */ public static boolean isPresent(String className) { - if (className == null || !ALLOWED_CLASS_NAMES.contains(className)) { + if (!isAllowed(className)) { return false; } try { - // nosemgrep: java.lang.security.audit.unsafe-reflection.unsafe-reflection // className is validated against the fixed allowlist above and the class is not initialized. + // nosemgrep: java.lang.security.audit.unsafe-reflection.unsafe-reflection Class.forName(className, false, JsonLibraryDetector.class.getClassLoader()); return true; } catch (ClassNotFoundException e) { diff --git a/core-api/src/main/java/com/optimizely/ab/internal/JsonParserProvider.java b/core-api/src/main/java/com/optimizely/ab/internal/JsonParserProvider.java index 14cf95b14..a93f47bf3 100644 --- a/core-api/src/main/java/com/optimizely/ab/internal/JsonParserProvider.java +++ b/core-api/src/main/java/com/optimizely/ab/internal/JsonParserProvider.java @@ -33,6 +33,10 @@ public enum JsonParserProvider { this.className = className; } + String getClassName() { + return className; + } + private boolean isAvailable() { return JsonLibraryDetector.isPresent(className); } diff --git a/core-api/src/test/java/com/optimizely/ab/config/parser/ConfigParserSupplierTest.java b/core-api/src/test/java/com/optimizely/ab/config/parser/ConfigParserSupplierTest.java new file mode 100644 index 000000000..27c082c6d --- /dev/null +++ b/core-api/src/test/java/com/optimizely/ab/config/parser/ConfigParserSupplierTest.java @@ -0,0 +1,33 @@ +/** + * Copyright 2026, Optimizely Inc. and contributors + * + * 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 com.optimizely.ab.config.parser; + +import com.optimizely.ab.internal.JsonLibraryDetector; +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + +public class ConfigParserSupplierTest { + + @Test + public void everyConfigParserSupplierClassNameIsAllowlisted() { + // guards against adding a parser to the enum and forgetting the JsonLibraryDetector allowlist + for (DefaultConfigParser.ConfigParserSupplier supplier : DefaultConfigParser.ConfigParserSupplier.values()) { + assertTrue(supplier + " is missing from the JsonLibraryDetector allowlist", + JsonLibraryDetector.isAllowed(supplier.getClassName())); + } + } +} diff --git a/core-api/src/test/java/com/optimizely/ab/internal/JsonLibraryDetectorTest.java b/core-api/src/test/java/com/optimizely/ab/internal/JsonLibraryDetectorTest.java index d3b242324..2a835e59a 100644 --- a/core-api/src/test/java/com/optimizely/ab/internal/JsonLibraryDetectorTest.java +++ b/core-api/src/test/java/com/optimizely/ab/internal/JsonLibraryDetectorTest.java @@ -37,6 +37,15 @@ public void classNamesOutsideTheAllowlistAreRejectedEvenWhenTheClassExists() { assertFalse(JsonLibraryDetector.isPresent("com.optimizely.ab.internal.JsonLibraryDetector")); } + @Test + public void everyJsonParserProviderClassNameIsAllowlisted() { + // guards against adding a parser to the enum and forgetting the allowlist + for (JsonParserProvider provider : JsonParserProvider.values()) { + assertTrue(provider + " is missing from the JsonLibraryDetector allowlist", + JsonLibraryDetector.isAllowed(provider.getClassName())); + } + } + @Test public void unknownAndMalformedNamesAreRejected() { assertFalse(JsonLibraryDetector.isPresent(null));