You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by vy...@apache.org on 2024/07/29 13:50:22 UTC
(logging-log4j2) 08/12: Split tests into dedicated modules
This is an automated email from the ASF dual-hosted git repository.
vy pushed a commit to branch fuzzing
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit 7fc3eb2138868ced6fe4deaabc1cdf6e1c846ef2
Author: Volkan Yazıcı <vo...@yazi.ci>
AuthorDate: Mon Jul 29 12:54:34 2024 +0200
Split tests into dedicated modules
---
{log4j-fuzz-test => log4j-core-fuzz-test}/pom.xml | 30 +++-----------
.../core}/fuzz/PatternLayoutWithParamFuzzer.java | 12 +++---
.../fuzz/PatternLayoutWithoutParamFuzzer.java | 12 +++---
log4j-fuzz-test/pom.xml | 24 +----------
.../org/apache/logging/log4j/fuzz/FuzzingUtil.java | 39 ++++--------------
.../.log4j-plugin-processing-activator | 1 +
.../pom.xml | 19 ++++-----
.../fuzz/JsonEncodingLayoutTesterAppender.java | 4 +-
.../json}/fuzz/JsonTemplateLayoutCodecFuzzer.java | 6 ++-
.../fuzz/JsonTemplateLayoutWithParamFuzzer.java | 2 +-
.../fuzz/JsonTemplateLayoutWithoutParamFuzzer.java | 2 +-
.../log4j/layout/template/json/fuzz/JsonUtil.java | 47 ++++++++++++++++++++++
log4j-parent/pom.xml | 6 +++
pom.xml | 6 ++-
14 files changed, 102 insertions(+), 108 deletions(-)
diff --git a/log4j-fuzz-test/pom.xml b/log4j-core-fuzz-test/pom.xml
similarity index 72%
copy from log4j-fuzz-test/pom.xml
copy to log4j-core-fuzz-test/pom.xml
index f170eb536e..a46b3bf552 100644
--- a/log4j-fuzz-test/pom.xml
+++ b/log4j-core-fuzz-test/pom.xml
@@ -26,25 +26,23 @@
<relativePath>../log4j-parent</relativePath>
</parent>
- <artifactId>log4j-fuzz-test</artifactId>
+ <artifactId>log4j-core-fuzz-test</artifactId>
- <name>Apache Log4j fuzz tests</name>
+ <name>Apache Log4j Core fuzz tests</name>
<properties>
<log4j.docgen.skip>true</log4j.docgen.skip>
<bnd.baseline.skip>true</bnd.baseline.skip>
<maven.deploy.skip>true</maven.deploy.skip>
+ <maven.test.skip>true</maven.test.skip>
<sign.skip>true</sign.skip>
<!--
~ OSGi and JPMS options
-->
- <bnd-module-name>org.apache.logging.log4j.fuzz</bnd-module-name>
- <Fragment-Host>org.apache.logging.log4j.fuzz</Fragment-Host>
-
- <!-- dependency versions -->
- <json.version>20240303</json.version>
+ <bnd-module-name>org.apache.logging.log4j.core.fuzz</bnd-module-name>
+ <Fragment-Host>org.apache.logging.log4j.core.fuzz</Fragment-Host>
</properties>
@@ -52,23 +50,7 @@
<dependency>
<groupId>org.apache.logging.log4j</groupId>
- <artifactId>log4j-layout-template-json</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.assertj</groupId>
- <artifactId>assertj-core</artifactId>
- </dependency>
-
- <dependency>
- <groupId>com.code-intelligence</groupId>
- <artifactId>jazzer-standalone</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.json</groupId>
- <artifactId>json</artifactId>
- <version>${json.version}</version>
+ <artifactId>log4j-fuzz-test</artifactId>
</dependency>
</dependencies>
diff --git a/log4j-fuzz-test/src/main/java/org/apache/logging/log4j/fuzz/PatternLayoutWithParamFuzzer.java b/log4j-core-fuzz-test/src/main/java/org/apache/logging/log4j/core/fuzz/PatternLayoutWithParamFuzzer.java
similarity index 78%
rename from log4j-fuzz-test/src/main/java/org/apache/logging/log4j/fuzz/PatternLayoutWithParamFuzzer.java
rename to log4j-core-fuzz-test/src/main/java/org/apache/logging/log4j/core/fuzz/PatternLayoutWithParamFuzzer.java
index 4c2fc6d4d8..ca3d636c18 100644
--- a/log4j-fuzz-test/src/main/java/org/apache/logging/log4j/fuzz/PatternLayoutWithParamFuzzer.java
+++ b/log4j-core-fuzz-test/src/main/java/org/apache/logging/log4j/core/fuzz/PatternLayoutWithParamFuzzer.java
@@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.logging.log4j.fuzz;
+package org.apache.logging.log4j.core.fuzz;
import static org.apache.logging.log4j.fuzz.FuzzingUtil.createLoggerContext;
import static org.apache.logging.log4j.fuzz.FuzzingUtil.logWithParams;
@@ -22,6 +22,7 @@ import static org.apache.logging.log4j.fuzz.FuzzingUtil.logWithParams;
import com.code_intelligence.jazzer.api.FuzzedDataProvider;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.fuzz.LayoutTesterAppender;
public final class PatternLayoutWithParamFuzzer {
@@ -30,11 +31,10 @@ public final class PatternLayoutWithParamFuzzer {
private static final Logger LOGGER;
static {
- LOGGER_CONTEXT =
- createLoggerContext(LayoutTesterAppender.PLUGIN_NAME, configBuilder -> configBuilder
- .newLayout("PatternLayout")
- // Enforce using a single message-based converter, i.e., `MessagePatternConverter`
- .addAttribute("pattern", "%m"));
+ LOGGER_CONTEXT = createLoggerContext(LayoutTesterAppender.PLUGIN_NAME, configBuilder -> configBuilder
+ .newLayout("PatternLayout")
+ // Enforce using a single message-based converter, i.e., `MessagePatternConverter`
+ .addAttribute("pattern", "%m"));
LOGGER = LOGGER_CONTEXT.getLogger(PatternLayoutWithParamFuzzer.class);
}
diff --git a/log4j-fuzz-test/src/main/java/org/apache/logging/log4j/fuzz/PatternLayoutWithoutParamFuzzer.java b/log4j-core-fuzz-test/src/main/java/org/apache/logging/log4j/core/fuzz/PatternLayoutWithoutParamFuzzer.java
similarity index 78%
rename from log4j-fuzz-test/src/main/java/org/apache/logging/log4j/fuzz/PatternLayoutWithoutParamFuzzer.java
rename to log4j-core-fuzz-test/src/main/java/org/apache/logging/log4j/core/fuzz/PatternLayoutWithoutParamFuzzer.java
index 7f9212c054..6130c2de76 100644
--- a/log4j-fuzz-test/src/main/java/org/apache/logging/log4j/fuzz/PatternLayoutWithoutParamFuzzer.java
+++ b/log4j-core-fuzz-test/src/main/java/org/apache/logging/log4j/core/fuzz/PatternLayoutWithoutParamFuzzer.java
@@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.logging.log4j.fuzz;
+package org.apache.logging.log4j.core.fuzz;
import static org.apache.logging.log4j.fuzz.FuzzingUtil.createLoggerContext;
import static org.apache.logging.log4j.fuzz.FuzzingUtil.logWithoutParams;
@@ -22,6 +22,7 @@ import static org.apache.logging.log4j.fuzz.FuzzingUtil.logWithoutParams;
import com.code_intelligence.jazzer.api.FuzzedDataProvider;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.fuzz.LayoutTesterAppender;
public final class PatternLayoutWithoutParamFuzzer {
@@ -30,11 +31,10 @@ public final class PatternLayoutWithoutParamFuzzer {
private static final Logger LOGGER;
static {
- LOGGER_CONTEXT =
- createLoggerContext(LayoutTesterAppender.PLUGIN_NAME, configBuilder -> configBuilder
- .newLayout("PatternLayout")
- // Enforce using a single message-based converter, i.e., `MessagePatternConverter`
- .addAttribute("pattern", "%m"));
+ LOGGER_CONTEXT = createLoggerContext(LayoutTesterAppender.PLUGIN_NAME, configBuilder -> configBuilder
+ .newLayout("PatternLayout")
+ // Enforce using a single message-based converter, i.e., `MessagePatternConverter`
+ .addAttribute("pattern", "%m"));
LOGGER = LOGGER_CONTEXT.getLogger(PatternLayoutWithoutParamFuzzer.class);
}
diff --git a/log4j-fuzz-test/pom.xml b/log4j-fuzz-test/pom.xml
index f170eb536e..7222732044 100644
--- a/log4j-fuzz-test/pom.xml
+++ b/log4j-fuzz-test/pom.xml
@@ -35,6 +35,7 @@
<log4j.docgen.skip>true</log4j.docgen.skip>
<bnd.baseline.skip>true</bnd.baseline.skip>
<maven.deploy.skip>true</maven.deploy.skip>
+ <maven.test.skip>true</maven.test.skip>
<sign.skip>true</sign.skip>
<!--
@@ -43,16 +44,13 @@
<bnd-module-name>org.apache.logging.log4j.fuzz</bnd-module-name>
<Fragment-Host>org.apache.logging.log4j.fuzz</Fragment-Host>
- <!-- dependency versions -->
- <json.version>20240303</json.version>
-
</properties>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
- <artifactId>log4j-layout-template-json</artifactId>
+ <artifactId>log4j-core</artifactId>
</dependency>
<dependency>
@@ -65,24 +63,6 @@
<artifactId>jazzer-standalone</artifactId>
</dependency>
- <dependency>
- <groupId>org.json</groupId>
- <artifactId>json</artifactId>
- <version>${json.version}</version>
- </dependency>
-
</dependencies>
- <build>
- <plugins>
-
- <!-- Activate the inherited `generate-uber-jar` configuration -->
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-shade-plugin</artifactId>
- </plugin>
-
- </plugins>
- </build>
-
</project>
diff --git a/log4j-fuzz-test/src/main/java/org/apache/logging/log4j/fuzz/FuzzingUtil.java b/log4j-fuzz-test/src/main/java/org/apache/logging/log4j/fuzz/FuzzingUtil.java
index 6c6dfbe83c..36ff8a9103 100644
--- a/log4j-fuzz-test/src/main/java/org/apache/logging/log4j/fuzz/FuzzingUtil.java
+++ b/log4j-fuzz-test/src/main/java/org/apache/logging/log4j/fuzz/FuzzingUtil.java
@@ -17,9 +17,7 @@
package org.apache.logging.log4j.fuzz;
import com.code_intelligence.jazzer.api.FuzzedDataProvider;
-import com.code_intelligence.jazzer.api.FuzzerSecurityIssueCritical;
import edu.umd.cs.findbugs.annotations.Nullable;
-import java.nio.charset.StandardCharsets;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
@@ -36,34 +34,12 @@ import org.apache.logging.log4j.core.config.builder.api.LayoutComponentBuilder;
import org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuilder;
import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration;
import org.apache.logging.log4j.util.Strings;
-import org.json.JSONObject;
-import org.json.JSONTokener;
-final class FuzzingUtil {
+public final class FuzzingUtil {
private static final int MAX_STRING_LENGTH = 30;
- static void assertValidJson(final String json) {
- final byte[] jsonBytes = json.getBytes(StandardCharsets.UTF_8);
- assertValidJson(jsonBytes);
- }
-
- static void assertValidJson(final byte[] jsonBytes) {
- // We deliberately use an external library instead of using `JsonReader` from Log4j.
- // This is to fuzz Log4j components in isolation.
- // We are not using Jackson or Gson, since they have token limitations due to performance reasons.
- // That is, for instance, Jackson supports numbers up to 1000 digits, which the fuzzer quickly bumps into.
- try {
- // `JSONObject` requires the input to be a JSON object, hence we are ensuring it to be so:
- final String json = "{\"x\":" + new String(jsonBytes, StandardCharsets.UTF_8) + "}";
- final JSONTokener jsonTokener = new JSONTokener(json);
- new JSONObject(jsonTokener);
- } catch (final Exception error) {
- throw new FuzzerSecurityIssueCritical("malformed JSON output", error);
- }
- }
-
- static LoggerContext createLoggerContext(
+ public static LoggerContext createLoggerContext(
final String appenderPluginName,
final Function<ConfigurationBuilder<?>, LayoutComponentBuilder> layoutSupplier) {
@@ -92,7 +68,7 @@ final class FuzzingUtil {
return Configurator.initialize(config);
}
- static void logWithoutParams(final Logger logger, final FuzzedDataProvider dataProvider) {
+ public static void logWithoutParams(final Logger logger, final FuzzedDataProvider dataProvider) {
final String message = dataProvider.consumeString(MAX_STRING_LENGTH);
final Throwable throwable = createThrowableLogParam(dataProvider);
if (throwable != null) {
@@ -112,15 +88,16 @@ final class FuzzingUtil {
return new Exception(errorMessage);
}
- static void logWithParams(final Logger logger, final FuzzedDataProvider dataProvider) {
+ public static void logWithParams(final Logger logger, final FuzzedDataProvider dataProvider) {
final int paramCount = dataProvider.consumeInt(1, 3);
final String message = Strings.repeat("{}", paramCount);
- final Object[] params = IntStream.range(0, paramCount)
+ final Throwable throwable = createThrowableLogParam(dataProvider);
+ final Object[] params = IntStream.range(0, paramCount + (throwable != null ? 1 : 0))
.mapToObj(ignored -> createLogParam(dataProvider, true))
.toArray(Object[]::new);
- final Throwable throwable = createThrowableLogParam(dataProvider);
if (throwable != null) {
- logger.error(message, params, throwable);
+ params[paramCount] = throwable;
+ logger.error(message, params);
} else {
logger.error(message, params);
}
diff --git a/log4j-layout-template-json-fuzz-test/.log4j-plugin-processing-activator b/log4j-layout-template-json-fuzz-test/.log4j-plugin-processing-activator
new file mode 100644
index 0000000000..ba133f3696
--- /dev/null
+++ b/log4j-layout-template-json-fuzz-test/.log4j-plugin-processing-activator
@@ -0,0 +1 @@
+This file is here to activate the `plugin-processing` Maven profile.
diff --git a/log4j-fuzz-test/pom.xml b/log4j-layout-template-json-fuzz-test/pom.xml
similarity index 82%
copy from log4j-fuzz-test/pom.xml
copy to log4j-layout-template-json-fuzz-test/pom.xml
index f170eb536e..9989080006 100644
--- a/log4j-fuzz-test/pom.xml
+++ b/log4j-layout-template-json-fuzz-test/pom.xml
@@ -26,22 +26,24 @@
<relativePath>../log4j-parent</relativePath>
</parent>
- <artifactId>log4j-fuzz-test</artifactId>
+ <artifactId>log4j-layout-template-json-fuzz-test</artifactId>
- <name>Apache Log4j fuzz tests</name>
+ <name>Apache Log4j JSON Template Layout fuzz tests</name>
<properties>
<log4j.docgen.skip>true</log4j.docgen.skip>
<bnd.baseline.skip>true</bnd.baseline.skip>
<maven.deploy.skip>true</maven.deploy.skip>
+ <maven.install.skip>true</maven.install.skip>
+ <maven.test.skip>true</maven.test.skip>
<sign.skip>true</sign.skip>
<!--
~ OSGi and JPMS options
-->
- <bnd-module-name>org.apache.logging.log4j.fuzz</bnd-module-name>
- <Fragment-Host>org.apache.logging.log4j.fuzz</Fragment-Host>
+ <bnd-module-name>org.apache.logging.log4j.layout.template.json.fuzz</bnd-module-name>
+ <Fragment-Host>org.apache.logging.log4j.layout.template.json.fuzz</Fragment-Host>
<!-- dependency versions -->
<json.version>20240303</json.version>
@@ -56,13 +58,8 @@
</dependency>
<dependency>
- <groupId>org.assertj</groupId>
- <artifactId>assertj-core</artifactId>
- </dependency>
-
- <dependency>
- <groupId>com.code-intelligence</groupId>
- <artifactId>jazzer-standalone</artifactId>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-fuzz-test</artifactId>
</dependency>
<dependency>
diff --git a/log4j-fuzz-test/src/main/java/org/apache/logging/log4j/fuzz/JsonEncodingLayoutTesterAppender.java b/log4j-layout-template-json-fuzz-test/src/main/java/org/apache/logging/log4j/layout/template/json/fuzz/JsonEncodingLayoutTesterAppender.java
similarity index 94%
rename from log4j-fuzz-test/src/main/java/org/apache/logging/log4j/fuzz/JsonEncodingLayoutTesterAppender.java
rename to log4j-layout-template-json-fuzz-test/src/main/java/org/apache/logging/log4j/layout/template/json/fuzz/JsonEncodingLayoutTesterAppender.java
index d8bdc06d43..f85b8e4a05 100644
--- a/log4j-fuzz-test/src/main/java/org/apache/logging/log4j/fuzz/JsonEncodingLayoutTesterAppender.java
+++ b/log4j-layout-template-json-fuzz-test/src/main/java/org/apache/logging/log4j/layout/template/json/fuzz/JsonEncodingLayoutTesterAppender.java
@@ -14,9 +14,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.logging.log4j.fuzz;
+package org.apache.logging.log4j.layout.template.json.fuzz;
-import static org.apache.logging.log4j.fuzz.FuzzingUtil.assertValidJson;
+import static org.apache.logging.log4j.layout.template.json.fuzz.JsonUtil.assertValidJson;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.Serializable;
diff --git a/log4j-fuzz-test/src/main/java/org/apache/logging/log4j/fuzz/JsonTemplateLayoutCodecFuzzer.java b/log4j-layout-template-json-fuzz-test/src/main/java/org/apache/logging/log4j/layout/template/json/fuzz/JsonTemplateLayoutCodecFuzzer.java
similarity index 92%
rename from log4j-fuzz-test/src/main/java/org/apache/logging/log4j/fuzz/JsonTemplateLayoutCodecFuzzer.java
rename to log4j-layout-template-json-fuzz-test/src/main/java/org/apache/logging/log4j/layout/template/json/fuzz/JsonTemplateLayoutCodecFuzzer.java
index 9c4f782888..0ca1d1fc0f 100644
--- a/log4j-fuzz-test/src/main/java/org/apache/logging/log4j/fuzz/JsonTemplateLayoutCodecFuzzer.java
+++ b/log4j-layout-template-json-fuzz-test/src/main/java/org/apache/logging/log4j/layout/template/json/fuzz/JsonTemplateLayoutCodecFuzzer.java
@@ -14,7 +14,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.logging.log4j.fuzz;
+package org.apache.logging.log4j.layout.template.json.fuzz;
+
+import static org.apache.logging.log4j.layout.template.json.fuzz.JsonUtil.assertValidJson;
import com.code_intelligence.jazzer.api.FuzzedDataProvider;
import org.apache.logging.log4j.layout.template.json.JsonTemplateLayoutDefaults;
@@ -46,6 +48,6 @@ public final class JsonTemplateLayoutCodecFuzzer {
.build();
jsonWriter.writeValue(object);
final String actualJson = jsonWriter.getStringBuilder().toString();
- FuzzingUtil.assertValidJson(actualJson);
+ assertValidJson(actualJson);
}
}
diff --git a/log4j-fuzz-test/src/main/java/org/apache/logging/log4j/fuzz/JsonTemplateLayoutWithParamFuzzer.java b/log4j-layout-template-json-fuzz-test/src/main/java/org/apache/logging/log4j/layout/template/json/fuzz/JsonTemplateLayoutWithParamFuzzer.java
similarity index 96%
rename from log4j-fuzz-test/src/main/java/org/apache/logging/log4j/fuzz/JsonTemplateLayoutWithParamFuzzer.java
rename to log4j-layout-template-json-fuzz-test/src/main/java/org/apache/logging/log4j/layout/template/json/fuzz/JsonTemplateLayoutWithParamFuzzer.java
index ac7a426ef9..a696bb9ea7 100644
--- a/log4j-fuzz-test/src/main/java/org/apache/logging/log4j/fuzz/JsonTemplateLayoutWithParamFuzzer.java
+++ b/log4j-layout-template-json-fuzz-test/src/main/java/org/apache/logging/log4j/layout/template/json/fuzz/JsonTemplateLayoutWithParamFuzzer.java
@@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.logging.log4j.fuzz;
+package org.apache.logging.log4j.layout.template.json.fuzz;
import static org.apache.logging.log4j.fuzz.FuzzingUtil.createLoggerContext;
import static org.apache.logging.log4j.fuzz.FuzzingUtil.logWithParams;
diff --git a/log4j-fuzz-test/src/main/java/org/apache/logging/log4j/fuzz/JsonTemplateLayoutWithoutParamFuzzer.java b/log4j-layout-template-json-fuzz-test/src/main/java/org/apache/logging/log4j/layout/template/json/fuzz/JsonTemplateLayoutWithoutParamFuzzer.java
similarity index 96%
rename from log4j-fuzz-test/src/main/java/org/apache/logging/log4j/fuzz/JsonTemplateLayoutWithoutParamFuzzer.java
rename to log4j-layout-template-json-fuzz-test/src/main/java/org/apache/logging/log4j/layout/template/json/fuzz/JsonTemplateLayoutWithoutParamFuzzer.java
index d80fe5a8db..5094c8fd40 100644
--- a/log4j-fuzz-test/src/main/java/org/apache/logging/log4j/fuzz/JsonTemplateLayoutWithoutParamFuzzer.java
+++ b/log4j-layout-template-json-fuzz-test/src/main/java/org/apache/logging/log4j/layout/template/json/fuzz/JsonTemplateLayoutWithoutParamFuzzer.java
@@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.logging.log4j.fuzz;
+package org.apache.logging.log4j.layout.template.json.fuzz;
import static org.apache.logging.log4j.fuzz.FuzzingUtil.createLoggerContext;
import static org.apache.logging.log4j.fuzz.FuzzingUtil.logWithoutParams;
diff --git a/log4j-layout-template-json-fuzz-test/src/main/java/org/apache/logging/log4j/layout/template/json/fuzz/JsonUtil.java b/log4j-layout-template-json-fuzz-test/src/main/java/org/apache/logging/log4j/layout/template/json/fuzz/JsonUtil.java
new file mode 100644
index 0000000000..4024ea7c1b
--- /dev/null
+++ b/log4j-layout-template-json-fuzz-test/src/main/java/org/apache/logging/log4j/layout/template/json/fuzz/JsonUtil.java
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you 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.apache.logging.log4j.layout.template.json.fuzz;
+
+import com.code_intelligence.jazzer.api.FuzzerSecurityIssueCritical;
+import java.nio.charset.StandardCharsets;
+import org.json.JSONObject;
+import org.json.JSONTokener;
+
+final class JsonUtil {
+
+ static void assertValidJson(final String json) {
+ final byte[] jsonBytes = json.getBytes(StandardCharsets.UTF_8);
+ assertValidJson(jsonBytes);
+ }
+
+ static void assertValidJson(final byte[] jsonBytes) {
+ // We deliberately use an external library instead of using `JsonReader` from Log4j.
+ // This is to fuzz Log4j components in isolation.
+ // We are not using Jackson or Gson, since they have token limitations due to performance reasons.
+ // That is, for instance, Jackson supports numbers up to 1000 digits, which the fuzzer quickly bumps into.
+ try {
+ // `JSONObject` requires the input to be a JSON object, hence we are ensuring it to be so:
+ final String json = "{\"x\":" + new String(jsonBytes, StandardCharsets.UTF_8) + "}";
+ final JSONTokener jsonTokener = new JSONTokener(json);
+ new JSONObject(jsonTokener);
+ } catch (final Exception error) {
+ throw new FuzzerSecurityIssueCritical("malformed JSON output", error);
+ }
+ }
+
+ private JsonUtil() {}
+}
diff --git a/log4j-parent/pom.xml b/log4j-parent/pom.xml
index 2d5decdd83..47fbec8bf2 100644
--- a/log4j-parent/pom.xml
+++ b/log4j-parent/pom.xml
@@ -169,6 +169,12 @@
<!-- BEGIN: Modules that are consumed only internally -->
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-fuzz-test</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-layout-template-json-test</artifactId>
diff --git a/pom.xml b/pom.xml
index 65c7857efa..1761648f49 100644
--- a/pom.xml
+++ b/pom.xml
@@ -230,7 +230,7 @@
<!-- the parent POM must come first: -->
<module>log4j-parent</module>
- <!-- Next unpublished modules in alphabetical order: -->
+ <!-- Next unpublished, but internally and *implicitly* (e.g., as a plugin dependency) consumed modules in alphabetical order: -->
<module>log4j-api-java9</module>
<module>log4j-core-java9</module>
@@ -242,6 +242,7 @@
<module>log4j-appserver</module>
<module>log4j-cassandra</module>
<module>log4j-core</module>
+ <module>log4j-core-fuzz-test</module>
<module>log4j-core-its</module>
<module>log4j-core-test</module>
<module>log4j-couchdb</module>
@@ -257,6 +258,7 @@
<module>log4j-jdbc-dbcp2</module>
<module>log4j-jul</module>
<module>log4j-layout-template-json</module>
+ <module>log4j-layout-template-json-fuzz-test</module>
<module>log4j-layout-template-json-test</module>
<module>log4j-mongodb</module>
<module>log4j-mongodb4</module>
@@ -367,7 +369,7 @@
<dependencies>
<!-- List of managed dependencies (in alphabetical order) that are intended for public consumption.
- Modules that are not used anywhere (e.g., `log4j-osgi-test`) should not be placed here.
+ Modules that are not used anywhere (e.g., `log4j-core-its`, `log4j-osgi-test`) should not be placed here.
Modules that are used only internally (e.g., `log4j-layout-template-json-test`) should be added to `dependencyManagement > dependencies` in `log4j-parent/pom.xml`. -->
<dependency>