Skip to content

Commit baa511d

Browse files
committed
test: cover the web plugin asset exemption and traversal guard
RequestedWithFilterTest: the GET-only exemption for /webplugins/ assets by path info and by request URI; POST, the discovery list, and other GETs still need the header. WebPluginServletTest: discovery lists only enabled extensions with a manifest; asset serving rejects parent, nested, cross-extension, absolute, unsafe-segment, and symlink escapes. JavaScriptServletTest: response shape and JSON validity. Signed-off-by: gibson9583 <cgibson@outlook.com>
1 parent 7d20cb1 commit baa511d

3 files changed

Lines changed: 449 additions & 0 deletions

File tree

‎server/src/test/java/com/mirth/connect/server/api/providers/RequestedWithFilterTest.java‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,90 @@ public void testDoFilterRequestedWithFalse() {
6666
}
6767
}
6868

69+
@Test
70+
// A web administrator loads plugin UI modules with <script>/import(), which cannot set
71+
// headers, so a GET for a web plugin asset passes without X-Requested-With.
72+
public void testDoFilterAllowsWebPluginAssetGetWithoutHeader() throws Exception {
73+
mirthProperties.setProperty("server.api.require-requested-with", "true");
74+
RequestedWithFilter testFilter = new RequestedWithFilter(mirthProperties);
75+
76+
HttpServletRequest mockReq = mockRequest("GET", "/webplugins/demo/web/plugin.js", null);
77+
HttpServletResponse mockResp = Mockito.mock(HttpServletResponse.class);
78+
FilterChain mockFilterChain = Mockito.mock(FilterChain.class);
79+
80+
testFilter.doFilter(mockReq, mockResp, mockFilterChain);
81+
verify(mockResp, never()).sendError(Mockito.anyInt(), Mockito.anyString());
82+
verify(mockFilterChain).doFilter(mockReq, mockResp);
83+
}
84+
85+
@Test
86+
// Without path info (a configured context path), the full request URI still identifies a
87+
// web plugin asset.
88+
public void testDoFilterAllowsWebPluginAssetGetByRequestUriWithoutHeader() throws Exception {
89+
mirthProperties.setProperty("server.api.require-requested-with", "true");
90+
RequestedWithFilter testFilter = new RequestedWithFilter(mirthProperties);
91+
92+
HttpServletRequest mockReq = mockRequest("GET", null, "/oie/api/webplugins/demo/plugin.json");
93+
HttpServletResponse mockResp = Mockito.mock(HttpServletResponse.class);
94+
FilterChain mockFilterChain = Mockito.mock(FilterChain.class);
95+
96+
testFilter.doFilter(mockReq, mockResp, mockFilterChain);
97+
verify(mockResp, never()).sendError(Mockito.anyInt(), Mockito.anyString());
98+
verify(mockFilterChain).doFilter(mockReq, mockResp);
99+
}
100+
101+
@Test
102+
// Only GET is exempt: any other method on the same path still needs the header.
103+
public void testDoFilterRejectsWebPluginPostWithoutHeader() throws Exception {
104+
mirthProperties.setProperty("server.api.require-requested-with", "true");
105+
RequestedWithFilter testFilter = new RequestedWithFilter(mirthProperties);
106+
107+
HttpServletRequest mockReq = mockRequest("POST", "/webplugins/demo/web/plugin.js", null);
108+
HttpServletResponse mockResp = Mockito.mock(HttpServletResponse.class);
109+
FilterChain mockFilterChain = Mockito.mock(FilterChain.class);
110+
111+
testFilter.doFilter(mockReq, mockResp, mockFilterChain);
112+
verify(mockResp).sendError(HttpServletResponse.SC_BAD_REQUEST, "All requests must have 'X-Requested-With' header");
113+
verify(mockFilterChain, never()).doFilter(mockReq, mockResp);
114+
}
115+
116+
@Test
117+
// The discovery list (/webplugins without a trailing segment) is not an asset and keeps
118+
// the header requirement.
119+
public void testDoFilterRejectsWebPluginListWithoutHeader() throws Exception {
120+
mirthProperties.setProperty("server.api.require-requested-with", "true");
121+
RequestedWithFilter testFilter = new RequestedWithFilter(mirthProperties);
122+
123+
HttpServletRequest mockReq = mockRequest("GET", "/webplugins", null);
124+
HttpServletResponse mockResp = Mockito.mock(HttpServletResponse.class);
125+
FilterChain mockFilterChain = Mockito.mock(FilterChain.class);
126+
127+
testFilter.doFilter(mockReq, mockResp, mockFilterChain);
128+
verify(mockResp).sendError(HttpServletResponse.SC_BAD_REQUEST, "All requests must have 'X-Requested-With' header");
129+
verify(mockFilterChain, never()).doFilter(mockReq, mockResp);
130+
}
131+
132+
@Test
133+
// Every other GET keeps the header requirement.
134+
public void testDoFilterRejectsOtherGetWithoutHeader() throws Exception {
135+
mirthProperties.setProperty("server.api.require-requested-with", "true");
136+
RequestedWithFilter testFilter = new RequestedWithFilter(mirthProperties);
137+
138+
HttpServletRequest mockReq = mockRequest("GET", "/channels", null);
139+
HttpServletResponse mockResp = Mockito.mock(HttpServletResponse.class);
140+
FilterChain mockFilterChain = Mockito.mock(FilterChain.class);
141+
142+
testFilter.doFilter(mockReq, mockResp, mockFilterChain);
143+
verify(mockResp).sendError(HttpServletResponse.SC_BAD_REQUEST, "All requests must have 'X-Requested-With' header");
144+
verify(mockFilterChain, never()).doFilter(mockReq, mockResp);
145+
}
146+
147+
private static HttpServletRequest mockRequest(String method, String pathInfo, String requestUri) {
148+
HttpServletRequest mockReq = Mockito.mock(HttpServletRequest.class);
149+
Mockito.when(mockReq.getMethod()).thenReturn(method);
150+
Mockito.when(mockReq.getPathInfo()).thenReturn(pathInfo);
151+
Mockito.when(mockReq.getRequestURI()).thenReturn(requestUri);
152+
return mockReq;
153+
}
154+
69155
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// SPDX-License-Identifier: MPL-2.0
2+
// SPDX-FileCopyrightText: 2026 Chris Gibson <cgibson@outlook.com>
3+
4+
package com.mirth.connect.server.api.servlets;
5+
6+
import static org.junit.Assert.assertEquals;
7+
import static org.junit.Assert.assertTrue;
8+
9+
import javax.ws.rs.core.MediaType;
10+
import javax.ws.rs.core.Response;
11+
12+
import org.junit.Before;
13+
import org.junit.BeforeClass;
14+
import org.junit.Test;
15+
16+
import com.fasterxml.jackson.databind.JsonNode;
17+
import com.fasterxml.jackson.databind.ObjectMapper;
18+
import com.google.inject.AbstractModule;
19+
import com.google.inject.Guice;
20+
import com.google.inject.Injector;
21+
import com.mirth.connect.server.api.ServletTestBase;
22+
import com.mirth.connect.server.controllers.ControllerFactory;
23+
24+
public class JavaScriptServletTest extends ServletTestBase {
25+
26+
private static final ObjectMapper MAPPER = new ObjectMapper();
27+
28+
private JavaScriptServlet servlet;
29+
30+
@BeforeClass
31+
public static void beforeClass() throws Exception {
32+
ServletTestBase.setup();
33+
34+
Injector injector = Guice.createInjector(new AbstractModule() {
35+
@Override
36+
protected void configure() {
37+
requestStaticInjection(ControllerFactory.class);
38+
bind(ControllerFactory.class).toInstance(controllerFactory);
39+
}
40+
});
41+
injector.getInstance(ControllerFactory.class);
42+
}
43+
44+
@Before
45+
public void beforeTest() {
46+
servlet = new JavaScriptServlet(request, sc);
47+
}
48+
49+
@Test
50+
public void testValidScriptReportsNoError() throws Exception {
51+
Response response = servlet.validateScript("var a = 1;\nvar b = a + 1;");
52+
53+
assertEquals(200, response.getStatus());
54+
assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getMediaType());
55+
JsonNode body = MAPPER.readTree((String) response.getEntity());
56+
assertTrue(body.get("error").isNull());
57+
}
58+
59+
@Test
60+
public void testInvalidScriptReportsErrorText() throws Exception {
61+
Response response = servlet.validateScript("var a = ;");
62+
63+
assertEquals(200, response.getStatus());
64+
JsonNode body = MAPPER.readTree((String) response.getEntity());
65+
assertTrue(body.get("error").isTextual());
66+
assertTrue(body.get("error").asText().length() > 0);
67+
}
68+
69+
@Test
70+
// The error text can carry quotes and newlines; the body must still parse as JSON.
71+
public void testErrorTextIsValidJson() throws Exception {
72+
Response response = servlet.validateScript("function f() {\n return \"unterminated;\n}");
73+
74+
JsonNode body = MAPPER.readTree((String) response.getEntity());
75+
assertTrue(body.get("error").isTextual());
76+
}
77+
78+
@Test
79+
public void testNullScriptIsTreatedAsEmpty() throws Exception {
80+
Response response = servlet.validateScript(null);
81+
82+
JsonNode body = MAPPER.readTree((String) response.getEntity());
83+
assertTrue(body.get("error").isNull());
84+
}
85+
}

0 commit comments

Comments
 (0)