Skip to content

Commit 2ad25cf

Browse files
authored
fix: disable keep-alive on GoogleAuth transporter to avoid Premature close errors (#10717)
### Description Disables HTTP keep-alive on the transporter utilized by `google-auth-library` to fetch and exchange auth tokens (e.g. via WIF or ADC). This works around a Node.js keep-alive socket reuse regression shipped in security updates for Node 22.23.0 and Node 24.17.0 (referenced in nodejs/node#63989) which causes token exchange requests to fail with `Premature close`. Fixes #10716 ### Scenarios Tested Tested minimal reproduction script `scripts/repro.ts` under Node v24.17.0 using Application Default Credentials: - Verified it fails on v15.22.1 and v15.22.2 (without the fix) with a `Premature close` GaxiosError. - Verified it successfully passes after applying the fix, making the Google API call and receiving a standard API response. - Ran all fast unit tests (`npm run mocha:fast`) successfully.
1 parent a0a3d67 commit 2ad25cf

3 files changed

Lines changed: 14 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Disable 'keep-alive' in `google-auth-library` calls to avoid `Premature close` errors on some Node versions (#10716).

src/apiv2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ function proxyURIFromEnv(): string | undefined {
168168
// https://github.com/node-fetch/node-fetch/issues/1767.
169169
const httpAgentNoKeepAlive = new http.Agent({ keepAlive: false });
170170
const httpsAgentNoKeepAlive = new https.Agent({ keepAlive: false });
171-
function noKeepAliveAgent(parsedURL: URL): http.Agent | https.Agent {
171+
export function noKeepAliveAgent(parsedURL: URL): http.Agent | https.Agent {
172172
return parsedURL.protocol === "https:" ? httpsAgentNoKeepAlive : httpAgentNoKeepAlive;
173173
}
174174

src/requireAuth.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,18 @@ function getAuthClient(config: GoogleAuthOptions): GoogleAuth {
2828
return authClient;
2929
}
3030

31-
authClient = new GoogleAuth(config);
31+
const authConfig: GoogleAuthOptions = {
32+
...config,
33+
clientOptions: {
34+
...config.clientOptions,
35+
transporterOptions: {
36+
...config.clientOptions?.transporterOptions,
37+
agent: apiv2.noKeepAliveAgent,
38+
},
39+
},
40+
};
41+
42+
authClient = new GoogleAuth(authConfig);
3243
return authClient;
3344
}
3445

0 commit comments

Comments
 (0)