Skip to content

fix(core): honor HTTPS_PROXY for X.509 workload identity mTLS - #9772

Open
abhiramshaji-cyber wants to merge 5 commits into
googleapis:mainfrom
abhiramshaji-cyber:fix-auth-mtls-proxy
Open

abhiramshaji-cyber wants to merge 5 commits into
googleapis:mainfrom
abhiramshaji-cyber:fix-auth-mtls-proxy

Conversation

@abhiramshaji-cyber

Copy link
Copy Markdown

Apps that sign in with a client certificate (X.509 workload identity federation) could not get a token behind a required proxy, and now those requests go through the proxy and still present the certificate.

What this code is for

When a credential file has credential_source.certificate, IdentityPoolClient.retrieveSubjectToken() asks CertificateSubjectTokenSupplier to read the client certificate and key, then builds two Gaxios instances that use them: one for the STS token exchange at sts.mtls.googleapis.com and one (this.transporter) for the service account impersonation call. In gaxios, #prepareRequest() decides how each request connects: a caller supplied agent wins, otherwise it builds an HttpsProxyAgent when HTTPS_PROXY/https_proxy is set and NO_PROXY does not match, otherwise it builds an https.Agent from cert and key.

Before this PR

A workload in a network where all outbound traffic must go through a proxy runs client.getAccessToken() with HTTPS_PROXY=http://127.0.0.1:3128, as in the steps @moibueno shared in #9594.

  1. retrieveSubjectToken() calls createMtlsHttpsAgent(), which returns new https.Agent({key, cert}).
  2. That agent is passed as new Gaxios({agent: mtlsAgent}) for STS and as agent on this.transporter.
  3. In #prepareRequest(), if (opts.agent) is true, so gaxios skips the proxy branch.
  4. The token request opens a direct TLS connection to sts.mtls.googleapis.com:443.
  5. The proxy logs 0 CONNECT requests, the firewall drops the direct connection, and getAccessToken() fails.

There is a second problem in gaxios. Even a request that reaches the gaxios proxy branch with cert and key does not send the certificate to the server: HttpsProxyAgent uses those options only for its own connection to the proxy, and the TLS connection to the server behind the proxy gets no client certificate. I checked this with a local CONNECT proxy and a local server that asks for a client certificate: the request went through the tunnel, but the server saw an empty peer certificate.

After this PR

Same workload, same HTTPS_PROXY, same getAccessToken() call.

  1. retrieveSubjectToken() calls getMtlsOptions(), which returns {cert, key} as strings.
  2. Those are passed as new Gaxios(mtlsOptions) for STS and spread into this.transporter, with agent: undefined so an inherited agent cannot hide the certificate.
  3. In #prepareRequest(), there is no agent yet, so the proxy branch runs, honoring HTTPS_PROXY, https_proxy and NO_PROXY the same way as every other gaxios request.
  4. The new HttpsProxyAgent adds cert and key to the TLS connection to the server whenever target.secureEndpoint is true.
  5. The proxy logs CONNECT sts.mtls.googleapis.com:443, the STS server receives the client certificate through the tunnel, and the token comes back. The impersonation call takes the same path.

When NO_PROXY matches the host, step 3 takes the existing cert/key branch instead and connects directly with an https.Agent, same as before.

The diagram

flowchart LR

classDef box fill:#eef1ff,stroke:#4c5bd4,color:#111827
classDef bad fill:#ffd6d6,stroke:#cc0000,color:#111827,stroke-width:3px
classDef good fill:#c8f0c8,stroke:#008800,color:#111827,stroke-width:3px

subgraph BEFORE["BEFORE: 0 CONNECT requests at the proxy"]
direction TB
  B1["getAccessToken() with HTTPS_PROXY set"] --> B2["retrieveSubjectToken() reads cert and key"]
  B2 --> B3["createMtlsHttpsAgent() returns https.Agent"]
  B3 --> B4{"opts.agent is set, proxy skipped"}
  B4 -->|"direct TLS to sts.mtls.googleapis.com"| B5["blocked, no token"]
  B4 -->|"no proxy in the network"| B6["direct mTLS works"]
end

