From 1fb98cc276bd5d3723d896cb574dd32eaaab67f6 Mon Sep 17 00:00:00 2001 From: Michael Lehenbauer Date: Thu, 19 Dec 2019 16:06:18 -0800 Subject: [PATCH 1/2] Avoid using $httpHeaders in environments that don't send an Origin header. There is a backend bug (b/145624756) causing $httpHeaders to be ignored if an Origin header is not sent. Via https://github.com/firebase/firebase-js-sdk/issues/1491 we've found that several environments can run into this issue so until the backend issue is fixed we are excluding them from using $httpHeaders. This may incur an extra CORS preflight round-trip in some cases, but this is much better than having our Authorization header get ignored. --- packages/firestore/CHANGELOG.md | 4 ++++ .../platform_browser/webchannel_connection.ts | 12 ++++++------ packages/util/src/environment.ts | 16 ++++++++++++++++ 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/packages/firestore/CHANGELOG.md b/packages/firestore/CHANGELOG.md index dd419f3929d..2cac668f4b1 100644 --- a/packages/firestore/CHANGELOG.md +++ b/packages/firestore/CHANGELOG.md @@ -1,4 +1,8 @@ # Unreleased +- [fixed] Fixed an issue where auth credentials were not respected in certain + browser environments (Electron 7, IE11 in trusted zone, UWP apps). (#1491) + +# 1.9.0 - [feature] Added support for storing and retrieving custom types in Firestore. Added support for strongly typed collections, documents, and queries. You can now use `withConverter()` to supply a custom data diff --git a/packages/firestore/src/platform_browser/webchannel_connection.ts b/packages/firestore/src/platform_browser/webchannel_connection.ts index 919b433c065..c1b64b61e84 100644 --- a/packages/firestore/src/platform_browser/webchannel_connection.ts +++ b/packages/firestore/src/platform_browser/webchannel_connection.ts @@ -25,7 +25,7 @@ import { XhrIo } from '@firebase/webchannel-wrapper'; -import { isReactNative } from '@firebase/util'; +import { isElectron, isIE, isReactNative, isUWP } from '@firebase/util'; import { Token } from '../api/credentials'; import { DatabaseId, DatabaseInfo } from '../core/database_info'; @@ -264,11 +264,11 @@ export class WebChannelConnection implements Connection { // formally defined here: // https://github.com/google/closure-library/blob/b0e1815b13fb92a46d7c9b3c30de5d6a396a3245/closure/goog/net/rpc/httpcors.js#L32 // - // But for some unclear reason (see - // https://github.com/firebase/firebase-js-sdk/issues/703), this breaks - // ReactNative and so we exclude it, which just means ReactNative may be - // subject to the extra network roundtrip for CORS preflight. - if (!isReactNative()) { + // TODO(b/145624756): There is a backend bug where $httpHeaders isn't respected if the request + // doesn't have an Origin header. So we have to exclude a few browser environments that are + // known to (sometimes) not include an Origin. See + // https://github.com/firebase/firebase-js-sdk/issues/1491. + if (!isReactNative() && !isElectron() && !isIE() && !isUWP()) { request.httpHeadersOverwriteParam = '$httpHeaders'; } diff --git a/packages/util/src/environment.ts b/packages/util/src/environment.ts index 512a3368f05..75f31e8d588 100644 --- a/packages/util/src/environment.ts +++ b/packages/util/src/environment.ts @@ -83,6 +83,22 @@ export function isReactNative(): boolean { ); } +/** Detects Electron apps. */ +export function isElectron(): boolean { + return getUA().indexOf('electron/') >= 0; +} + +/** Detects Internet Explorer. */ +export function isIE(): boolean { + const ua = getUA(); + return ua.indexOf('MSIE ') >= 0 || ua.indexOf('Trident/') >= 0; +} + +/** Detects Universal Windows Platform apps. */ +export function isUWP(): boolean { + return getUA().indexOf('MSAppHost/') >= 0; +} + /** * Detect whether the current SDK build is the Node version. * From e16fb4a10e73aa15addd314f0ebffa9c12c84c1e Mon Sep 17 00:00:00 2001 From: Michael Lehenbauer Date: Thu, 19 Dec 2019 16:24:44 -0800 Subject: [PATCH 2/2] Fix Electron/ casing. --- packages/util/src/environment.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/util/src/environment.ts b/packages/util/src/environment.ts index 75f31e8d588..cb4bd4c9b91 100644 --- a/packages/util/src/environment.ts +++ b/packages/util/src/environment.ts @@ -85,7 +85,7 @@ export function isReactNative(): boolean { /** Detects Electron apps. */ export function isElectron(): boolean { - return getUA().indexOf('electron/') >= 0; + return getUA().indexOf('Electron/') >= 0; } /** Detects Internet Explorer. */