Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 761334b2 authored by Kim Schulz's avatar Kim Schulz Committed by Zhihai Xu
Browse files

MAP: make MMS parsing more robust

Some devices are not following the specs correctly and send plan-text as MMS without the right headers.
This patch makes the MMS parser more robust against faulty MMS messages.

Bug: 11161383
Change-Id: If5e59f9daaab4537cfe5d06e6203ae783e311fd3
parent 274728b4
Loading
Loading
Loading
Loading
+9 −17
Original line number Diff line number Diff line
@@ -662,9 +662,7 @@ public class BluetoothMapContentObserver {
                        /* Send message if folder is outbox */
                        /* to do, support MMS in the future */
                        /*
                        if (folder.equals("outbox")) {
                           handle = sendMmsMessage(folder, phone, (BluetoothMapbMessageMmsEmail)msg);
                        }
                        */
                        break;
                    }
@@ -720,7 +718,6 @@ public class BluetoothMapContentObserver {
         *else if folder !outbox:
         *1) push message to folder
         * */
        if (folder != null && (folder.equalsIgnoreCase("outbox")||  folder.equalsIgnoreCase("drafts"))) {
        long handle = pushMmsToFolder(Mms.MESSAGE_BOX_DRAFTS, to_address, msg);
        /* if invalid handle (-1) then just return the handle - else continue sending (if folder is outbox) */
        if (BluetoothMapAppParams.INVALID_VALUE_PARAMETER != handle && folder.equalsIgnoreCase("outbox")) {
@@ -731,11 +728,6 @@ public class BluetoothMapContentObserver {
            mContext.sendBroadcast(sendIntent);
        }
        return handle;
        } else {
            /* not allowed to push mms to anything but outbox/drafts */
            throw  new IllegalArgumentException("Cannot push message to other folders than outbox/drafts");
        }

    }


+2 −1
Original line number Diff line number Diff line
@@ -239,10 +239,11 @@ public class BluetoothMapObexServer extends ServerRequestHandler {
            if(folderName == null || folderName.equals("")) {
                folderName = mCurrentFolder.getName();
            }
            if(!folderName.equals("outbox") && !folderName.equals("draft")) {
            if(!folderName.equalsIgnoreCase("outbox") && !folderName.equalsIgnoreCase("draft")) {
                if(D) Log.d(TAG, "Push message only allowed to outbox and draft. folderName: " + folderName);
                return ResponseCodes.OBEX_HTTP_NOT_ACCEPTABLE;
            }
            folderName = folderName.toLowerCase();
            /*  - Read out the message
             *  - Decode into a bMessage
             *  - send it.
+45 −40
Original line number Diff line number Diff line
@@ -481,6 +481,7 @@ public class BluetoothMapbMessageMmsEmail extends BluetoothMapbMessage {
                {
                    if(contentTypeParts[j].contains("boundary")) {
                        boundary = contentTypeParts[j].split("boundary[\\s]*=", 2)[1].trim();
                        boundary = boundary.replaceAll("\"", ""); // " is allowed around a boundary, but is not allowed as part of the boundary
                    }
                }
            }
@@ -497,14 +498,13 @@ public class BluetoothMapbMessageMmsEmail extends BluetoothMapbMessage {
    private void parseMmsMimePart(String partStr) {
        String[] parts = partStr.split("\r\n\r\n", 2); // Split the header from the body
        String body;
        MimePart newPart = addMimePart();
        String partEncoding = encoding; /* Use the overall encoding as default */
        if(parts.length != 2) {
            body = partStr;
        } else {
            body = parts[1];
        }
            String[] headers = parts[0].split("\r\n");
        MimePart newPart = addMimePart();
        String partEncoding = encoding; /* Use the overall encoding as default */

            for(String header : headers) {
                if(header.length() == 0)
@@ -513,8 +513,12 @@ public class BluetoothMapbMessageMmsEmail extends BluetoothMapbMessage {
                if(header.trim() == "" || header.trim().equals("--")) // Skip empty lines(the \r\n after the boundary tag) and endBoundary tags
                    continue;
                String[] headerParts = header.split(":",2);
            if(headerParts.length != 2)
                throw new IllegalArgumentException("part-Header not formatted correctly: " + header);
                if(headerParts.length != 2) {
                    //throw new IllegalArgumentException("part-Header not formatted correctly: " + header);
                    // If we find a part without headers, treat the entire content as body
                    body = partStr;
                    break;
                }
                String headerType = headerParts[0].toUpperCase();
                String headerValue = headerParts[1].trim();
                if(headerType.contains("CONTENT-TYPE")) {
@@ -542,6 +546,7 @@ public class BluetoothMapbMessageMmsEmail extends BluetoothMapbMessage {
                    if(D) Log.w(TAG,"Skipping unknown part-header: " + headerType + " (" + header + ")");
                }
            }
        }
        // Now for the body
        newPart.data = decodeBody(body, partEncoding);
    }
@@ -607,7 +612,7 @@ public class BluetoothMapbMessageMmsEmail extends BluetoothMapbMessage {
        else
        {
            mimeParts = messageBody.split("--" + boundary);
            for(int i = 0; i < mimeParts.length - 1; i++) {
            for(int i = 1; i < mimeParts.length - 1; i++) {
                String part = mimeParts[i];
                if (part != null && (part.length() > 0))
                    parseMmsMimePart(part);