Skip to content

Commit a2bcdea

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

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed

exist-core/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,7 @@
678678
<includes>
679679
<include>project-suppression.xml</include>
680680
<include>src/test/java/org/exist/xquery/ImportFromPkgTest.java</include>
681+
<include>src/test/java/org/exist/xquery/WatchdogTest.java</include>
681682
<include>src/test/java/org/exist/xquery/value/DateTimeTypesTest.java</include>
682683
<include>src/test/java/org/exist/xquery/functions/fn/FunXmlToJsonTest.java</include>
683684
<include>src/test/resources-filtered/org/exist/xquery/import-from-pkg-test.conf.xml</include>
@@ -790,6 +791,7 @@
790791
<include>src/test/java/org/exist/xmldb/CreateCollectionsTest.java</include>
791792
<include>src/test/java/org/exist/xquery/XQueryFunctionsTest.java</include>
792793
<include>src/main/java/org/exist/xquery/functions/fn/FunXmlToJson.java</include>
794+
<include>src/main/java/org/exist/xquery/functions/fn/LoadXQueryModule.java</include>
793795
<include>src/test/java/org/exist/xquery/functions/transform/TransformFromPkgTest.java</include>
794796
<include>src/test/java/org/exist/xquery/value/Base64BinaryValueTypeTest.java</include>
795797
<include>src/main/java/org/exist/xslt/XsltURIResolverHelper.java</include>
@@ -891,10 +893,12 @@
891893
<exclude>src/main/java/org/exist/xmldb/RemoteRestoreService.java</exclude>
892894
<exclude>src/main/java/org/exist/xmlrpc/ExistRpcTypeFactory.java</exclude>
893895
<exclude>src/test/java/org/exist/xquery/ImportFromPkgTest.java</exclude>
896+
<exclude>src/test/java/org/exist/xquery/WatchdogTest.java</exclude>
894897
<exclude>src/main/java/org/exist/xquery/XQueryContext.java</exclude>
895898
<exclude>src/main/java/org/exist/xquery/functions/fn/FunUriCollection.java</exclude>
896899
<exclude>src/main/java/org/exist/xquery/functions/fn/FunXmlToJson.java</exclude>
897900
<exclude>src/test/java/org/exist/xquery/functions/fn/FunXmlToJsonTest.java</exclude>
901+
<exclude>src/main/java/org/exist/xquery/functions/fn/LoadXQueryModule.java</exclude>
898902
<exclude>src/main/java/org/exist/xquery/functions/system/GetUptime.java</exclude>
899903
<exclude>src/main/java/org/exist/xquery/functions/system/Shutdown.java</exclude>
900904
<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: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
31+
import static org.junit.Assert.*;
32+
33+
/**
34+
* @author <a href="mailto:[email protected]">Adam Retter</a>
35+
*/
36+
public class WatchdogTest {
37+
38+
@ClassRule
39+
public static final ExistXmldbEmbeddedServer existEmbeddedServer = new ExistXmldbEmbeddedServer(false, true, true);
40+
41+
@BeforeClass
42+
public static void setup() throws XMLDBException {
43+
final String queryModule = "module namespace nodes = \"http://nodes\";\n" +
44+
"\n" +
45+
"declare function nodes:many()\n" +
46+
"{\n" +
47+
" <a id=\"too-many\" output-size-limit=\"{util:get-option('exist:output-size-limit')}\">\n" +
48+
" <b><c/><d/><e><f/></e><g/></b>\n" +
49+
" <b><c/><d/><e><f/></e><g/></b>\n" +
50+
" <b><c/><d/><e><f/></e><g/></b>\n" +
51+
" </a>\n" +
52+
"};";
53+
54+
try (final Collection dbCollection = existEmbeddedServer.getRoot();
55+
final Collection watchdogTestCollection = existEmbeddedServer.createCollection(dbCollection, "watchdog-test")) {
56+
final Resource nodesModule = watchdogTestCollection.createResource("nodes.xqm", "BinaryResource");
57+
nodesModule.setContent(queryModule);
58+
watchdogTestCollection.storeResource(nodesModule);
59+
}
60+
}
61+
62+
@Test
63+
public void outputSizeLimitUnderInImportModule() throws XMLDBException {
64+
final String query =
65+
"declare option exist:output-size-limit \"100\";\n" +
66+
"import module namespace nodes = \"http://nodes\" at \"/db/watchdog-test/nodes.xqm\";\n" +
67+
"nodes:many()";
68+
69+
final String result = existEmbeddedServer.executeOneValue(query);
70+
assertNotNull(result);
71+
}
72+
73+
@Test
74+
public void outputSizeLimitOverInImportModule() {
75+
final String query =
76+
"declare option exist:output-size-limit \"4\";\n" +
77+
"import module namespace nodes = \"http://nodes\" at \"/db/watchdog-test/nodes.xqm\";\n" +
78+
"nodes:many()";
79+
80+
try {
81+
existEmbeddedServer.executeOneValue(query);
82+
} catch (final XMLDBException e) {
83+
assertTrue(e.getMessage().startsWith("exerr:ERROR The constructed document fragment exceeded the predefined output-size-limit"));
84+
}
85+
}
86+
87+
@Test
88+
public void outputSizeLimitUnderInLoadXQueryModule() throws XMLDBException {
89+
final String query =
90+
"declare option exist:output-size-limit \"100\";\n" +
91+
"let $nodes-mod := fn:load-xquery-module(\"http://nodes\", map{ \"location-hints\": \"/db/watchdog-test/nodes.xqm\" })\n" +
92+
"let $nodes-mod-many := $nodes-mod?functions?(QName(\"http://nodes\", \"many\"))?0\n" +
93+
"return\n" +
94+
" $nodes-mod-many()";
95+
96+
final String result = existEmbeddedServer.executeOneValue(query);
97+
assertNotNull(result);
98+
}
99+
100+
@Test
101+
public void outputSizeLimitOverInLoadXQueryModule() throws XMLDBException {
102+
final String query =
103+
"declare option exist:output-size-limit \"4\";\n" +
104+
"let $nodes-mod := fn:load-xquery-module(\"http://nodes\", map{ \"location-hints\": \"/db/watchdog-test/nodes.xqm\" })\n" +
105+
"let $nodes-mod-many := $nodes-mod?functions?(QName(\"http://nodes\", \"many\"))?0\n" +
106+
"return\n" +
107+
" $nodes-mod-many()";
108+
109+
try {
110+
existEmbeddedServer.executeOneValue(query);
111+
} catch (final XMLDBException e) {
112+
assertTrue(e.getMessage().startsWith("exerr:ERROR The constructed document fragment exceeded the predefined output-size-limit"));
113+
}
114+
}
115+
}

0 commit comments

Comments
 (0)