snoopdave commented on code in PR #263:
URL: https://github.com/apache/roller/pull/263#discussion_r4576367195
##########
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);
}
- /**
- * Creates a new handler mapping. Loads the property file from the
classpath
- * resource {@value #HANDLER_MAPPING_RESOURCE}.
- */
- protected XmlRpcHandlerMapping newXmlRpcHandlerMapping() throws
XmlRpcException {
- URL url = Thread.currentThread().getContextClassLoader()
- .getResource(HANDLER_MAPPING_RESOURCE);
- if (url == null) {
- throw new XmlRpcException("Failed to locate resource " +
HANDLER_MAPPING_RESOURCE);
+ /** The largest request body accepted, in bytes. */
+ int maxRequestBytes() {
Review Comment:
🐞Claude Issue: **Important:** The request cap follows
`uploads.file.maxsize`, and the whole body (line 100) is read into a `byte[]`
and then built into a JDOM tree before anyone is authenticated. A site that
allows 100 MB uploads accepts about 137 MB per anonymous request, and the DOM
takes several times that in heap. This is much better than the old unbounded
gzip path, but consider a fixed ceiling (for example 32 MiB) whatever the
upload size is. Otherwise, say in the install guide that the upload size also
sets the XML-RPC request cap.
--
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]