Skip to content

Commit 48c3940

Browse files
committed
Added uTests & isCloudflareWorker usage comments
1 parent c2a5286 commit 48c3940

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

.changeset/khaki-numbers-nail.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
'@firebase/util': minor
44
---
55

6-
Suppress the use of the `fetch` parameter `referrPolicy` within Auth for `fetch` requests originating from Cloudflare Workers. Clouldflare Worker environments do not support this parameter and throw when it's used.
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.

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

Lines changed: 61 additions & 0 deletions
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,66 @@ 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+
/* eslint-disable no-var */
317+
var referrerPolicySet: boolean = false;
318+
/* eslint-enable no-var */
319+
mockFetch.setUpWithOverride(
320+
(input: RequestInfo | URL, request?: RequestInit) => {
321+
if (request !== undefined && request.referrerPolicy !== undefined) {
322+
referrerPolicySet = true;
323+
}
324+
return new Promise<never>((_, reject) =>
325+
reject(new Error('network error'))
326+
);
327+
}
328+
);
329+
const promise = _performApiRequest(
330+
auth,
331+
HttpMethod.POST,
332+
Endpoint.SIGN_UP,
333+
request
334+
);
335+
await expect(promise).to.be.rejectedWith(
336+
FirebaseError,
337+
'auth/network-request-failed'
338+
);
339+
expect(referrerPolicySet).to.be.true;
340+
});
341+
342+
it('should not have referrerPolicy set on Cloudflare workers', async () => {
343+
sinon.stub(utils, 'isCloudflareWorker').returns(true);
344+
/* eslint-disable no-var */
345+
var referrerPolicySet: boolean = false;
346+
/* eslint-enable no-var */
347+
mockFetch.setUpWithOverride(
348+
(input: RequestInfo | URL, request?: RequestInit) => {
349+
if (request !== undefined && request.referrerPolicy !== undefined) {
350+
referrerPolicySet = true;
351+
}
352+
return new Promise<never>((_, reject) =>
353+
reject(new Error('network error'))
354+
);
355+
}
356+
);
357+
const promise = _performApiRequest(
358+
auth,
359+
HttpMethod.POST,
360+
Endpoint.SIGN_UP,
361+
request
362+
);
363+
await expect(promise).to.be.rejectedWith(
364+
FirebaseError,
365+
'auth/network-request-failed'
366+
);
367+
expect(referrerPolicySet).to.be.false;
368+
sinon.restore();
369+
});
370+
});
371+
311372
context('with network issues', () => {
312373
afterEach(mockFetch.tearDown);
313374

packages/auth/src/api/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ export async function _performApiRequest<T, V>(
154154
...body
155155
};
156156

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 */
157161
if (!isCloudflareWorker()) {
158162
fetchArgs.referrerPolicy = 'no-referrer';
159163
}

0 commit comments

Comments
 (0)