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

Commit 431abc44 authored by John Reck's avatar John Reck Committed by Evelyn Torres
Browse files

Add mimetype filter to LocalImageResolver

Only allow "trusted" codecs

Test: LocalImageResolverTest
Bug: 456471290
Flag: EXEMPT BUGFIX

(cherry picked from commit e69ce2095f902a9f2ebd1871e9a0bda06908f0ab)
Cherrypick-From: https://googleplex-android-review.googlesource.com/q/commit:e6ddc7828e6b3b7221eea7a2a760686766baa6d2
Merged-In: Idafd13a01311f4966bd99c4a5de17b49bd0b8586
Change-Id: Idafd13a01311f4966bd99c4a5de17b49bd0b8586
parent 06ce16e3
Loading
Loading
Loading
Loading
+23 −0
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import android.net.Uri;
import android.util.Size;

import java.io.IOException;
import java.util.Locale;

/** A class to extract Drawables from a MessagingStyle/ConversationStyle message. */
public class LocalImageResolver {
@@ -90,11 +91,33 @@ public class LocalImageResolver {
    private static void onHeaderDecoded(ImageDecoder decoder, ImageDecoder.ImageInfo info,
            ImageDecoder.Source source) {
        final Size size = info.getSize();
        final String mimeType = info.getMimeType();
        boolean isAllowedCodec = false;
        if (mimeType != null) {
            switch (mimeType.toLowerCase(Locale.US)) {
                case "image/png":
                case "image/jpeg":
                case "image/webp":
                case "image/gif":
                case "image/bmp":
                case "image/x-ico":
                case "image/vnd.wap.wbmp":
                case "image/heif":
                case "image/heic":
                case "image/avif":
                    isAllowedCodec = true;
                    break;
            }
        }
        if (!isAllowedCodec) {
            throw new RuntimeException("Image mime type (" + mimeType + ") is not allowed.");
        }
        final int originalSize = Math.max(size.getHeight(), size.getWidth());
        final double ratio = (originalSize > MAX_SAFE_ICON_SIZE_PX)
                ? originalSize * 1f / MAX_SAFE_ICON_SIZE_PX
                : 1.0;
        decoder.setTargetSampleSize(getPowerOfTwoForSampleRatio(ratio));

    }

    /**