Skip to content

Commit 623dba3

Browse files
author
renkelvin
authored
Fail open and send auth request to the GCIP backend if Recaptcha toke… (#7254)
1 parent 6d4d187 commit 623dba3

File tree

3 files changed

+28
-53
lines changed

3 files changed

+28
-53
lines changed

packages/auth/src/core/credentials/email.test.ts

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -184,32 +184,17 @@ describe('core/credentials/email', () => {
184184
});
185185
});
186186

187-
it('calls sign in with password with recaptcha forced refresh succeed', async () => {
187+
it('calls sign in with password with recaptcha forced refresh', async () => {
188188
if (typeof window === 'undefined') {
189189
return;
190190
}
191-
// Mock recaptcha js loading method and manually set window.recaptcha
191+
// Mock recaptcha js loading method but not set window.recaptcha to simulate recaptcha token retrieval failure
192192
sinon
193193
.stub(jsHelpers, '_loadJS')
194194
.returns(Promise.resolve(new Event('')));
195-
const recaptcha = new MockGreCAPTCHATopLevel();
196-
window.grecaptcha = recaptcha;
197-
const stub = sinon.stub(recaptcha.enterprise, 'execute');
198-
199-
// First verification should fail with 'wrong-site-key'
200-
stub
201-
.withArgs('wrong-site-key', {
202-
action: RecaptchaActionName.SIGN_IN_WITH_PASSWORD
203-
})
204-
.rejects();
205-
// Second verifcation should succeed with site key refreshed
206-
stub
207-
.withArgs('site-key', {
208-
action: RecaptchaActionName.SIGN_IN_WITH_PASSWORD
209-
})
210-
.returns(Promise.resolve('recaptcha-response'));
195+
window.grecaptcha = undefined;
211196

212-
mockEndpointWithParams(
197+
const getRecaptchaConfigMock = mockEndpointWithParams(
213198
Endpoint.GET_RECAPTCHA_CONFIG,
214199
{
215200
clientType: RecaptchaClientType.WEB,
@@ -218,21 +203,14 @@ describe('core/credentials/email', () => {
218203
recaptchaConfigResponseEnforce
219204
);
220205
await auth.initializeRecaptchaConfig();
221-
auth._agentRecaptchaConfig!.siteKey = 'wrong-site-key';
206+
auth._agentRecaptchaConfig!.siteKey = 'cached-site-key';
222207

223-
const idTokenResponse = await credential._getIdTokenResponse(auth);
224-
expect(idTokenResponse.idToken).to.eq('id-token');
225-
expect(idTokenResponse.refreshToken).to.eq('refresh-token');
226-
expect(idTokenResponse.expiresIn).to.eq('1234');
227-
expect(idTokenResponse.localId).to.eq(serverUser.localId);
228-
expect(apiMock.calls[0].request).to.eql({
229-
captchaResponse: 'recaptcha-response',
230-
clientType: RecaptchaClientType.WEB,
231-
email: 'some-email',
232-
password: 'some-password',
233-
recaptchaVersion: RecaptchaVersion.ENTERPRISE,
234-
returnSecureToken: true
235-
});
208+
await expect(credential._getIdTokenResponse(auth)).to.be.rejectedWith(
209+
'No reCAPTCHA enterprise script loaded.'
210+
);
211+
// Should call getRecaptchaConfig once to refresh the cached recaptcha config
212+
expect(getRecaptchaConfigMock.calls.length).to.eq(2);
213+
expect(auth._agentRecaptchaConfig?.siteKey).to.eq('site-key');
236214
});
237215

238216
it('calls fallback to recaptcha flow when receiving MISSING_RECAPTCHA_TOKEN error', async () => {

packages/auth/src/platform_browser/recaptcha/recaptcha_enterprise_verifier.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ import * as mockFetch from '../../../test/helpers/mock_fetch';
2727
import { ServerError } from '../../api/errors';
2828

2929
import { MockGreCAPTCHATopLevel } from './recaptcha_mock';
30-
import { RecaptchaEnterpriseVerifier } from './recaptcha_enterprise_verifier';
30+
import {
31+
RecaptchaEnterpriseVerifier,
32+
FAKE_TOKEN
33+
} from './recaptcha_enterprise_verifier';
3134

3235
use(chaiAsPromised);
3336
use(sinonChai);
@@ -81,7 +84,7 @@ describe('platform_browser/recaptcha/recaptcha_enterprise_verifier', () => {
8184
expect(await verifier.verify()).to.eq('recaptcha-response');
8285
});
8386

84-
it('reject if error is thrown when retieve site key', async () => {
87+
it('reject if error is thrown when retrieve site key', async () => {
8588
mockEndpointWithParams(
8689
Endpoint.GET_RECAPTCHA_CONFIG,
8790
request,
@@ -102,19 +105,16 @@ describe('platform_browser/recaptcha/recaptcha_enterprise_verifier', () => {
102105
);
103106
});
104107

105-
it('reject if error is thrown when retieve recaptcha token', async () => {
108+
it('return fake recaptcha token if error is thrown when retrieve recaptcha token', async () => {
106109
mockEndpointWithParams(
107110
Endpoint.GET_RECAPTCHA_CONFIG,
108111
request,
109112
recaptchaConfigResponseEnforce
110113
);
111114
sinon
112115
.stub(recaptcha.enterprise, 'execute')
113-
.returns(Promise.reject(Error('retieve-recaptcha-token-error')));
114-
await expect(verifier.verify()).to.be.rejectedWith(
115-
Error,
116-
'retieve-recaptcha-token-error'
117-
);
116+
.returns(Promise.reject(Error('retrieve-recaptcha-token-error')));
117+
expect(await verifier.verify()).to.eq(FAKE_TOKEN);
118118
});
119119
});
120120
});

packages/auth/src/platform_browser/recaptcha/recaptcha_enterprise_verifier.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const RECAPTCHA_ENTERPRISE_URL =
3333
'https://www.google.com/recaptcha/enterprise.js?render=';
3434

3535
export const RECAPTCHA_ENTERPRISE_VERIFIER_TYPE = 'recaptcha-enterprise';
36+
export const FAKE_TOKEN = 'NO_RECAPTCHA';
3637

3738
export class RecaptchaEnterpriseVerifier {
3839
/**
@@ -105,18 +106,14 @@ export class RecaptchaEnterpriseVerifier {
105106
const grecaptcha = window.grecaptcha;
106107
if (isEnterprise(grecaptcha)) {
107108
grecaptcha.enterprise.ready(() => {
108-
try {
109-
grecaptcha.enterprise
110-
.execute(siteKey, { action })
111-
.then(token => {
112-
resolve(token);
113-
})
114-
.catch(error => {
115-
reject(error);
116-
});
117-
} catch (error) {
118-
reject(error);
119-
}
109+
grecaptcha.enterprise
110+
.execute(siteKey, { action })
111+
.then(token => {
112+
resolve(token);
113+
})
114+
.catch(() => {
115+
resolve(FAKE_TOKEN);
116+
});
120117
});
121118
} else {
122119
reject(Error('No reCAPTCHA enterprise script loaded.'));

0 commit comments

Comments
 (0)