1515import org .neo4j .graphdb .ResourceIterator ;
1616import org .neo4j .test .rule .DbmsRule ;
1717import org .neo4j .test .rule .ImpermanentDbmsRule ;
18- import org .xml .sax .SAXParseException ;
1918
2019import java .io .File ;
2120import java .nio .charset .Charset ;
3332import static apoc .util .MapUtil .map ;
3433import static apoc .util .TestUtil .*;
3534import static java .util .Arrays .asList ;
36- import static org .hamcrest .CoreMatchers .containsString ;
37- import static org .hamcrest .MatcherAssert .assertThat ;
3835import static org .junit .Assert .assertEquals ;
3936import static org .junit .Assert .assertFalse ;
4037import static org .junit .Assert .assertNotNull ;
@@ -458,10 +455,25 @@ public void testLoadXmlPreventXXEVulnerabilityThrowsQueryExecutionException() {
458455 r .next ();
459456 r .close ();
460457 });
461- } catch (Exception e ) {
458+ } catch (QueryExecutionException e ) {
462459 Throwable except = ExceptionUtils .getRootCause (e );
463- assertTrue (except instanceof SAXParseException );
464- assertEquals ("DOCTYPE is disallowed when the feature \" http://apache.org/xml/features/disallow-doctype-decl\" set to true." , except .getMessage ());
460+ assertTrue (except instanceof RuntimeException );
461+ assertEquals (except .getMessage (), "XML documents with a DOCTYPE are not allowed." );
462+ throw e ;
463+ }
464+ }
465+
466+ @ Test (expected = QueryExecutionException .class )
467+ public void testLoadXmlPreventBillionLaughVulnerabilityThrowsQueryExecutionException () {
468+ try {
469+ testResult (db , "CALL apoc.load.xml('" + TestUtil .getUrlFileName ("xml/billion_laughs.xml" ) + "')" , (r ) -> {
470+ r .next ();
471+ r .close ();
472+ });
473+ } catch (QueryExecutionException e ) {
474+ Throwable except = ExceptionUtils .getRootCause (e );
475+ assertTrue (except instanceof RuntimeException );
476+ assertEquals (except .getMessage (), "XML documents with a DOCTYPE are not allowed." );
465477 throw e ;
466478 }
467479 }
@@ -474,10 +486,26 @@ public void testXmlParsePreventXXEVulnerabilityThrowsQueryExecutionException() {
474486 r .next ();
475487 r .close ();
476488 });
477- } catch (Exception e ) {
489+ } catch (QueryExecutionException e ) {
478490 Throwable except = ExceptionUtils .getRootCause (e );
479- assertTrue (except instanceof SAXParseException );
480- assertEquals ("DOCTYPE is disallowed when the feature \" http://apache.org/xml/features/disallow-doctype-decl\" set to true." , except .getMessage ());
491+ assertTrue (except instanceof RuntimeException );
492+ assertEquals (except .getMessage (), "XML documents with a DOCTYPE are not allowed." );
493+ throw e ;
494+ }
495+ }
496+
497+ @ Test (expected = QueryExecutionException .class )
498+ public void testXmlParsePreventBillionLaughsVulnerabilityThrowsQueryExecutionException () {
499+ try {
500+ final var xml = "<?xml version=\" 1.0\" ?><!DOCTYPE lolz [<!ENTITY lol \" lol\" ><!ENTITY lol1 \" &lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;\" >]><foo>&lol1;</foo>" ;
501+ testResult (db , "RETURN apoc.xml.parse('" + xml + "')" , (r ) -> {
502+ r .next ();
503+ r .close ();
504+ });
505+ } catch (QueryExecutionException e ) {
506+ Throwable except = ExceptionUtils .getRootCause (e );
507+ assertTrue (except instanceof RuntimeException );
508+ assertEquals (except .getMessage (), "XML documents with a DOCTYPE are not allowed." );
481509 throw e ;
482510 }
483511 }
@@ -489,10 +517,25 @@ public void testImportXmlPreventXXEVulnerabilityThrowsQueryExecutionException()
489517 r .next ();
490518 r .close ();
491519 });
492- } catch (Exception e ) {
520+ } catch (QueryExecutionException e ) {
521+ Throwable except = ExceptionUtils .getRootCause (e );
522+ assertTrue (except instanceof RuntimeException );
523+ assertEquals (except .getMessage (), "XML documents with a DOCTYPE are not allowed." );
524+ throw e ;
525+ }
526+ }
527+
528+ @ Test (expected = QueryExecutionException .class )
529+ public void testImportXmlPreventBillionLaughsVulnerabilityThrowsQueryExecutionException () {
530+ try {
531+ testResult (db , "CALL apoc.import.xml('" + TestUtil .getUrlFileName ("xml/billion_laughs.xml" ) + "')" , (r ) -> {
532+ r .next ();
533+ r .close ();
534+ });
535+ } catch (QueryExecutionException e ) {
493536 Throwable except = ExceptionUtils .getRootCause (e );
494- assertTrue (except instanceof com . ctc . wstx . exc . WstxParsingException );
495- assertThat (except .getMessage (), containsString ( "Encountered a reference to external entity \" xxe \" " ) );
537+ assertTrue (except instanceof RuntimeException );
538+ assertEquals (except .getMessage (), "XML documents with a DOCTYPE are not allowed." );
496539 throw e ;
497540 }
498541 }
0 commit comments