This is an automated email from the ASF dual-hosted git repository.

robertlazarski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/axis-axis2-java-core.git


The following commit(s) were added to refs/heads/master by this push:
     new 621be80824 Add disableJSONHtmlEncoding to serve JSON strings without 
HTML encoding
621be80824 is described below

commit 621be80824543c801aa358ec087bb80ec041f6fe
Author: Robert Lazarski <[email protected]>
AuthorDate: Wed Nov 18 05:04:15 2026 -1000

    Add disableJSONHtmlEncoding to serve JSON strings without HTML encoding
    
    Since 1.8.0 every String a Gson or Moshi JSON formatter writes goes
    through OWASP Encode.forHtmlContent as a reflected XSS control, with
    no way to turn it off and no mention outside the Spring Boot guides.
    That changes data for every non-HTML consumer and double-encodes
    values a service had already encoded. The new axis2.xml or services.xml
    parameter, default false, keeps the encoding on; set to true the six
    formatters write strings as produced. Documented in the JSON guides.
    
    Co-Authored-By: Claude Fable 5.1 <[email protected]>
---
 .../apache/axis2/json/factory/JsonConstant.java    |  11 ++
 .../axis2/json/factory/JsonHtmlEncoding.java       |  54 ++++++++
 .../org/apache/axis2/json/gson/JsonFormatter.java  |   9 +-
 .../json/gsonh2/EnhancedGsonJsonFormatter.java     |  12 +-
 .../org/apache/axis2/json/moshi/JsonFormatter.java |   8 +-
 .../json/moshih2/EnhancedMoshiJsonFormatter.java   |  11 +-
 .../streaming/JSONStreamingMessageFormatter.java   |   6 +-
 .../streaming/MoshiStreamingMessageFormatter.java  |  59 +++++----
 .../axis2/json/factory/JsonHtmlEncodingTest.java   | 137 +++++++++++++++++++++
 modules/kernel/conf/axis2.xml                      |   4 +
 modules/webapp/conf/axis2.xml                      |   4 +
 .../docs/json-springboot-tomcat11-userguide.xml    |   5 +-
 src/site/xdoc/docs/json-springboot-userguide.xml   |   7 +-
 src/site/xdoc/docs/json_support_gson.xml           |  14 +++
 14 files changed, 298 insertions(+), 43 deletions(-)

