Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -70,13 +71,12 @@ ConfigParser get() {
return supplier.get();
}

String getClassName() {
return className;
}

private boolean isPresent() {
try {
Class.forName(className);
return true;
} catch (ClassNotFoundException e) {
return false;
}
return JsonLibraryDetector.isPresent(className);
}
}
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 ========//
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* 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.
* <p>
* 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<String> ALLOWED_CLASS_NAMES = Collections.unmodifiableSet(new HashSet<String>(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 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 (!isAllowed(className)) {
return false;
}

try {
// 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) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,12 @@ public enum JsonParserProvider {
this.className = className;
}

String getClassName() {
return className;
}

private boolean isAvailable() {
try {
Class.forName(className);
return true;
} catch (ClassNotFoundException e) {
return false;
}
return JsonLibraryDetector.isPresent(className);
}

public static JsonParserProvider getDefaultParser() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* 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 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));
assertFalse(JsonLibraryDetector.isPresent(""));
assertFalse(JsonLibraryDetector.isPresent("not.a.real.Clazz"));
assertFalse(JsonLibraryDetector.isPresent("com.google.gson.Gson "));
}
}
7 changes: 4 additions & 3 deletions java-quickstart/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading