Skip to content

Commit 049d848

Browse files
metsmakristelmerilain
authored andcommitted
Disable by default XML_PARSE_HUGE
IB-8686 Signed-off-by: Raul Metsma <raul@metsma.ee>
1 parent 324b908 commit 049d848

File tree

5 files changed

+66
-10
lines changed

5 files changed

+66
-10
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ jobs:
2222
run: |
2323
brew update
2424
brew install --formula swig doxygen boost
25-
brew unlink python@3.12 || true
2625
brew unlink python@3.13 || true
26+
brew unlink python@3.14 || true
2727
brew unlink openssl@3 || true
2828
brew unlink xz
29+
sudo rm -rf /Library/Frameworks/Python.framework/Versions/3.13
30+
sudo rm -rf /Library/Frameworks/Python.framework/Versions/3.14
2931
- name: Cache
3032
uses: actions/cache@v4
3133
id: cache

src/XMLDocument.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,13 +315,19 @@ struct XMLDocument: public unique_free_d<xmlFreeDoc>, public XMLNode
315315
return is->good() || is->eof() ? int(is->gcount()) : -1;
316316
}, nullptr, &is, XML_CHAR_ENCODING_NONE));
317317
#if VERSION_CHECK(XMLSEC_VERSION_MAJOR, XMLSEC_VERSION_MINOR, XMLSEC_VERSION_SUBMINOR) >= VERSION_CHECK(1, 3, 0)
318-
ctxt->options |= xmlSecParserGetDefaultOptions();
318+
ctxt->options |= xmlSecParserGetDefaultOptions() & ~XML_PARSE_HUGE;
319319
#else
320-
ctxt->options |= XML_PARSE_NOENT|XML_PARSE_DTDLOAD|XML_PARSE_DTDATTR|XML_PARSE_NONET;
320+
ctxt->options |= XML_PARSE_NOENT|XML_PARSE_DTDLOAD|XML_PARSE_DTDATTR|XML_PARSE_NONET|XML_PARSE_NODICT;
321321
#endif
322322
ctxt->loadsubset |= XML_DETECT_IDS|XML_COMPLETE_ATTRS;
323323
if(hugeFile)
324+
{
324325
ctxt->options |= XML_PARSE_HUGE;
326+
#if LIBXML_VERSION < 21300
327+
if(ctxt->sax)
328+
ctxt->sax->entityDecl = 0;
329+
#endif
330+
}
325331
auto result = xmlParseDocument(ctxt.get());
326332
if(result != 0 || !ctxt->wellFormed)
327333
{

test/data/xml-bomb-attr.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
2+
<!DOCTYPE data [
3+
<!ENTITY ent0 "LoL">
4+
<!ENTITY ent1 "&ent0;&ent0;&ent0;&ent0;&ent0;&ent0;&ent0;&ent0;">
5+
<!ENTITY ent2 "&ent1;&ent1;&ent1;&ent1;&ent1;&ent1;&ent1;&ent1;">
6+
<!ENTITY ent3 "&ent2;&ent2;&ent2;&ent2;&ent2;&ent2;&ent2;&ent2;">
7+
<!ENTITY ent4 "&ent3;&ent3;&ent3;&ent3;&ent3;&ent3;&ent3;&ent3;">
8+
<!ENTITY ent5 "&ent4;&ent4;&ent4;&ent4;&ent4;&ent4;&ent4;&ent4;">
9+
<!ENTITY ent6 "&ent5;&ent5;&ent5;&ent5;&ent5;&ent5;&ent5;&ent5;">
10+
<!ENTITY ent7 "&ent6;&ent6;&ent6;&ent6;&ent6;&ent6;&ent6;&ent6;">
11+
<!ENTITY ent8 "&ent7;&ent7;&ent7;&ent7;&ent7;&ent7;&ent7;&ent7;">
12+
<!ENTITY ent9 "&ent8;&ent8;&ent8;&ent8;&ent8;&ent8;&ent8;&ent8;">
13+
<!ENTITY ent10 "&ent9;&ent9;&ent9;&ent9;&ent9;&ent9;&ent9;&ent9;">
14+
<!ENTITY ent11 "&ent10;&ent10;&ent10;&ent10;&ent10;&ent10;&ent10;&ent10;">
15+
<!ENTITY ent12 "&ent11;&ent11;&ent11;&ent11;&ent11;&ent11;&ent11;&ent11;">
16+
<!ENTITY ent13 "&ent12;&ent12;&ent12;&ent12;&ent12;&ent12;&ent12;&ent12;">
17+
]>
18+
<manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0">
19+
<manifest:file-entry manifest:full-path="/" manifest:media-type="application/vnd.etsi.asic-e+zip"/>
20+
<manifest:file-entry manifest:full-path="test.txt" manifest:media-type="application/octet-stream" x="&ent13;" />
21+
</manifest:manifest>

test/data/xml-bomb-cont.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE lolz [
3+
<!ENTITY lol "lol">
4+
<!ELEMENT lolz (#PCDATA)>
5+
<!ENTITY lol1 "&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;">
6+
<!ENTITY lol2 "&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;">
7+
<!ENTITY lol3 "&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;">
8+
<!ENTITY lol4 "&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;">
9+
<!ENTITY lol5 "&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;">
10+
<!ENTITY lol6 "&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;">
11+
<!ENTITY lol7 "&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;">
12+
<!ENTITY lol8 "&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;">
13+
<!ENTITY lol9 "&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;">
14+
]>
15+
<SignedDoc format="DIGIDOC-XML" version="1.3" xmlns="http://www.sk.ee/DigiDoc/v1.3.0#">
16+
<DataFile ContentType="EMBEDDED_BASE64" Filename="test.txt" Id="D0" MimeType="text/plain" Size="4" xmlns="http://www.sk.ee/DigiDoc/v1.3.0#">&lol9;</DataFile>
17+
</SignedDoc>

test/libdigidocpp_boost.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,12 @@
2525
#include <DataFile.h>
2626
#include <Signature.h>
2727
#include <XmlConf.h>
28+
#include <XMLDocument.h>
2829
#include <crypto/Digest.h>
2930
#include <crypto/PKCS12Signer.h>
3031
#include <crypto/X509Crypto.h>
3132
#include <util/DateTime.h>
3233

33-
#include <xmlsec/xmlsec.h>
34-
35-
constexpr auto VERSION_CHECK(int major, int minor, int patch)
36-
{
37-
return (major<<16)|(minor<<8)|patch;
38-
}
39-
4034
namespace digidoc
4135
{
4236

@@ -590,3 +584,19 @@ BOOST_AUTO_TEST_CASE(OpenInvalidMimetypeContainer)
590584
BOOST_CHECK_THROW(Container::openPtr("test-invalid.asics"), Exception);
591585
}
592586
BOOST_AUTO_TEST_SUITE_END()
587+
588+
BOOST_AUTO_TEST_SUITE(XMLTestSuite)
589+
BOOST_AUTO_TEST_CASE(XMLBomb)
590+
{
591+
BOOST_CHECK_EQUAL(XMLDocument("xml-bomb-attr.xml"), false);
592+
BOOST_CHECK_EQUAL(XMLDocument("xml-bomb-cont.xml"), false);
593+
if(std::fstream f{"xml-bomb-attr.xml"})
594+
BOOST_CHECK_THROW(XMLDocument::openStream(f), Exception);
595+
if(std::fstream f{"xml-bomb-cont.xml"})
596+
BOOST_CHECK_THROW(XMLDocument::openStream(f), Exception);
597+
if(std::fstream f{"xml-bomb-attr.xml"})
598+
BOOST_CHECK_THROW(XMLDocument::openStream(f, {}, true), Exception);
599+
if(std::fstream f{"xml-bomb-cont.xml"})
600+
BOOST_CHECK_THROW(XMLDocument::openStream(f, {}, true), Exception);
601+
}
602+
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)