Skip to content

Commit 07f02f8

Browse files
committed
Make the session id not depend on fetching the fid
1 parent 0605925 commit 07f02f8

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

firebase-crashlytics/src/androidTest/java/com/google/firebase/crashlytics/internal/common/IdManagerTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package com.google.firebase.crashlytics.internal.common;
1616

17+
import static com.google.common.truth.Truth.assertThat;
1718
import static com.google.firebase.crashlytics.internal.common.DataCollectionArbiterTest.MOCK_ARBITER_DISABLED;
1819
import static com.google.firebase.crashlytics.internal.common.DataCollectionArbiterTest.MOCK_ARBITER_ENABLED;
1920
import static org.mockito.Mockito.mock;
@@ -209,4 +210,32 @@ public void testInstanceIdRotatesWithDataCollectionFlag() {
209210
cachedFid = prefs.getString(IdManager.PREFKEY_FIREBASE_IID, null);
210211
assertEquals(cachedFid, originalFid);
211212
}
213+
214+
public void testIdForEntropy() {
215+
prefs.edit().putString(IdManager.PREFKEY_INSTALLATION_UUID, "Seveneves").apply();
216+
IdManager idManager = createIdManager(null, MOCK_ARBITER_ENABLED);
217+
218+
assertThat(idManager.getIdForEntropy()).isEqualTo(CommonUtils.sha1("Seveneves"));
219+
}
220+
221+
public void testIdForEntropy_arbiterDisabled() {
222+
prefs.edit().putString(IdManager.PREFKEY_INSTALLATION_UUID, "Seveneves").apply();
223+
IdManager idManager = createIdManager(null, MOCK_ARBITER_DISABLED);
224+
225+
// Don't use the Crashlytics id, since data collection is disabled.
226+
assertThat(idManager.getIdForEntropy()).isNotEqualTo(CommonUtils.sha1("Seveneves"));
227+
assertThat(idManager.getIdForEntropy()).hasLength(32); // Length of random uuid.
228+
}
229+
230+
public void testIdForEntropy_arbiterDisabled_syntheticFid() {
231+
prefs
232+
.edit()
233+
.putString(IdManager.PREFKEY_INSTALLATION_UUID, "Seveneves")
234+
.putString(IdManager.PREFKEY_FIREBASE_IID, "SYN_") // Synthetic fid
235+
.apply();
236+
IdManager idManager = createIdManager(null, MOCK_ARBITER_ENABLED);
237+
238+
// Do use the cached Crashlytics id, since the cached fid is synthetic.
239+
assertThat(idManager.getIdForEntropy()).isEqualTo(CommonUtils.sha1("Seveneves"));
240+
}
212241
}

firebase-crashlytics/src/main/java/com/google/firebase/crashlytics/internal/common/CLSUUID.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ class CLSUUID {
4040
this.populateSequenceNumber(bytes);
4141
this.populatePID(bytes);
4242

43-
// sha1 it to ensure the string is long enough, has only valid hex characters, and to
44-
// increase entropy.
45-
final String idSha = CommonUtils.sha1(idManager.getInstallIds().getCrashlyticsInstallId());
43+
final String idSha = idManager.getIdForEntropy();
4644
final String timeSeqPid = CommonUtils.hexify(bytes);
4745

4846
_clsId =

firebase-crashlytics/src/main/java/com/google/firebase/crashlytics/internal/common/IdManager.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ public class IdManager implements InstallIdProvider {
5555
// Stores a Crashlytics-specific install id, and Firebase installation id.
5656
private InstallIds installIds;
5757

58+
/** Stores a hash of an arbitrary id, for use in increasing entropy in session ids. */
59+
private String idForEntropy;
60+
5861
/**
5962
* @param appContext Application {@link Context}
6063
* @param appIdentifier the value to return as the App identifier (usually a package name), must
@@ -148,6 +151,29 @@ public synchronized InstallIds getInstallIds() {
148151
return installIds;
149152
}
150153

154+
/** Returns a hash of an arbitrary id, for use in increasing entropy in session ids. */
155+
synchronized String getIdForEntropy() {
156+
if (idForEntropy != null) {
157+
return idForEntropy;
158+
}
159+
160+
SharedPreferences prefs = CommonUtils.getSharedPrefs(appContext);
161+
String cachedFid = prefs.getString(PREFKEY_FIREBASE_IID, null);
162+
String cachedUuid = prefs.getString(PREFKEY_INSTALLATION_UUID, null);
163+
164+
// If the fid is synthetic, or data collection is enabled, use the cached uuid.
165+
// The cached uuid is good enough, even if it's not current, to create enough entropy.
166+
if ((isSyntheticFid(cachedFid) || dataCollectionArbiter.isAutomaticDataCollectionEnabled())
167+
&& cachedUuid != null) {
168+
idForEntropy = CommonUtils.sha1(cachedUuid);
169+
} else {
170+
// Otherwise, generate a new uuid just for this process.
171+
idForEntropy = formatId(UUID.randomUUID().toString());
172+
}
173+
174+
return idForEntropy;
175+
}
176+
151177
/**
152178
* Returns true if we have not cached an InstallIds, or should force refresh the fid.
153179
*
@@ -213,7 +239,9 @@ private synchronized String createAndCacheCrashlyticsInstallId(
213239
return iid;
214240
}
215241

216-
/** @return the package name that identifies this App. */
242+
/**
243+
* @return the package name that identifies this App.
244+
*/
217245
public String getAppIdentifier() {
218246
return appIdentifier;
219247
}

0 commit comments

Comments
 (0)