summaryrefslogtreecommitdiff
path: root/xfa/fxfa/parser
diff options
context:
space:
mode:
Diffstat (limited to 'xfa/fxfa/parser')
-rw-r--r--xfa/fxfa/parser/cxfa_document.cpp14
-rw-r--r--xfa/fxfa/parser/cxfa_document_parser.cpp3
-rw-r--r--xfa/fxfa/parser/cxfa_document_parser_unittest.cpp127
3 files changed, 137 insertions, 7 deletions
diff --git a/xfa/fxfa/parser/cxfa_document.cpp b/xfa/fxfa/parser/cxfa_document.cpp
index 6860c83376..9e5143c5a4 100644
--- a/xfa/fxfa/parser/cxfa_document.cpp
+++ b/xfa/fxfa/parser/cxfa_document.cpp
@@ -1447,17 +1447,19 @@ CFXJSE_Engine* CXFA_Document::GetScriptContext() const {
XFA_VERSION CXFA_Document::RecognizeXFAVersionNumber(
const WideString& wsTemplateNS) {
WideStringView wsTemplateURIPrefix(kTemplateNS);
- size_t nPrefixLength = wsTemplateURIPrefix.GetLength();
- if (WideStringView(wsTemplateNS.c_str(), wsTemplateNS.GetLength()) !=
- wsTemplateURIPrefix) {
+ if (wsTemplateNS.GetLength() <= wsTemplateURIPrefix.GetLength())
return XFA_VERSION_UNKNOWN;
- }
- auto nDotPos = wsTemplateNS.Find('.', nPrefixLength);
+
+ size_t prefixLength = wsTemplateURIPrefix.GetLength();
+ if (WideStringView(wsTemplateNS.c_str(), prefixLength) != wsTemplateURIPrefix)
+ return XFA_VERSION_UNKNOWN;
+
+ auto nDotPos = wsTemplateNS.Find('.', prefixLength);
if (!nDotPos.has_value())
return XFA_VERSION_UNKNOWN;
int8_t iMajor = FXSYS_wtoi(
- wsTemplateNS.Mid(nPrefixLength, nDotPos.value() - nPrefixLength).c_str());
+ wsTemplateNS.Mid(prefixLength, nDotPos.value() - prefixLength).c_str());
int8_t iMinor =
FXSYS_wtoi(wsTemplateNS
.Mid(nDotPos.value() + 1,
diff --git a/xfa/fxfa/parser/cxfa_document_parser.cpp b/xfa/fxfa/parser/cxfa_document_parser.cpp
index f773a36897..fe246d2300 100644
--- a/xfa/fxfa/parser/cxfa_document_parser.cpp
+++ b/xfa/fxfa/parser/cxfa_document_parser.cpp
@@ -325,7 +325,7 @@ bool XFA_RecognizeRichText(CFX_XMLElement* pRichTextXMLNode) {
CXFA_DocumentParser::CXFA_DocumentParser(CXFA_Document* pFactory)
: m_pFactory(pFactory) {}
-CXFA_DocumentParser::~CXFA_DocumentParser() {}
+CXFA_DocumentParser::~CXFA_DocumentParser() = default;
bool CXFA_DocumentParser::Parse(const RetainPtr<IFX_SeekableStream>& pStream,
XFA_PacketType ePacketID) {
@@ -1137,6 +1137,7 @@ void CXFA_DocumentParser::ParseInstruction(CXFA_Node* pXFANode,
CFX_XMLInstruction* pXMLInstruction,
XFA_PacketType ePacketID) {
const std::vector<WideString>& target_data = pXMLInstruction->GetTargetData();
+
if (pXMLInstruction->IsOriginalXFAVersion()) {
if (target_data.size() > 1 &&
(pXFANode->GetDocument()->RecognizeXFAVersionNumber(target_data[0]) !=
diff --git a/xfa/fxfa/parser/cxfa_document_parser_unittest.cpp b/xfa/fxfa/parser/cxfa_document_parser_unittest.cpp
new file mode 100644
index 0000000000..9f68fc143b
--- /dev/null
+++ b/xfa/fxfa/parser/cxfa_document_parser_unittest.cpp
@@ -0,0 +1,127 @@
+// Copyright 2018 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "xfa/fxfa/parser/cxfa_document_parser.h"
+#include "core/fxcrt/cfx_memorystream.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "testing/test_support.h"
+#include "xfa/fxfa/parser/cxfa_document.h"
+
+class CXFA_DocumentParserTest : public testing::Test {
+ public:
+ void SetUp() override {
+ doc_ = pdfium::MakeUnique<CXFA_Document>(nullptr);
+ parser_ = pdfium::MakeUnique<CXFA_DocumentParser>(doc_.get());
+ }
+
+ void TearDown() override {
+ // Hold the XML tree until we cleanup the document.
+ std::unique_ptr<CFX_XMLNode> root = parser_->GetXMLRoot();
+ parser_ = nullptr;
+ doc_ = nullptr;
+ }
+
+ CXFA_Document* GetDoc() const { return doc_.get(); }
+ CXFA_DocumentParser* GetParser() const { return parser_.get(); }
+
+ private:
+ std::unique_ptr<CXFA_Document> doc_;
+ std::unique_ptr<CXFA_DocumentParser> parser_;
+};
+
+TEST_F(CXFA_DocumentParserTest, XMLInstructionScriptOff) {
+ const char* input =
+ "<config>\n"
+ "<?originalXFAVersion http://www.xfa.org/schema/xfa-template/2.7 "
+ "v2.7-scripting:0 ?>\n"
+ "</config>";
+ EXPECT_FALSE(GetDoc()->HasFlag(XFA_DOCFLAG_Scripting));
+
+ auto stream = pdfium::MakeRetain<CFX_MemoryStream>(
+ reinterpret_cast<uint8_t*>(const_cast<char*>(input)), strlen(input),
+ false);
+ ASSERT_TRUE(GetParser()->Parse(stream, XFA_PacketType::Config));
+
+ CXFA_Node* root = GetParser()->GetRootNode();
+ ASSERT_TRUE(root != nullptr);
+ EXPECT_FALSE(GetDoc()->HasFlag(XFA_DOCFLAG_Scripting));
+}
+
+TEST_F(CXFA_DocumentParserTest, XMLInstructionsScriptOn) {
+ const char* input =
+ "<config>\n"
+ "<?originalXFAVersion http://www.xfa.org/schema/xfa-template/2.7 "
+ "v2.7-scripting:1 ?>\n"
+ "</config>";
+
+ EXPECT_FALSE(GetDoc()->HasFlag(XFA_DOCFLAG_Scripting));
+
+ auto stream = pdfium::MakeRetain<CFX_MemoryStream>(
+ reinterpret_cast<uint8_t*>(const_cast<char*>(input)), strlen(input),
+ false);
+ ASSERT_TRUE(GetParser()->Parse(stream, XFA_PacketType::Config));
+
+ CXFA_Node* root = GetParser()->GetRootNode();
+ ASSERT_TRUE(root != nullptr);
+ EXPECT_TRUE(GetDoc()->HasFlag(XFA_DOCFLAG_Scripting));
+}
+
+TEST_F(CXFA_DocumentParserTest, XMLInstructionsStrictScope) {
+ const char* input =
+ "<config>"
+ "<?acrobat JavaScript strictScoping ?>\n"
+ "</config>";
+
+ EXPECT_FALSE(GetDoc()->HasFlag(XFA_DOCFLAG_StrictScoping));
+
+ auto stream = pdfium::MakeRetain<CFX_MemoryStream>(
+ reinterpret_cast<uint8_t*>(const_cast<char*>(input)), strlen(input),
+ false);
+ ASSERT_TRUE(GetParser()->Parse(stream, XFA_PacketType::Config));
+
+ CXFA_Node* root = GetParser()->GetRootNode();
+ ASSERT_TRUE(root != nullptr);
+ EXPECT_TRUE(GetDoc()->HasFlag(XFA_DOCFLAG_StrictScoping));
+}
+
+TEST_F(CXFA_DocumentParserTest, XMLInstructionsStrictScopeBad) {
+ const char* input =
+ "<config>"
+ "<?acrobat JavaScript otherScoping ?>\n"
+ "</config>";
+
+ EXPECT_FALSE(GetDoc()->HasFlag(XFA_DOCFLAG_StrictScoping));
+
+ auto stream = pdfium::MakeRetain<CFX_MemoryStream>(
+ reinterpret_cast<uint8_t*>(const_cast<char*>(input)), strlen(input),
+ false);
+ ASSERT_TRUE(GetParser()->Parse(stream, XFA_PacketType::Config));
+
+ CXFA_Node* root = GetParser()->GetRootNode();
+ ASSERT_TRUE(root != nullptr);
+ EXPECT_FALSE(GetDoc()->HasFlag(XFA_DOCFLAG_StrictScoping));
+}
+
+TEST_F(CXFA_DocumentParserTest, MultipleXMLInstructions) {
+ const char* input =
+ "<config>"
+ "<?originalXFAVersion http://www.xfa.org/schema/xfa-template/2.7 "
+ "v2.7-scripting:1 ?>\n"
+ "<?acrobat JavaScript strictScoping ?>\n"
+ "</config>";
+
+ EXPECT_FALSE(GetDoc()->HasFlag(XFA_DOCFLAG_Scripting));
+ EXPECT_FALSE(GetDoc()->HasFlag(XFA_DOCFLAG_StrictScoping));
+
+ auto stream = pdfium::MakeRetain<CFX_MemoryStream>(
+ reinterpret_cast<uint8_t*>(const_cast<char*>(input)), strlen(input),
+ false);
+ ASSERT_TRUE(GetParser()->Parse(stream, XFA_PacketType::Config));
+
+ CXFA_Node* root = GetParser()->GetRootNode();
+ ASSERT_TRUE(root != nullptr);
+
+ EXPECT_TRUE(GetDoc()->HasFlag(XFA_DOCFLAG_Scripting));
+ EXPECT_TRUE(GetDoc()->HasFlag(XFA_DOCFLAG_StrictScoping));
+}