subgraph AFTER["AFTER: CONNECT through the proxy, certificate sent"]
direction TB
  A1["getAccessToken() with HTTPS_PROXY set"] --> A2["retrieveSubjectToken() reads cert and key"]
  A2 --> A3["getMtlsOptions() returns cert and key"]
  A3 --> A4{"THE CHANGE: gaxios chooses the agent"}
  A4 -->|"HttpsProxyAgent adds cert to inner TLS"| A5["token through the proxy"]
  A4 -->|"NO_PROXY matches"| A6["direct mTLS, same as before"]
end

B1 ~~~ A1

class B1,B2,B3,B6 box
class A1,A2,A3,A6 box
class B4,B5 bad
class A4,A5 good

linkStyle 3 stroke:#cc0000,stroke-width:5px
linkStyle 8 stroke:#008800,stroke-width:5px

style BEFORE fill:#fff5f5,stroke:#cc0000,color:#111827
style AFTER fill:#f3fff3,stroke:#008800,color:#111827
Loading

The change

There are two edits, and both are needed for the token request to work behind a proxy, plus one cache change in gaxios that the second edit makes necessary.

  • google-auth-library: createMtlsHttpsAgent() is replaced by getMtlsOptions(), and retrieveSubjectToken() passes cert and key to Gaxios instead of a prebuilt agent. This is the first option @moibueno suggested in the issue, and it reuses the proxy and NO_PROXY handling gaxios already has, so the library does not read proxy variables itself. CertificateSubjectTokenSupplier is not exported from the package index.
  • gaxios: in the proxy branch of #prepareRequest(), when both cert and key are set, the HttpsProxyAgent instance gets a connect that adds them to the options for the TLS connection to the server (only when target.secureEndpoint is true). The options for the connection to the proxy itself stay as they were.
  • gaxios: the proxy agent cache key now includes cert and key when both are set, instead of the proxy URL alone. Since the cached agent now presents its certificate to every https server it reaches, reusing it would send one caller's certificate on a later request made without one, and a first request without a certificate would make later mTLS requests go out without it.

Tests: test.identitypoolclient.ts checks that both the STS and impersonation requests use a proxy agent carrying the certificate with HTTPS_PROXY, that https_proxy plus a NO_PROXY match on the STS host sends STS direct with the certificate and impersonation through the proxy, and that a transporterOptions.agent does not drop the certificate. test.getch.ts checks that the proxy agent passes cert and key to the server connection, and that requests without a certificate or with a different one get their own agent. The first two auth tests and the new gaxios tests fail without this change.

A few things I would really value your opinion on, since you know this code far better than I do:

  • google-auth-library depends on gaxios from npm (^8.3.1). Until the gaxios floor is raised, a user with HTTPS_PROXY set and an older gaxios would now go through the proxy without the certificate, so a setup that works today because direct connections happen to be allowed would start failing at STS. Would you prefer that I bump the floor in this PR, or right after the gaxios release, or do you usually handle that in the dependency floor updates?
  • I kept both packages in one PR because neither half fixes the issue alone. I am very happy to split it into a gaxios PR and an auth PR if that is easier to review and release.
  • Two existing gaxios tests (should load the proxy from the cache with mTLS and the Bun fetch test) expected a request without a certificate to reuse the cached mTLS proxy agent. I updated them so that request now gets its own agent with no certificate, because otherwise the certificate would reach servers it was not meant for. If that reuse was relied on somewhere I am not aware of, I would be grateful to hear it and happy to find another way.

What changes for a user

  • With HTTPS_PROXY or https_proxy set, the STS token exchange and the impersonation call now go through the proxy and present the client certificate to Google.

  • NO_PROXY is honored for these requests, the same way as for every other request the library makes.

  • Without any proxy set, requests connect directly with the client certificate, as before.

  • A custom transporterOptions.agent is still replaced for these requests, so the certificate is always used, as before.

  • The workaround from the issue that replaced createMtlsHttpsAgent on the supplier is no longer needed; that method is no longer called.

  • Thank you @moibueno for the very clear report and the two suggested fixes, which made this easy to follow.

  • Make sure to open an issue as a bug/issue before writing your code

  • Ensure the tests and linter pass

  • Code coverage does not decrease (if any source code was changed)

  • Appropriate docs were updated (if necessary)

Fixes #9594

