Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions core/src/main/java/org/codelibs/saml2/core/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SAXParseException> errors = new ArrayList<>();

/**
Expand All @@ -26,28 +33,34 @@ 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());
}
}

@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());
}
}

@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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("<samlp:Response xmlns:samlp=\"urn:oasis:names:tc:SAML:2.0:protocol\""
+ " xmlns:saml=\"urn:oasis:names:tc:SAML:2.0:assertion\" ID=\"r1\" Version=\"2.0\""
+ " IssueInstant=\"2026-01-01T00:00:00Z\"><samlp:Extensions>");
for (int i = 0; i < 3000; i++) {
xml.append("<saml:Attribute/>"); // the Name attribute is required
}
xml.append("</samlp:Extensions><samlp:Status><samlp:StatusCode Value=\"urn:oasis:names:tc:SAML:2.0:status:Success\"/>"
+ "</samlp:Status></samlp:Response>");

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 = "<xml><EntityDescriptor>";
Expand Down
Loading