mraible commented on code in PR #263:
URL: https://github.com/apache/roller/pull/263#discussion_r4576658018


##########
app/src/main/java/org/apache/roller/weblogger/webservices/xmlrpc/XmlRpcRequestParser.java:
##########
@@ -0,0 +1,269 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  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.  For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
+
+package org.apache.roller.weblogger.webservices.xmlrpc;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.time.DateTimeException;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
+import java.util.Base64;
+import java.util.Date;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.roller.weblogger.util.SafeSAXBuilder;
+import org.jdom2.Document;
+import org.jdom2.Element;
+import org.jdom2.JDOMException;
+import org.jdom2.Namespace;
+
+/**
+ * Reads an XML-RPC <code>methodCall</code>.
+ *
+ * <p>Only the value types of the XML-RPC specification are accepted:
+ * string, int (or i4), boolean, double, dateTime.iso8601, base64, struct and
+ * array. Vendor extensions such as nil, i8 or serialized Java objects are
+ * refused. Documents are parsed with {@link SafeSAXBuilder}, which refuses
+ * document type declarations and resolves nothing a document names.
+ *
+ * <p>Structs become {@link Map}s and arrays become {@code Object[]}, as they
+ * did with the Apache XML-RPC library, so the handlers' casts still hold.
+ */
+final class XmlRpcRequestParser {
+
+    /** Deepest nesting of structs and arrays accepted. Blogger and MetaWeblog 
calls need four. */
+    static final int MAX_DEPTH = 32;
+
+    /** Characters the XML-RPC specification allows in a method name. */
+    private static final Pattern METHOD_NAME = 
Pattern.compile("[A-Za-z0-9_.:/]{1,128}");
+
+    /**
+     * The specification's <code>yyyyMMddTHH:mm:ss</code>, plus what clients 
send
+     * in practice: dashes and colons, fractional seconds and a zone offset.
+     */
+    private static final Pattern DATE_TIME = Pattern.compile(
+            
"(\\d{4})-?(\\d{2})-?(\\d{2})T(\\d{2}):?(\\d{2}):?(\\d{2})(?:\\.(\\d{1,9}))?"
+                    + "(Z|[+-]\\d{2}:?\\d{2})?");
+
+    private XmlRpcRequestParser() {
+    }
+
+    static XmlRpcRequest parse(byte[] body) throws XmlRpcException {
+        Document document;
+        try {
+            document = new SafeSAXBuilder().build(new 
ByteArrayInputStream(body));

Review Comment:
   This was a false positive, and I checked it instead of taking that on trust. 
`SafeSAXBuilder` already refused document type declarations when this was 
flagged; 9a149a8af only repeats the setting where CodeQL can see it.
   
   I ran 16 hostile documents through three parsers: a plain `SAXBuilder` as 
the control, `SafeSAXBuilder` alone (the code as flagged) and 
`XmlRpcRequestParser`. The control read a local file through an external entity 
and fetched six URLs from a listener: a general entity, an external DTD, a 
parameter entity, and the entity again with the document in UTF-16 with and 
without a BOM and in EBCDIC. The other two refused every one of those with no 
fetch and no file read. XInclude, a schema location and a stylesheet 
instruction fetched nothing in any of the three.
   
   Over HTTP against this branch on Tomcat 11 and JDK 25, the same attacks, 
plain and gzip-encoded, all return `Failed to parse XML-RPC request`, and an 
entity aimed at `/etc/hostname` in the method name does not come back in the 
fault.
   
   58ae110d7 adds the test that was missing: a DOCTYPE is refused without 
reading the file it names.



##########
app/src/main/java/org/apache/roller/weblogger/webservices/xmlrpc/RollerXmlRpcServlet.java:
##########
@@ -16,198 +16,133 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-/*
- * Derived from org.apache.xmlrpc.webserver.XmlRpcServlet
- * in Apache XML-RPC 3.1.3. Forked into the Roller codebase because
- * xmlrpc-server has no Jakarta Servlet compatible release.
- */
 package org.apache.roller.weblogger.webservices.xmlrpc;
 
 import java.io.IOException;
-import java.lang.reflect.InvocationTargetException;
-import java.net.URL;
-import java.util.Enumeration;
+import java.io.InputStream;
+import java.util.Locale;
+import java.util.Map;
+import java.util.zip.GZIPInputStream;
 
-import jakarta.servlet.ServletConfig;
-import jakarta.servlet.ServletException;
 import jakarta.servlet.http.HttpServlet;
 import jakarta.servlet.http.HttpServletRequest;
 import jakarta.servlet.http.HttpServletResponse;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.apache.xmlrpc.XmlRpcConfig;
-import org.apache.xmlrpc.XmlRpcException;
-import org.apache.xmlrpc.common.TypeConverterFactory;
-import org.apache.xmlrpc.server.AbstractReflectiveHandlerMapping;
-import org.apache.xmlrpc.server.PropertyHandlerMapping;
-import org.apache.xmlrpc.server.RequestProcessorFactoryFactory;
-import org.apache.xmlrpc.server.XmlRpcHandlerMapping;
-import org.apache.xmlrpc.server.XmlRpcServer;
-import org.apache.xmlrpc.util.ReflectionUtil;
-
+import org.apache.roller.weblogger.config.WebloggerRuntimeConfig;
 
 /**
- * A servlet for processing XML-RPC requests, derived from the Apache XML-RPC
- * library's {@code XmlRpcServlet}. The typical use is to configure it in
- * {@code web.xml} with an {@code XmlRpcServlet.properties} resource on the
- * classpath that maps handler names to implementation classes.
+ * The Blogger and MetaWeblog XML-RPC endpoint.
  *
- * <p>The servlet accepts the following init parameters:</p>
- * <table border="1">
- *   <tr><th>Name</th><th>Description</th></tr>
- *   <tr><td>enabledForExtensions</td><td>Sets the value of
- *     {@link XmlRpcConfig#isEnabledForExtensions()} to true.</td></tr>
- * </table>
+ * <p>Calls are answered by {@link BloggerAPIHandler} under
+ * <code>blogger.*</code> and {@link MetaWeblogAPIHandler} under
+ * <code>metaWeblog.*</code>; see {@link XmlRpcServer}. The endpoint is gated 
by
+ * <code>XmlRpcEnabledFilter</code> while <code>webservices.enableXmlRpc</code>
+ * is off.
+ *
+ * <p>Request bodies are capped before they are parsed: at 1 MiB, or, while
+ * uploads are on, at what the largest allowed upload takes as base64 plus room
+ * for the call around it. The cap applies to the decompressed size of a
+ * gzip-encoded request too.
  */
 public class RollerXmlRpcServlet extends HttpServlet {
-    private static final long serialVersionUID = 2348768267234L;
+    private static final long serialVersionUID = 1L;
     private static final Log log = 
LogFactory.getLog(RollerXmlRpcServlet.class);
 
-    /**
-     * Classpath location of the handler mapping properties file.
-     * Roller ships this at {@code 
org/apache/xmlrpc/webserver/XmlRpcServlet.properties}.
-     */
-    private static final String HANDLER_MAPPING_RESOURCE =
-            "org/apache/xmlrpc/webserver/XmlRpcServlet.properties";
+    /** Smallest request cap, in bytes; also the cap while uploads are off. */
+    static final int MIN_MAX_REQUEST_BYTES = 1024 * 1024;
+
+    /** Room for the methodCall around a base64 upload, in bytes. */
+    static final int ENVELOPE_BYTES = 64 * 1024;
 
-    private RollerXmlRpcServletServer server;
-    private AbstractReflectiveHandlerMapping.AuthenticationHandler 
authenticationHandler;
-    private RequestProcessorFactoryFactory requestProcessorFactoryFactory;
-    private TypeConverterFactory typeConverterFactory;
+    static final Map<String, Class<?>> HANDLERS = Map.of(
+            "blogger", BloggerAPIHandler.class,
+            "metaWeblog", MetaWeblogAPIHandler.class);
 
-    /**
-     * Returns the servlet's instance of {@link RollerXmlRpcServletServer}.
-     * @return The configurable server instance.
-     */
-    public RollerXmlRpcServletServer getXmlRpcServletServer() {
-        return server;
+    private transient XmlRpcServer server;
+
+    @Override
+    public void init() {
+        server = new XmlRpcServer(HANDLERS);
     }
 
-    private void handleInitParameters(ServletConfig pConfig) throws 
ServletException {
-        for (Enumeration en = pConfig.getInitParameterNames(); 
en.hasMoreElements(); ) {
-            String name = (String) en.nextElement();
-            String value = pConfig.getInitParameter(name);
-            try {
-                if (!ReflectionUtil.setProperty(this, name, value)
-                    && !ReflectionUtil.setProperty(server, name, value)
-                    && !ReflectionUtil.setProperty(server.getConfig(), name, 
value)) {
-                    throw new ServletException("Unknown init parameter " + 
name);
-                }
-            } catch (IllegalAccessException e) {
-                throw new ServletException("Illegal access to instance of " + 
server.getClass().getName()
-                        + " while setting property " + name + ": " + 
e.getMessage(), e);
-            } catch (InvocationTargetException e) {
-                Throwable t = e.getTargetException();
-                throw new ServletException("Failed to invoke setter for 
property " + name
-                        + " on instance of " + server.getClass().getName()
-                        + ": " + t.getMessage(), t);
+    @Override
+    protected void doPost(HttpServletRequest request, HttpServletResponse 
response)
+            throws IOException {
+        int maxBytes = maxRequestBytes();
+        if (request.getContentLengthLong() > maxBytes) {
+            sendText(response, 
HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE, "Request is too large");
+            return;
+        }
+
+        InputStream in = request.getInputStream();
+        String encoding = request.getHeader("Content-Encoding");
+        boolean gzip = false;
+        if (encoding != null && !encoding.isBlank()
+                && !"identity".equalsIgnoreCase(encoding.trim())) {
+            String name = encoding.trim().toLowerCase(Locale.ROOT);
+            if (!"gzip".equals(name) && !"x-gzip".equals(name)) {
+                sendText(response, 
HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE,
+                        "Unsupported Content-Encoding");
+                return;
             }
+            gzip = true;
         }
-    }
 
-    public void init(ServletConfig pConfig) throws ServletException {
-        super.init(pConfig);
+        byte[] body;
         try {
-            server = newXmlRpcServer(pConfig);
-            handleInitParameters(pConfig);
-            server.setHandlerMapping(newXmlRpcHandlerMapping());
-        } catch (XmlRpcException e) {
-            try {
-                log("Failed to create XmlRpcServer: " + e.getMessage(), e);
-            } catch (Throwable ignore) {
+            if (gzip) {
+                in = new GZIPInputStream(in);
             }
-            throw new ServletException(e);
+            // Read one byte past the cap, so an oversized body can be detected
+            // without holding more than that in memory.
+            body = in.readNBytes(maxBytes + 1);
+        } catch (IOException e) {
+            if (!gzip) {
+                throw e;
+            }
+            log.debug("Rejecting XML-RPC request with an unreadable gzip 
body", e);
+            sendText(response, HttpServletResponse.SC_BAD_REQUEST, "Invalid 
gzip request body");
+            return;
+        }
+        if (body.length > maxBytes) {
+            sendText(response, 
HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE, "Request is too large");
+            return;
         }
-    }
-
-    /** Sets the servlet's {@link 
AbstractReflectiveHandlerMapping.AuthenticationHandler}. */
-    public void 
setAuthenticationHandler(AbstractReflectiveHandlerMapping.AuthenticationHandler 
pHandler) {
-        authenticationHandler = pHandler;
-    }
-
-    /** Returns the servlet's {@link 
AbstractReflectiveHandlerMapping.AuthenticationHandler}. */
-    public AbstractReflectiveHandlerMapping.AuthenticationHandler 
getAuthenticationHandler() {
-        return authenticationHandler;
-    }
-
-    /** Sets the servlet's {@link RequestProcessorFactoryFactory}. */
-    public void 
setRequestProcessorFactoryFactory(RequestProcessorFactoryFactory pFactory) {
-        requestProcessorFactoryFactory = pFactory;
-    }
-
-    /** Returns the servlet's {@link RequestProcessorFactoryFactory}. */
-    public RequestProcessorFactoryFactory getRequestProcessorFactoryFactory() {
-        return requestProcessorFactoryFactory;
-    }
-
-    /** Sets the servlet's {@link TypeConverterFactory}. */
-    public void setTypeConverterFactory(TypeConverterFactory pFactory) {
-        typeConverterFactory = pFactory;
-    }
-
-    /** Returns the servlet's {@link TypeConverterFactory}. */
-    public TypeConverterFactory getTypeConverterFactory() {
-        return typeConverterFactory;
-    }
 
-    /**
-     * Creates a new instance of {@link RollerXmlRpcServletServer} to process 
requests.
-     * @param pConfig The servlet's configuration.
-     * @throws XmlRpcException Creating the server failed.
-     */
-    protected RollerXmlRpcServletServer newXmlRpcServer(ServletConfig pConfig)
-            throws XmlRpcException {
-        return new RollerXmlRpcServletServer();
+        byte[] result = server.execute(body);
+        response.setStatus(HttpServletResponse.SC_OK);
+        response.setContentType("text/xml;charset=UTF-8");
+        response.setContentLength(result.length);
+        response.getOutputStream().write(result);

Review Comment:
   The flagged flow is not a leak: every `XmlRpcException` is built with text 
meant for the client. I read each place one is created. They carry fixed 
strings, or a name the caller sent (the method name, or an unsupported value 
type). None carries another exception's message. 9a149a8af renamed the 
accessor, which is why CodeQL stopped reporting it; what is sent did not change.
   
   Checking this did turn up a real leak nearby, new in this pull request and 
fixed in 58ae110d7. The parser caught `JDOMException` and `IOException`, but 
JDOM refuses some documents with unchecked exceptions. An XML 1.1 request 
holding a control-character reference, or an element name starting with a 
supplementary-plane character, passes the JDK's parser and fails JDOM's 
verifier. That escaped the servlet, so an unauthenticated request got an HTTP 
500 with `error.jsp`, which prints the exception's message, and about 180 lines 
of stack trace went to the log each time. Master answers the same requests with 
a fault.
   
   The parser now turns any failure to build the document into the usual parse 
fault, and `XmlRpcServer.execute` answers the generic fault for any other 
unchecked exception. `XmlRpcServerTest` covers both XML 1.1 cases; it fails 
without the fix. Over HTTP the four requests that returned 500 now return 
`Failed to parse XML-RPC request` and log nothing.
   
   Left as it is on master: 13 faults name a handler class and method, such as 
`ERROR in MetaWeblogAPIHandler.newPost`, and the one in `blogger.deletePost` 
appends the exception's class name.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to