Skip to content

Avatar picker decodes the full-res image with no size cap, OOM risk #10

Description

@CanReader

The avatar picker reads and decodes the whole selected image into memory with no size limit before uploading. In ProfileMenu's photo-picker callback:

ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[8192];
int len;
while ((len = stream.read(buffer)) != -1) out.write(buffer, 0, len);
byte[] imageBytes = out.toByteArray();
...
Bitmap preview = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
...
String base64 = Base64.getEncoder().encodeToString(imageBytes);

The server caps avatars at 5MB and 8000x8000 and checks the header before decoding (nice), but the client does none of that first. If the user picks a big photo (modern phone cameras easily produce 20-50MP images), the client loads the whole file into a byte[], decodes it to a full-resolution Bitmap for the preview, and base64-encodes the whole thing. On a low-end device that's an easy OutOfMemoryError and a crash, and even when it doesn't crash it uploads a huge payload the server is just going to reject anyway.

Worth checking the file size up front and using BitmapFactory.Options with inSampleSize to downscale before decoding, so the client never holds the full-res bitmap.

File: android/src/com/focus/kingdom/ui/screen/ProfileMenu.java, the pickMedia callback, around line 130-154.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions