From 07472758ee55eecafb802c0cef2b46ad90efb8b9 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 5 Apr 2024 18:03:44 +0200 Subject: [PATCH] Detect Undertow RequestTooBigException message in handleParseFailure Closes gh-32549 --- .../StandardMultipartHttpServletRequest.java | 5 +++-- ...andardMultipartHttpServletRequestTests.java | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/multipart/support/StandardMultipartHttpServletRequest.java b/spring-web/src/main/java/org/springframework/web/multipart/support/StandardMultipartHttpServletRequest.java index e0d1a3a68d9c..b1d2191eadc5 100644 --- a/spring-web/src/main/java/org/springframework/web/multipart/support/StandardMultipartHttpServletRequest.java +++ b/spring-web/src/main/java/org/springframework/web/multipart/support/StandardMultipartHttpServletRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -118,7 +118,8 @@ protected void handleParseFailure(Throwable ex) { String msg = cause.getMessage(); if (msg != null) { msg = msg.toLowerCase(); - if (msg.contains("exceed") && (msg.contains("size") || msg.contains("length"))) { + if ((msg.contains("exceed") && (msg.contains("size") || msg.contains("length"))) || + (msg.contains("request") && (msg.contains("big") || msg.contains("large")))) { throw new MaxUploadSizeExceededException(-1, ex); } } diff --git a/spring-web/src/test/java/org/springframework/web/multipart/support/StandardMultipartHttpServletRequestTests.java b/spring-web/src/test/java/org/springframework/web/multipart/support/StandardMultipartHttpServletRequestTests.java index 4a3b2613bd81..dfdadda286ec 100644 --- a/spring-web/src/test/java/org/springframework/web/multipart/support/StandardMultipartHttpServletRequestTests.java +++ b/spring-web/src/test/java/org/springframework/web/multipart/support/StandardMultipartHttpServletRequestTests.java @@ -123,6 +123,14 @@ void jetty12MaxLengthExceededException() { .isThrownBy(() -> requestWithException(ex)).withCause(ex); } + @Test // gh-32549 + void undertowRequestTooBigException() { + IOException ex = new IOException("Connection terminated as request was larger than 10000"); + + assertThatExceptionOfType(MaxUploadSizeExceededException.class) + .isThrownBy(() -> requestWithException(ex)).withCause(ex); + } + private static StandardMultipartHttpServletRequest requestWithPart(String name, String disposition, String content) { MockHttpServletRequest request = new MockHttpServletRequest(); @@ -142,4 +150,14 @@ public Collection getParts() throws ServletException { return new StandardMultipartHttpServletRequest(request); } + private static StandardMultipartHttpServletRequest requestWithException(IOException ex) { + MockHttpServletRequest request = new MockHttpServletRequest() { + @Override + public Collection getParts() throws IOException { + throw ex; + } + }; + return new StandardMultipartHttpServletRequest(request); + } + }