Skip to content

Conversation

@voxpelli
Copy link
Contributor

@voxpelli voxpelli commented Aug 25, 2023

The RFC9110 HTTP Semantics spec says:

A user agent SHOULD send a User-Agent header field in each request unless specifically configured not to do so.

So I think we should.

Convention is that a library appends their user-agent to the one that's been set at the level above them, hence the user provided user-agent comes before the default one set in this module.

To provide context, the version number of this module + the home page of it is included in the User-Agent. That way an API can diagnose possibly faulty consumers and file an issue.

If someone wouldn't want to expose that info in a header, they can always set a custom User-Agent header directly in the http settings. Especially if #225 gets merged.

Checklist

Copy link
Contributor

@Uzlopak Uzlopak left a comment

Choose a reason for hiding this comment

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

I dont like the idea to expose unnecessary information in security context. Also this is not a browser.

@voxpelli
Copy link
Contributor Author

I dont like the idea to expose unnecessary information in security context.

Can you elaborate? It's a SHOULD in the HTTP spec and common good practice?

Also this is not a browser.

User agent is the term for any HTTP client, see: https://www.rfc-editor.org/rfc/rfc9110#name-user-agents

@voxpelli voxpelli requested a review from mcollina August 26, 2023 11:49
@Uzlopak
Copy link
Contributor

Uzlopak commented Aug 26, 2023

I actually dont see any gain from this. Only exposing information. We also dont send in other plugins any user agent.

I think this is a semver major if we by default send user agent.

Imho by default dont send user agent is the best, but still i dont think it is good decision to add this feature.

@voxpelli
Copy link
Contributor Author

@Uzlopak
Copy link
Contributor

Uzlopak commented Aug 26, 2023

Yes, setting it to fixed value without require etc. is definetly better. I would prefer that any time.

@voxpelli
Copy link
Contributor Author

Some more points of reference:

Yes, setting it to fixed value without require etc. is definetly better. I would prefer that any time.

@Uzlopak Is this as a reply to #226 (comment) ?

@Uzlopak
Copy link
Contributor

Uzlopak commented Aug 26, 2023

I would set the user agent to 'fastify-oauth2'.

@voxpelli
Copy link
Contributor Author

@Uzlopak Done ✔️ I can get behind that, especially considering that it appears to be the common practice in the npm ecosystem nowadays 👍

@voxpelli voxpelli requested a review from Uzlopak August 26, 2023 12:57
voxpelli and others added 6 commits August 26, 2023 17:10
Every library appends their user-agent to the one that's been set at a level above them.

Hence any provided user-agent comes before the default user agent.

To provide context, the version number of `@fastify/oauth2` + the home page of it is included in the User-Agent. That way an API can diagnose possibly faulty consumers and file an issue.

If someone wouldn't want to expose this, they can always set a custom User-Agent header directly in the http settings. Especially if fastify#225 gets merged.
Co-authored-by: Manuel Spigolon <behemoth89@gmail.com>
@voxpelli voxpelli force-pushed the voxpelli/default-user-agent branch from 4a8b663 to cb1a992 Compare August 26, 2023 15:38
@voxpelli
Copy link
Contributor Author

Rebased to do #226 (comment)

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

index.js Outdated
} = options

const userAgent = options.userAgent
? `${options.userAgent} ${USER_AGENT}`
Copy link
Contributor

Choose a reason for hiding this comment

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

So you can not truely override the useragent?

Copy link
Contributor

@Uzlopak Uzlopak Aug 27, 2023

Choose a reason for hiding this comment

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

Expected more something like

  let userAgent = {}
  switch (options.userAgent) {
    case false:
      break
    case undefined:
      userAgent['User-Agent'] = USER_AGENT
      break
    default:
      userAgent['User-Agent'] = options.userAgent
      break
  }

  const credentials = {
    ...options.credentials,
    http: {
      ...options.credentials.http,
      headers: {
        ...userAgent,
        ...options.credentials.http?.headers
      }
    }
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So you can not truely override the useragent?

You can, if you override the http settings

On other comment:

I prefer using const over let and avoid switch unless checking one bar against multiple values (as a missing break can cause havoc and it’s a bit unusual in that body of a case doesn’t come with its own scope, unless you add one, so it’s easy for people to make mistakes with)

Copy link
Contributor

Choose a reason for hiding this comment

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

If I set userAgent to "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)", then the plugin is changing the useragent to: "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0) fastify/oauth2"

So how can I override the useragent?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Uzlopak This is what the RFC 9110 says, my emphasis:

The User-Agent field value consists of one or more product identifiers, each followed by zero or more comments (Section 5.6.5), which together identify the user agent software and its significant subproducts. By convention, the product identifiers are listed in decreasing order of their significance for identifying the user agent software. Each product identifier consists of a name and optional version.

This is what this PR up to now has done + I have added tests + comments on how to override it if one wants to.

But to avoid confusion I have now removed such prepending of the additional user-agent and instead outright replaces the default one. Removes possibly valuable information but I hope that fixes your concerns.

The RFC 9110 states:

"The User-Agent field value consists of one or more product identifiers, each followed by zero or more comments (Section 5.6.5), which together identify the user agent software and its significant subproducts. By convention, the product identifiers are listed in decreasing order of their significance for identifying the user agent software."

But since many are not aware of it, it may be better to remove it
@voxpelli voxpelli requested a review from Uzlopak August 27, 2023 16:55
Copy link
Contributor

@Uzlopak Uzlopak left a comment

Choose a reason for hiding this comment

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

LGTM

@Uzlopak Uzlopak merged commit 6d3f637 into fastify:master Aug 27, 2023
@voxpelli voxpelli deleted the voxpelli/default-user-agent branch August 27, 2023 20:01
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.

4 participants