forked from alkacon/opencms-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve error handling during xml validation
The form editor does not provide useful info when the type definition declares an invalid regex or if the regex matching results in error. This commit catches a PatternSyntaxException or a StackOverflowError thrown during validation, logs it in the server logs, and informs the user about this issue.
- Loading branch information
Showing
12 changed files
with
320 additions
and
10 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
...urces/system/workplace/locales/de/messages/org/opencms/xml/content/messages_de.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
test/org/opencms/xml/content/TestCmsXmlContentValidation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/* | ||
* This library is part of OpenCms - | ||
* the Open Source Content Management System | ||
* | ||
* Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* For further information about Alkacon Software GmbH & Co. KG, please see the | ||
* company website: http://www.alkacon.com | ||
* | ||
* For further information about OpenCms, please see the | ||
* project website: http://www.opencms.org | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
*/ | ||
|
||
package org.opencms.xml.content; | ||
|
||
import junit.extensions.TestSetup; | ||
import junit.framework.Test; | ||
import junit.framework.TestSuite; | ||
|
||
import org.opencms.file.CmsObject; | ||
import org.opencms.i18n.CmsEncoder; | ||
import org.opencms.main.CmsEvent; | ||
import org.opencms.main.I_CmsEventListener; | ||
import org.opencms.main.OpenCms; | ||
import org.opencms.test.OpenCmsTestCase; | ||
import org.opencms.test.OpenCmsTestProperties; | ||
import org.opencms.util.CmsFileUtil; | ||
import org.opencms.xml.CmsXmlContentDefinition; | ||
import org.opencms.xml.CmsXmlEntityResolver; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.HashMap; | ||
import java.util.Locale; | ||
|
||
/** | ||
* Tests the OpenCms XML content validation with regex rules<p> | ||
*/ | ||
public class TestCmsXmlContentValidation extends OpenCmsTestCase { | ||
final String SCHEMA_SYSTEM_ID = "dummy://xmlcontent-definition-testregex.xsd"; | ||
|
||
/** | ||
* Default JUnit constructor.<p> | ||
* | ||
* @param arg0 JUnit parameters | ||
*/ | ||
public TestCmsXmlContentValidation(String arg0) { | ||
|
||
super(arg0); | ||
} | ||
|
||
/** | ||
* Test suite for this test class.<p> | ||
* | ||
* @return the test suite | ||
*/ | ||
public static Test suite() { | ||
|
||
OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH); | ||
|
||
TestSuite suite = new TestSuite(); | ||
suite.setName(TestCmsXmlContentValidation.class.getName()); | ||
|
||
suite.addTest(new TestCmsXmlContentValidation("testHandlingOfPatternSyntaxExceptionDuringValidation")); | ||
suite.addTest(new TestCmsXmlContentValidation("testHandlingOfStackOverflowErrorDuringValidation")); | ||
|
||
TestSetup wrapper = new TestSetup(suite) { | ||
|
||
@Override | ||
protected void setUp() { | ||
|
||
setupOpenCms("simpletest", "/"); | ||
} | ||
|
||
@Override | ||
protected void tearDown() { | ||
|
||
removeOpenCms(); | ||
} | ||
}; | ||
|
||
return wrapper; | ||
} | ||
|
||
public void testHandlingOfStackOverflowErrorDuringValidation() throws Exception { | ||
final CmsXmlContentErrorHandler validationResult = validateXmlFile("org/opencms/xml/content/xmlcontent-definition-evilregex.xsd"); | ||
|
||
assertEquals("Number of registered errors", 1, validationResult.getErrors().size()); | ||
final String recordedError = validationResult.getErrors(Locale.ENGLISH).get("String[1]"); | ||
final String expectedMessage = Messages.get().getBundle(Locale.ENGLISH).key(Messages.GUI_EDITOR_XMLCONTENT_CANNOT_VALIDATE_ERROR_3).split("\\{")[0]; | ||
assertTrue("Expected error during validation not registered in the error handler. Recorded error: '" + recordedError + "'. Expected message: '" + expectedMessage + "'", | ||
recordedError.contains(expectedMessage)); | ||
} | ||
|
||
public void testHandlingOfPatternSyntaxExceptionDuringValidation() throws Exception { | ||
final CmsXmlContentErrorHandler validationResult = validateXmlFile("org/opencms/xml/content/xmlcontent-definition-malformedregex.xsd"); | ||
|
||
assertEquals("Number of registered errors", 1, validationResult.getErrors().size()); | ||
final String recordedError = validationResult.getErrors(Locale.ENGLISH).get("String[1]"); | ||
final String expectedMessage = Messages.get().getBundle(Locale.ENGLISH).key(Messages.GUI_EDITOR_XMLCONTENT_INVALID_RULE_3).split("\\{")[0]; | ||
assertTrue("Expected error during validation not registered in the error handler. Recorded error: '" + recordedError + "'. Expected message: '" + expectedMessage + "'", | ||
recordedError.contains(expectedMessage)); | ||
} | ||
|
||
private CmsXmlContentErrorHandler validateXmlFile(String SCHEMA_FILENAME) throws Exception { | ||
echo("Testing the handling of errors during validation using schema " + SCHEMA_FILENAME); | ||
CmsObject cms = getCmsObject(); | ||
CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms); | ||
|
||
cacheSchema(resolver, SCHEMA_SYSTEM_ID, SCHEMA_FILENAME); | ||
|
||
// now read the XML content | ||
String content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-1-mod7.xml", CmsEncoder.ENCODING_UTF_8); | ||
CmsXmlContent xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver); | ||
|
||
// validate the XML structure | ||
final CmsXmlContentErrorHandler validationResult = xmlcontent.validate(getCmsObject()); | ||
assertFalse("Warning were recorded but not expected. " + validationResult.getWarnings(Locale.ENGLISH), validationResult.hasWarnings()); | ||
return validationResult; | ||
} | ||
|
||
/** | ||
* Updates the OpenCms XML entity resolver cache with a changed XML schema id.<p> | ||
* | ||
* @param resolver the OpenCms XML entity resolver to use | ||
* @param id the XML schema id to update in the resolver | ||
* @param filename the name of the file in the RFS where to read the new schema content from | ||
* @throws Exception if something goes wrong | ||
*/ | ||
private void cacheSchema(CmsXmlEntityResolver resolver, String id, String filename) throws Exception { | ||
|
||
// fire "clear cache" event to clear up previously cached schemas | ||
OpenCms.fireCmsEvent(new CmsEvent(I_CmsEventListener.EVENT_CLEAR_CACHES, new HashMap<String, Object>())); | ||
// read the XML from the given file and store it in the resolver | ||
String content = CmsFileUtil.readFile(filename, CmsEncoder.ENCODING_UTF_8); | ||
CmsXmlContentDefinition definition = CmsXmlContentDefinition.unmarshal(content, id, resolver); | ||
System.out.println(definition.getSchema().asXML()); | ||
CmsXmlEntityResolver.cacheSystemId(id, definition.getSchema().asXML().getBytes(StandardCharsets.UTF_8)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<Multitests | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="dummy://xmlcontent-definition-testregex.xsd"> | ||
|
||
<Multitest language="en"> | ||
<String>1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 | ||
4567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 | ||
4567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 | ||
4567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 | ||
4567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 | ||
4567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 | ||
4567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 | ||
4567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 | ||
4567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 | ||
4567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 | ||
4567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 | ||
4567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 | ||
4567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 | ||
4567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 | ||
4567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 | ||
4567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 | ||
4567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 | ||
4567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 | ||
4567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 | ||
4567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 | ||
4567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 | ||
4567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 | ||
4567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 | ||
4567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890</String> | ||
</Multitest> | ||
</Multitests> |
39 changes: 39 additions & 0 deletions
39
test/org/opencms/xml/content/xmlcontent-definition-evilregex.xsd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> | ||
|
||
<xsd:include schemaLocation="opencms://opencms-xmlcontent.xsd"/> | ||
<xsd:element name="Multitests" type="OpenCmsMultitests"/> | ||
|
||
<xsd:complexType name="OpenCmsMultitests"> | ||
<xsd:sequence> | ||
<xsd:element name="Multitest" type="OpenCmsMultitest" minOccurs="0" maxOccurs="unbounded"/> | ||
</xsd:sequence> | ||
</xsd:complexType> | ||
|
||
<xsd:complexType name="OpenCmsMultitest"> | ||
<xsd:sequence> | ||
<xsd:element name="String" type="OpenCmsString" minOccurs="0" maxOccurs="unbounded" /> | ||
</xsd:sequence> | ||
<xsd:attribute name="language" type="OpenCmsLocale" use="required"/> | ||
</xsd:complexType> | ||
|
||
<xsd:annotation> | ||
<xsd:appinfo> | ||
<FieldSettings> | ||
<Setting> | ||
<PropertyName>String</PropertyName> | ||
<!-- | ||
This kind of regex is compiled into a recursive call, which results in a StackOverflow error | ||
when used on a very large string. Source: https://stackoverflow.com/a/7510006 | ||
Reported ticket in the Java Bug Database Report: https://bugs.java.com/bugdatabase/view_bug.do?bug_id=6337993 | ||
--> | ||
<RuleRegex>(.|\R)*</RuleRegex> | ||
<RuleType>error</RuleType> | ||
<Error>error</Error> | ||
<Search>listtitle</Search> | ||
</Setting> | ||
</FieldSettings> | ||
</xsd:appinfo> | ||
</xsd:annotation> | ||
</xsd:schema> |
Oops, something went wrong.