Skip to content

Commit b3c7e40

Browse files
committed
[bugfix] Ensure that fn:load-xquery-module participates with the XQueryWatchdog
Closes eXist-db/exist#5569
1 parent 8803d12 commit b3c7e40

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

exist-core/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,7 @@
724724
<includes>
725725
<include>project-suppression.xml</include>
726726
<include>src/test/java/org/exist/xquery/ImportFromPkgTest.java</include>
727+
<include>src/test/java/org/exist/xquery/WatchdogTest.java</include>
727728
<include>src/test/java/org/exist/xquery/value/DateTimeTypesTest.java</include>
728729
<include>src/test/java/org/exist/xquery/functions/fn/FunXmlToJsonTest.java</include>
729730
<include>src/test/resources-filtered/org/exist/xquery/import-from-pkg-test.conf.xml</include>
@@ -836,6 +837,7 @@
836837
<include>src/test/java/org/exist/xmldb/CreateCollectionsTest.java</include>
837838
<include>src/test/java/org/exist/xquery/XQueryFunctionsTest.java</include>
838839
<include>src/main/java/org/exist/xquery/functions/fn/FunXmlToJson.java</include>
840+
<include>src/main/java/org/exist/xquery/functions/fn/LoadXQueryModule.java</include>
839841
<include>src/test/java/org/exist/xquery/functions/transform/TransformFromPkgTest.java</include>
840842
<include>src/test/java/org/exist/xquery/value/Base64BinaryValueTypeTest.java</include>
841843
<include>src/main/java/org/exist/xslt/XsltURIResolverHelper.java</include>
@@ -938,10 +940,12 @@
938940
<exclude>src/main/java/org/exist/xmldb/RemoteRestoreService.java</exclude>
939941
<exclude>src/main/java/org/exist/xmlrpc/ExistRpcTypeFactory.java</exclude>
940942
<exclude>src/test/java/org/exist/xquery/ImportFromPkgTest.java</exclude>
943+
<exclude>src/test/java/org/exist/xquery/WatchdogTest.java</exclude>
941944
<exclude>src/main/java/org/exist/xquery/XQueryContext.java</exclude>
942945
<exclude>src/main/java/org/exist/xquery/functions/fn/FunUriCollection.java</exclude>
943946
<exclude>src/main/java/org/exist/xquery/functions/fn/FunXmlToJson.java</exclude>
944947
<exclude>src/test/java/org/exist/xquery/functions/fn/FunXmlToJsonTest.java</exclude>
948+
<exclude>src/main/java/org/exist/xquery/functions/fn/LoadXQueryModule.java</exclude>
945949
<exclude>src/main/java/org/exist/xquery/functions/system/GetUptime.java</exclude>
946950
<exclude>src/main/java/org/exist/xquery/functions/system/Shutdown.java</exclude>
947951
<exclude>src/main/java/org/exist/xquery/value/AbstractDateTimeValue.java</exclude>

