Skip to content

Commit 16d62d4

Browse files
authored
Remove referrerPolicy from Cloudflare Worker fetch requests (#8393)
The Cloudflare Worker runner runtime doesn't support the `fetch` parameter `referrerPolicy` and throws if one is defined. In attempt to better support Cloudlfare, we will remove this parameter from `fetch` requests when we detect the SDK running in a Cloudflare runner enviornment.
1 parent ca4dbcf commit 16d62d4

File tree

5 files changed

+86
-9
lines changed

5 files changed

+86
-9
lines changed

.changeset/khaki-numbers-nail.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@firebase/auth': patch
3+
'@firebase/util': minor
4+
---
5+
6+
Suppress the use of the `fetch` parameter `referrerPolicy` within Auth for `fetch` requests originating from Cloudflare Workers. Clouldflare Worker environments do not support this parameter and throw when it's used.

common/api-review/util.api.md

+5
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,11 @@ export function isBrowser(): boolean;
264264
// @public (undocumented)
265265
export function isBrowserExtension(): boolean;
266266

267+
// Warning: (ae-missing-release-tag) "isCloudflareWorker" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
268+
//
269+
// @public
270+
export function isCloudflareWorker(): boolean;
271+
267272
// Warning: (ae-missing-release-tag) "isElectron" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
268273
//
269274
// @public

packages/auth/src/api/index.test.ts

+47
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { useFakeTimers } from 'sinon';
2222
import sinonChai from 'sinon-chai';
2323

2424
import { FirebaseError, getUA } from '@firebase/util';
25+
import * as utils from '@firebase/util';
2526

2627
import { mockEndpoint } from '../../test/helpers/api/helper';
2728
import { testAuth, TestAuth } from '../../test/helpers/mock_auth';
@@ -308,6 +309,52 @@ describe('api/_performApiRequest', () => {
308309
});
309310
});
310311

312+
context('referer policy exists on fetch request', () => {
313+
afterEach(mockFetch.tearDown);
314+
315+
it('should have referrerPolicy set', async () => {
316+
let referrerPolicySet: boolean = false;
317+
mockFetch.setUpWithOverride(
318+
(input: RequestInfo | URL, request?: RequestInit) => {
319+
if (request !== undefined && request.referrerPolicy !== undefined) {
320+
referrerPolicySet = true;
321+
}
322+
return Promise.resolve(new Response(JSON.stringify(serverResponse)));
323+
}
324+
);
325+
const promise = _performApiRequest<typeof request, typeof serverResponse>(
326+
auth,
327+
HttpMethod.POST,
328+
Endpoint.SIGN_UP,
329+
request
330+
);
331+
await expect(promise).to.be.fulfilled;
332+
expect(referrerPolicySet).to.be.true;
333+
});
334+
335+
it('should not have referrerPolicy set on Cloudflare workers', async () => {
336+
sinon.stub(utils, 'isCloudflareWorker').returns(true);
337+
let referrerPolicySet: boolean = false;
338+
mockFetch.setUpWithOverride(
339+
(input: RequestInfo | URL, request?: RequestInit) => {
340+
if (request !== undefined && request.referrerPolicy !== undefined) {
341+
referrerPolicySet = true;
342+
}
343+
return Promise.resolve(new Response(JSON.stringify(serverResponse)));
344+
}
345+
);
346+
const promise = _performApiRequest<typeof request, typeof serverResponse>(
347+
auth,
348+
HttpMethod.POST,
349+
Endpoint.SIGN_UP,
350+
request
351+
);
352+
await expect(promise).to.be.fulfilled;
353+
expect(referrerPolicySet).to.be.false;
354+
sinon.restore();
355+
});
356+
});
357+
311358
context('with network issues', () => {
312359
afterEach(mockFetch.tearDown);
313360

packages/auth/src/api/index.ts

+16-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { FirebaseError, querystring } from '@firebase/util';
18+
import { FirebaseError, isCloudflareWorker, querystring } from '@firebase/util';
1919

2020
import { AuthErrorCode, NamedErrorParams } from '../core/errors';
2121
import {
@@ -148,14 +148,23 @@ export async function _performApiRequest<T, V>(
148148
headers[HttpHeader.X_FIREBASE_LOCALE] = auth.languageCode;
149149
}
150150

151+
const fetchArgs: RequestInit = {
152+
method,
153+
headers,
154+
...body
155+
};
156+
157+
/* Security-conscious server-side frameworks tend to have built in mitigations for referrer
158+
problems". See the Cloudflare GitHub issue #487: Error: The 'referrerPolicy' field on
159+
'RequestInitializerDict' is not implemented."
160+
https://github.com/cloudflare/next-on-pages/issues/487 */
161+
if (!isCloudflareWorker()) {
162+
fetchArgs.referrerPolicy = 'no-referrer';
163+
}
164+
151165
return FetchProvider.fetch()(
152166
_getFinalTarget(auth, auth.config.apiHost, path, query),
153-
{
154-
method,
155-
headers,
156-
referrerPolicy: 'no-referrer',
157-
...body
158-
}
167+
fetchArgs
159168
);
160169
});
161170
}

packages/util/src/environment.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export function isNode(): boolean {
7979
}
8080

8181
/**
82-
* Detect Browser Environment
82+
* Detect Browser Environment.
8383
* Note: This will return true for certain test frameworks that are incompletely
8484
* mimicking a browser, and should not lead to assuming all browser APIs are
8585
* available.
@@ -89,7 +89,7 @@ export function isBrowser(): boolean {
8989
}
9090

9191
/**
92-
* Detect Web Worker context
92+
* Detect Web Worker context.
9393
*/
9494
export function isWebWorker(): boolean {
9595
return (
@@ -99,6 +99,16 @@ export function isWebWorker(): boolean {
9999
);
100100
}
101101

102+
/**
103+
* Detect Cloudflare Worker context.
104+
*/
105+
export function isCloudflareWorker(): boolean {
106+
return (
107+
typeof navigator !== 'undefined' &&
108+
navigator.userAgent === 'Cloudflare-Workers'
109+
);
110+
}
111+
102112
/**
103113
* Detect browser extensions (Chrome and Firefox at least).
104114
*/

0 commit comments

Comments
 (0)