Skip to content

Commit ced51d0

Browse files
authored
Put some guardrails around the recaptcha host language param (#3734)
* Add some guardrails for recaptcha host language param * Formatting
1 parent 4d9dc69 commit ced51d0

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

packages-exp/auth-exp/src/platform_browser/recaptcha/recaptcha_loader.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ describe('platform-browser/recaptcha/recaptcha_loader', () => {
142142
const loader = new ReCaptchaLoaderImpl();
143143
expect(await loader.load(auth)).to.eq(_window().grecaptcha);
144144
});
145+
146+
it('fails if the host language is invalid', async () => {
147+
expect(() => loader.load(auth, 'javascript:injection')).to.throw(
148+
FirebaseError,
149+
'auth/argument-error'
150+
);
151+
});
145152
});
146153
});
147154
});

packages-exp/auth-exp/src/platform_browser/recaptcha/recaptcha_loader.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import { querystring } from '@firebase/util';
1919

2020
import { AUTH_ERROR_FACTORY, AuthErrorCode } from '../../core/errors';
21+
import { assert } from '../../core/util/assert';
2122
import { Delay } from '../../core/util/delay';
2223
import { Auth, AuthCore } from '../../model/auth';
2324
import { _window } from '../auth_window';
@@ -45,6 +46,10 @@ export class ReCaptchaLoaderImpl implements ReCaptchaLoader {
4546
private readonly librarySeparatelyLoaded = !!_window().grecaptcha;
4647

4748
load(auth: AuthCore, hl = ''): Promise<Recaptcha> {
49+
assert(isHostLanguageValid(hl), AuthErrorCode.ARGUMENT_ERROR, {
50+
appName: auth.name
51+
});
52+
4853
if (this.shouldResolveImmediately(hl)) {
4954
return Promise.resolve(_window().grecaptcha!);
5055
}
@@ -123,6 +128,10 @@ export class ReCaptchaLoaderImpl implements ReCaptchaLoader {
123128
}
124129
}
125130

131+
function isHostLanguageValid(hl: string): boolean {
132+
return hl.length <= 6 && /^\s*[a-zA-Z0-9\-]*\s*$/.test(hl);
133+
}
134+
126135
export class MockReCaptchaLoaderImpl implements ReCaptchaLoader {
127136
async load(auth: Auth): Promise<Recaptcha> {
128137
return new MockReCaptcha(auth);

0 commit comments

Comments
 (0)