Skip to content

Commit bebecda

Browse files
authored
Replace node-fetch dependency with undici (#7705)
Update our dependency on aging `node-fetch` `v2.6.7` to `undici` `v5.26.5`. This should fix some vulnerabilities within node-fetch as well as fix user issue #7660.
1 parent a89e05b commit bebecda

File tree

27 files changed

+147757
-195
lines changed

27 files changed

+147757
-195
lines changed

.changeset/real-dolls-type.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
'@firebase/auth-compat': minor
3+
'@firebase/firestore': minor
4+
'@firebase/functions': minor
5+
'@firebase/storage': minor
6+
'@firebase/auth': minor
7+
'firebase': minor
8+
---
9+
10+
Replaced node-fetch v2.6.7 dependency with the latest version of undici (v5.26.5) in Node.js SDK
11+
builds for auth, firestore, functions and storage.

.yarn/releases/yarn-1.22.11.cjs

+147,406
Large diffs are not rendered by default.

integration/messaging/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"express": "4.18.2",
1616
"geckodriver": "2.0.4",
1717
"mocha": "9.2.2",
18-
"node-fetch": "2.6.7",
18+
"undici": "5.26.5",
1919
"selenium-assistant": "6.1.1"
2020
}
2121
}

integration/messaging/test/utils/sendMessage.js

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

18-
const fetch = require('node-fetch');
18+
const undici = require('undici');
1919
const FCM_SEND_ENDPOINT = 'https://fcm.googleapis.com/fcm/send';
2020
// Rotatable fcm server key. It's generally a bad idea to expose server keys. The reason is to
2121
// simplify testing process (no need to implement server side decryption of git secret). The
@@ -28,7 +28,7 @@ module.exports = async payload => {
2828
'Requesting to send an FCM message with payload: ' + JSON.stringify(payload)
2929
);
3030

