mirror of
https://github.com/daijro/camoufox.git
synced 2026-10-04 08:00:19 +00:00
fix(python): give NewContext identities the browser's Firefox version
NewContext() and AsyncNewContext() passed ff_version=None through to generate_context_fingerprint(), so a context's user agent kept the version fpgen drew (e.g. Firefox/146) while the browser underneath was 152. They now default ff_version to the major version of Playwright's Browser.version, which Juggler reports from MOZ_APP_VERSION_DISPLAY, so the UA always names the browser the page is actually talking to. An explicit ff_version still wins. The docstrings said each context gets "its own real fingerprint preset"; the default has been an fpgen draw, with a preset only when one is passed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
165e68f831
commit
46e5c8d1a4
@@ -0,0 +1,36 @@
|
||||
"""A context's user agent carries the Firefox version of the browser it runs in.
|
||||
|
||||
Without an explicit ff_version, NewContext kept whatever version fpgen drew
|
||||
(e.g. Firefox/146) on a 160 browser, so the UA disagreed with every
|
||||
version-dependent API the page could probe.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import re
|
||||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
|
||||
from camoufox import async_api, sync_api
|
||||
|
||||
|
||||
def _user_agent(init_script):
|
||||
return re.search(r'setNavigatorUserAgent\("([^"]+)"\)', init_script).group(1)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("api", ["sync", "async"])
|
||||
def test_user_agent_matches_the_browser_version(api):
|
||||
context = mock.MagicMock()
|
||||
browser = mock.MagicMock()
|
||||
# Playwright's Browser.version for Firefox: MOZ_APP_VERSION_DISPLAY.
|
||||
browser.version = "160.0.1"
|
||||
if api == "sync":
|
||||
browser.new_context.return_value = context
|
||||
sync_api.NewContext(browser, os="linux")
|
||||
else:
|
||||
context.add_init_script = mock.AsyncMock()
|
||||
browser.new_context = mock.AsyncMock(return_value=context)
|
||||
asyncio.run(async_api.AsyncNewContext(browser, os="linux"))
|
||||
|
||||
user_agent = _user_agent(context.add_init_script.call_args.args[0])
|
||||
assert "Firefox/160.0" in user_agent and "rv:160.0" in user_agent, user_agent
|
||||
@@ -34,6 +34,7 @@ EXIT = {"status": "success", "query": "203.0.113.7", "timezone": "Europe/Paris"}
|
||||
|
||||
def _sync_browser():
|
||||
browser = mock.MagicMock()
|
||||
browser.version = "152.0.4"
|
||||
return browser
|
||||
|
||||
|
||||
@@ -41,6 +42,7 @@ def _async_browser():
|
||||
context = mock.MagicMock()
|
||||
context.add_init_script = mock.AsyncMock()
|
||||
browser = mock.MagicMock()
|
||||
browser.version = "152.0.4"
|
||||
browser.new_context = mock.AsyncMock(return_value=context)
|
||||
return browser
|
||||
|
||||
|
||||
Reference in New Issue
Block a user