This is an automated email from the ASF dual-hosted git repository. snoopdave pushed a commit to branch pinger-response-handling in repository https://gitbox.apache.org/repos/asf/roller.git
commit 6254574fb00a4c01aca558503ea4c3c2621e658f Author: David M. Johnson <[email protected]> AuthorDate: Thu Nov 5 17:07:44 2026 -0500 Send weblog update pings without an XML-RPC client Roller now builds the weblogUpdates.ping request and reads the response itself, parsing it with the project's hardened XML parser (SecureXmlParsers) and reading only the flerror and message fields the protocol defines. Adds connection and read timeouts and a 1 MiB cap on the response. Removes the now-unused xmlrpc-client dependency; xmlrpc-common/server remain for the inbound XML-RPC servlet. Adds WeblogUpdatePingerTest. --- CHANGES.md | 6 + app/pom.xml | 6 - .../business/pings/WeblogUpdatePinger.java | 271 +++++++++++++++++++-- .../business/pings/WeblogUpdatePingerTest.java | 172 +++++++++++++ 4 files changed, 423 insertions(+), 32 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index c63e2de41..aac78aa39 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -25,6 +25,12 @@ ### Behaviour changes worth reading before upgrading +- **Weblog update pings no longer use the XML-RPC client library.** Roller + builds the `weblogUpdates.ping` request and reads the response itself, with a + connection timeout, a read timeout and a 1 MB cap on the response, and reads + only the `flerror` and `message` fields the protocol defines. Ping behaviour + is otherwise unchanged. + - **AtomPub honours `webservices.enableAtomPub` on every request.** While the setting is off, every AtomPub URL answers 404, not only the service document. - **AtomPub entry bodies default to a 1 MiB limit.** A larger entry is refused diff --git a/app/pom.xml b/app/pom.xml index 59e10851b..cb54682e5 100644 --- a/app/pom.xml +++ b/app/pom.xml @@ -388,12 +388,6 @@ limitations under the License. <version>${xmlrpc-version}</version> </dependency> - <dependency> - <groupId>org.apache.xmlrpc</groupId> - <artifactId>xmlrpc-client</artifactId> - <version>${xmlrpc-version}</version> - </dependency> - <dependency> <groupId>org.apache.xmlrpc</groupId> <artifactId>xmlrpc-server</artifactId> diff --git a/app/src/main/java/org/apache/roller/weblogger/business/pings/WeblogUpdatePinger.java b/app/src/main/java/org/apache/roller/weblogger/business/pings/WeblogUpdatePinger.java index 49d0e22f9..42fbe206e 100644 --- a/app/src/main/java/org/apache/roller/weblogger/business/pings/WeblogUpdatePinger.java +++ b/app/src/main/java/org/apache/roller/weblogger/business/pings/WeblogUpdatePinger.java @@ -23,15 +23,26 @@ import org.apache.commons.logging.LogFactory; import org.apache.roller.weblogger.config.PingConfig; import org.apache.roller.weblogger.pojos.PingTarget; import org.apache.roller.weblogger.pojos.Weblog; +import org.apache.roller.weblogger.util.SecureXmlParsers; import org.apache.xmlrpc.XmlRpcException; -import org.apache.xmlrpc.client.XmlRpcClient; -import org.apache.xmlrpc.client.XmlRpcClientConfigImpl; +import org.xml.sax.Attributes; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.DefaultHandler; +import javax.xml.parsers.SAXParser; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.UnknownHostException; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; @@ -39,11 +50,16 @@ import java.util.Set; /** * Utility for sending a weblog update ping. * - * This implements the <code>WeblogUpdates.ping<code> XML-RPC call described at - * <a href="http://www.xmlrpc.com/weblogsCom">www.xmlrpc.com</a> - * as well as some variants required to interoperate with certain - * buggy but popular ping targets. + * <p>This implements the <code>weblogUpdates.ping</code> XML-RPC call described + * at <a href="http://www.xmlrpc.com/weblogsCom">www.xmlrpc.com</a>, as well as + * some variants required to interoperate with certain buggy but popular ping + * targets. * + * <p>Roller builds the request and reads the response itself rather than through + * an XML-RPC client library. The response is parsed with {@link SecureXmlParsers}, + * which refuses document type declarations and resolves nothing the document + * names, and only the <code>flerror</code> and <code>message</code> fields the + * protocol defines are read; any other content is ignored. * * @author <a href="mailto:[email protected]">Anil Gangolli</a> * @author llavandowska (for code refactored from the now-defunct <code>RollerXmlRpcClient</code>) @@ -51,6 +67,17 @@ import java.util.Set; public final class WeblogUpdatePinger { public static final Log LOGGER = LogFactory.getLog(WeblogUpdatePinger.class); + private static final String PING_METHOD_NAME = "weblogUpdates.ping"; + + /** How long to wait to open a connection to a ping target, in milliseconds. */ + private static final int CONNECT_TIMEOUT_MS = 15000; + + /** How long to wait for a ping target's response, in milliseconds. */ + private static final int READ_TIMEOUT_MS = 30000; + + /** Upper bound on the response we will read from a ping target, in bytes. */ + private static final int MAX_RESPONSE_BYTES = 1024 * 1024; + /** * Conveys a ping result. */ @@ -96,7 +123,7 @@ public final class WeblogUpdatePinger { * @param website the website that changed (from which the ping originates) * @return the result message string sent by the server. * @throws IOException if an IOException occurs during the ping - * @throws XmlRpcException if the XML RPC client throws one + * @throws XmlRpcException if the ping target reports a fault */ public static PingResult sendPing(PingTarget pingTarget, Weblog website) throws IOException, XmlRpcException { String websiteUrl = website.getAbsoluteURL(); @@ -114,12 +141,9 @@ public final class WeblogUpdatePinger { LOGGER.debug("Executing ping to '" + pingTargetUrl + "' for website '" + websiteUrl + "' (" + website.getName() + ")" + (variantOptions.isEmpty() ? "" : " with variant options " + variantOptions)); } - // Send the ping. - XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl(); - config.setServerURL(new URL(pingTargetUrl)); - XmlRpcClient client = new XmlRpcClient(); - client.setConfig(config); - PingResult pingResult = parseResult(client.execute("weblogUpdates.ping", params.toArray())); + // Send the ping and read the response. + byte[] response = exchange(pingTargetUrl, buildRequest(params)); + PingResult pingResult = parseResponse(response); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Ping result is: " + pingResult); @@ -127,29 +151,224 @@ public final class WeblogUpdatePinger { return pingResult; } - private static PingResult parseResult(Object obj) { - // Deal with the fact that some buggy ping targets may not respond with the proper struct type. - if (obj == null) { - return new PingResult(null,null); + /** + * Build the XML-RPC <code>weblogUpdates.ping</code> request body for the + * given string parameters. + */ + static String buildRequest(List<String> params) { + StringBuilder sb = new StringBuilder(256); + sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); + sb.append("<methodCall><methodName>").append(PING_METHOD_NAME).append("</methodName>"); + sb.append("<params>"); + for (String param : params) { + sb.append("<param><value><string>"); + xmlEscape(sb, param); + sb.append("</string></value></param>"); + } + sb.append("</params></methodCall>"); + return sb.toString(); + } + + /** + * POST the request body to the ping target and return the response bytes, + * capped at {@link #MAX_RESPONSE_BYTES}. + */ + private static byte[] exchange(String pingTargetUrl, String requestBody) throws IOException { + URL url = new URL(pingTargetUrl); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + try { + conn.setRequestMethod("POST"); + conn.setConnectTimeout(CONNECT_TIMEOUT_MS); + conn.setReadTimeout(READ_TIMEOUT_MS); + conn.setDoOutput(true); + conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8"); + conn.setRequestProperty("User-Agent", "Apache Roller"); + + byte[] body = requestBody.getBytes(StandardCharsets.UTF_8); + conn.setFixedLengthStreamingMode(body.length); + try (OutputStream out = conn.getOutputStream()) { + out.write(body); + } + + int status = conn.getResponseCode(); + InputStream in = (status >= 200 && status < 300) ? conn.getInputStream() : conn.getErrorStream(); + return readCapped(in); + } finally { + conn.disconnect(); + } + } + + /** Read at most {@link #MAX_RESPONSE_BYTES} bytes from the stream. */ + private static byte[] readCapped(InputStream in) throws IOException { + if (in == null) { + return new byte[0]; + } + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + byte[] chunk = new byte[8192]; + int total = 0; + int read; + while ((read = in.read(chunk)) != -1) { + total += read; + if (total > MAX_RESPONSE_BYTES) { + throw new IOException("Ping target response exceeded " + MAX_RESPONSE_BYTES + " bytes"); + } + buffer.write(chunk, 0, read); } + return buffer.toByteArray(); + } + + /** + * Parse an XML-RPC ping response. Only <code>flerror</code> and + * <code>message</code> are read from a normal response; a fault is turned + * into an {@link XmlRpcException}. + * + * @throws XmlRpcException if the response is a fault + */ + static PingResult parseResponse(byte[] response) throws IOException, XmlRpcException { + if (response == null || response.length == 0) { + return new PingResult(null, null); + } + PingResponseHandler handler = new PingResponseHandler(); try { - // normal case: response is a struct (represented as a Map) with Boolean flerror and String fields. - Map<?, ?> result = (Map<?, ?>) obj; - return new PingResult((Boolean) result.get("flerror"), (String) result.get("message")); - } catch (Exception ex) { - // exception case: The caller responded with an unexpected type, though parsed at the basic XML RPC level. - // This effectively assumes flerror = false, and sets message = obj.toString(); + SAXParser parser = SecureXmlParsers.newSAXParserFactory().newSAXParser(); + parser.parse(new InputSource(new ByteArrayInputStream(response)), handler); + } catch (SAXException | javax.xml.parsers.ParserConfigurationException ex) { + // The target responded with something that is not a well-formed, safe + // XML-RPC response. Treat it as a non-error transmission, mirroring the + // prior lenient handling of buggy targets, without exposing the body. if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Invalid ping result of type: " + obj.getClass().getName() + "; proceeding with stand-in representative."); + LOGGER.debug("Could not parse ping response as XML-RPC; proceeding.", ex); + } + return new PingResult(null, null); + } + + if (handler.isFault()) { + throw new XmlRpcException(handler.getFaultCode(), handler.getFaultString()); + } + return new PingResult(handler.getFlerror(), handler.getMessage()); + } + + private static void xmlEscape(StringBuilder sb, String value) { + if (value == null) { + return; + } + for (int i = 0; i < value.length(); i++) { + char c = value.charAt(i); + switch (c) { + case '&': sb.append("&"); break; + case '<': sb.append("<"); break; + case '>': sb.append(">"); break; + case '"': sb.append("""); break; + case '\'': sb.append("'"); break; + default: sb.append(c); + } + } + } + + /** + * Collects just the fields Roller needs from an XML-RPC ping response. + * Scalar values, including any {@code <base64>}, are read only as text. + */ + private static final class PingResponseHandler extends DefaultHandler { + private final StringBuilder text = new StringBuilder(); + private final Map<String, String> members = new HashMap<>(); + private boolean inFault; + private boolean inStruct; + private String memberName; + private String scalar; + + @Override + public void startElement(String uri, String localName, String qName, Attributes attributes) { + text.setLength(0); + if ("fault".equals(qName)) { + inFault = true; + } else if ("struct".equals(qName)) { + inStruct = true; + } else if ("member".equals(qName)) { + memberName = null; + scalar = null; + } + } + + @Override + public void characters(char[] ch, int start, int length) { + text.append(ch, start, length); + } + + @Override + public void endElement(String uri, String localName, String qName) { + switch (qName) { + case "name": + memberName = text.toString().trim(); + break; + case "string": + case "boolean": + case "int": + case "i4": + case "double": + case "dateTime.iso8601": + case "base64": + // Record the raw text only; base64 is not decoded. + scalar = text.toString(); + break; + case "value": + if (scalar == null) { + // An XML-RPC <value> with no type child is a string. + scalar = text.toString(); + } + break; + case "member": + if (memberName != null && inStruct) { + members.put(memberName, scalar != null ? scalar : ""); + } + scalar = null; + break; + case "struct": + inStruct = false; + break; + default: + break; + } + } + + boolean isFault() { + return inFault; + } + + int getFaultCode() { + String code = members.get("faultCode"); + if (code != null) { + try { + return Integer.parseInt(code.trim()); + } catch (NumberFormatException ignored) { + // fall through to default + } } - return new PingResult(null,obj.toString()); + return 0; + } + + String getFaultString() { + return members.get("faultString"); + } + + Boolean getFlerror() { + String value = members.get("flerror"); + if (value == null) { + return null; + } + value = value.trim(); + return "1".equals(value) || "true".equalsIgnoreCase(value); + } + + String getMessage() { + return members.get("message"); } } /** * Decide if the given exception appears to warrant later retrial attempts. * - * @param ex an exception thrown by the <coce>sendPing</code> operation + * @param ex an exception thrown by the <code>sendPing</code> operation * @return true if the error warrants retrial */ public static boolean shouldRetry(Exception ex) { diff --git a/app/src/test/java/org/apache/roller/weblogger/business/pings/WeblogUpdatePingerTest.java b/app/src/test/java/org/apache/roller/weblogger/business/pings/WeblogUpdatePingerTest.java new file mode 100644 index 000000000..7ef435e60 --- /dev/null +++ b/app/src/test/java/org/apache/roller/weblogger/business/pings/WeblogUpdatePingerTest.java @@ -0,0 +1,172 @@ +/* + * 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.business.pings; + +import org.apache.roller.weblogger.business.pings.WeblogUpdatePinger.PingResult; +import org.apache.xmlrpc.XmlRpcException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Base64; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Tests for {@link WeblogUpdatePinger}, covering how it builds the + * {@code weblogUpdates.ping} request and reads the response. + * + * <p>The response reader extracts only the {@code flerror} and {@code message} + * fields the protocol defines, reads every scalar as text, ignores response + * content it does not use, and refuses document type declarations. + */ +public class WeblogUpdatePingerTest { + + /** Set true if {@link Probe} is ever reconstructed from bytes. */ + private static volatile boolean probeReconstructed; + + @BeforeEach + public void reset() { + probeReconstructed = false; + } + + /** A serializable object whose reconstruction from bytes is observable. */ + static final class Probe implements Serializable { + private static final long serialVersionUID = 1L; + + private void readObject(ObjectInputStream in) throws java.io.IOException, ClassNotFoundException { + in.defaultReadObject(); + probeReconstructed = true; + } + } + + private static byte[] serialize(Serializable object) throws Exception { + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + try (ObjectOutputStream out = new ObjectOutputStream(bytes)) { + out.writeObject(object); + } + return bytes.toByteArray(); + } + + @Test + public void readsFlerrorAndMessageFromNormalResponse() throws Exception { + String response = "<?xml version=\"1.0\"?>" + + "<methodResponse><params><param><value><struct>" + + "<member><name>flerror</name><value><boolean>0</boolean></value></member>" + + "<member><name>message</name><value><string>Thanks for the ping.</string></value></member>" + + "</struct></value></param></params></methodResponse>"; + + PingResult result = WeblogUpdatePinger.parseResponse(response.getBytes(StandardCharsets.UTF_8)); + assertFalse(result.isError()); + assertEquals("Thanks for the ping.", result.getMessage()); + } + + @Test + public void readsErrorFlagFromResponse() throws Exception { + String response = "<?xml version=\"1.0\"?>" + + "<methodResponse><params><param><value><struct>" + + "<member><name>flerror</name><value><boolean>1</boolean></value></member>" + + "<member><name>message</name><value><string>Unknown blog.</string></value></member>" + + "</struct></value></param></params></methodResponse>"; + + PingResult result = WeblogUpdatePinger.parseResponse(response.getBytes(StandardCharsets.UTF_8)); + assertTrue(result.isError()); + assertEquals("Unknown blog.", result.getMessage()); + } + + @Test + public void faultResponseBecomesException() throws Exception { + String response = "<?xml version=\"1.0\"?>" + + "<methodResponse><fault><value><struct>" + + "<member><name>faultCode</name><value><int>4</int></value></member>" + + "<member><name>faultString</name><value><string>boom</string></value></member>" + + "</struct></value></fault></methodResponse>"; + + XmlRpcException thrown = assertThrows(XmlRpcException.class, + () -> WeblogUpdatePinger.parseResponse(response.getBytes(StandardCharsets.UTF_8))); + assertEquals(4, thrown.code); + assertEquals("boom", thrown.getMessage()); + } + + @Test + public void controlProbeIsReconstructedByAnObjectStream() throws Exception { + // Control: reading the probe through an ObjectInputStream reconstructs it. + byte[] serialized = serialize(new Probe()); + try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(serialized))) { + in.readObject(); + } + assertTrue(probeReconstructed); + } + + @Test + public void base64ResponseMemberIsReadAsTextOnly() throws Exception { + // A base64 member is read as text; the reader does not turn it into an object. + String embedded = Base64.getEncoder().encodeToString(serialize(new Probe())); + String response = "<?xml version=\"1.0\"?>" + + "<methodResponse><fault><value><struct>" + + "<member><name>faultCode</name><value><int>4</int></value></member>" + + "<member><name>faultString</name><value><string>boom</string></value></member>" + + "<member><name>payload</name><value><base64>" + embedded + "</base64></value></member>" + + "</struct></value></fault></methodResponse>"; + + assertThrows(XmlRpcException.class, + () -> WeblogUpdatePinger.parseResponse(response.getBytes(StandardCharsets.UTF_8))); + assertFalse(probeReconstructed, "a base64 response member must be read as text, not reconstructed"); + } + + @Test + public void doctypeInResponseIsRefused() { + // A response that declares a DTD is refused, and nothing it names is resolved. + String response = "<?xml version=\"1.0\"?>" + + "<!DOCTYPE methodResponse [<!ENTITY x \"y\">]>" + + "<methodResponse><params><param><value><struct>" + + "<member><name>flerror</name><value><boolean>0</boolean></value></member>" + + "<member><name>message</name><value><string>&x;</string></value></member>" + + "</struct></value></param></params></methodResponse>"; + + PingResult result = parse(response); + assertFalse(result.isError()); + } + + @Test + public void buildsWellFormedEscapedRequest() { + String request = WeblogUpdatePinger.buildRequest(Arrays.asList("A & B <blog>", "http://example.test/")); + assertTrue(request.contains("<methodName>weblogUpdates.ping</methodName>")); + assertTrue(request.contains("A & B <blog>"), "parameters must be XML-escaped"); + assertTrue(request.contains("<string>http://example.test/</string>")); + assertFalse(request.contains("<blog>"), "raw markup must not leak into the request"); + } + + private static PingResult parse(String response) { + try { + return WeblogUpdatePinger.parseResponse(response.getBytes(StandardCharsets.UTF_8)); + } catch (Exception ex) { + throw new AssertionError("parsing should not throw for this response", ex); + } + } +}
