Skip to content

Commit 1974838

Browse files
authored
Don't send placeholder FAC token to Cloud Functions (#2948)
* Do not send the placeholder token to the functions backend in the error case. * Add tests.
1 parent 893be51 commit 1974838

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

firebase-functions/src/androidTest/java/com/google/firebase/functions/FirebaseContextProviderTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class FirebaseContextProviderTest {
3333
private static final String AUTH_TOKEN = "authToken";
3434
private static final String IID_TOKEN = "iidToken";
3535
private static final String APP_CHECK_TOKEN = "appCheckToken";
36+
private static final String ERROR = "errorString";
3637

3738
private static final InternalAuthProvider fixedAuthProvider =
3839
new TestInternalAuthProvider(() -> AUTH_TOKEN);
@@ -45,6 +46,8 @@ public class FirebaseContextProviderTest {
4546
new TestFirebaseInstanceIdInternal(IID_TOKEN);
4647
private static final InternalAppCheckTokenProvider fixedAppCheckProvider =
4748
new TestInternalAppCheckTokenProvider(APP_CHECK_TOKEN);
49+
private static final InternalAppCheckTokenProvider errorAppCheckProvider =
50+
new TestInternalAppCheckTokenProvider(APP_CHECK_TOKEN, ERROR);
4851

4952
@Test
5053
public void getContext_whenAuthAndAppCheckAreNotAvailable_shouldContainOnlyIid()
@@ -98,6 +101,19 @@ public void getContext_whenOnlyAuthIsAvailableAndNotSignedIn_shouldContainOnlyIi
98101
assertThat(context.getInstanceIdToken()).isEqualTo(IID_TOKEN);
99102
}
100103

104+
@Test
105+
public void getContext_whenOnlyAppCheckIsAvailableAndHasError_shouldContainOnlyIid()
106+
throws ExecutionException, InterruptedException {
107+
FirebaseContextProvider contextProvider =
108+
new FirebaseContextProvider(
109+
absentProvider(), providerOf(fixedIidProvider), deferredOf(errorAppCheckProvider));
110+
111+
HttpsCallableContext context = Tasks.await(contextProvider.getContext());
112+
assertThat(context.getAuthToken()).isNull();
113+
assertThat(context.getInstanceIdToken()).isEqualTo(IID_TOKEN);
114+
assertThat(context.getAppCheckToken()).isNull();
115+
}
116+
101117
@Test
102118
public void getContext_whenAuthAndAppCheckAreAvailable_shouldContainAuthAppCheckTokensAndIid()
103119
throws ExecutionException, InterruptedException {

firebase-functions/src/androidTest/java/com/google/firebase/functions/TestInternalAppCheckTokenProvider.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,23 @@ public FirebaseException getError() {
4343
};
4444
}
4545

46+
public TestInternalAppCheckTokenProvider(String testToken, String error) {
47+
this.testToken =
48+
new AppCheckTokenResult() {
49+
@NonNull
50+
@Override
51+
public String getToken() {
52+
return testToken;
53+
}
54+
55+
@Nullable
56+
@Override
57+
public FirebaseException getError() {
58+
return new FirebaseException(error);
59+
}
60+
};
61+
}
62+
4663
@NonNull
4764
@Override
4865
public Task<AppCheckTokenResult> getToken(boolean forceRefresh) {

firebase-functions/src/main/java/com/google/firebase/functions/FirebaseContextProvider.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ private Task<String> getAppCheckToken() {
100100
.onSuccessTask(
101101
result -> {
102102
if (result.getError() != null) {
103-
Log.w(
104-
TAG,
105-
"Error getting App Check token; using placeholder token instead. Error: "
106-
+ result.getError());
103+
// If there was an error getting the App Check token, do NOT send the placeholder
104+
// token. Only valid App Check tokens should be sent to the functions backend.
105+
Log.w(TAG, "Error getting App Check token. Error: " + result.getError());
106+
return Tasks.forResult(null);
107107
}
108108
return Tasks.forResult(result.getToken());
109109
});

0 commit comments

Comments
 (0)