Skip to content

Commit 097bfc5

Browse files
authored
Merge pull request #799 from jooby-project/788
MediaType improvement for malware attack against known route. fix #788
2 parents b0c1759 + 560ea5d commit 097bfc5

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

jooby/src/main/java/org/jooby/Err.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,25 @@
3939
@SuppressWarnings("serial")
4040
public class Err extends RuntimeException {
4141

42+
/**
43+
* Exception thrown from {@link MediaType#parse(String)} in case of encountering an invalid media
44+
* type specification String.
45+
*
46+
* @author edgar
47+
*/
48+
public static class BadMediaType extends Err {
49+
50+
/**
51+
* Creates a new {@link BadMediaType}.
52+
*
53+
* @param message
54+
*/
55+
public BadMediaType(final String message) {
56+
super(Status.BAD_REQUEST, message);
57+
}
58+
59+
}
60+
4261
/**
4362
* Missing parameter/header or request attribute.
4463
*

jooby/src/main/java/org/jooby/MediaType.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -501,8 +501,9 @@ public final String toString() {
501501
*
502502
* @param type A media type to parse.
503503
* @return An immutable {@link MediaType}.
504+
* @throws Err.BadMediaType For bad media types.
504505
*/
505-
public static MediaType valueOf(final String type) {
506+
public static MediaType valueOf(final String type) throws Err.BadMediaType {
506507
return parse(type).get(0);
507508
}
508509

@@ -530,12 +531,14 @@ public int hashCode() {
530531
result.add(all);
531532
} else {
532533
String[] typeAndSubtype = parts[0].split("/");
533-
checkArgument(typeAndSubtype.length == 2, "Bad media type found '%s' while parsing '%s'",
534-
type, value);
534+
if (typeAndSubtype.length != 2) {
535+
throw new Err.BadMediaType(value);
536+
}
535537
String stype = typeAndSubtype[0].trim();
536538
String subtype = typeAndSubtype[1].trim();
537-
checkArgument(!(stype.equals("*") && !subtype.equals("*")),
538-
"Bad media type found '%s' while parsing '%s'", type, value);
539+
if ("*".equals(stype) && !"*".equals(subtype)) {
540+
throw new Err.BadMediaType(value);
541+
}
539542
Map<String, String> parameters = DEFAULT_PARAMS;
540543
if (parts.length > 1) {
541544
parameters = new LinkedHashMap<>(DEFAULT_PARAMS);
@@ -560,8 +563,9 @@ public int hashCode() {
560563
*
561564
* @param types Media types to parse.
562565
* @return An list of immutable {@link MediaType}.
566+
* @throws Err.BadMediaType For bad media types.
563567
*/
564-
public static List<MediaType> valueOf(final String... types) {
568+
public static List<MediaType> valueOf(final String... types) throws Err.BadMediaType {
565569
requireNonNull(types, "Types are required.");
566570
List<MediaType> result = new ArrayList<>();
567571
for (String type : types) {
@@ -575,8 +579,9 @@ public static List<MediaType> valueOf(final String... types) {
575579
*
576580
* @param value The string separated by commas.
577581
* @return One ore more {@link MediaType}.
582+
* @throws Err.BadMediaType For bad media types.
578583
*/
579-
public static List<MediaType> parse(final String value) {
584+
public static List<MediaType> parse(final String value) throws Err.BadMediaType {
580585
return cache.computeIfAbsent(value, MediaType::parseInternal);
581586
}
582587

jooby/src/test/java/org/jooby/MediaTypeTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,17 +179,17 @@ public void eq() {
179179
assertNotEquals(MediaType.json, new Object());
180180
}
181181

182-
@Test(expected = IllegalArgumentException.class)
182+
@Test(expected = Err.BadMediaType.class)
183183
public void badMediaType() {
184184
MediaType.valueOf("");
185185
}
186186

187-
@Test(expected = IllegalArgumentException.class)
187+
@Test(expected = Err.BadMediaType.class)
188188
public void badMediaType2() {
189189
MediaType.valueOf("application/and/something");
190190
}
191191

192-
@Test(expected = IllegalArgumentException.class)
192+
@Test(expected = Err.BadMediaType.class)
193193
public void badMediaType3() {
194194
MediaType.valueOf("*/json");
195195
}

0 commit comments

Comments
 (0)