From f3b68ab35552c5684f027b7c83a54bebc763ce95 Mon Sep 17 00:00:00 2001 From: daijro Date: Sat, 5 Oct 2024 02:10:43 -0500 Subject: [PATCH] pythonlib: Assert OS is valid 0.2.6 - Removed mobile OS support due to detection issues - Validate the passed os name & that its lowercase --- pythonlib/README.md | 2 +- pythonlib/camoufox/async_api.py | 2 +- pythonlib/camoufox/exceptions.py | 8 +++++++ pythonlib/camoufox/sync_api.py | 2 +- pythonlib/camoufox/utils.py | 39 +++++++++++++++++++++++++------- pythonlib/pyproject.toml | 2 +- 6 files changed, 43 insertions(+), 12 deletions(-) diff --git a/pythonlib/README.md b/pythonlib/README.md index b324c01..b3c0d6a 100644 --- a/pythonlib/README.md +++ b/pythonlib/README.md @@ -92,7 +92,7 @@ Parameters: Camoufox properties to use. os (Optional[ListOrString]): Operating system to use for the fingerprint generation. - Can be "windows", "macos", "linux", "android", "ios", or a list to randomly choose from. + Can be "windows", "macos", "linux", or a list to randomly choose from. Default: ["windows", "macos", "linux"] block_images (Optional[bool]): Whether to block all images. diff --git a/pythonlib/camoufox/async_api.py b/pythonlib/camoufox/async_api.py index 843aba0..d9eae01 100644 --- a/pythonlib/camoufox/async_api.py +++ b/pythonlib/camoufox/async_api.py @@ -71,7 +71,7 @@ async def AsyncNewBrowser( Camoufox properties to use. (read https://github.com/daijro/camoufox/blob/main/README.md) os (Optional[ListOrString]): Operating system to use for the fingerprint generation. - Can be "windows", "macos", "linux", "android", "ios", or a list to randomly choose from. + Can be "windows", "macos", "linux", or a list to randomly choose from. Default: ["windows", "macos", "linux"] block_images (Optional[bool]): Whether to block all images. diff --git a/pythonlib/camoufox/exceptions.py b/pythonlib/camoufox/exceptions.py index fcad52a..bcbb6d5 100644 --- a/pythonlib/camoufox/exceptions.py +++ b/pythonlib/camoufox/exceptions.py @@ -108,3 +108,11 @@ class NonFirefoxFingerprint(Exception): """ ... + + +class InvalidOS(ValueError): + """ + Raised when the target OS is invalid. + """ + + ... diff --git a/pythonlib/camoufox/sync_api.py b/pythonlib/camoufox/sync_api.py index e3a5098..5994274 100644 --- a/pythonlib/camoufox/sync_api.py +++ b/pythonlib/camoufox/sync_api.py @@ -71,7 +71,7 @@ def NewBrowser( Camoufox properties to use. (read https://github.com/daijro/camoufox/blob/main/README.md) os (Optional[ListOrString]): Operating system to use for the fingerprint generation. - Can be "windows", "macos", "linux", "android", "ios", or a list to randomly choose from. + Can be "windows", "macos", "linux", or a list to randomly choose from. Default: ["windows", "macos", "linux"] block_images (Optional[bool]): Whether to block all images. diff --git a/pythonlib/camoufox/utils.py b/pythonlib/camoufox/utils.py index e55bd38..5890d8d 100644 --- a/pythonlib/camoufox/utils.py +++ b/pythonlib/camoufox/utils.py @@ -19,7 +19,12 @@ from .addons import ( get_debug_port, threaded_try_load_addons, ) -from .exceptions import InvalidPropertyType, NonFirefoxFingerprint, UnknownProperty +from .exceptions import ( + InvalidOS, + InvalidPropertyType, + NonFirefoxFingerprint, + UnknownProperty, +) from .fingerprints import from_browserforge, generate_fingerprint from .ip import Proxy, public_ip, valid_ipv4, valid_ipv6 from .locale import geoip_allowed, get_geolocation, normalize_locale @@ -135,7 +140,7 @@ def determine_ua_os(user_agent: str) -> Literal['mac', 'win', 'lin']: parsed_ua = user_agent_parser.ParseOS(user_agent).get('family') if not parsed_ua: raise ValueError("Could not determine OS from user agent") - if parsed_ua.startswith("Mac") or parsed_ua.startswith("iOS"): + if parsed_ua.startswith("Mac"): return "mac" if parsed_ua.startswith("Windows"): return "win" @@ -180,15 +185,13 @@ def check_custom_fingerprint(fingerprint: Fingerprint) -> None: Asserts that the passed BrowserForge fingerprint is a valid Firefox fingerprint. and warns the user that passing their own fingerprint is not recommended. """ - if any(browser in fingerprint.navigator.userAgent for browser in ('Firefox', 'FxiOS')): - return - # Tell the user what browser they're using - parsed_ua = user_agent_parser.ParseUserAgent(fingerprint.navigator.userAgent).get( + # Check what the browser is + browser_name = user_agent_parser.ParseUserAgent(fingerprint.navigator.userAgent).get( 'family', 'Non-Firefox' ) - if parsed_ua: + if browser_name != 'Firefox': raise NonFirefoxFingerprint( - f'"{parsed_ua}" fingerprints are not supported in Camoufox. ' + f'"{browser_name}" fingerprints are not supported in Camoufox. ' 'Using fingerprints from a browser other than Firefox WILL lead to detection. ' 'If this is intentional, pass `i_know_what_im_doing=True`.' ) @@ -200,6 +203,22 @@ def check_custom_fingerprint(fingerprint: Fingerprint) -> None: ) +def check_valid_os(os: ListOrString) -> None: + """ + Checks if the target OS is valid. + """ + if not isinstance(os, str): + for os_name in os: + check_valid_os(os_name) + return + # Assert that the OS is lowercase + if not os.islower(): + raise InvalidOS(f"OS values must be lowercase: '{os}'") + # Assert that the OS is supported by Camoufox + if os not in ('windows', 'macos', 'linux'): + raise InvalidOS(f"Camoufox does not support the OS: '{os}'") + + def _clean_locals(data: Dict[str, Any]) -> Dict[str, Any]: """ Gets the launch options from the locals of the function. @@ -268,6 +287,10 @@ def get_launch_options( if firefox_user_prefs is None: firefox_user_prefs = {} + # Assert the target OS is valid + if os: + check_valid_os(os) + # Add the default addons add_default_addons(addons, exclude_addons) diff --git a/pythonlib/pyproject.toml b/pythonlib/pyproject.toml index 2a4a1dd..d1e60a2 100644 --- a/pythonlib/pyproject.toml +++ b/pythonlib/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "camoufox" -version = "0.2.5" +version = "0.2.6" description = "Wrapper around Playwright to help launch Camoufox" authors = ["daijro "] license = "MIT"