googleapis / google-auth-library-nodejs

🔑 Google Auth Library for Node.js
Apache License 2.0
1.72k stars 377 forks source link

Default User-Agent header is overwritten by DefaultTransporter #1890

Closed josephdpurcell closed 2 hours ago

josephdpurcell commented 2 hours ago

Please make sure you have searched for information in the following guides.

A screenshot that you have tested with "Try this API".

I could not find this package in the apis explorer.

Screenshot 2024-11-01 at 12 54 01

Link to the code that reproduces this issue. A link to a public Github Repository or gist with a minimal reproduction.

https://gist.github.com/josephdpurcell/b434efc4870e4e7ba8387f9f7b98fd47

A step-by-step description of how to reproduce the issue, based on the linked reproduction.

  1. Download the test-user-agent.ts file into a directory that has google-auth-library NPM package installed
  2. Run ts-node test-user-agent.ts
  3. Notice that the User-Agent sent in the request is google-api-nodejs-client/9.14.2

A clear and concise description of what the bug is, and what you expected to happen.

If you set the default request header User-Agent to foo/1 and you do not overwrite the User-Agent header in the per-request configuration then I would expect foo/1 to get sent out. When I say "per-request configuration" I mean like this:

idTokenClient.request({
  headers: { 'User-Agent': 'bar/1' },
});

Currently, what happens is this order of precedence:

  1. The per-request User-Agent gets appended with google-api-nodejs-client/9.14.2. Example, if you pass bar/1 the User-Agent sent is bar/1 google-api-nodejs-client/9.14.2.
  2. If you do not set per-request User-Agent then the User-Agent sent is google-api-nodejs-client/9.14.2.
  3. If you have a default User-Agent set it will still be google-api-nodejs-client/9.14.2.

I expect the order of precedence to be:

  1. No change. It's OK for the per-request User-Agent to be appended.
  2. If you do not set per-request User-Agent then the default User-Agent should be used. It's OK for the default User-Agent to be appended.
  3. If neither a per-request User-Agent nor a default User-Agent is set then the User-Agent is google-api-nodejs-client/9.14.2.

A clear and concise description WHY you expect this behavior, i.e., was it a recent change, there is documentation that points to this behavior, etc. **

I do not want to have to pass the User-Agent on every request call. I want to set the User-Agent as a default request header and that be respected by the library.

github-actions[bot] commented 2 hours ago

Issue was opened with an invalid reproduction link. Please make sure the repository is a valid, publicly-accessible github repository, and make sure the url is complete (example: https://github.com/googleapis/google-cloud-node)

josephdpurcell commented 2 hours ago

Not sure why this was closed. I linked to a public gist, which the instructions say is acceptable.

https://github.com/googleapis/google-auth-library-nodejs/issues/1489#issuecomment-1331404312 is a workaround that can be used. I have a variation here that uses the default https://gist.github.com/josephdpurcell/b434efc4870e4e7ba8387f9f7b98fd47#file-workaround-ts.

I suppose at least this issue is documented for the next person.