Harden XML parsing via commons-secure-xml (#766) * Harden XML parsing via commons-secure-xml Create XmlStringLookup's document builder and XPath factories through org.apache.commons:commons-secure-xml. The secure factories enable FEATURE_SECURE_PROCESSING and install a non-removable entity-resolver floor on every parser they produce: external DTD and entity lookups are resolved to empty content instead of being fetched, and internal entity expansion is bounded, regardless of the JAXP implementation on the classpath. Changes: - Add the commons-secure-xml dependency (1.0.0-SNAPSHOT until its first release). - Route factory creation through SecureDocumentBuilderFactory and SecureXPathFactory in XmlStringLookup when the instance's feature map enables secure processing. The documented opt-outs keep their meaning: feature maps without secure processing, and the standard javax.xml.accessExternalDTD system property (which the secure factory's resolver floor would otherwise ignore), keep using the plain JAXP factories, so external entity resolution can be restored where it is wanted. - Adapt the secure-path tests to the secure contract: a parser may either reject a document with an external reference or parse it with the reference resolved to empty content; the tests now assert that the external content does not leak into the result instead of expecting one fixed failure mode. - Run the CI and CodeQL builds with -Puse-apache-snapshots (inherited from the org.apache:apache parent POM) so the commons-secure-xml SNAPSHOT resolves; CodeQL's autobuild receives the profile through MAVEN_ARGS. Assisted-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MHgnMnGWHQoH2zD2jFdoMT * Use the Commons Secure XML 1.0.0 release candidate Bump org.apache.commons:commons-secure-xml from 1.0.0-SNAPSHOT to 1.0.0 and add the temporary staging repository https://repository.apache.org/content/repositories/orgapachecommons-1962/ after Central, so the vote gets downstream CI results. Drop the -Puse-apache-snapshots profile from the CI workflows, which the release version no longer needs. Remove the staging repository once 1.0.0 is released. Assisted-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0167e29ScPEdfzJnEFm95imK * Bump Apache Commons Secure XML from 1.0.0-SNAPSHOT to 1.0.0 * Drop the insecure XmlStringLookup fallback Commons Secure XML blocks external resource fetching with an entity resolver rather than with parser features, so the opt-outs the previous commit documented never worked: neither a feature map without secure processing nor the javax.xml.accessExternalDTD system property reaches the resolver floor. Always create the factories through SecureDocumentBuilderFactory and SecureXPathFactory instead. Changes: - Remove the plain-JAXP fallback from XmlStringLookup.lookup(String), along with the DEFAULT_XML_FEATURES and DEFAULT_XPATH_FEATURES maps that only existed to select it. - Document the two aspects Commons Secure XML secures separately: FEATURE_SECURE_PROCESSING governs processing limits and remains settable, while external DTD subsets and entities are blocked outright and cannot be re-enabled. - Remove the references to the "XmlStringLookup.secure" system property, which was never introduced. - Disable the tests that expect an external entity to resolve, and tighten the leak assertion now that the parser no longer rejects such documents. - Shorten the changelog entry and state the behavior change. Assisted-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Y7VfssZLkK11bBY2kVyyY4 * Add missing tests * Fence external XML references in XmlStringLookup Commons Secure XML blocks external DTD subsets and entities with a non-removable entity-resolver floor, and lets a caller opt specific resources back in by installing a resolver of their own. Reuse the Path fences the lookup already accepts for that: a caller who declared trusted roots for the document gets its follow-up resources resolved from those same roots, and from nowhere else. Changes: - Add PathFenceResolver, a package-private EntityResolver that resolves a system identifier only when it lands inside the fence and refuses anything else, including any non-file reference. XmlStringLookup installs it only when the fence has roots, so an unfenced lookup resolves no external resource at all. - Parse with the document's own URI, so relative system identifiers resolve against the document as XML requires, rather than against the process working directory. - Add PathFence.isEmpty() to tell an empty fence from a real allow. - Reword the XmlStringLookup and StringLookupFactory Javadoc: secure processing covers two separate aspects, the FEATURE_SECURE_PROCESSING limits that callers can still turn off and the external references that only a fence can opt back in. Drop the references to the XmlStringLookup.secure system property, which was never implemented. - Move the external-reference fixtures to src/test/resources/XmlStringLookup, with the referenced files in the root and the documents that reference them one directory below, so one pair of files covers both the allowed and the refused case depending on where the fence points. Assisted-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QoqFgJRAAHAHhvJTfDRfUt * Add a changelog entry for the base URI fix XmlStringLookup used to parse through DocumentBuilder.parse(InputStream), which supplies no base URI, so the parser expanded a relative system identifier against the process working directory rather than against the document. That defect shipped in 1.5 and was reachable through StringLookupFactory.xmlStringLookup(Map) with a feature map that left external references enabled, so record it as a fix of its own rather than folding it into the Commons Secure XML entries. Assisted-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QoqFgJRAAHAHhvJTfDRfUt * Cover the malformed system identifier branch of PathFenceResolver PathFenceResolver.toPath catches URISyntaxException, IllegalArgumentException and FileSystemNotFoundException, but every refusal the tests exercised went through the preceding scheme check instead, leaving that catch untested. - testMalformedSystemIdIsRefused covers the URISyntaxException: an unencoded space, the realistic way a system identifier goes malformed. - testUnconvertibleFileUrlIsRefused covers the IllegalArgumentException from Paths.get on a 'file:' URI that parses and passes the scheme check but names no path: an opaque one, and one carrying a fragment. Both assert on the cause, so a change that made the scheme check reject these earlier would not silently keep the tests green. Assisted-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QoqFgJRAAHAHhvJTfDRfUt * Drop the unreachable FileSystemNotFoundException catch Paths.get(URI) raises FileSystemNotFoundException only for a scheme whose provider is not installed, and PathFenceResolver.toPath rejects every scheme but 'file' before it gets there, so that catch type could never fire. Assisted-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QoqFgJRAAHAHhvJTfDRfUt --------- Co-authored-by: Gary Gregory <garydgregory@gmail.com>
Apache Commons Text is a set of utility functions and reusable components for processing and manipulating text in a Java environment.
More information can be found on the Apache Commons Text homepage. The Javadoc can be browsed. Questions related to the usage of Apache Commons Text should be posted to the user mailing list.
You can download source and binaries from our download page.
Alternatively, you can pull it from the central Maven repositories:
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-text</artifactId> <version>1.15.0</version> </dependency>
Building requires a Java JDK and Apache Maven. The required Java version is found in the pom.xml as the maven.compiler.source property.
From a command shell, run mvn without arguments to invoke the default Maven goal to run all tests and checks.
We accept Pull Requests via GitHub. The developer mailing list is the main channel of communication for contributors. There are some guidelines which will make applying PRs easier for us:
mvn.mvn (without arguments). This runs the default goal which contains all build checks.mvn clean site -Dcommons.jacoco.haltOnFailure=false -PjacocoIf you plan to contribute on a regular basis, please consider filing a contributor license agreement. You can learn more about contributing via GitHub in our contribution guidelines.
This code is licensed under the Apache License v2.
See the NOTICE.txt file for required notices and attributions.
You like Apache Commons Text? Then donate back to the ASF to support development.
Please see the list of components