Skip to content

Commit

Permalink
openStream should not be intercepted if Firebase Perf is not initiali…
Browse files Browse the repository at this point in the history
…zed (#5885)

Proposed fix for #5584:

### Issue
Crash happens if a URL connection is opened e.g. `url.openStream()` when
`TransportManager` has not been initialized. If developer wants to
programmatically enable Firebase using FirebaseOptions, but have
included the Firebase Perf plugin, the app will crash after doing a
network call.

### Steps to reproduce
1. Add Firebase perf plugin
- Module `build.gradle`
```
plugins {
    alias(libs.plugins.android.application) apply false
    alias(libs.plugins.jetbrains.kotlin.android) apply false
    id("com.google.gms.google-services") version "4.4.1" apply false
    id("com.google.firebase.firebase-perf") version "1.4.2" apply false
}
```
- App `build.gradle`
```
plugins {
    alias(libs.plugins.android.application)
    alias(libs.plugins.jetbrains.kotlin.android)
    id("com.google.gms.google-services")
    id("com.google.firebase.firebase-perf")
}
```
2. Disable Firebase
```xml
        <provider
            android:name="com.google.firebase.provider.FirebaseInitProvider"
            android:authorities="${applicationId}.firebaseinitprovider"
            tools:node="remove"
            />   
```
3. Do a network call
```
val url = URL("https", "www.amazon.com", 443, "")
val inputStream = url.openStream()
```

### Investigation
1. `openStream` in `FirebasePerfUrlConnection` intercepts all urls that
opens connections.
5. `openStream` requires `TransportManager` to be initialized
6. `TransportManager` is only initialized after `FirebasePerformance`
has been initialized

### Solution
If `TransportManager` is not initialized, we should simply return the
URL openStream and not intercept the connection.
  • Loading branch information
argzdev authored and rlazo committed May 2, 2024
1 parent ff67a85 commit 4f585e6
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
3 changes: 2 additions & 1 deletion firebase-perf/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Unreleased

[fixed] Fixed an `ExceptionInInitializerError` where the `url.openStream()` causes a crash if
FirebasePerf is not yet initialized.

# 20.5.2
* [changed] Bump internal dependencies.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public static InputStream openStream(final URL url) throws IOException {
*/
static InputStream openStream(URLWrapper wrapper, TransportManager transportManager, Timer timer)
throws IOException {
if (!TransportManager.getInstance().isInitialized()) {
return wrapper.openConnection().getInputStream();
}
timer.reset();
long startTime = timer.getMicros();
NetworkRequestMetricBuilder builder = NetworkRequestMetricBuilder.builder(transportManager);
Expand Down

0 comments on commit 4f585e6

Please sign in to comment.