Crate: add default strategy user agent by samford · Pull Request #23832 · Homebrew/brew · GitHub
Skip to content

Crate: add default strategy user agent - #23832

Merged
MikeMcQuaid merged 1 commit into
mainfrom
livecheck/crate-add-default-user-agent
Sep 6, 2026
Merged

Crate: add default strategy user agent#23832
MikeMcQuaid merged 1 commit into
mainfrom
livecheck/crate-add-default-user-agent

Conversation

@samford

@samford samford commented Sep 5, 2026

Copy link
Copy Markdown
Member

  • Have you followed our Contributing guidelines?
  • Have you checked for other open Pull Requests for the same change?
  • Have you explained what your changes do? Performance claims (e.g. "this is faster") must include brew benchmark results.
  • Have you explained why you'd like these changes included, not just what they do?
  • For bug fixes, have you given step-by-step brew commands to reproduce the bug?
  • Have you written new tests (excluding integration tests)? Here's an example.
  • Have you successfully run brew lgtm (style, typechecking and tests) locally?

  • I did not use AI/LLM to create this PR, or I disclosed the tool/model below and reviewed its output; I did not attribute commits to AI and will answer maintainer questions and review comments myself without AI/LLM.

The crates.io API now returns a 403 (Forbidden) response for all requests seemingly unless they use a browser user agent (i.e., :default and :curl fail). Checks using livecheck's Crate strategy have continued to work because the user agent fallback logic in Strategy::page_content tries the URL again using the :browser user agent (after failing with :default) and that works. However, this fallback logic will be deprecated in the near future and these checks will error instead (not to mention we don't need to be trying with a user agent we know will fail before using one that will work).

This issue affects all of the Crate strategy checks, so this adds logic to the Crates::find_versions method to set a :browser user agent if one isn't set in the livecheck block. For the sake of simplicity, I haven't included this in the match_data return value as something that would be surfaced in debug and verbose JSON output. It's technically possible (and I prototyped it) but it requires some changes to the JSON structure and I wasn't convinced it was worth it. I may revisit it in the future but for now I'm treating it as an implementation detail.

Copilot AI balanced review requested due to automatic review settings September 5, 2026 22:41

Copilot AI 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.

🟡 Changes recommended

The updated fetched-content test expects one page_content call but triggers two.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Sets the Crate livecheck strategy’s default user agent to :browser so crates.io API requests avoid 403 responses.

Changes:

  • Defaults fetched crate requests to the browser user agent.
  • Preserves explicitly configured user agents.
  • Adds coverage for both behaviours.
File summaries
File Description
Library/Homebrew/livecheck/strategy/crate.rb Applies the browser user agent by default.
Library/Homebrew/test/livecheck/strategy/crate_spec.rb Tests default and explicit user agents.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Balanced

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Comment thread Library/Homebrew/test/livecheck/strategy/crate_spec.rb
The crates.io API now returns a 403 (Forbidden) response for all
requests seemingly unless they use a browser user agent (i.e.,
`:default` and `:curl` fail). Checks using livecheck's `Crate`
strategy have continued to work because the user agent fallback logic
in `Strategy::page_content` tries the URL again using the `:browser`
user agent (after failing with `:default`) and that works. However,
this fallback logic will be deprecated in the near future and these
checks will error instead (not to mention we don't need to be trying
with a user agent we know will fail before using one that will work).

This issue affects all of the `Crate` strategy checks, so this adds
logic to the `Crates::find_versions` method to set a `:browser` user
agent if one isn't set in the `livecheck` block. For the sake of
simplicity, I haven't included this in the `match_data` return value
as something that would be surfaced in debug and verbose JSON output.
It's technically possible (and I prototyped it) but it requires some
changes to the JSON structure and I wasn't convinced it was worth it.
I may revisit it in the future but for now I'm treating it as an
implementation detail.
@samford
samford force-pushed the livecheck/crate-add-default-user-agent branch from 4487f09 to d8b62ce Compare September 6, 2026 00:07

@MikeMcQuaid MikeMcQuaid 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.

Thanks!

Comment on lines +98 to 99

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.

Not this PR: would be nice to refactor these APIs to stop passing around options hashes which can't really be typed.

@MikeMcQuaid
MikeMcQuaid added this pull request to the merge queue Sep 6, 2026
Merged via the queue into main with commit 4e7c6a3 Sep 6, 2026
51 checks passed
@MikeMcQuaid
MikeMcQuaid deleted the livecheck/crate-add-default-user-agent branch September 6, 2026 08:32
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.

3 participants