davsclaus opened a new pull request, #30599: URL: https://github.com/apache/camel/pull/30599
## What [CAMEL-27274](https://issues.apache.org/jira/browse/CAMEL-27274) asks for the XML functions that are missing in Simple, so routes do not need Groovy or XSLT for them. New functions: | Function | What it does | Not XML/JSON, or not well-formed | |---|---|---| | `${compact(exp)}` | The inverse of `pretty`: removes the whitespace between XML elements or JSON tokens | as-is | | `${stripXmlDeclaration(exp)}` | Removes `<?xml ...?>` as text, without parsing | as-is | | `${stripXmlComments(exp)}` | Removes `<!-- -->` comments | as-is | | `${stripXmlNamespaces(exp)}` | Removes `xmlns` declarations and the prefixes of elements and attributes (`xml:lang` and the like are kept) | as-is | | `${canonicalXml(exp,mode)}` | W3C canonical XML (C14N, without comments); mode `exclusive` (default) or `inclusive` | fails the evaluation | | `${isValidXml(exp)}` | Whether it is well-formed XML | `false` | | `${isValidJson(exp)}` | Whether it is valid JSON (RFC 8259, strict) | `false` | | `${unescape(kind,exp)}` | The inverse of `escape`, same kinds: html, xml, json, js, sql, url | invalid sequences kept | `exp` is optional everywhere and defaults to the message body. `canonicalXml` fails instead of passing the value through, because a pass-through would make two payloads compare as different without saying why. ## Security The message body usually comes from an untrusted sender, so every XML function that parses goes through one hardened setup in the new `XmlStringHelper` (`camel-xml-jaxp-util`): - `SAXParserFactory.newDefaultInstance()`: always the JDK parser, whichever parser (Woodstox, Xerces) is on the classpath, so the features below are known to it - DOCTYPE disallowed, external general/parameter entities off, external DTD loading off, secure processing on, `ACCESS_EXTERNAL_DTD`/`ACCESS_EXTERNAL_SCHEMA` empty - the parser reads characters (a `StringReader`), so the declared encoding plays no part XML with a DOCTYPE is rejected by all of them: `isValidXml` gives `false` even when the document is well-formed, `canonicalXml` fails, and the others return the value as-is. No entity is expanded. Tests cover an external file entity, an external DTD, a plain DOCTYPE and internal entity expansion for every function. `canonicalXml` uses the canonicalizer of the JDK (`javax.xml.crypto.dsig.TransformService`, module `java.xml.crypto`), so no new JAR is needed. Its own parser blocks external access but does accept a DOCTYPE with internal entities (checked on JDK 25). So the hardened parser checks the value first, and only XML without a DOCTYPE reaches the canonicalizer. ## Where the code is - `camel-xml-jaxp-util`: `XmlStringHelper` (well-formed check, strip, compact, canonicalize). `camel-core-languages` already depends on this module, and it needs only the JDK. - `camel-util-json`: `JsonStringHelper`, a strict scanner that does not recurse, used to check and compact JSON. `Jsoner` accepts trailing commas (`{"a":1,}`), so it could not serve for `isValidJson`. The scanner keeps numbers and strings exactly as written, and nesting 100,000 levels deep does not overflow the stack. - `camel-util`: `EscapeHelper.unescape*`. The entity decoder decodes `&lt;` once, so it gives `<`, and digits must be ASCII. - `camel-core-languages`: `XmlJsonExpressionBuilder`, the function parsing, the `SimpleConstants` metadata and the did-you-mean names. Regenerated `simple.json` and the catalog copies. - Docs: `simple-functions.adoc`, with examples for SOAP namespaces, comparing XML, and a content-based router. ## Tests - New: `XmlStringHelperTest`, `JsonStringHelperTest`, plus additions to `EscapeHelperTest` (including that `unescape(escape(x)) == x` for every kind). - Additions to `OutputFunctionFactoryTest`, `MiscFunctionFactoryTest` and `StringFunctionFactoryTest`. - All 764 Simple tests in `camel-core` pass, as do the full test suites of `camel-util`, `camel-util-json`, `camel-xml-jaxp-util` and `camel-core-languages`, and `-Psourcecheck`. ## Notes - A possible downstream follow-up for Camel Quarkus: `canonicalXml` looks up the JDK `XMLDSig` provider, which a native image may need to register. - Not changed: the existing `XmlPrettyPrinter` writes the `encoding` value where `standalone` belongs. That is a separate one-line bug. _Claude Code on behalf of davsclaus_ 🤖 Generated with [Claude Code](https://claude.com/claude-code) -- 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]
