From 44b8ef2bafbfaebdc68d0557f89f35385c6668c8 Mon Sep 17 00:00:00 2001 From: Adam Rauch Date: Tue, 13 Oct 2026 15:40:26 -0700 Subject: [PATCH 1/3] Add test cases for bad requests --- src/org/labkey/test/tests/WebDavTest.java | 45 +++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/org/labkey/test/tests/WebDavTest.java b/src/org/labkey/test/tests/WebDavTest.java index 1f4441a27c..bedb7c8935 100644 --- a/src/org/labkey/test/tests/WebDavTest.java +++ b/src/org/labkey/test/tests/WebDavTest.java @@ -18,7 +18,12 @@ import com.github.sardine.DavResource; import com.github.sardine.Sardine; +import org.apache.hc.client5.http.classic.methods.HttpPost; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.core5.http.ClassicHttpResponse; +import org.apache.hc.core5.http.ContentType; import org.apache.hc.core5.http.HttpStatus; +import org.apache.hc.core5.http.io.entity.ByteArrayEntity; import org.junit.Assume; import org.junit.Before; import org.junit.BeforeClass; @@ -149,6 +154,46 @@ public void testBadUrlResponse() throws Exception assertEquals("Wrong response for unparsable parameter", HttpStatus.SC_BAD_REQUEST, response.getResponseCode()); } + @Test + public void testUnparseableParameters() throws Exception + { + String beginUrl = WebTestHelper.buildURL("project", getProjectName(), "begin"); + assertGetResponse(HttpStatus.SC_OK, beginUrl); + assertGetResponse(HttpStatus.SC_BAD_REQUEST, beginUrl + "?x=%C3%28"); + assertGetResponse(HttpStatus.SC_BAD_REQUEST, beginUrl + "?x=%uf"); + assertGetResponse(HttpStatus.SC_BAD_REQUEST, beginUrl + "?=foo"); + // The 404 page reads the request parameters + assertGetResponse(HttpStatus.SC_BAD_REQUEST, WebTestHelper.buildURL("project", getProjectName(), "noSuchAction") + "?x=%uf"); + } + + @Test + public void testMalformedMultipart() throws Exception + { + String beginUrl = WebTestHelper.buildURL("project", getProjectName(), "begin"); + String truncated = "--XYZ\r\nContent-Disposition: form-data; name=\"a\"\r\n\r\nvalue"; + + assertPostResponse(HttpStatus.SC_BAD_REQUEST, beginUrl, "multipart/form-data; boundary=XYZ", truncated); + assertPostResponse(HttpStatus.SC_BAD_REQUEST, beginUrl, "multipart/form-data", "junk"); + assertPostResponse(HttpStatus.SC_BAD_REQUEST, baseWebDavUrl, "multipart/form-data; boundary=XYZ", truncated); + } + + private void assertGetResponse(int expectedStatus, String url) throws IOException + { + SimpleHttpResponse response = new SimpleHttpRequest(url).getResponse(); + assertEquals("Wrong response for " + url, expectedStatus, response.getResponseCode()); + } + + private void assertPostResponse(int expectedStatus, String url, String contentType, String body) throws IOException + { + HttpPost post = new HttpPost(url); + post.setEntity(new ByteArrayEntity(body.getBytes(StandardCharsets.UTF_8), ContentType.parse(contentType))); + try (CloseableHttpClient client = WebTestHelper.getHttpClient()) + { + int status = client.execute(post, ClassicHttpResponse::getCode); + assertEquals("Wrong response for POST of " + contentType + " to " + url, expectedStatus, status); + } + } + @Test public void testWebfiles() throws Exception { From ab309e9f9260cfb18892574d072db1d78a202e2d Mon Sep 17 00:00:00 2001 From: Adam Rauch Date: Wed, 14 Oct 2026 15:01:23 -0700 Subject: [PATCH 2/3] Move test cases --- src/org/labkey/test/tests/WebDavTest.java | 52 +------ .../tests/devtools/ErrorResponseTest.java | 129 ++++++++++++++++++ .../test/tests/devtools/SendErrorTest.java | 78 ----------- 3 files changed, 133 insertions(+), 126 deletions(-) create mode 100644 src/org/labkey/test/tests/devtools/ErrorResponseTest.java delete mode 100644 src/org/labkey/test/tests/devtools/SendErrorTest.java diff --git a/src/org/labkey/test/tests/WebDavTest.java b/src/org/labkey/test/tests/WebDavTest.java index bedb7c8935..69bc6b90a5 100644 --- a/src/org/labkey/test/tests/WebDavTest.java +++ b/src/org/labkey/test/tests/WebDavTest.java @@ -18,12 +18,7 @@ import com.github.sardine.DavResource; import com.github.sardine.Sardine; -import org.apache.hc.client5.http.classic.methods.HttpPost; -import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; -import org.apache.hc.core5.http.ClassicHttpResponse; -import org.apache.hc.core5.http.ContentType; import org.apache.hc.core5.http.HttpStatus; -import org.apache.hc.core5.http.io.entity.ByteArrayEntity; import org.junit.Assume; import org.junit.Before; import org.junit.BeforeClass; @@ -61,9 +56,10 @@ @BaseWebDriverTest.ClassTimeout(minutes = 4) public class WebDavTest extends BaseWebDriverTest { - private static final String TEXT = "Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal.\n"+ - "Now we are engaged in a great civil war, testing whether that nation, or any nation, so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this.\n"+ - "But, in a larger sense, we can not dedicate, we can not consecrate, we can not hallow this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us-that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion-that we here highly resolve that these dead shall not have died in vain-that this nation, under God, shall have a new birth of freedom-and that government of the people, by the people, for the people, shall not perish from the earth."; + private static final String TEXT = """ + Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal. + Now we are engaged in a great civil war, testing whether that nation, or any nation, so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this. + But, in a larger sense, we can not dedicate, we can not consecrate, we can not hallow this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us-that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion-that we here highly resolve that these dead shall not have died in vain-that this nation, under God, shall have a new birth of freedom-and that government of the people, by the people, for the people, shall not perish from the earth."""; private final String OTHER_PROJECT = "WebFileCache Project"; private final String baseWebDavUrl = WebDavUtils.buildBaseWebDavUrl(getProjectName(), "@files/"); @@ -154,46 +150,6 @@ public void testBadUrlResponse() throws Exception assertEquals("Wrong response for unparsable parameter", HttpStatus.SC_BAD_REQUEST, response.getResponseCode()); } - @Test - public void testUnparseableParameters() throws Exception - { - String beginUrl = WebTestHelper.buildURL("project", getProjectName(), "begin"); - assertGetResponse(HttpStatus.SC_OK, beginUrl); - assertGetResponse(HttpStatus.SC_BAD_REQUEST, beginUrl + "?x=%C3%28"); - assertGetResponse(HttpStatus.SC_BAD_REQUEST, beginUrl + "?x=%uf"); - assertGetResponse(HttpStatus.SC_BAD_REQUEST, beginUrl + "?=foo"); - // The 404 page reads the request parameters - assertGetResponse(HttpStatus.SC_BAD_REQUEST, WebTestHelper.buildURL("project", getProjectName(), "noSuchAction") + "?x=%uf"); - } - - @Test - public void testMalformedMultipart() throws Exception - { - String beginUrl = WebTestHelper.buildURL("project", getProjectName(), "begin"); - String truncated = "--XYZ\r\nContent-Disposition: form-data; name=\"a\"\r\n\r\nvalue"; - - assertPostResponse(HttpStatus.SC_BAD_REQUEST, beginUrl, "multipart/form-data; boundary=XYZ", truncated); - assertPostResponse(HttpStatus.SC_BAD_REQUEST, beginUrl, "multipart/form-data", "junk"); - assertPostResponse(HttpStatus.SC_BAD_REQUEST, baseWebDavUrl, "multipart/form-data; boundary=XYZ", truncated); - } - - private void assertGetResponse(int expectedStatus, String url) throws IOException - { - SimpleHttpResponse response = new SimpleHttpRequest(url).getResponse(); - assertEquals("Wrong response for " + url, expectedStatus, response.getResponseCode()); - } - - private void assertPostResponse(int expectedStatus, String url, String contentType, String body) throws IOException - { - HttpPost post = new HttpPost(url); - post.setEntity(new ByteArrayEntity(body.getBytes(StandardCharsets.UTF_8), ContentType.parse(contentType))); - try (CloseableHttpClient client = WebTestHelper.getHttpClient()) - { - int status = client.execute(post, ClassicHttpResponse::getCode); - assertEquals("Wrong response for POST of " + contentType + " to " + url, expectedStatus, status); - } - } - @Test public void testWebfiles() throws Exception { diff --git a/src/org/labkey/test/tests/devtools/ErrorResponseTest.java b/src/org/labkey/test/tests/devtools/ErrorResponseTest.java new file mode 100644 index 0000000000..6413a14888 --- /dev/null +++ b/src/org/labkey/test/tests/devtools/ErrorResponseTest.java @@ -0,0 +1,129 @@ +/* + * Copyright (c) 2024-2026 LabKey Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.labkey.test.tests.devtools; + +import org.apache.hc.client5.http.classic.methods.HttpPost; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.core5.http.ClassicHttpResponse; +import org.apache.hc.core5.http.ContentType; +import org.apache.hc.core5.http.HttpStatus; +import org.apache.hc.core5.http.io.entity.ByteArrayEntity; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.labkey.remoteapi.CommandException; +import org.labkey.remoteapi.CommandResponse; +import org.labkey.remoteapi.GetCommand; +import org.labkey.test.BaseWebDriverTest; +import org.labkey.test.WebTestHelper; +import org.labkey.test.categories.DRT; +import org.labkey.test.categories.Daily; +import org.labkey.test.util.SimpleHttpRequest; +import org.labkey.test.util.SimpleHttpResponse; +import org.labkey.test.util.core.webdav.WebDavUtils; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.List; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +/** + * We expect {@code response.sendError(statusCode, message)} to send the provided message in the response, but this + * wasn't happening by default on embedded Tomcat distributions. This simple test verifies the fix. See Issue 51110. + */ +@Category({DRT.class, Daily.class}) +public class ErrorResponseTest extends BaseWebDriverTest +{ + @Override + protected String getProjectName() + { + return null; + } + + @Override + public List getAssociatedModules() + { + return List.of("DeveloperTools"); + } + + @Test + public void testSendError() throws IOException + { + SendErrorCommand sendErrorCommand = new SendErrorCommand(); + final CommandResponse response; + try + { + response = sendErrorCommand.execute(WebTestHelper.getRemoteApiConnection(), "/"); + fail("Expected execute() to throw a CommandException but succeeded with: " + response.getText()); + } + catch (CommandException e) + { + checker().verifyThat("Response status code", e.getStatusCode(), equalTo(400)); + checker().verifyThat("Response text", e.getResponseText(), containsString("This is a very bad request!")); + } + } + + private static class SendErrorCommand extends GetCommand + { + protected SendErrorCommand() + { + super("test", "sendError"); + } + } + + @Test + public void testUnparseableParameters() throws Exception + { + String beginUrl = WebTestHelper.buildURL("project", getProjectName(), "begin"); + assertGetResponse(HttpStatus.SC_OK, beginUrl); + assertGetResponse(HttpStatus.SC_BAD_REQUEST, beginUrl + "?x=%C3%28"); + assertGetResponse(HttpStatus.SC_BAD_REQUEST, beginUrl + "?x=%uf"); + assertGetResponse(HttpStatus.SC_BAD_REQUEST, beginUrl + "?=foo"); + // The 404 page reads the request parameters + assertGetResponse(HttpStatus.SC_BAD_REQUEST, WebTestHelper.buildURL("project", getProjectName(), "noSuchAction") + "?x=%uf"); + } + + @Test + public void testMalformedMultipart() throws Exception + { + String beginUrl = WebTestHelper.buildURL("project", getProjectName(), "begin"); + String truncated = "--XYZ\r\nContent-Disposition: form-data; name=\"a\"\r\n\r\nvalue"; + + assertPostResponse(HttpStatus.SC_BAD_REQUEST, beginUrl, "multipart/form-data; boundary=XYZ", truncated); + assertPostResponse(HttpStatus.SC_BAD_REQUEST, beginUrl, "multipart/form-data", "junk"); + assertPostResponse(HttpStatus.SC_BAD_REQUEST, WebDavUtils.buildBaseWebDavUrl(getProjectName(), "@files/"), "multipart/form-data; boundary=XYZ", truncated); + } + + private void assertGetResponse(int expectedStatus, String url) throws IOException + { + SimpleHttpResponse response = new SimpleHttpRequest(url).getResponse(); + assertEquals("Wrong response for " + url, expectedStatus, response.getResponseCode()); + } + + private void assertPostResponse(int expectedStatus, String url, String contentType, String body) throws IOException + { + HttpPost post = new HttpPost(url); + post.setEntity(new ByteArrayEntity(body.getBytes(StandardCharsets.UTF_8), ContentType.parse(contentType))); + try (CloseableHttpClient client = WebTestHelper.getHttpClient()) + { + int status = client.execute(post, ClassicHttpResponse::getCode); + assertEquals("Wrong response for POST of " + contentType + " to " + url, expectedStatus, status); + } + } +} diff --git a/src/org/labkey/test/tests/devtools/SendErrorTest.java b/src/org/labkey/test/tests/devtools/SendErrorTest.java deleted file mode 100644 index 30fa174bda..0000000000 --- a/src/org/labkey/test/tests/devtools/SendErrorTest.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright (c) 2024-2026 LabKey Corporation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.labkey.test.tests.devtools; - -import org.junit.Test; -import org.junit.experimental.categories.Category; -import org.labkey.remoteapi.CommandException; -import org.labkey.remoteapi.CommandResponse; -import org.labkey.remoteapi.GetCommand; -import org.labkey.test.BaseWebDriverTest; -import org.labkey.test.WebTestHelper; -import org.labkey.test.categories.DRT; -import org.labkey.test.categories.Daily; - -import java.io.IOException; -import java.util.List; - -import static org.hamcrest.CoreMatchers.containsString; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.fail; - -/** - * We expect {@code response.sendError(statusCode, message)} to send the provided message in the response, but this - * wasn't happening by default on embedded Tomcat distributions. This simple test verifies the fix. See Issue 51110. - */ -@Category({DRT.class, Daily.class}) -public class SendErrorTest extends BaseWebDriverTest -{ - @Override - protected String getProjectName() - { - return null; - } - - @Override - public List getAssociatedModules() - { - return List.of("DeveloperTools"); - } - - @Test - public void testSendError() throws IOException - { - SendErrorCommand sendErrorCommand = new SendErrorCommand(); - final CommandResponse response; - try - { - response = sendErrorCommand.execute(WebTestHelper.getRemoteApiConnection(), "/"); - fail("Expected execute() to throw a CommandException but succeeded with: " + response.getText()); - } - catch (CommandException e) - { - checker().verifyThat("Response status code", e.getStatusCode(), equalTo(400)); - checker().verifyThat("Response text", e.getResponseText(), containsString("This is a very bad request!")); - } - } - - private static class SendErrorCommand extends GetCommand - { - protected SendErrorCommand() - { - super("test", "sendError"); - } - } -} From db5d2cd7a0f7bcab68ed5b02531dab4e703713ed Mon Sep 17 00:00:00 2001 From: labkey-jeckels Date: Wed, 14 Oct 2026 16:42:11 -0700 Subject: [PATCH 3/3] Additional tests; multipart handling --- .../labkey/test/tests/devtools/ErrorResponseTest.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/org/labkey/test/tests/devtools/ErrorResponseTest.java b/src/org/labkey/test/tests/devtools/ErrorResponseTest.java index 6413a14888..440d5fa234 100644 --- a/src/org/labkey/test/tests/devtools/ErrorResponseTest.java +++ b/src/org/labkey/test/tests/devtools/ErrorResponseTest.java @@ -107,9 +107,19 @@ public void testMalformedMultipart() throws Exception assertPostResponse(HttpStatus.SC_BAD_REQUEST, beginUrl, "multipart/form-data; boundary=XYZ", truncated); assertPostResponse(HttpStatus.SC_BAD_REQUEST, beginUrl, "multipart/form-data", "junk"); + // Tomcat doesn't parse other multipart types when reading parameters, so this fails later, in the action's multipart resolution + assertPostResponse(HttpStatus.SC_BAD_REQUEST, beginUrl, "multipart/mixed; boundary=XYZ", truncated); assertPostResponse(HttpStatus.SC_BAD_REQUEST, WebDavUtils.buildBaseWebDavUrl(getProjectName(), "@files/"), "multipart/form-data; boundary=XYZ", truncated); } + @Test + public void testOversizedMultipartFields() throws Exception + { + // Non-file parts count against the connector's maxPostSize, 2MB by default + String oversized = "--XYZ\r\nContent-Disposition: form-data; name=\"a\"\r\n\r\n" + "x".repeat(3 * 1024 * 1024) + "\r\n--XYZ--\r\n"; + assertPostResponse(HttpStatus.SC_REQUEST_TOO_LONG, WebTestHelper.buildURL("project", getProjectName(), "begin"), "multipart/form-data; boundary=XYZ", oversized); + } + private void assertGetResponse(int expectedStatus, String url) throws IOException { SimpleHttpResponse response = new SimpleHttpRequest(url).getResponse();