Skip to content

Limit the OAuth2 access token on avatar fetches to the user info origin - #349

Merged
umputun merged 1 commit into
masterfrom
avatar-plain-client
Nov 28, 2026
Merged

umputun merged 1 commit into
masterfrom
avatar-plain-client

Conversation

@paskal

@paskal paskal commented Nov 28, 2026

Copy link
Copy Markdown
Collaborator

Previously, the OAuth2 callback passed the avatar saver the client returned by oauth2.Config.Client, which adds the user's access token to every request it makes. The avatar proxy fetched the user's picture with it, so the access token went to whichever host the picture URL named, and to any host a redirect led to.

After this change, the saver gets a client that adds the token only to requests for the scheme, host and port of the provider's user info URL. The check runs on every request, so a redirect from the info host to another host drops the token on that hop. A host with non-ASCII characters never matches. Pictures on other hosts are fetched without the token. All picture requests go through the oauth2 client's base transport and redirect policy, with the 5 second timeout the other providers' avatar clients already use.

Built-in providers:

  • Microsoft keeps working: its picture, https://graph.microsoft.com/beta/me/photo/$value, needs the bearer token and is served by the same host as its user info URL.
  • Google, GitHub, Facebook, Yandex, X, Patreon and Discord return pictures on public CDN hosts, which load without the token. Battle.net returns no picture. OAuth1, Apple, verify, direct and Telegram already used a plain client.
  • The custom provider (go-oauth2/oauth2 server) and the dev provider serve the picture from the same host as the user info, so they keep the token.

A custom provider whose picture needs the token outside the origin of its InfoURL now gets an identicon from the default avatar proxy. A custom AvatarSaver receives the restricted client too. The AvatarSaver doc comment and the README describe the new behaviour.

Verified:

  • TestOauth2AvatarFetchSendsTokenOnlyToInfoHost, in both modules, runs the OAuth2 login against servers that record the Authorization header: the picture on the info host gets the token, a picture on another host and the target of a redirect from the info host do not. It fails with either the original client or a plain one in place of the new client.
  • TestOauth2AvatarClientTransport checks which picture URLs get the token through the oauth2 client's base transport, including a host that differs only by a non-ASCII letter, and that the client keeps the oauth2 client's redirect policy. TestURLOrigin covers default ports, host case, a scheme change, a different port and a subdomain.
  • go test -race for both modules, the _example build, and golangci-lint v2.15.0 with the CI flags for both modules.

The OAuth2 callback handed the avatar saver the oauth2 client, which adds
the user's access token to every request, so the picture was fetched with
the token from whatever host the picture URL named.

The saver now gets a client that adds the token only to requests for the
scheme, host and port of the provider's user info URL, checked on every
redirect hop; a host with non-ASCII characters never matches. Requests go
through the oauth2 client's base transport and redirect policy, with the
5 second timeout the other providers' avatar clients already use. The
Microsoft Graph photo, which needs the token and is served by the info
host, keeps loading; pictures on CDNs and other hosts are fetched without
the token.
@paskal
paskal requested a review from umputun as a code owner November 28, 2026 00:29
@coveralls

coveralls commented Nov 28, 2026 •

Copy link
Copy Markdown

Coverage Report for CI Build 46088161991

Coverage increased (+0.004%) to 88.456%

Details

  • Coverage increased (+0.004%) from the base build.
  • Patch coverage: 4 uncovered changes across 1 file (33 of 37 lines covered, 89.19%).
  • No coverage regressions found.

Uncovered Changes

File Changed Covered %
provider/oauth2.go 37 33 89.19%

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 4184
Covered Lines: 3701
Line Coverage: 88.46%
Coverage Strength: 19.48 hits per line

💛 - Coveralls

@paskal
paskal requested a balanced review from Copilot November 28, 2026 00:45

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@umputun umputun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

@umputun
umputun merged commit 17f2b4a into master Nov 28, 2026
9 checks passed
@umputun
umputun deleted the avatar-plain-client branch November 28, 2026 00:52
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