diff --git a/core/src/main/java/org/codelibs/saml2/core/util/Util.java b/core/src/main/java/org/codelibs/saml2/core/util/Util.java index 671cdcd..a5cdd1c 100644 --- a/core/src/main/java/org/codelibs/saml2/core/util/Util.java +++ b/core/src/main/java/org/codelibs/saml2/core/util/Util.java @@ -791,8 +791,8 @@ public static String base64decodedInflated(final String input) { final byte[] decoded = Base64.decodeBase64(input); // Inflater + final Inflater decompressor = new Inflater(true); try { - final Inflater decompressor = new Inflater(true); decompressor.setInput(decoded); final byte[] result = new byte[1024]; StringBuilder inflated = new StringBuilder(); @@ -802,11 +802,13 @@ public static String base64decodedInflated(final String input) { limit += 1; inflated.append(new String(result, 0, resultLength, StandardCharsets.UTF_8)); } - decompressor.end(); return inflated.toString(); } catch (final Exception e) { LOGGER.debug("Failed to decode {}", input, e); return new String(decoded); + } finally { + // the native memory of the inflater is released here, not left to the garbage collector + decompressor.end(); } } diff --git a/core/src/main/java/org/codelibs/saml2/core/util/XMLErrorAccumulatorHandler.java b/core/src/main/java/org/codelibs/saml2/core/util/XMLErrorAccumulatorHandler.java index 32664fc..8b27460 100644 --- a/core/src/main/java/org/codelibs/saml2/core/util/XMLErrorAccumulatorHandler.java +++ b/core/src/main/java/org/codelibs/saml2/core/util/XMLErrorAccumulatorHandler.java @@ -16,6 +16,13 @@ */ public class XMLErrorAccumulatorHandler extends DefaultHandler { private static final Logger LOGGER = LoggerFactory.getLogger(XMLErrorAccumulatorHandler.class); + /** + * The most errors that are kept. A message that is checked against a schema comes from an + * anonymous sender, and a document with tens of thousands of invalid elements would otherwise + * keep one exception per element, with its message, for the rest of the check. The first ones + * say what is wrong with the document; the rest add nothing. + */ + private static final int MAX_ERRORS = 100; private final List errors = new ArrayList<>(); /** @@ -26,7 +33,7 @@ public XMLErrorAccumulatorHandler() { @Override public void error(final SAXParseException e) throws SAXException { - errors.add(e); + add(e); if (LOGGER.isDebugEnabled()) { LOGGER.debug("ErrorHandler#error: parsing xml: {}", e.getMessage()); } @@ -34,7 +41,7 @@ public void error(final SAXParseException e) throws SAXException { @Override public void fatalError(final SAXParseException e) throws SAXException { - errors.add(e); + add(e); if (LOGGER.isDebugEnabled()) { LOGGER.debug("ErrorHandler#fatalError: parsing xml: {}", e.getMessage()); } @@ -42,12 +49,18 @@ public void fatalError(final SAXParseException e) throws SAXException { @Override public void warning(final SAXParseException e) throws SAXException { - errors.add(e); + add(e); if (LOGGER.isDebugEnabled()) { LOGGER.debug("ErrorHandler#warning: parsing xml: {}", e.getMessage()); } } + private void add(final SAXParseException e) { + if (errors.size() < MAX_ERRORS) { + errors.add(e); + } + } + /** * Returns the accumulated parsing exceptions. * diff --git a/core/src/test/java/org/codelibs/saml2/core/test/util/UtilsTest.java b/core/src/test/java/org/codelibs/saml2/core/test/util/UtilsTest.java index f5a5b18..f4ac5b7 100644 --- a/core/src/test/java/org/codelibs/saml2/core/test/util/UtilsTest.java +++ b/core/src/test/java/org/codelibs/saml2/core/test/util/UtilsTest.java @@ -303,6 +303,44 @@ private static String nested(int depth) { return sb.toString(); } + /** + * Tests the XMLErrorAccumulatorHandler class + * Case: a document with far more invalid elements than the handler keeps + * + * @see org.codelibs.saml2.core.util.XMLErrorAccumulatorHandler + */ + @Test + public void testSchemaErrorsAreKeptUpToALimit() throws Exception { + StringBuilder xml = new StringBuilder(""); + for (int i = 0; i < 3000; i++) { + xml.append(""); // the Name attribute is required + } + xml.append("" + + ""); + + javax.xml.validation.Validator validator = SchemaFactory.loadFromUrl(SchemaFactory.SAML_SCHEMA_PROTOCOL_2_0).newValidator(); + org.codelibs.saml2.core.util.XMLErrorAccumulatorHandler errors = new org.codelibs.saml2.core.util.XMLErrorAccumulatorHandler(); + validator.setErrorHandler(errors); + validator.validate(new javax.xml.transform.dom.DOMSource(Util.loadXML(xml.toString()))); + + assertTrue(errors.hasError()); + assertEquals(100, errors.getErrorXML().size()); + assertFalse(Util.validateXML(Util.loadXML(xml.toString()), SchemaFactory.SAML_SCHEMA_PROTOCOL_2_0)); + } + + /** + * Tests the base64decodedInflated method + * Case: a payload that is not deflated data is answered with its decoded text, as before + * + * @see org.codelibs.saml2.core.util.Util#base64decodedInflated + */ + @Test + public void testBase64decodedInflatedOfBytesThatAreNotDeflated() { + assertEquals("not deflated", Util.base64decodedInflated(Util.base64encoder("not deflated"))); + } + @Test public void testValidateXMLBadFormat() throws Exception { String metadataUnloaded = "";