fix: derive Chromium User-Agent from actual browser version
CI / build (push) Successful in 1m45s
CI / test (push) Successful in 1m47s
CI / vet (push) Failing after 3m55s

The hardcoded DefaultChromiumUserAgent said Chrome/131 while the
installed Chromium was v136. Chromium's sec-ch-ua header is generated
from the real engine version, so sites comparing User-Agent against
sec-ch-ua detected the mismatch as bot traffic and returned 403.

Now the User-Agent is built after browser launch using browser.Version(),
keeping the Chrome/N token in sync with sec-ch-ua's Chromium;v="N".

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-11 04:03:00 +00:00
parent 3b6d864330
commit 9987b94233
3 changed files with 50 additions and 9 deletions
+15 -9
View File
@@ -52,15 +52,11 @@ func initBrowser(opt BrowserOptions) (*browserInitResult, error) {
return nil, ErrInvalidBrowserSelection
}
// Auto-select a User-Agent matching the browser engine when the caller hasn't set one.
if opt.UserAgent == "" {
switch opt.Browser {
case BrowserChromium:
opt.UserAgent = DefaultChromiumUserAgent
default:
opt.UserAgent = DefaultFirefoxUserAgent
}
}
// User-Agent auto-selection is deferred until after browser launch so we
// can read the real browser version and build a UA that matches the
// sec-ch-ua header Chromium sends automatically. A mismatched version
// (e.g. UA says Chrome/131 while sec-ch-ua says Chromium/136) is a
// well-known bot-detection signal that causes 403s on many sites.
// Collect launch args and init scripts, starting with any stealth-mode presets.
stealth := opt.Stealth == nil || *opt.Stealth
@@ -117,6 +113,16 @@ func initBrowser(opt BrowserOptions) (*browserInitResult, error) {
}
}
// Auto-select User-Agent now that we know the real browser version.
if opt.UserAgent == "" {
switch opt.Browser {
case BrowserChromium:
opt.UserAgent = chromiumUserAgent(browser.Version())
default:
opt.UserAgent = DefaultFirefoxUserAgent
}
}
var viewport *playwright.Size
if opt.Dimensions.Width > 0 && opt.Dimensions.Height > 0 {
viewport = &playwright.Size{