diff --git a/modules/json/src/org/apache/axis2/json/factory/JsonConstant.java 
b/modules/json/src/org/apache/axis2/json/factory/JsonConstant.java
index 7248ea233b..d0bccd40ee 100644
--- a/modules/json/src/org/apache/axis2/json/factory/JsonConstant.java
+++ b/modules/json/src/org/apache/axis2/json/factory/JsonConstant.java
@@ -36,6 +36,17 @@ public class JsonConstant {
      */
     public static final String FIELD_FILTER = "jsonFieldFilter";
 
+    /**
+     * Name of the parameter, in axis2.xml or services.xml, that turns off the 
HTML
+     * content encoding of String values in JSON responses. By default every 
String
+     * written by the Gson and Moshi formatters goes through OWASP
+     * {@code Encode.forHtmlContent}, so {@code &}, {@code <} and {@code >} 
are served
+     * as entities. Set to {@code true} to serve strings as the service 
produced them.
+     *
+     * @see JsonHtmlEncoding
+     */
+    public static final String DISABLE_JSON_HTML_ENCODING = 
"disableJSONHtmlEncoding";
+
     public static final String IS_JSON_STREAM = "isJsonStream";
 
     public static final String GSON_XML_STREAM_READER = "GsonXMLStreamReader";
diff --git 
a/modules/json/src/org/apache/axis2/json/factory/JsonHtmlEncoding.java 
b/modules/json/src/org/apache/axis2/json/factory/JsonHtmlEncoding.java
new file mode 100644
index 0000000000..3b304e159a
--- /dev/null
+++ b/modules/json/src/org/apache/axis2/json/factory/JsonHtmlEncoding.java
@@ -0,0 +1,54 @@
+/*
+ * 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.axis2.json.factory;
+
+import org.apache.axis2.context.MessageContext;
+import org.apache.axis2.description.Parameter;
+import org.apache.axis2.util.JavaUtils;
+
+/**
+ * Decides whether the JSON formatters HTML-encode the String values they 
write.
+ *
+ * <p>The encoding ({@code &}, {@code <} and {@code >} as entities, applied 
once per
+ * String) is on by default as a reflected-XSS control for consumers that 
place a
+ * response value in an HTML page. It also changes the data for every other 
consumer,
+ * so it can be turned off with the {@link 
JsonConstant#DISABLE_JSON_HTML_ENCODING}
+ * parameter. The parameter is looked up through the message's operation, 
service,
+ * service group and axis configuration, so it can be set globally in 
axis2.xml or
+ * for one service in services.xml.</p>
+ */
+public final class JsonHtmlEncoding {
+
+    private JsonHtmlEncoding() {
+    }
+
+    /**
+     * @param msgCtxt the outgoing message context, may be null
+     * @return true when {@link JsonConstant#DISABLE_JSON_HTML_ENCODING} is 
set to true
+     *         for this message; false otherwise, which keeps the encoding on
+     */
+    public static boolean isDisabled(MessageContext msgCtxt) {
+        if (msgCtxt == null) {
+            return false;
+        }
+        Parameter parameter = 
msgCtxt.getParameter(JsonConstant.DISABLE_JSON_HTML_ENCODING);
+        return parameter != null && 
JavaUtils.isTrueExplicitly(parameter.getValue());
+    }
+}
diff --git a/modules/json/src/org/apache/axis2/json/gson/JsonFormatter.java 
b/modules/json/src/org/apache/axis2/json/gson/JsonFormatter.java
index b58cb6a642..838783c390 100644
--- a/modules/json/src/org/apache/axis2/json/gson/JsonFormatter.java
+++ b/modules/json/src/org/apache/axis2/json/gson/JsonFormatter.java
@@ -28,6 +28,7 @@ import org.apache.axis2.AxisFault;
 import org.apache.axis2.Constants;
 import org.apache.axis2.context.MessageContext;
 import org.apache.axis2.json.factory.JsonConstant;
+import org.apache.axis2.json.factory.JsonHtmlEncoding;
 import org.apache.axis2.kernel.MessageFormatter;
 import org.apache.axis2.wsdl.WSDLConstants;
 import org.apache.commons.logging.Log;
@@ -99,9 +100,11 @@ public class JsonFormatter implements MessageFormatter {
 
             } else {
                 try {
-                    GsonBuilder gsonBuilder = new GsonBuilder(); 
-                    // XSS protection, encode JSON Strings as HTML
-                    gsonBuilder.registerTypeAdapter(String.class, new 
JsonHtmlEncoder());
+                    GsonBuilder gsonBuilder = new GsonBuilder();
+                    if (!JsonHtmlEncoding.isDisabled(outMsgCtxt)) {
+                        // XSS protection, encode JSON Strings as HTML
+                        gsonBuilder.registerTypeAdapter(String.class, new 
JsonHtmlEncoder());
+                    }
                     Gson gson = gsonBuilder.create();
                     jsonWriter.beginObject();
                     jsonWriter.name(JsonConstant.RESPONSE);
diff --git 
a/modules/json/src/org/apache/axis2/json/gsonh2/EnhancedGsonJsonFormatter.java 
b/modules/json/src/org/apache/axis2/json/gsonh2/EnhancedGsonJsonFormatter.java
index 1d7c15cc0a..021faaee12 100644
--- 
a/modules/json/src/org/apache/axis2/json/gsonh2/EnhancedGsonJsonFormatter.java
+++ 
b/modules/json/src/org/apache/axis2/json/gsonh2/EnhancedGsonJsonFormatter.java
@@ -28,6 +28,7 @@ import org.apache.axis2.Constants;
 import org.apache.axis2.context.MessageContext;
 import org.apache.axis2.context.ConfigurationContext;
 import org.apache.axis2.json.factory.JsonConstant;
+import org.apache.axis2.json.factory.JsonHtmlEncoding;
 import org.apache.axis2.json.gson.JsonHtmlEncoder;
 import org.apache.axis2.json.gson.GsonXMLStreamWriter;
 import org.apache.axis2.kernel.MessageFormatter;
@@ -277,10 +278,13 @@ public class EnhancedGsonJsonFormatter implements 
MessageFormatter {
 
         try {
             // Create optimized GSON instance with enhanced configuration
-            Gson gson = new GsonBuilder()
-                .registerTypeAdapter(String.class, new JsonHtmlEncoder())
-                .setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
-                .create();
+            GsonBuilder gsonBuilder = new GsonBuilder()
+                .setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
+            if (!JsonHtmlEncoding.isDisabled(outMsgCtxt)) {
+                // XSS protection, encode JSON Strings as HTML
+                gsonBuilder.registerTypeAdapter(String.class, new 
JsonHtmlEncoder());
+            }
+            Gson gson = gsonBuilder.create();
 
             // Create JsonWriter with character encoding
             if (charSetEncoding == null) {
diff --git a/modules/json/src/org/apache/axis2/json/moshi/JsonFormatter.java 
b/modules/json/src/org/apache/axis2/json/moshi/JsonFormatter.java
index 475d20f93b..9bd15bc159 100644
--- a/modules/json/src/org/apache/axis2/json/moshi/JsonFormatter.java
+++ b/modules/json/src/org/apache/axis2/json/moshi/JsonFormatter.java
@@ -32,6 +32,7 @@ import org.apache.axis2.AxisFault;
 import org.apache.axis2.Constants;
 import org.apache.axis2.context.MessageContext;
 import org.apache.axis2.json.factory.JsonConstant;
+import org.apache.axis2.json.factory.JsonHtmlEncoding;
 import org.apache.axis2.kernel.MessageFormatter;
 import org.apache.axis2.wsdl.WSDLConstants;
 import org.apache.commons.logging.Log;
@@ -60,7 +61,12 @@ public class JsonFormatter implements MessageFormatter {
         String msg;
 
         try {
-            Moshi moshi = new Moshi.Builder().add(String.class, new 
JsonHtmlEncoder()).add(Date.class, new Rfc3339DateJsonAdapter()).build();
+            Moshi.Builder moshiBuilder = new Moshi.Builder();
+            if (!JsonHtmlEncoding.isDisabled(outMsgCtxt)) {
+                // XSS protection, encode JSON Strings as HTML
+                moshiBuilder.add(String.class, new JsonHtmlEncoder());
+            }
+            Moshi moshi = moshiBuilder.add(Date.class, new 
Rfc3339DateJsonAdapter()).build();
             JsonAdapter<Object> adapter = moshi.adapter(Object.class);
             BufferedSink sink = Okio.buffer(Okio.sink(outputStream));
             jsonWriter = JsonWriter.of(sink);
diff --git 
a/modules/json/src/org/apache/axis2/json/moshih2/EnhancedMoshiJsonFormatter.java
 
b/modules/json/src/org/apache/axis2/json/moshih2/EnhancedMoshiJsonFormatter.java
index 6f42a26849..de4eabdcb1 100644
--- 
a/modules/json/src/org/apache/axis2/json/moshih2/EnhancedMoshiJsonFormatter.java
+++ 
b/modules/json/src/org/apache/axis2/json/moshih2/EnhancedMoshiJsonFormatter.java
@@ -31,6 +31,7 @@ import org.apache.axis2.Constants;
 import org.apache.axis2.context.MessageContext;
 import org.apache.axis2.context.ConfigurationContext;
 import org.apache.axis2.json.factory.JsonConstant;
+import org.apache.axis2.json.factory.JsonHtmlEncoding;
 import org.apache.axis2.json.moshi.JsonHtmlEncoder;
 import org.apache.axis2.json.moshi.MoshiXMLStreamWriter;
 import org.apache.axis2.kernel.MessageFormatter;
@@ -278,10 +279,12 @@ public class EnhancedMoshiJsonFormatter implements 
MessageFormatter {
 
         try {
             // Create optimized Moshi instance with enhanced adapters
-            Moshi moshi = new Moshi.Builder()
-                .add(String.class, new JsonHtmlEncoder())
-                .add(Date.class, new Rfc3339DateJsonAdapter())
-                .build();
+            Moshi.Builder moshiBuilder = new Moshi.Builder();
+            if (!JsonHtmlEncoding.isDisabled(outMsgCtxt)) {
+                // XSS protection, encode JSON Strings as HTML
+                moshiBuilder.add(String.class, new JsonHtmlEncoder());
+            }
+            Moshi moshi = moshiBuilder.add(Date.class, new 
Rfc3339DateJsonAdapter()).build();
             JsonAdapter<Object> adapter = moshi.adapter(Object.class);
 
             // Create buffered sink with optimization (concept from HTTP/2 
integration)
diff --git 
a/modules/json/src/org/apache/axis2/json/streaming/JSONStreamingMessageFormatter.java
 
b/modules/json/src/org/apache/axis2/json/streaming/JSONStreamingMessageFormatter.java
index 4e37684950..ac89e4b901 100644
--- 
a/modules/json/src/org/apache/axis2/json/streaming/JSONStreamingMessageFormatter.java
+++ 
b/modules/json/src/org/apache/axis2/json/streaming/JSONStreamingMessageFormatter.java
@@ -30,6 +30,7 @@ import org.apache.axis2.context.MessageContext;
 import org.apache.axis2.description.AxisService;
 import org.apache.axis2.description.Parameter;
 import org.apache.axis2.json.factory.JsonConstant;
+import org.apache.axis2.json.factory.JsonHtmlEncoding;
 import org.apache.axis2.json.gson.GsonXMLStreamWriter;
 import org.apache.axis2.json.gson.JsonHtmlEncoder;
 import org.apache.axis2.kernel.MessageFormatter;
@@ -226,7 +227,10 @@ public class JSONStreamingMessageFormatter implements 
MessageFormatter {
                                      Object retObj) throws AxisFault {
         try {
             GsonBuilder gsonBuilder = new GsonBuilder();
-            gsonBuilder.registerTypeAdapter(String.class, new 
JsonHtmlEncoder());
+            if (!JsonHtmlEncoding.isDisabled(outMsgCtxt)) {
+                // XSS protection, encode JSON Strings as HTML
+                gsonBuilder.registerTypeAdapter(String.class, new 
JsonHtmlEncoder());
+            }
             Gson gson = gsonBuilder.create();
 
             jsonWriter.beginObject();
diff --git 
a/modules/json/src/org/apache/axis2/json/streaming/MoshiStreamingMessageFormatter.java
 
b/modules/json/src/org/apache/axis2/json/streaming/MoshiStreamingMessageFormatter.java
index 2319d3d616..5ecd21fcfa 100644
--- 
a/modules/json/src/org/apache/axis2/json/streaming/MoshiStreamingMessageFormatter.java
+++ 
b/modules/json/src/org/apache/axis2/json/streaming/MoshiStreamingMessageFormatter.java
@@ -35,6 +35,7 @@ import org.apache.axis2.context.MessageContext;
 import org.apache.axis2.description.AxisService;
 import org.apache.axis2.description.Parameter;
 import org.apache.axis2.json.factory.JsonConstant;
+import org.apache.axis2.json.factory.JsonHtmlEncoding;
 import org.apache.axis2.json.moshi.JsonHtmlEncoder;
 import org.apache.axis2.json.moshi.MoshiXMLStreamWriter;
 import org.apache.axis2.kernel.MessageFormatter;
@@ -105,11 +106,7 @@ public class MoshiStreamingMessageFormatter implements 
MessageFormatter {
                 + flushInterval + " byte flush interval");
         }
 
-        Moshi moshi = new Moshi.Builder()
-            .add(String.class, new JsonHtmlEncoder())
-            .add(Date.class, new Rfc3339DateJsonAdapter())
-            .build();
-        JsonAdapter<Object> adapter = moshi.adapter(Object.class);
+        Moshi moshi = moshiFor(outMsgCtxt);
 
         try (BufferedSink sink = Okio.buffer(Okio.sink(flushingStream));
              JsonWriter jsonWriter = JsonWriter.of(sink)) {
@@ -123,7 +120,7 @@ public class MoshiStreamingMessageFormatter implements 
MessageFormatter {
                 writeElementResponse(outMsgCtxt, jsonWriter, preserve);
 
             } else {
-                writeObjectResponse(jsonWriter, adapter, retObj, outMsgCtxt);
+                writeObjectResponse(jsonWriter, moshi, retObj, outMsgCtxt);
             }
 
             jsonWriter.flush();
@@ -197,7 +194,7 @@ public class MoshiStreamingMessageFormatter implements 
MessageFormatter {
      * never written, so no capture buffer is needed.</p>
      */
     @SuppressWarnings("unchecked")
-    private void writeObjectResponse(JsonWriter jsonWriter, 
JsonAdapter<Object> adapter,
+    private void writeObjectResponse(JsonWriter jsonWriter, Moshi moshi,
                                      Object retObj, MessageContext outMsgCtxt) 
throws AxisFault {
         try {
             jsonWriter.beginObject();
@@ -205,9 +202,9 @@ public class MoshiStreamingMessageFormatter implements 
MessageFormatter {
 
             Object filterProp = 
outMsgCtxt.getProperty(JsonConstant.FIELD_FILTER);
             if (filterProp instanceof Set && !((Set<?>) filterProp).isEmpty()) 
{
-                writeFilteredObject(jsonWriter, retObj, (Set<String>) 
filterProp);
+                writeFilteredObject(jsonWriter, moshi, retObj, (Set<String>) 
filterProp);
             } else {
-                adapter.toJson(jsonWriter, retObj);
+                moshi.adapter(Object.class).toJson(jsonWriter, retObj);
             }
 
             jsonWriter.endObject();
@@ -234,11 +231,19 @@ public class MoshiStreamingMessageFormatter implements 
MessageFormatter {
      * through to the client as soon as serialization produces enough bytes
      * to trigger a flush.</p>
      */
-    /** Shared Moshi instance for field-level serialization (thread-safe, 
reusable). */
-    private static final Moshi FIELD_FILTER_MOSHI = new Moshi.Builder()
+    /** Shared Moshi instances (thread-safe, reusable), with and without the 
HTML encoding of Strings. */
+    private static final Moshi HTML_ENCODING_MOSHI = new Moshi.Builder()
         .add(String.class, new JsonHtmlEncoder())
         .add(Date.class, new Rfc3339DateJsonAdapter())
         .build();
+    private static final Moshi PLAIN_MOSHI = new Moshi.Builder()
+        .add(Date.class, new Rfc3339DateJsonAdapter())
+        .build();
+
+    /** The Moshi to serialize this message with, see {@link 
JsonHtmlEncoding#isDisabled}. */
+    private static Moshi moshiFor(MessageContext msgCtxt) {
+        return JsonHtmlEncoding.isDisabled(msgCtxt) ? PLAIN_MOSHI : 
HTML_ENCODING_MOSHI;
+    }
 
     /**
      * Serialize only the requested fields from the return object.
@@ -289,7 +294,7 @@ public class MoshiStreamingMessageFormatter implements 
MessageFormatter {
      * cannot be selected, as the dot is always interpreted as a nesting
      * delimiter.</p>
      */
-    private void writeFilteredObject(JsonWriter jsonWriter, Object retObj,
+    private void writeFilteredObject(JsonWriter jsonWriter, Moshi moshi, 
Object retObj,
                                      Set<String> allowedFields)
             throws IOException {
 
@@ -377,7 +382,7 @@ public class MoshiStreamingMessageFormatter implements 
MessageFormatter {
                 Set<String> subFields = nestedSpecs.get(field.getName());
                 if (subFields != null && value != null) {
                     // Nested filtering: serialize container but prune its 
contents
-                    writeFilteredNested(jsonWriter, value, subFields,
+                    writeFilteredNested(jsonWriter, moshi, value, subFields,
                         field.getGenericType(), fieldCache);
                 } else if (value == null) {
                     jsonWriter.nullValue();
@@ -385,7 +390,7 @@ public class MoshiStreamingMessageFormatter implements 
MessageFormatter {
                     // No nested specs: serialize the entire field value as-is
                     @SuppressWarnings("unchecked")
                     JsonAdapter<Object> fieldAdapter =
-                        (JsonAdapter<Object>) FIELD_FILTER_MOSHI.adapter(
+                        (JsonAdapter<Object>) moshi.adapter(
                             field.getGenericType());
                     fieldAdapter.toJson(jsonWriter, value);
                 }
@@ -456,7 +461,7 @@ public class MoshiStreamingMessageFormatter implements 
MessageFormatter {
      * {"records":[{"id":"item-1"},{"id":"item-2"}]}}}}</p>
      */
     @SuppressWarnings("unchecked")
-    private void writeFilteredNested(JsonWriter jsonWriter, Object value,
+    private void writeFilteredNested(JsonWriter jsonWriter, Moshi moshi, 
Object value,
                                      Set<String> subFields, Type declaredType,
                                      java.util.Map<Class<?>, List<Field>> 
fieldCache)
             throws IOException {
@@ -496,14 +501,14 @@ public class MoshiStreamingMessageFormatter implements 
MessageFormatter {
                 if (element == null) {
                     jsonWriter.nullValue();
                 } else if (element instanceof java.util.Map) {
-                    writeFilteredMap(jsonWriter, (java.util.Map<?, ?>) element,
+                    writeFilteredMap(jsonWriter, moshi, (java.util.Map<?, ?>) 
element,
                         immediateKeep, deeperSpecs, fieldCache);
                 } else if (element instanceof java.util.Collection) {
                     // Nested collection — recurse with the same sub-fields
-                    writeFilteredNested(jsonWriter, element, subFields,
+                    writeFilteredNested(jsonWriter, moshi, element, subFields,
                         Object.class, fieldCache);
                 } else {
-                    writeFilteredPojo(jsonWriter, element,
+                    writeFilteredPojo(jsonWriter, moshi, element,
                         immediateKeep, deeperSpecs, fieldCache);
                 }
             }
@@ -515,18 +520,18 @@ public class MoshiStreamingMessageFormatter implements 
MessageFormatter {
              * where keys are "records", "metadata", "diagnostics", etc.
              * Filter keys by immediateKeep, then recurse into deeperSpecs.
              */
-            writeFilteredMap(jsonWriter, (java.util.Map<?, ?>) value,
+            writeFilteredMap(jsonWriter, moshi, (java.util.Map<?, ?>) value,
                 immediateKeep, deeperSpecs, fieldCache);
 
         } else if (value.getClass().getName().startsWith("java.lang.")) {
             /* Scalar — nothing to filter inside. */
             JsonAdapter<Object> adapter =
-                (JsonAdapter<Object>) FIELD_FILTER_MOSHI.adapter(declaredType);
+                (JsonAdapter<Object>) moshi.adapter(declaredType);
             adapter.toJson(jsonWriter, value);
 
         } else {
             /* Single POJO — filter its declared fields recursively. */
-            writeFilteredPojo(jsonWriter, value, immediateKeep, deeperSpecs, 
fieldCache);
+            writeFilteredPojo(jsonWriter, moshi, value, immediateKeep, 
deeperSpecs, fieldCache);
         }
     }
 
@@ -539,7 +544,7 @@ public class MoshiStreamingMessageFormatter implements 
MessageFormatter {
      * that have {@code deeperSpecs}, the value is recursively filtered.</p>
      */
     @SuppressWarnings("unchecked")
-    private void writeFilteredMap(JsonWriter jsonWriter, java.util.Map<?, ?> 
map,
+    private void writeFilteredMap(JsonWriter jsonWriter, Moshi moshi, 
java.util.Map<?, ?> map,
                                   Set<String> immediateKeep,
                                   java.util.Map<String, Set<String>> 
deeperSpecs,
                                   java.util.Map<Class<?>, List<Field>> 
fieldCache)
@@ -559,14 +564,14 @@ public class MoshiStreamingMessageFormatter implements 
MessageFormatter {
             Set<String> deeper = deeperSpecs.get(key);
             if (deeper != null && entryValue != null) {
                 // This key has deeper sub-field specs — recurse
-                writeFilteredNested(jsonWriter, entryValue, deeper,
+                writeFilteredNested(jsonWriter, moshi, entryValue, deeper,
                     Object.class, fieldCache);
             } else if (entryValue == null) {
                 jsonWriter.nullValue();
             } else {
                 // No deeper filtering — serialize the full value
                 JsonAdapter<Object> valAdapter =
-                    (JsonAdapter<Object>) 
FIELD_FILTER_MOSHI.adapter(Object.class);
+                    (JsonAdapter<Object>) moshi.adapter(Object.class);
                 valAdapter.toJson(jsonWriter, entryValue);
             }
         }
@@ -586,7 +591,7 @@ public class MoshiStreamingMessageFormatter implements 
MessageFormatter {
      * collection where every element is the same type.</p>
      */
     @SuppressWarnings("unchecked")
-    private void writeFilteredPojo(JsonWriter jsonWriter, Object pojo,
+    private void writeFilteredPojo(JsonWriter jsonWriter, Moshi moshi, Object 
pojo,
                                    Set<String> immediateKeep,
                                    java.util.Map<String, Set<String>> 
deeperSpecs,
                                    java.util.Map<Class<?>, List<Field>> 
fieldCache)
@@ -615,13 +620,13 @@ public class MoshiStreamingMessageFormatter implements 
MessageFormatter {
             Set<String> deeper = deeperSpecs != null ? 
deeperSpecs.get(field.getName()) : null;
             if (deeper != null && value != null) {
                 // Recurse into this field's value
-                writeFilteredNested(jsonWriter, value, deeper,
+                writeFilteredNested(jsonWriter, moshi, value, deeper,
                     field.getGenericType(), fieldCache);
             } else if (value == null) {
                 jsonWriter.nullValue();
             } else {
                 JsonAdapter<Object> adapter =
-                    (JsonAdapter<Object>) FIELD_FILTER_MOSHI.adapter(
+                    (JsonAdapter<Object>) moshi.adapter(
                         field.getGenericType());
                 adapter.toJson(jsonWriter, value);
             }
diff --git 
a/modules/json/test/org/apache/axis2/json/factory/JsonHtmlEncodingTest.java 
b/modules/json/test/org/apache/axis2/json/factory/JsonHtmlEncodingTest.java
new file mode 100644
index 0000000000..2ff03d5ac2
--- /dev/null
+++ b/modules/json/test/org/apache/axis2/json/factory/JsonHtmlEncodingTest.java
@@ -0,0 +1,137 @@
+/*
+ * 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.axis2.json.factory;
+
+import com.google.gson.JsonParser;
+import org.apache.axiom.om.OMOutputFormat;
+import org.apache.axis2.Constants;
+import org.apache.axis2.context.ConfigurationContext;
+import org.apache.axis2.context.MessageContext;
+import org.apache.axis2.description.AxisService;
+import org.apache.axis2.description.Parameter;
+import org.apache.axis2.engine.AxisConfiguration;
+import org.apache.axis2.json.gsonh2.EnhancedGsonJsonFormatter;
+import org.apache.axis2.json.moshih2.EnhancedMoshiJsonFormatter;
+import org.apache.axis2.json.streaming.JSONStreamingMessageFormatter;
+import org.apache.axis2.json.streaming.MoshiStreamingMessageFormatter;
+import org.apache.axis2.kernel.MessageFormatter;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.ByteArrayOutputStream;
+import java.util.Arrays;
+import java.util.LinkedHashSet;
+
+/**
+ * The {@code disableJSONHtmlEncoding} parameter turns off the HTML content 
encoding
+ * of String values in every JSON formatter; without it the encoding stays on.
+ */
+public class JsonHtmlEncodingTest {
+
+    private static final String RAW = "AT&T <b>bold</b>";
+    private static final String ENCODED = "AT&amp;T &lt;b&gt;bold&lt;/b&gt;";
+
+    public static class Payload {
+        public String text;
+        public int count;
+
+        public Payload(String text, int count) {
+            this.text = text;
+            this.count = count;
+        }
+    }
+
+    private static MessageContext context(boolean disabledGlobally) throws 
Exception {
+        MessageContext ctx = new MessageContext();
+        ctx.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, 
"UTF-8");
+        ctx.setProperty(JsonConstant.RETURN_OBJECT, new Payload(RAW, 7));
+        ctx.setProperty(JsonConstant.RETURN_TYPE, Payload.class);
+        AxisConfiguration axisConfig = new AxisConfiguration();
+        if (disabledGlobally) {
+            axisConfig.addParameter(new 
Parameter(JsonConstant.DISABLE_JSON_HTML_ENCODING, "true"));
+        }
+        ctx.setConfigurationContext(new ConfigurationContext(axisConfig));
+        return ctx;
+    }
+
+    private static String textOf(MessageFormatter formatter, MessageContext 
ctx) throws Exception {
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        formatter.writeTo(ctx, new OMOutputFormat(), out, false);
+        String json = out.toString("UTF-8");
+        return JsonParser.parseString(json).getAsJsonObject()
+                
.getAsJsonObject(JsonConstant.RESPONSE).get("text").getAsString();
+    }
+
+    private static MessageFormatter[] formatters() {
+        return new MessageFormatter[] {
+                new org.apache.axis2.json.gson.JsonFormatter(),
+                new org.apache.axis2.json.moshi.JsonFormatter(),
+                new JSONStreamingMessageFormatter(),
+                new MoshiStreamingMessageFormatter(),
+                new EnhancedGsonJsonFormatter(),
+                new EnhancedMoshiJsonFormatter()
+        };
+    }
+
+    @Test
+    public void encodesByDefault() throws Exception {
+        for (MessageFormatter formatter : formatters()) {
+            Assert.assertEquals(formatter.getClass().getName(), ENCODED,
+                    textOf(formatter, context(false)));
+        }
+    }
+
+    @Test
+    public void parameterTurnsEncodingOff() throws Exception {
+        for (MessageFormatter formatter : formatters()) {
+            Assert.assertEquals(formatter.getClass().getName(), RAW,
+                    textOf(formatter, context(true)));
+        }
+    }
+
+    @Test
+    public void parameterOnTheServiceAlone() throws Exception {
+        MessageContext ctx = context(false);
+        AxisService service = new AxisService("plainTextService");
+        service.addParameter(new 
Parameter(JsonConstant.DISABLE_JSON_HTML_ENCODING, "true"));
+        ctx.setAxisService(service);
+        Assert.assertEquals(RAW, textOf(new MoshiStreamingMessageFormatter(), 
ctx));
+        Assert.assertEquals(RAW, textOf(new 
org.apache.axis2.json.gson.JsonFormatter(), ctx));
+    }
+
+    @Test
+    public void fieldFilteringHonoursTheParameter() throws Exception {
+        MessageContext on = context(false);
+        on.setProperty(JsonConstant.FIELD_FILTER, new 
LinkedHashSet<String>(Arrays.asList("text")));
+        Assert.assertEquals(ENCODED, textOf(new 
MoshiStreamingMessageFormatter(), on));
+        Assert.assertEquals(ENCODED, textOf(new 
JSONStreamingMessageFormatter(), on));
+
+        MessageContext off = context(true);
+        off.setProperty(JsonConstant.FIELD_FILTER, new 
LinkedHashSet<String>(Arrays.asList("text")));
+        Assert.assertEquals(RAW, textOf(new MoshiStreamingMessageFormatter(), 
off));
+        Assert.assertEquals(RAW, textOf(new JSONStreamingMessageFormatter(), 
off));
+    }
+
+    @Test
+    public void nullContextKeepsEncodingOn() {
+        Assert.assertFalse(JsonHtmlEncoding.isDisabled(null));
+        Assert.assertFalse(JsonHtmlEncoding.isDisabled(new MessageContext()));
+    }
+}
diff --git a/modules/kernel/conf/axis2.xml b/modules/kernel/conf/axis2.xml
index 2266400d00..8b1c63ff9e 100644
--- a/modules/kernel/conf/axis2.xml
+++ b/modules/kernel/conf/axis2.xml
@@ -26,6 +26,10 @@
     <parameter name="enableMTOM">false</parameter>
     <parameter name="enableSwA">false</parameter>
     <parameter name="enableJSONOnly">false</parameter>
+    <!-- The Gson and Moshi JSON formatters HTML-encode every String value 
they write
+         (& < > become entities) as a reflected XSS control. Set to true to 
serve strings
+         exactly as the service produced them. Can also be set per service in 
services.xml. -->
+    <parameter name="disableJSONHtmlEncoding">false</parameter>
 
     <!--Uncomment if you want to enable file caching for attachments -->
     <!--parameter name="cacheAttachments">true</parameter>
diff --git a/modules/webapp/conf/axis2.xml b/modules/webapp/conf/axis2.xml
index 167acc086b..4ade43bd05 100644
--- a/modules/webapp/conf/axis2.xml
+++ b/modules/webapp/conf/axis2.xml
@@ -26,6 +26,10 @@
     <parameter name="enableMTOM">false</parameter>
     <parameter name="enableSwA">false</parameter>
     <parameter name="enableJSONOnly">false</parameter>
+    <!-- The Gson and Moshi JSON formatters HTML-encode every String value 
they write
+         (& < > become entities) as a reflected XSS control. Set to true to 
serve strings
+         exactly as the service produced them. Can also be set per service in 
services.xml. -->
+    <parameter name="disableJSONHtmlEncoding">false</parameter>
 
     <!--Uncomment if you want to enable file caching for attachments -->
     <!--parameter name="cacheAttachments">true</parameter>
diff --git a/src/site/xdoc/docs/json-springboot-tomcat11-userguide.xml 
b/src/site/xdoc/docs/json-springboot-tomcat11-userguide.xml
index 6815bbb675..12921b5713 100644
--- a/src/site/xdoc/docs/json-springboot-tomcat11-userguide.xml
+++ b/src/site/xdoc/docs/json-springboot-tomcat11-userguide.xml
@@ -297,7 +297,10 @@ Also provided is a test service, TestwsService. It 
includes two POJO Objects as
 be expected, TestwsRequest and TestwsResponse. This service attempts to return
 a String with some Javascript, that is HTML encoded by Axis2 and thereby
 eliminating the possibility of a Javascript engine executing the response i.e. 
a
-reflected XSS attack.
+reflected XSS attack. This encoding applies to every String in every JSON 
response
+and changes data such as <tt>AT&amp;T</tt>; it can be turned off with the
+<tt>disableJSONHtmlEncoding</tt> parameter, see the
+<a href="json_support_gson.html#native_approach">JSON Support Guide</a>.
 </p>
 
 <p>
diff --git a/src/site/xdoc/docs/json-springboot-userguide.xml 
b/src/site/xdoc/docs/json-springboot-userguide.xml
index 0813fc01de..64dd3a9048 100644
--- a/src/site/xdoc/docs/json-springboot-userguide.xml
+++ b/src/site/xdoc/docs/json-springboot-userguide.xml
@@ -199,8 +199,11 @@ enableJSONOnly creates a secure, hardened API endpoint 
that accepts only authent
 Also provided is a test service, TestwsService. It includes two POJO Objects 
as would
 be expected, TestwsRequest and TestwsResponse. This service attempts to return 
 a String with some Javascript, that is HTML encoded by Axis2 and thereby
-eliminating the possibility of a Javascript engine executing the response i.e. 
a 
-reflected XSS attack. 
+eliminating the possibility of a Javascript engine executing the response i.e. 
a
+reflected XSS attack. This encoding applies to every String in every JSON 
response
+and changes data such as <tt>AT&amp;T</tt>; it can be turned off with the
+<tt>disableJSONHtmlEncoding</tt> parameter, see the
+<a href="json_support_gson.html#native_approach">JSON Support Guide</a>.
 </p>
 
 <p>
diff --git a/src/site/xdoc/docs/json_support_gson.xml 
b/src/site/xdoc/docs/json_support_gson.xml
index 9bb4a00dbb..cf7d2f4dd2 100644
--- a/src/site/xdoc/docs/json_support_gson.xml
+++ b/src/site/xdoc/docs/json_support_gson.xml
@@ -137,6 +137,20 @@
                 a pure JSON string to the wire using JsonWriter in google-gson 
API. The native approach doesn’t support
                 namespaces. If you need namespace support with JSON then go 
through XML Stream API based approach.</p>
 
+            <p>Every String value that the Native approach formatters write 
(the Gson and Moshi
+                JsonFormatter, the streaming formatters and the HTTP/2 
enhanced formatters) is HTML
+                content encoded once: <tt>&amp;</tt>, <tt>&lt;</tt> and 
<tt>&gt;</tt> are served as
+                <tt>&amp;amp;</tt>, <tt>&amp;lt;</tt> and <tt>&amp;gt;</tt>. 
This protects a browser that
+                inserts a response value into a page as HTML against reflected 
XSS, but it also changes
+                the data for every other consumer: <tt>AT&amp;T</tt> arrives 
as <tt>AT&amp;amp;T</tt>, and
+                a value the service had already encoded is encoded twice. 
Request strings are not
+                decoded. To serve strings exactly as the service produced 
them, set the
+                <tt>disableJSONHtmlEncoding</tt> parameter to <tt>true</tt> in 
<tt>axis2.xml</tt>, or in
+                <tt>services.xml</tt> to turn it off for one service only. The 
consumer is then
+                responsible for encoding a value before placing it in HTML.</p>
+<pre><![CDATA[
+    <parameter name="disableJSONHtmlEncoding">true</parameter>
+]]></pre>
 
         </section>
 

Reply via email to