Skip to content
Merged
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 @@ -235,7 +235,9 @@ private static String decodeWord(final String word) throws ParseException, Unsup
// get the decoded byte data and convert into a string.
final var decodedData = out.toByteArray();
return new String(decodedData, javaCharset(charset));
} catch (final IOException e) {
} catch (final IOException | IllegalArgumentException e) {
// IllegalArgumentException is thrown by the Base64 decoder on a malformed final unit; treat it like a
// quoted-printable decode failure so both encodings reject malformed input the same way.
throw new UnsupportedEncodingException("Invalid RFC 2047 encoding");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ private static void assertEncoded(final String expected, final String encoded) t
assertEquals(expected, MimeUtils.decodeText(encoded));
}

@Test
void testDecodeInvalidBase64() {
// A base64 payload whose final unit is truncated ("A" is a single base64 char) is rejected the same way as a
// malformed quoted-printable payload, rather than leaking an IllegalArgumentException from the base64 decoder.
assertThrows(UnsupportedEncodingException.class, () -> MimeUtils.decodeText("=?UTF-8?B?A?="));
}

@Test
void testDecodeInvalidEncoding() {
assertThrows(UnsupportedEncodingException.class, () -> MimeUtils.decodeText("=?invalid?B?xyz-?="));
Expand Down
Loading