Skip to content

Fix "array index out of bounds" problem reported by LGTM.com #23485

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
*/
public final class ContentDisposition {

private static final String INVALID_HEADER_FIELD_PARAMETER_FORMAT =
"Invalid header field parameter format (as defined in RFC 5987)";

@Nullable
private final String type;

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

/**
* Decode the given header field param as describe in RFC 5987.
* Decode the given header field param as described in RFC 5987.
* <p>Only the US-ASCII, UTF-8 and ISO-8859-1 charsets are supported.
* @param input the header field param
* @return the encoded header field param
Expand All @@ -383,13 +386,18 @@ private static String decodeHeaderFieldParam(String input) {
bos.write((char) b);
index++;
}
else if (b == '%') {
char[] array = { (char)value[index + 1], (char)value[index + 2]};
bos.write(Integer.parseInt(String.valueOf(array), 16));
else if (b == '%' && index < value.length - 2) {
char[] array = new char[]{(char) value[index + 1], (char) value[index + 2]};
try {
bos.write(Integer.parseInt(String.valueOf(array), 16));
}
catch (NumberFormatException ex) {
throw new IllegalArgumentException(INVALID_HEADER_FIELD_PARAMETER_FORMAT, ex);
}
index+=3;
}
else {
throw new IllegalArgumentException("Invalid header field parameter format (as defined in RFC 5987)");
throw new IllegalArgumentException(INVALID_HEADER_FIELD_PARAMETER_FORMAT);
}
}
return new String(bos.toByteArray(), charset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
*/
public class ContentDispositionTests {


@Test
public void parseTest() {
ContentDisposition disposition = ContentDisposition
Expand Down Expand Up @@ -198,4 +197,22 @@ public void decodeHeaderFieldParamInvalidCharset() {
ReflectionUtils.invokeMethod(decode, null, "UTF-16''test"));
}

@Test
public void decodeHeaderFieldParamShortInvalidEncodedFilename() {
Method decode = ReflectionUtils.findMethod(ContentDisposition.class,
"decodeHeaderFieldParam", String.class);
ReflectionUtils.makeAccessible(decode);
assertThatIllegalArgumentException().isThrownBy(() ->
ReflectionUtils.invokeMethod(decode, null, "UTF-8''%A"));
}

@Test
public void decodeHeaderFieldParamLongerInvalidEncodedFilename() {
Method decode = ReflectionUtils.findMethod(ContentDisposition.class,
"decodeHeaderFieldParam", String.class);
ReflectionUtils.makeAccessible(decode);
assertThatIllegalArgumentException().isThrownBy(() ->
ReflectionUtils.invokeMethod(decode, null, "UTF-8''%A.txt"));
}

}