Skip to content

Movie client-side popup origin validation to _after_ popup is opened #4514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages-exp/auth-exp/src/core/auth/initialize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ describe('core/auth/initialize', () => {
): void {
cb(true);
}
async _originValidation(): Promise<void> {}
async _completeRedirectFn(
_auth: Auth,
_resolver: PopupRedirectResolver,
Expand Down
1 change: 1 addition & 0 deletions packages-exp/auth-exp/src/model/popup_redirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export interface PopupRedirectResolverInternal extends PopupRedirectResolver {
cb: (support: boolean) => unknown
): void;
_redirectPersistence: Persistence;
_originValidation(auth: Auth): Promise<void>;

// This is needed so that auth does not have a hard dependency on redirect
_completeRedirectFn: (
Expand Down
39 changes: 21 additions & 18 deletions packages-exp/auth-exp/src/platform_browser/popup_redirect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,6 @@ describe('platform_browser/popup_redirect', () => {
);
});

it('validates the origin', async () => {
await resolver._initialize(auth);

await resolver._openPopup(auth, provider, event);
expect(validateOrigin._validateOrigin).to.have.been.calledWith(auth);
});

it('throws an error if apiKey is unspecified', async () => {
delete (auth.config as Partial<Config>).apiKey;
await resolver._initialize(auth);
Expand All @@ -132,17 +125,6 @@ describe('platform_browser/popup_redirect', () => {
resolver._openPopup(auth, provider, event)
).to.be.rejectedWith(FirebaseError, 'auth/invalid-api-key');
});

it('rejects immediately if origin validation fails', async () => {
await resolver._initialize(auth);
(validateOrigin._validateOrigin as sinon.SinonStub).returns(
Promise.reject(new Error('invalid-origin'))
);

await expect(
resolver._openPopup(auth, provider, event)
).to.be.rejectedWith(Error, 'invalid-origin');
});
});

context('#_openRedirect', () => {
Expand Down Expand Up @@ -213,6 +195,27 @@ describe('platform_browser/popup_redirect', () => {
});
});

context('#_originValidation', () => {
it('validates the origin', async () => {
await resolver._initialize(auth);

await resolver._originValidation(auth);
expect(validateOrigin._validateOrigin).to.have.been.calledWith(auth);
});

it('rejects if origin validation fails', async () => {
await resolver._initialize(auth);
(validateOrigin._validateOrigin as sinon.SinonStub).returns(
Promise.reject(new Error('invalid-origin'))
);

await expect(resolver._originValidation(auth)).to.be.rejectedWith(
Error,
'invalid-origin'
);
});
});

context('#_initialize', () => {
it('returns different manager for a different auth', async () => {
const manager = await resolver._initialize(auth);
Expand Down
5 changes: 2 additions & 3 deletions packages-exp/auth-exp/src/platform_browser/popup_redirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ class BrowserPopupRedirectResolver implements PopupRedirectResolverInternal {
'_initialize() not called before _openPopup()'
);

await this.originValidation(auth);
const url = _getRedirectUrl(
auth,
provider,
Expand All @@ -90,7 +89,7 @@ class BrowserPopupRedirectResolver implements PopupRedirectResolverInternal {
authType: AuthEventType,
eventId?: string
): Promise<never> {
await this.originValidation(auth);
await this._originValidation(auth);
_setWindowLocation(
_getRedirectUrl(auth, provider, authType, _getCurrentUrl(), eventId)
);
Expand Down Expand Up @@ -154,7 +153,7 @@ class BrowserPopupRedirectResolver implements PopupRedirectResolverInternal {
);
}

private originValidation(auth: AuthInternal): Promise<void> {
_originValidation(auth: AuthInternal): Promise<void> {
const key = auth._key();
if (!this.originValidationPromises[key]) {
this.originValidationPromises[key] = _validateOrigin(auth);
Expand Down
10 changes: 7 additions & 3 deletions packages-exp/auth-exp/src/platform_browser/strategies/popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,17 @@ class PopupOperation extends AbstractPopupRedirectOperation {
);
this.authWindow.associatedEvent = eventId;

// Check for web storage support _after_ the popup is loaded. Checking for
// web storage is slow (on the order of a second or so). Rather than
// waiting on that before opening the window, optimistically open the popup
// Check for web storage support and origin validation _after_ the popup is
// loaded. These operations are slow (~1 second or so) Rather than
// waiting on them before opening the window, optimistically open the popup
// and check for storage support at the same time. If storage support is
// not available, this will cause the whole thing to reject properly. It
// will also close the popup, but since the promise has already rejected,
// the popup closed by user poll will reject into the void.
this.resolver._originValidation(this.auth).catch(e => {
this.reject(e);
});

this.resolver._isIframeWebStorageSupported(this.auth, isSupported => {
if (!isSupported) {
this.reject(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ class CordovaPopupRedirectResolver implements PopupRedirectResolverInternal {
throw new Error('Method not implemented.');
}

_originValidation(): Promise<void> {
return Promise.resolve();
}

private attachCallbackListeners(
auth: AuthInternal,
manager: AuthEventManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,7 @@ export function makeMockPopupRedirectResolver(
_redirectPersistence?: Persistence;

async _completeRedirectFn(): Promise<void> {}

async _originValidation(): Promise<void> {}
};
}