diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/Installation.java b/sentry-android-core/src/main/java/io/sentry/android/core/Installation.java index fcd4406a5f8..63cd25ffcab 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/Installation.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/Installation.java @@ -21,6 +21,9 @@ final class Installation { private static final Charset UTF_8 = Charset.forName("UTF-8"); + // The id is a UUID, so a valid file is far below this + private static final int MAX_INSTALLATION_FILE_SIZE = 1024; + static final @NotNull AutoClosableReentrantLock staticLock = new AutoClosableReentrantLock(); private Installation() {} @@ -43,7 +46,10 @@ public static String id(final @NotNull Context context) throws RuntimeException return deviceId; } deviceId = readInstallationFile(installation); - } catch (Throwable e) { + if (deviceId == null) { + deviceId = writeInstallationFile(installation); + } + } catch (IOException e) { throw new RuntimeException(e); } } @@ -51,12 +57,22 @@ public static String id(final @NotNull Context context) throws RuntimeException } } + /** + * Returns null when the file is larger than an id can be, or can't be read, so the caller treats + * it as corrupt. The size check keeps an oversized file from being read into memory. + */ @TestOnly - static @NotNull String readInstallationFile(final @NotNull File installation) throws IOException { + static @Nullable String readInstallationFile(final @NotNull File installation) { try (final RandomAccessFile f = new RandomAccessFile(installation, "r")) { - final byte[] bytes = new byte[(int) f.length()]; + final long length = f.length(); + if (length > MAX_INSTALLATION_FILE_SIZE) { + return null; + } + final byte[] bytes = new byte[(int) length]; f.readFully(bytes); return new String(bytes, UTF_8); + } catch (IOException e) { + return null; } } diff --git a/sentry-ktor-client/src/main/java/io/sentry/ktorClient/SentryKtorClientUtils.kt b/sentry-ktor-client/src/main/java/io/sentry/ktorClient/SentryKtorClientUtils.kt index 4ab9310d6b1..55e5d2ca6bb 100644 --- a/sentry-ktor-client/src/main/java/io/sentry/ktorClient/SentryKtorClientUtils.kt +++ b/sentry-ktor-client/src/main/java/io/sentry/ktorClient/SentryKtorClientUtils.kt @@ -2,7 +2,6 @@ package io.sentry.ktorClient import io.ktor.client.request.HttpRequest import io.ktor.client.statement.HttpResponse -import io.ktor.client.statement.bodyAsBytes import io.ktor.http.Headers import io.ktor.http.contentLength import io.ktor.util.toMap @@ -22,7 +21,7 @@ import io.sentry.util.UrlUtils import java.util.concurrent.TimeUnit internal object SentryKtorClientUtils { - internal suspend fun captureClientError( + internal fun captureClientError( scopes: IScopes, request: HttpRequest, response: HttpResponse, @@ -50,9 +49,8 @@ internal object SentryKtorClientUtils { cookies = CookieUtils.filterSetCookie(response.headers["Set-Cookie"], scopes.options) headers = getResponseHeaders(scopes, response.headers) statusCode = response.status.value - try { - bodySize = response.bodyAsBytes().size.toLong() - } catch (_: Throwable) {} + // Content-Length rather than reading the body, which would copy all of it into memory + bodySize = response.contentLength() } event.request = sentryRequest diff --git a/sentry/src/main/java/io/sentry/cache/tape/QueueFile.java b/sentry/src/main/java/io/sentry/cache/tape/QueueFile.java index 137f1cd9156..c93d19ebf75 100644 --- a/sentry/src/main/java/io/sentry/cache/tape/QueueFile.java +++ b/sentry/src/main/java/io/sentry/cache/tape/QueueFile.java @@ -379,7 +379,7 @@ boolean ringRead(long position, byte[] buffer, int offset, int count) throws IOE resetFile(); } catch (IOException e) { throw e; - } catch (Throwable e) { + } catch (IndexOutOfBoundsException e) { // most likely the file is corrupt, so we delete it and recreate, accepting data loss resetFile(); }