31-
const response = await fetch(FCM_SEND_ENDPOINT, {
31+
const response = await undici.fetch(FCM_SEND_ENDPOINT, {
3232
method: 'POST',
3333
body: JSON.stringify(payload),
3434
headers: {

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@
153153
"tslint": "6.1.3",
154154
"typedoc": "0.16.11",
155155
"typescript": "4.7.4",
156+
"undici": "5.26.5",
156157
"watch": "1.0.2",
157158
"webpack": "5.76.0",
158159
"yargs": "17.7.2"

packages/auth-compat/index.node.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@
2323
*/
2424
export * from './index';
2525
import { FetchProvider } from '@firebase/auth/internal';
26-
import * as fetchImpl from 'node-fetch';
26+
import {
27+
fetch as undiciFetch,
28+
Headers as undiciHeaders,
29+
Response as undiciResponse
30+
} from 'undici';
2731
import './index';
2832

2933
FetchProvider.initialize(
30-
fetchImpl.default as unknown as typeof fetch,
31-
fetchImpl.Headers as unknown as typeof Headers,
32-
fetchImpl.Response as unknown as typeof Response
34+
undiciFetch as unknown as typeof fetch,
35+
undiciHeaders as unknown as typeof Headers,
36+
undiciResponse as unknown as typeof Response
3337
);

packages/auth-compat/karma.conf.js

+12
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717

1818
const karmaBase = require('../../config/karma.base');
19+
const webpackBase = require('../../config/webpack.test');
1920
const { argv } = require('yargs');
2021

2122
const files = ['src/**/*.test.ts'];
@@ -29,6 +30,17 @@ module.exports = function (config) {
2930
// frameworks to use
3031
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
3132
frameworks: ['mocha'],
33+
// undici is a fetch polyfill that test helpers call for Node tests, and browser tests should
34+
// ingore its import to avoid compilation errors in those test helpers.
35+
webpack: {
36+
...webpackBase,
37+
resolve: {
38+
...webpackBase.resolve,
39+
alias: {
40+
'undici': false
41+
}
42+
}
43+
},
3244

3345
client: Object.assign({}, karmaBase.client, getClientConfig())
3446
});

packages/auth-compat/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"@firebase/auth-types": "0.12.0",
5555
"@firebase/component": "0.6.4",
5656
"@firebase/util": "1.9.3",
57-
"node-fetch": "2.6.7",
57+
"undici": "5.26.5",
5858
"tslib": "^2.1.0"
5959
},
6060
"license": "Apache-2.0",

packages/auth/karma.conf.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717

1818
const karmaBase = require('../../config/karma.base');
19+
const webpackBase = require('../../config/webpack.test');
1920
const { argv } = require('yargs');
2021

2122
module.exports = function (config) {
@@ -26,7 +27,17 @@ module.exports = function (config) {
2627
// frameworks to use
2728
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
2829
frameworks: ['mocha'],
29-
30+
// undici is a fetch polyfill that test helpers call for Node tests, and browser tests should
31+
// ingore its import to avoid compilation errors in those test helpers.
32+
webpack: {
33+
...webpackBase,
34+
resolve: {
35+
...webpackBase.resolve,
36+
alias: {
37+
'undici': false
38+
}
39+
}
40+
},
3041
client: Object.assign({}, karmaBase.client, getClientConfig(argv))
3142
});
3243

packages/auth/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
"@firebase/component": "0.6.4",
116116
"@firebase/logger": "0.4.0",
117117
"@firebase/util": "1.9.3",
118-
"node-fetch": "2.6.7",
118+
"undici": "5.26.5",
119119
"tslib": "^2.1.0"
120120
},
121121
"license": "Apache-2.0",

packages/auth/src/platform_node/index.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,18 @@ import { ClientPlatform } from '../core/util/version';
2727
import { AuthImpl } from '../core/auth/auth_impl';
2828

2929
import { FetchProvider } from '../core/util/fetch_provider';
30-
import * as fetchImpl from 'node-fetch';
3130
import { getDefaultEmulatorHost } from '@firebase/util';
31+
import {
32+
fetch as undiciFetch,
33+
Headers as undiciHeaders,
34+
Response as undiciResponse
35+
} from 'undici';
3236

3337
// Initialize the fetch polyfill, the types are slightly off so just cast and hope for the best
3438
FetchProvider.initialize(
35-
fetchImpl.default as unknown as typeof fetch,
36-
fetchImpl.Headers as unknown as typeof Headers,
37-
fetchImpl.Response as unknown as typeof Response
39+
undiciFetch as unknown as typeof fetch,
40+
undiciHeaders as unknown as typeof Headers,
41+
undiciResponse as unknown as typeof Response
3842
);
3943

4044
// First, we set up the various platform-specific features for Node (register

packages/auth/test/helpers/integration/emulator_rest_helpers.ts

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

18-
import * as fetchImpl from 'node-fetch';
18+
import { fetch as undiciFetch, RequestInit as undiciRequestInit } from 'undici';
1919
import { getAppConfig, getEmulatorUrl } from './settings';
2020

2121
export interface VerificationSession {
@@ -87,10 +87,10 @@ function buildEmulatorUrlForPath(endpoint: string): string {
8787
function doFetch(url: string, request?: RequestInit): ReturnType<typeof fetch> {
8888
if (typeof document !== 'undefined') {
8989
return fetch(url, request);
90+
} else {
91+
return undiciFetch(
92+
url,
93+
request as undiciRequestInit
94+
) as unknown as ReturnType<typeof fetch>;
9095
}
91-
92-
return fetchImpl.default(
93-
url,
94-
request as fetchImpl.RequestInit
95-
) as unknown as ReturnType<typeof fetch>;
9696
}

packages/firestore/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102
"@firebase/webchannel-wrapper": "0.10.3",
103103
"@grpc/grpc-js": "~1.9.0",
104104
"@grpc/proto-loader": "^0.7.8",
105-
"node-fetch": "2.6.7",
105+
"undici": "5.26.5",
106106
"tslib": "^2.1.0"
107107
},
108108
"peerDependencies": {

packages/firestore/src/platform/node_lite/connection.ts

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

18-
import nodeFetch from 'node-fetch';
18+
import { fetch as undiciFetch } from 'undici';
1919

2020
import { DatabaseInfo } from '../../core/database_info';
2121
import { Connection } from '../../remote/connection';
@@ -25,8 +25,8 @@ export { newConnectivityMonitor } from '../browser/connection';
2525

2626
/** Initializes the HTTP connection for the REST API. */
2727
export function newConnection(databaseInfo: DatabaseInfo): Connection {
28-
// node-fetch is meant to be API compatible with `fetch`, but its type doesn't
28+
// undici is meant to be API compatible with `fetch`, but its type doesn't
2929
// match 100%.
3030
// eslint-disable-next-line @typescript-eslint/no-explicit-any
31-
return new FetchConnection(databaseInfo, nodeFetch as any);
31+
return new FetchConnection(databaseInfo, undiciFetch as any);
3232
}

packages/functions/karma.conf.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717

1818
const karmaBase = require('../../config/karma.base');
19+
const webpackBase = require('../../config/webpack.test');
1920

2021
const files = [`src/**/*.test.ts`];
2122

@@ -25,7 +26,18 @@ module.exports = function (config) {
2526
files,
2627
// frameworks to use
2728
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
28-
frameworks: ['mocha']
29+
frameworks: ['mocha'],
30+
// undici is a fetch polyfill that test helpers call for Node tests, and browser tests should
31+
// ingore its import to avoid compilation errors in those test helpers.
32+
webpack: {
33+
...webpackBase,
34+
resolve: {
35+
...webpackBase.resolve,
36+
alias: {
37+
'undici': false
38+
}
39+
}
40+
}
2941
});
3042

3143
config.set(karmaConfig);

packages/functions/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
"@firebase/auth-interop-types": "0.2.1",
7272
"@firebase/app-check-interop-types": "0.3.0",
7373
"@firebase/util": "1.9.3",
74-
"node-fetch": "2.6.7",
74+
"undici": "5.26.5",
7575
"tslib": "^2.1.0"
7676
},
7777
"nyc": {

packages/functions/src/index.node.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
* limitations under the License.
1616
*/
1717
import { registerFunctions } from './config';
18-
import nodeFetch from 'node-fetch';
18+
import { fetch as undiciFetch } from 'undici';
1919

2020
export * from './api';
2121

2222
// eslint-disable-next-line @typescript-eslint/no-explicit-any
23-
registerFunctions(nodeFetch as any, 'node');
23+
registerFunctions(undiciFetch as any, 'node');

packages/functions/test/utils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
2121
import { AppCheckInternalComponentName } from '@firebase/app-check-interop-types';
2222
import { FunctionsService } from '../src/service';
2323
import { connectFunctionsEmulator } from '../src/api';
24-
import nodeFetch from 'node-fetch';
24+
import { fetch as undiciFetch } from 'undici';
2525
import { MessagingInternalComponentName } from '../../../packages/messaging-interop-types';
2626

2727
export function makeFakeApp(options: FirebaseOptions = {}): FirebaseApp {
@@ -59,7 +59,7 @@ export function createTestService(
5959
)
6060
): FunctionsService {
6161
const fetchImpl: typeof fetch =
62-
typeof window !== 'undefined' ? fetch.bind(window) : (nodeFetch as any);
62+
typeof window !== 'undefined' ? fetch.bind(window) : (undiciFetch as any);
6363
const functions = new FunctionsService(
6464
app,
6565
authProvider,

0 commit comments

Comments
 (0)