Skip to content

Commit f084b63

Browse files
jbduncanrstoyanchev
authored andcommitted
Fix "array index out of bounds" problem reported by LGTM.com
1 parent 670cbb9 commit f084b63

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

spring-web/src/main/java/org/springframework/http/ContentDisposition.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
*/
4343
public final class ContentDisposition {
4444

45+
private static final String INVALID_HEADER_FIELD_PARAMETER_FORMAT =
46+
"Invalid header field parameter format (as defined in RFC 5987)";
47+
4548
@Nullable
4649
private final String type;
4750

@@ -357,7 +360,7 @@ else if (!escaped && ch == '"') {
357360
}
358361

359362
/**
360-
* Decode the given header field param as describe in RFC 5987.
363+
* Decode the given header field param as described in RFC 5987.
361364
* <p>Only the US-ASCII, UTF-8 and ISO-8859-1 charsets are supported.
362365
* @param input the header field param
363366
* @return the encoded header field param
@@ -383,13 +386,18 @@ private static String decodeHeaderFieldParam(String input) {
383386
bos.write((char) b);
384387
index++;
385388
}
386-
else if (b == '%') {
387-
char[] array = { (char)value[index + 1], (char)value[index + 2]};
388-
bos.write(Integer.parseInt(String.valueOf(array), 16));
389+
else if (b == '%' && index < value.length - 2) {
390+
char[] array = new char[]{(char) value[index + 1], (char) value[index + 2]};
391+
try {
392+
bos.write(Integer.parseInt(String.valueOf(array), 16));
393+
}
394+
catch (NumberFormatException ex) {
395+
throw new IllegalArgumentException(INVALID_HEADER_FIELD_PARAMETER_FORMAT, ex);
396+
}
389397
index+=3;
390398
}
391399
else {
392-
throw new IllegalArgumentException("Invalid header field parameter format (as defined in RFC 5987)");
400+
throw new IllegalArgumentException(INVALID_HEADER_FIELD_PARAMETER_FORMAT);
393401
}
394402
}
395403
return new String(bos.toByteArray(), charset);

spring-web/src/test/java/org/springframework/http/ContentDispositionTests.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import static org.assertj.core.api.Assertions.assertThat;
3030
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
31+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
3132

3233
/**
3334
* Unit tests for {@link ContentDisposition}
@@ -36,7 +37,6 @@
3637
*/
3738
public class ContentDispositionTests {
3839

39-
4040
@Test
4141
public void parseTest() {
4242
ContentDisposition disposition = ContentDisposition
@@ -198,4 +198,22 @@ public void decodeHeaderFieldParamInvalidCharset() {
198198
ReflectionUtils.invokeMethod(decode, null, "UTF-16''test"));
199199
}
200200

201+
@Test
202+
public void decodeHeaderFieldParamShortInvalidEncodedFilename() {
203+
Method decode = ReflectionUtils.findMethod(ContentDisposition.class,
204+
"decodeHeaderFieldParam", String.class);
205+
ReflectionUtils.makeAccessible(decode);
206+
assertThatIllegalArgumentException().isThrownBy(() ->
207+
ReflectionUtils.invokeMethod(decode, null, "UTF-8''%A"));
208+
}
209+
210+
@Test
211+
public void decodeHeaderFieldParamLongerInvalidEncodedFilename() {
212+
Method decode = ReflectionUtils.findMethod(ContentDisposition.class,
213+
"decodeHeaderFieldParam", String.class);
214+
ReflectionUtils.makeAccessible(decode);
215+
assertThatIllegalArgumentException().isThrownBy(() ->
216+
ReflectionUtils.invokeMethod(decode, null, "UTF-8''%A.txt"));
217+
}
218+
201219
}

0 commit comments

Comments
 (0)