github-advanced-security[bot] commented on code in PR #263: URL: https://github.com/apache/roller/pull/263#discussion_r4575644705
########## 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: ## CodeQL / Resolving XML external entity in user-controlled data XML parsing depends on a [user-provided value](1) without guarding against external entity expansion. [Show more details](https://github.com/apache/roller/security/code-scanning/151) ########## 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: ## CodeQL / Information exposure through an error message [Error information](1) can be exposed to an external user. [Show more details](https://github.com/apache/roller/security/code-scanning/150) -- 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]
