Skip to content

Conversation

yanolab
Copy link
Contributor

@yanolab yanolab commented Jul 20, 2025

What

Set browser like user-agent for download.

fix: #4005

Why

I also faced this issue recently. And I was deeply look into it.
It seems the storage service or CDN verifying user-agent to reject bot access.
Since the current implementation is using default user-agent.
It might be identified as a bot.
If I changed the user-agent to browser like agent, the success rate was dramatically increased.

Check List

  • Add userAgentTransport to wrap HTTP requests with Mozilla User-Agent
  • This helps avoid being blocked by cloud storage services like Azure/S3
  • Applies to both direct GitHub API downloads and redirect URL downloads

- Add userAgentTransport to wrap HTTP requests with Mozilla User-Agent
- This helps avoid being blocked by cloud storage services like Azure/S3
- Applies to both direct GitHub API downloads and redirect URL downloads
@yanolab yanolab marked this pull request as ready for review July 20, 2025 00:14
@suzuki-shunsuke suzuki-shunsuke added the bug Something isn't working label Jul 20, 2025
@suzuki-shunsuke
Copy link
Member

Thank you for your investigation and contribution!
Looks good to me, but do you have any references to support your modifications?

@yanolab
Copy link
Contributor Author

yanolab commented Jul 20, 2025

Sorry, I don't have reference. I just had many trial and errors. Since this is their internal specification, it might be difficult to give evidences.

@suzuki-shunsuke
Copy link
Member

https://docs.github.com/en/rest/using-the-rest-api/getting-started-with-the-rest-api?apiVersion=2022-11-28#user-agent

All API requests must include a valid User-Agent header. The User-Agent header identifies the user or application that is making the request.

By default, GitHub CLI sends a valid User-Agent header. However, GitHub recommends using your GitHub username, or the name of your application, for the User-Agent header value. This allows GitHub to contact you if there are problems.

The following is an example User-Agent for an app named Awesome-Octocat-App:

User-Agent: Awesome-Octocat-App
Requests with no User-Agent header will be rejected. If you provide an invalid User-Agent header, you will receive a 403 Forbidden response.

@yanolab
Copy link
Contributor Author

yanolab commented Jul 20, 2025

Nice! It seems to be good to set user-agent in the other places as well. Shall I do it?

@suzuki-shunsuke
Copy link
Member

go-github usually creates http.Request by Client.NewRequest, and it sets the user agent header.

https://github.com/google/go-github/blob/718c304661aa42ba7a01da5b54b2ce85f2f48234/github/repos_releases.go#L347
https://github.com/google/go-github/blob/ff1666c9062605cb0a64d1433889c4e54eccba1f/github/github.go#L36
https://github.com/google/go-github/blob/ff1666c9062605cb0a64d1433889c4e54eccba1f/github/github.go#L426-L428
https://github.com/google/go-github/blob/ff1666c9062605cb0a64d1433889c4e54eccba1f/github/github.go#L562-L564

But when go-github downloads an asset from a redirect URL, it creates http.Request by http.NewRequest.
And seems like user agent header isn't set.

https://github.com/google/go-github/blob/718c304661aa42ba7a01da5b54b2ce85f2f48234/github/repos_releases.go#L385-L392

I'll contribute to go-github.

@suzuki-shunsuke
Copy link
Member

Nice! It seems to be good to set user-agent in the other places as well. Shall I do it?

Looks good. Thank you.

@yanolab
Copy link
Contributor Author

yanolab commented Jul 20, 2025

go-github usually creates http.Request by Client.NewRequest, and it sets the user agent header.

google/go-github@718c304/github/repos_releases.go#L347 google/go-github@ff1666c/github/github.go#L36 google/go-github@ff1666c/github/github.go#L426-L428 google/go-github@ff1666c/github/github.go#L562-L564

But when go-github downloads an asset from a redirect URL, it creates http.Request by http.NewRequest. And seems like user agent header isn't set.

google/go-github@718c304/github/repos_releases.go#L385-L392

I'll contribute to go-github.

Makes sense. Can we use this fix until you fixed original issue of go-github as a workaround?

@suzuki-shunsuke
Copy link
Member

Sure!


func (t *userAgentTransport) RoundTrip(req *http.Request) (*http.Response, error) {
// Add a browser-like User-Agent to avoid being blocked by cloud storage services
req.Header.Set("User-Agent", "Mozilla/5.0 (compatible; aqua/2.0; +https://aquaproj.github.io/)")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@suzuki-shunsuke As per additional investigation of us, it seems we don't need to align browser like user agent, what user-agent is preferred for you?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

#4019 (comment)

  • I tried to use go-github/v73.0.0 for user-agent, but it's flaky.
  • I tried to use Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 that is recent chrome user-agent. It's stable.

Since using recent browser to download assets always is succeeded, it might be better to use actual browser user-agent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed to use go-github/{version} for user-agent by default. But prepared AQUA_DOWNLOAD_USER_AGENT variable as well. Could you tell me your opinion 🙇

Copy link
Member

Choose a reason for hiding this comment

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

Hmm. I don't have any strong opinion.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry. I didn't see your above two comments when I posted a previous comment.

I tried to use go-github/v73.0.0 for user-agent, but it's flaky.
I tried to use Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 that is recent chrome user-agent. It's stable.

I see. Then I think Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 is better than go-github/{version}.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed it to chrome browser one. It's stable than go-gihub/{version} but sometime it's failed.
There might be other essential for success 🤔

@yanolab
Copy link
Contributor Author

yanolab commented Jul 20, 2025

ugh... when I test the change again, it becomes unstable again...

@yanolab yanolab changed the title fix: Add browser-like User-Agent to GitHub Release downloads fix: Add AQUA_DOWNLOAD_USER_AGENT variable and set User-Agent to GitHub Release downloads Jul 20, 2025
@yanolab yanolab requested a review from suzuki-shunsuke July 20, 2025 05:04
@suzuki-shunsuke suzuki-shunsuke added this to the v2.53.6 milestone Jul 20, 2025
@suzuki-shunsuke suzuki-shunsuke merged commit 5af95e3 into aquaproj:main Jul 20, 2025
18 checks passed
@github-project-automation github-project-automation bot moved this to Done in main Jul 20, 2025
@suzuki-shunsuke
Copy link
Member

I'll revert this pr.
Thank you for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Intermittent 403 errors when downloading private GitHub release
2 participants