gnodet-bot commented on code in PR #30599:
URL: https://github.com/apache/camel/pull/30599#discussion_r4658422084


##########
core/camel-xml-jaxp-util/src/main/java/org/apache/camel/util/xml/XmlStringHelper.java:
##########
@@ -0,0 +1,429 @@
+/*
+ * 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.camel.util.xml;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.StringReader;
+import java.nio.charset.StandardCharsets;
+import java.security.GeneralSecurityException;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import javax.xml.XMLConstants;
+import javax.xml.crypto.Data;
+import javax.xml.crypto.OctetStreamData;
+import javax.xml.crypto.dsig.CanonicalizationMethod;
+import javax.xml.crypto.dsig.TransformException;
+import javax.xml.crypto.dsig.TransformService;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.ext.DefaultHandler2;
+import org.xml.sax.helpers.DefaultHandler;
+
+/**
+ * Helpers that work on XML held as a String: check it is well-formed, strip 
the declaration, comments or namespaces,
+ * compact it, or canonicalize it.
+ * <p>
+ * The XML is often a message body from an untrusted sender, so every method 
that parses uses the parser of the JDK with
+ * DOCTYPE declarations disallowed: no DTD, no entity of any kind (internal or 
external), and no access to files or the
+ * network. XML with a DOCTYPE is rejected.
+ */
+public final class XmlStringHelper {
+
+    private XmlStringHelper() {
+    }
+
+    /**
+     * Whether the value is well-formed XML. A document with a DOCTYPE 
declaration is not accepted.
+     *
+     * @param  xml the XML
+     * @return     <code>true</code> if the value is well-formed XML without a 
DOCTYPE, <code>false</code> otherwise
+     *             (also for <code>null</code> or a blank value)
+     */
+    public static boolean isWellFormed(String xml) {
+        if (xml == null || xml.isBlank()) {
+            return false;
+        }
+        try {
+            parse(xml, new DefaultHandler());
+            return true;
+        } catch (SAXException | IOException e) {
+            return false;
+        }
+    }
+
+    /**
+     * Removes the XML declaration ({@code <?xml version="1.0"?>}) and the 
whitespace after it. This is a text
+     * operation: the XML is not parsed, and the rest of the value is returned 
unchanged.
+     *
+     * @param  xml the XML
+     * @return     the XML without the declaration, or the value as-is if it 
has none
+     */
+    public static String stripDeclaration(String xml) {
+        if (xml == null) {
+            return null;
+        }
+        int pos = 0;
+        if (xml.startsWith("")) {
+            pos++;
+        }
+        while (pos < xml.length() && isXmlSpace(xml.charAt(pos))) {
+            pos++;
+        }
+        // <?xml-stylesheet ...?> is a processing instruction, not the 
declaration
+        int after = pos + 5;
+        if (!xml.startsWith("<?xml", pos) || after >= xml.length() || 
!isXmlSpace(xml.charAt(after))) {
+            return xml;
+        }
+        int end = xml.indexOf("?>", after);
+        if (end == -1) {
+            return xml;
+        }
+        pos = end + 2;
+        while (pos < xml.length() && isXmlSpace(xml.charAt(pos))) {
+            pos++;
+        }
+        return xml.substring(pos);
+    }
+
+    /**
+     * Removes all the comments.
+     *
+     * @param  xml          the XML
+     * @return              the XML without comments
+     * @throws SAXException if the value is not well-formed XML, or has a 
DOCTYPE declaration
+     * @throws IOException  if the value cannot be read
+     */
+    public static String stripComments(String xml) throws SAXException, 
IOException {
+        return rewrite(xml, false, true, false);
+    }
+
+    /**
+     * Removes the namespace declarations ({@code xmlns} and {@code 
xmlns:prefix}) and the namespace prefixes of
+     * elements and attributes, so {@code <ns1:Order xmlns:ns1="urn:a" 
ns1:id="1">} becomes {@code <Order id="1">}.
+     * Attributes in the {@code xml:} namespace, such as {@code xml:lang}, are 
kept as they are. When two attributes of
+     * an element have the same local name, the first one is kept. Prefixes 
inside attribute values or text, such as in
+     * {@code xsi:type="ns1:Item"}, are not changed.
+     *
+     * @param  xml          the XML
+     * @return              the XML without namespaces
+     * @throws SAXException if the value is not well-formed XML, or has a 
DOCTYPE declaration
+     * @throws IOException  if the value cannot be read
+     */
+    public static String stripNamespaces(String xml) throws SAXException, 
IOException {
+        return rewrite(xml, true, false, false);
+    }
+
+    /**
+     * Removes the whitespace-only text between elements, and the line breaks 
between the declaration, comments and the
+     * root element, so the XML is on a single line (the inverse of pretty 
printing). Text with other characters than
+     * whitespace, and CDATA sections, are kept as they are.
+     *
+     * @param  xml          the XML
+     * @return              the compact XML
+     * @throws SAXException if the value is not well-formed XML, or has a 
DOCTYPE declaration
+     * @throws IOException  if the value cannot be read
+     */
+    public static String compact(String xml) throws SAXException, IOException {
+        return rewrite(xml, false, false, true);
+    }
+
+    /**
+     * Canonicalizes the XML (W3C Canonical XML, without comments) using the 
canonicalizer of the JDK
+     * ({@code javax.xml.crypto.dsig}), so two documents that differ only in 
formatting (attribute order, quotes, empty
+     * element tags, namespace declarations) give the same result. The XML 
declaration and comments are removed.
+     *
+     * @param  xml                      the XML
+     * @param  exclusive                <code>true</code> for Exclusive XML 
Canonicalization (unused namespace
+     *                                  declarations are removed), 
<code>false</code> for (inclusive) Canonical XML 1.0
+     * @return                          the canonical XML
+     * @throws SAXException             if the value is not well-formed XML, 
or has a DOCTYPE declaration
+     * @throws IOException              if the value cannot be read
+     * @throws GeneralSecurityException if the JDK has no canonicalizer for 
the algorithm
+     * @throws TransformException       if the XML cannot be canonicalized
+     */
+    public static String canonicalize(String xml, boolean exclusive)
+            throws SAXException, IOException, GeneralSecurityException, 
TransformException {
+        if (xml == null) {
+            return null;
+        }
+        // the JDK canonicalizer parses on its own and accepts a DOCTYPE with 
internal entities, so only XML that
+        // passes the hardened parser reaches it
+        parse(xml, new DefaultHandler());
+
+        String algorithm = exclusive ? CanonicalizationMethod.EXCLUSIVE : 
CanonicalizationMethod.INCLUSIVE;
+        TransformService transform = TransformService.getInstance(algorithm, 
"DOM");
+        transform.init(null);
+        // without a declaration the canonicalizer reads the bytes as UTF-8, 
whichever encoding the declaration names
+        byte[] bytes = stripDeclaration(xml).getBytes(StandardCharsets.UTF_8);
+        Data data = transform.transform(new OctetStreamData(new 
ByteArrayInputStream(bytes)), null);
+        try (InputStream is = ((OctetStreamData) data).getOctetStream()) {
+            return new String(is.readAllBytes(), StandardCharsets.UTF_8);
+        }
+    }
+
+    private static String rewrite(String xml, boolean stripNamespaces, boolean 
stripComments, boolean compact)
+            throws SAXException, IOException {
+        if (xml == null) {
+            return null;
+        }
+        XmlWriter writer = new XmlWriter(stripNamespaces, stripComments, 
compact);
+        parse(xml, writer);
+        return writer.toString();
+    }
+
+    private static void parse(String xml, DefaultHandler handler) throws 
SAXException, IOException {
+        SAXParser parser = newParser();
+        if (handler instanceof DefaultHandler2) {
+            
parser.setProperty("http://xml.org/sax/properties/lexical-handler";, handler);
+        }
+        // read from characters so the encoding in the declaration does not 
matter
+        parser.parse(new InputSource(new StringReader(xml)), handler);
+    }
+
+    private static SAXParser newParser() throws SAXException {
+        // the parser of the JDK, whichever parser is on the classpath, so all 
the features below are known to it

Review Comment:
   ⚠️ **Misleading comment.** `SAXParserFactory.newDefaultInstance()` (since 
Java 9) returns the **JDK built-in** SAX parser **always**, bypassing classpath 
service discovery (`META-INF/services`, system property). This is the 
**opposite** of what `newInstance()` does. The intent here is correct — you 
want the JDK parser so the hardening features are guaranteed to be supported — 
but the comment "whichever parser is on the classpath" describes 
`newInstance()` semantics, not `newDefaultInstance()`.
   
   ```suggestion
           // the built-in SAX parser of the JDK, regardless of what SAX 
providers are on the classpath
   ```



-- 
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