X.509 workload identity federation built its own https.Agent with the
client certificate and handed it to gaxios. When gaxios is given an
agent it skips its proxy handling, so the STS token exchange and the
service account impersonation call ignored HTTPS_PROXY, https_proxy and
NO_PROXY, and getAccessToken() failed in networks where outbound traffic
must go through a proxy.

google-auth-library now passes the certificate and key to gaxios as
cert and key options, so the existing proxy branch in gaxios decides
whether to use the proxy and applies NO_PROXY. The client transporter
drops any inherited agent so the certificate is always used, as it was
before.

In gaxios, the proxy branch gave cert and key to HttpsProxyAgent, which
only uses them for the connection to the proxy itself. The TLS
connection to the server behind the proxy had no client certificate, so
an mTLS endpoint would reject it. The agent now adds cert and key to
that inner TLS connection as well.

Fixes googleapis#9594
The proxy agent now presents cert and key to every https server it
reaches, not only to the proxy. Caching it by proxy URL alone meant a
later request on the same Gaxios instance without a certificate would
send the cached one to a server it was never meant for, and a first
request without a certificate would make later mTLS requests silently
go out without one.

The cache key now includes cert and key when both are set, so each
certificate gets its own agent and requests without one get a plain
proxy agent. The two existing tests that expected a request without a
certificate to reuse the mTLS agent are updated to the new behavior.
The test inspects the options the auth library hands to gaxios and that
the proxy branch is chosen. Delivery of the certificate to the server
behind the proxy is covered by the gaxios test, so the name now says
that instead of claiming the server receives it.
@abhiramshaji-cyber
abhiramshaji-cyber requested review from a team as code owners October 29, 2026 12:18
@github-actions
github-actions Bot requested a review from quirogas October 29, 2026 12:18

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates gaxios and google-auth-library-nodejs to support mTLS client certificates when routing requests through a proxy. It replaces custom HTTPS agent creation in CertificateSubjectTokenSupplier with passing cert and key options directly to Gaxios, which now caches proxy agents using a key that incorporates these certificates. The feedback highlights three key areas for improvement: avoiding potential corruption of binary DER-formatted certificates by removing unnecessary .toString() calls, performing a deep copy of transporter defaults to prevent accidental mutation of nested configuration objects, and refining the proxy agent cache key logic to prevent cache pollution when only one of cert or key is provided.

Comment thread core/packages/gaxios/src/gaxios.ts Resolved
@abhiramshaji-cyber

Copy link
Copy Markdown
Author

Hi @quirogas, the Gemini review points are addressed (the agent cache key fix is in with tests, and I left notes on the other two). Whenever you have a moment, would you mind approving the workflow runs and taking a look? Happy to change anything. Thank you!

@abhiramshaji-cyber

Copy link
Copy Markdown
Author

Hi @quirogas, hope you're doing well! It's me again from my note here on 10/29. I know there's a lot to review across a monorepo this big, so no rush at all; the workflow runs here and on #9775 are still waiting, and I'd be glad to know if anything else would help or if you'd like either one shaped differently. Out of curiosity, now that gaxios and the auth library live together under core, are there areas there where outside help would be most useful? Any tips for contributing here are very welcome too, thank you!

@abhiramshaji-cyber

Copy link
Copy Markdown
Author

Hi @quirogas, hope you're doing well! It's me again from my note here on 11/10. I can see you've been busy with a lot of CI and Renovate work lately, so thank you for all of that, and no rush at all. The workflow runs here and on #9775 are still waiting whenever you get a chance. Is there anything else that would make these easier to review, or anything you'd like changed in either one? Any feedback is very welcome, thank you!

@abhiramshaji-cyber

Copy link
Copy Markdown
Author

Hi @shivanee-p, hope you're doing well! I've seen you reviewing a lot of the recent core changes, so I hope it's okay to ask you here. I merged the latest main into this branch with no conflicts, and the gaxios and auth library unit tests all pass locally. The workflow runs are waiting on a maintainer approval again, so would you mind approving them when you have a moment? And if someone else is a better fit to review this one, I'd be really grateful for a pointer. Thank you so much!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

google-auth-library: X.509 workload identity federation (mTLS) ignores HTTPS_PROXY for the STS token exchange

1 participant