exist-core/src/main/java/org/exist/xquery/functions/fn/LoadXQueryModule.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
/*
2+
* Elemental
3+
* Copyright (C) 2024, Evolved Binary Ltd
4+
*
5+
6+
* https://www.evolvedbinary.com | https://www.elemental.xyz
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; version 2.1.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* NOTE: Parts of this file contain code from 'The eXist-db Authors'.
22+
* The original license header is included below.
23+
*
24+
* =====================================================================
25+
*
226
* eXist-db Open Source Native XML Database
327
* Copyright (C) 2001 The eXist-db Authors
428
*
@@ -149,6 +173,7 @@ public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathExce
149173
// create temporary context so main context is not polluted
150174
final XQueryContext tempContext = new XQueryContext(context.getBroker().getBrokerPool(), context.getProfiler());
151175
try {
176+
tempContext.setWatchDog(context.getWatchDog());
152177
tempContext.setModuleLoadPath(context.getModuleLoadPath());
153178
setExternalVars(externalVars, tempContext::declareGlobalVariable);
154179
tempContext.prepareForExecution();
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Elemental
3+
* Copyright (C) 2024, Evolved Binary Ltd
4+
*
5+
6+
* https://www.evolvedbinary.com | https://www.elemental.xyz
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; version 2.1.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*/
21+
package org.exist.xquery;
22+
23+
import org.exist.test.ExistXmldbEmbeddedServer;
24+
import org.junit.BeforeClass;
25+
import org.junit.ClassRule;
26+
import org.junit.Test;
27+
import org.xmldb.api.base.Collection;
28+
import org.xmldb.api.base.Resource;
29+
import org.xmldb.api.base.XMLDBException;
30+
import org.xmldb.api.modules.BinaryResource;
31+
32+
import static org.junit.Assert.*;
33+
34+
/**
35+
* @author <a href="mailto:[email protected]">Adam Retter</a>
36+
*/
37+
public class WatchdogTest {
38+
39+
@ClassRule
40+
public static final ExistXmldbEmbeddedServer existEmbeddedServer = new ExistXmldbEmbeddedServer(false, true, true);
41+
42+
@BeforeClass
43+
public static void setup() throws XMLDBException {
44+
final String queryModule = "module namespace nodes = \"http://nodes\";\n" +
45+
"\n" +
46+
"declare function nodes:many()\n" +
47+
"{\n" +
48+
" <a id=\"too-many\" output-size-limit=\"{util:get-option('exist:output-size-limit')}\">\n" +
49+
" <b><c/><d/><e><f/></e><g/></b>\n" +
50+
" <b><c/><d/><e><f/></e><g/></b>\n" +
51+
" <b><c/><d/><e><f/></e><g/></b>\n" +
52+
" </a>\n" +
53+
"};";
54+
55+
try (final Collection dbCollection = existEmbeddedServer.getRoot();
56+
final Collection watchdogTestCollection = existEmbeddedServer.createCollection(dbCollection, "watchdog-test")) {
57+
final Resource nodesModule = watchdogTestCollection.createResource("nodes.xqm", BinaryResource.class);
58+
nodesModule.setContent(queryModule);
59+
watchdogTestCollection.storeResource(nodesModule);
60+
}
61+
}
62+
63+
@Test
64+
public void outputSizeLimitUnderInImportModule() throws XMLDBException {
65+
final String query =
66+
"declare option exist:output-size-limit \"100\";\n" +
67+
"import module namespace nodes = \"http://nodes\" at \"/db/watchdog-test/nodes.xqm\";\n" +
68+
"nodes:many()";
69+
70+
final String result = existEmbeddedServer.executeOneValue(query);
71+
assertNotNull(result);
72+
}
73+
74+
@Test
75+
public void outputSizeLimitOverInImportModule() {
76+
final String query =
77+
"declare option exist:output-size-limit \"4\";\n" +
78+
"import module namespace nodes = \"http://nodes\" at \"/db/watchdog-test/nodes.xqm\";\n" +
79+
"nodes:many()";
80+
81+
try {
82+
existEmbeddedServer.executeOneValue(query);
83+
} catch (final XMLDBException e) {
84+
assertTrue(e.getMessage().startsWith("exerr:ERROR The constructed document fragment exceeded the predefined output-size-limit"));
85+
}
86+
}
87+
88+
@Test
89+
public void outputSizeLimitUnderInLoadXQueryModule() throws XMLDBException {
90+
final String query =
91+
"declare option exist:output-size-limit \"100\";\n" +
92+
"let $nodes-mod := fn:load-xquery-module(\"http://nodes\", map{ \"location-hints\": \"/db/watchdog-test/nodes.xqm\" })\n" +
93+
"let $nodes-mod-many := $nodes-mod?functions?(QName(\"http://nodes\", \"many\"))?0\n" +
94+
"return\n" +
95+
" $nodes-mod-many()";
96+
97+
final String result = existEmbeddedServer.executeOneValue(query);
98+
assertNotNull(result);
99+
}
100+
101+
@Test
102+
public void outputSizeLimitOverInLoadXQueryModule() throws XMLDBException {
103+
final String query =
104+
"declare option exist:output-size-limit \"4\";\n" +
105+
"let $nodes-mod := fn:load-xquery-module(\"http://nodes\", map{ \"location-hints\": \"/db/watchdog-test/nodes.xqm\" })\n" +
106+
"let $nodes-mod-many := $nodes-mod?functions?(QName(\"http://nodes\", \"many\"))?0\n" +
107+
"return\n" +
108+
" $nodes-mod-many()";
109+
110+
try {
111+
existEmbeddedServer.executeOneValue(query);
112+
} catch (final XMLDBException e) {
113+
assertTrue(e.getMessage().startsWith("exerr:ERROR The constructed document fragment exceeded the predefined output-size-limit"));
114+
}
115+
}
116+
}

0 commit comments

Comments
 (0)