-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enhance XML Loader to tolerate whitespace #576
Merged
+272
−2
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,10 +22,76 @@ import org.apache.daffodil.api.StringSchemaSource | |
import org.apache.daffodil.xml.DaffodilXMLLoader | ||
import org.junit.Assert._ | ||
import org.junit.Test | ||
import org.xml.sax.SAXParseException | ||
|
||
import scala.collection.mutable.ArrayBuffer | ||
import scala.xml.SAXParseException | ||
|
||
class TestXMLLoader { | ||
|
||
@Test | ||
def test_schemaLoad(): Unit = { | ||
val data = | ||
"""<?xml version="1.0"?> | ||
|<xs:schema | ||
|targetNamespace="http://example.com" | ||
|xmlns:ex="http://example.com" | ||
|xmlns:xs="http://www.w3.org/2001/XMLSchema" | ||
|xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
|xmlns:dfdl="http://www.ogf.org/dfdl/dfdl-1.0/"> | ||
| <xs:include schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/> | ||
| <xs:annotation> | ||
| <xs:appinfo source="http://www.ogf.org/dfdl/"> | ||
| <dfdl:format lengthKind="delimited" ref="ex:GeneralFormat"/> | ||
| </xs:appinfo> | ||
| </xs:annotation> | ||
| <xs:element name="e1"> | ||
| <xs:complexType> | ||
| <xs:sequence> | ||
| <xs:element name="s1" type="xs:int"/> | ||
| </xs:sequence> | ||
| </xs:complexType> | ||
| </xs:element> | ||
|</xs:schema> | ||
|""".stripMargin | ||
val loader = new DaffodilXMLLoader() | ||
val ss = StringSchemaSource(data) | ||
val root = | ||
loader.load(ss, None, false) | ||
assertEquals("http://example.com", (root \ "@targetNamespace").text) | ||
} | ||
|
||
@Test | ||
def test_startsWithPINotProlog(): Unit = { | ||
val data = | ||
"""<?xml-model href="../Schematron/iCalendar.sch" type="application/xml" schematypens="http://purl.oclc.org/dsdl/schematron"?> | ||
|<xs:schema | ||
|targetNamespace="http://example.com" | ||
|xmlns:ex="http://example.com" | ||
|xmlns:xs="http://www.w3.org/2001/XMLSchema" | ||
|xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
|xmlns:dfdl="http://www.ogf.org/dfdl/dfdl-1.0/"> | ||
| <xs:include schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/> | ||
| <xs:annotation> | ||
| <xs:appinfo source="http://www.ogf.org/dfdl/"> | ||
| <dfdl:format lengthKind="delimited" ref="ex:GeneralFormat"/> | ||
| </xs:appinfo> | ||
| </xs:annotation> | ||
| <xs:element name="e1"> | ||
| <xs:complexType> | ||
| <xs:sequence> | ||
| <xs:element name="s1" type="xs:int"/> | ||
| </xs:sequence> | ||
| </xs:complexType> | ||
| </xs:element> | ||
|</xs:schema> | ||
|""".stripMargin | ||
val loader = new DaffodilXMLLoader() | ||
val ss = StringSchemaSource(data) | ||
val root = | ||
loader.load(ss, None, false) | ||
assertEquals("http://example.com", (root \ "@targetNamespace").text) | ||
} | ||
|
||
/** | ||
* Characterize behavior of scala's xml loader w.r.t. CDATA preservation. | ||
* | ||
|
@@ -83,7 +149,7 @@ b&"<>]]>""")) | |
loader.load(ss, None, false, true) | ||
} | ||
val m = e.getMessage() | ||
assertTrue(m.contains("DOCTYPE is disallowed")) | ||
assertTrue(m.contains("DOCTYPE")) | ||
} | ||
|
||
/** | ||
|
@@ -137,4 +203,66 @@ b&"<>]]>""")) | |
xmlFromDafLoaderNormalized.text) | ||
|
||
} | ||
|
||
@Test def testLoaderToleratesXMLWithLeadingWhitespace(): Unit = { | ||
val xmlTextWithLeadingWhitespace = " \n" + "<data>foo</data>\n" | ||
val loader = new DaffodilXMLLoader() | ||
val ss = StringSchemaSource(xmlTextWithLeadingWhitespace) | ||
val xml = loader.load(ss, None, addPositionAttributes = false) | ||
assertEquals("foo", xml.text) | ||
} | ||
|
||
@Test def testLoaderToleratesXMLWithLeadingComments(): Unit = { | ||
val xmlText = " \n" + | ||
" <!-- a comment --> \n <data>foo</data>\n" | ||
val loader = new DaffodilXMLLoader() | ||
val ss = StringSchemaSource(xmlText) | ||
val xml = loader.load(ss, None, addPositionAttributes = false) | ||
assertEquals("foo", xml.text) | ||
} | ||
|
||
@Test def testLoaderToleratesXMLWithPI(): Unit = { | ||
val xmlText = " \n" + | ||
"<?aProcInstr yadda yadda ?>\n" + | ||
" <!-- a comment --> \n <data>foo</data>\n" | ||
val loader = new DaffodilXMLLoader() | ||
val ss = StringSchemaSource(xmlText) | ||
val xml = loader.load(ss, None, addPositionAttributes = false) | ||
assertEquals("foo", xml.text) | ||
} | ||
mbeckerle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@Test def testLoaderCatchesVarousBadXML(): Unit = { | ||
val xmlText = " \n" + // no prolog some whitespace (tolerated) | ||
"&AnEntityRef;\n" + // entity refs not allowed | ||
"random text\n" + // just text not allowed | ||
"<data>foo</data>\n" + | ||
"<!-- comment afterwards --><another>element</another>\n&AnotherEntityRef;\nmore random text\n" // other bad stuff. | ||
val teh = new TestErrorHandler() | ||
val loader = new DaffodilXMLLoader(teh) | ||
val ss = StringSchemaSource(xmlText) | ||
val xml = loader.load(ss, None, addPositionAttributes = false) | ||
val msgs = teh.exceptions.map{ _.getMessage() }.mkString("\n") | ||
println(msgs) | ||
assertTrue(msgs.contains("non-empty text nodes not allowed")) | ||
assertTrue(msgs.contains("random text")) | ||
assertTrue(msgs.contains("more random text")) | ||
assertTrue(msgs.contains("exactly one element")) | ||
} | ||
} | ||
|
||
class TestErrorHandler extends org.xml.sax.ErrorHandler { | ||
|
||
val exceptions = new ArrayBuffer[SAXParseException] | ||
|
||
def warning(exception: SAXParseException) = { | ||
exceptions += exception | ||
} | ||
|
||
def error(exception: SAXParseException) = { | ||
exceptions += exception | ||
} | ||
|
||
def fatalError(exception: SAXParseException) = { | ||
exceptions += exception | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No newline at end of file (but up to you). |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suppose upstream takes your document() change to tolerate whitespace before the first
<
character and other big fixes, could you also suggest they take a change to make it easier to load XML securely? Maybe the upstream PR could let us override a doctype function or something like that so we don't need to duplicate any upstream code in our constructing parser implementation here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interestingly, the scala xml library now defaults (as of version 2.0.0) to "secure". So they've taken that step.
What I don't know is the exact details of what they consider "secure". Does it apply to the constructing parser as well as the regular load calls, etc. Also, e.g., do they disallow DocTypes entirely, or just external entity usage, or ???
Which is why the earlier PR to specify no doctype to be used, so we know exactly how strict we are being.
I will try to get changes to the constructing parser upstreamed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Scala XML ticket: scala/scala-xml#532