|
1 | 1 | """Interfaces for launching and remotely controlling web browsers.""" |
2 | | -# Maintained by Georg Brandl. |
3 | 2 |
|
| 3 | +import builtins # because we override open |
4 | 4 | import os |
| 5 | +lazy import plistlib |
5 | 6 | import shlex |
6 | 7 | import shutil |
7 | 8 | import sys |
@@ -492,10 +493,15 @@ def register_standard_browsers(): |
492 | 493 | _tryorder = [] |
493 | 494 |
|
494 | 495 | if sys.platform == 'darwin': |
495 | | - register("MacOSX", None, MacOSXOSAScript('default')) |
496 | | - register("chrome", None, MacOSXOSAScript('google chrome')) |
497 | | - register("firefox", None, MacOSXOSAScript('firefox')) |
498 | | - register("safari", None, MacOSXOSAScript('safari')) |
| 496 | + register("MacOS", None, MacOS('default')) |
| 497 | + register("MacOSX", None, MacOS('default')) # backward compat alias |
| 498 | + register("chrome", None, MacOS('google chrome')) |
| 499 | + register("chromium", None, MacOS('chromium')) |
| 500 | + register("firefox", None, MacOS('firefox')) |
| 501 | + register("safari", None, MacOS('safari')) |
| 502 | + register("opera", None, MacOS('opera')) |
| 503 | + register("microsoft-edge", None, MacOS('microsoft edge')) |
| 504 | + register("brave", None, MacOS('brave browser')) |
499 | 505 | # macOS can use below Unix support (but we prefer using the macOS |
500 | 506 | # specific stuff) |
501 | 507 |
|
@@ -614,8 +620,80 @@ def open(self, url, new=0, autoraise=True): |
614 | 620 | # |
615 | 621 |
|
616 | 622 | if sys.platform == 'darwin': |
| 623 | + def _macos_default_browser_bundle_id(): |
| 624 | + """Return the bundle ID of the default web browser. |
| 625 | +
|
| 626 | + Reads the LaunchServices preferences file that macOS maintains |
| 627 | + when the user sets a default browser. Returns 'com.apple.Safari' |
| 628 | + if the file is absent or no https handler is recorded, because on |
| 629 | + a fresh macOS installation Safari is the default browser and the |
| 630 | + LaunchServices plist is not written until the user explicitly |
| 631 | + changes their default browser. |
| 632 | + """ |
| 633 | + plist = os.path.expanduser( |
| 634 | + '~/Library/Preferences/com.apple.LaunchServices/' |
| 635 | + 'com.apple.launchservices.secure.plist' |
| 636 | + ) |
| 637 | + try: |
| 638 | + with builtins.open(plist, 'rb') as f: |
| 639 | + data = plistlib.load(f) |
| 640 | + for handler in data.get('LSHandlers', []): |
| 641 | + if handler.get('LSHandlerURLScheme') == 'https': |
| 642 | + return (handler.get('LSHandlerRoleAll') |
| 643 | + or handler.get('LSHandlerRoleViewer')) |
| 644 | + except (OSError, KeyError, ValueError): |
| 645 | + pass |
| 646 | + return 'com.apple.Safari' |
| 647 | + |
| 648 | + class MacOS(BaseBrowser): |
| 649 | + """Launcher class for macOS browsers, using /usr/bin/open. |
| 650 | +
|
| 651 | + For http/https URLs with the default browser, /usr/bin/open is called |
| 652 | + directly; macOS routes these to the registered browser. |
| 653 | +
|
| 654 | + For all other URL schemes (e.g. file://) and for named browsers, |
| 655 | + /usr/bin/open -b <bundle-id> is used so that the URL is always passed |
| 656 | + to a browser application rather than dispatched by the OS file handler. |
| 657 | + This prevents file injection attacks where a file:// URL pointing to an |
| 658 | + executable bundle could otherwise be launched by the OS. |
| 659 | +
|
| 660 | + Named browsers with known bundle IDs use -b; unknown names fall back |
| 661 | + to -a. |
| 662 | + """ |
| 663 | + |
| 664 | + _BUNDLE_IDS = { |
| 665 | + 'google chrome': 'com.google.Chrome', |
| 666 | + 'firefox': 'org.mozilla.firefox', |
| 667 | + 'safari': 'com.apple.Safari', |
| 668 | + 'chromium': 'org.chromium.Chromium', |
| 669 | + 'opera': 'com.operasoftware.Opera', |
| 670 | + 'microsoft edge': 'com.microsoft.edgemac', |
| 671 | + 'brave browser': 'com.brave.Browser', |
| 672 | + } |
| 673 | + |
| 674 | + def open(self, url, new=0, autoraise=True): |
| 675 | + sys.audit("webbrowser.open", url) |
| 676 | + self._check_url(url) |
| 677 | + if self.name == 'default': |
| 678 | + proto, sep, _ = url.partition(':') |
| 679 | + if sep and proto.lower() in {'http', 'https'}: |
| 680 | + cmd = ['/usr/bin/open', url] |
| 681 | + else: |
| 682 | + bundle_id = _macos_default_browser_bundle_id() |
| 683 | + cmd = ['/usr/bin/open', '-b', bundle_id, url] |
| 684 | + else: |
| 685 | + bundle_id = self._BUNDLE_IDS.get(self.name.lower()) |
| 686 | + if bundle_id: |
| 687 | + cmd = ['/usr/bin/open', '-b', bundle_id, url] |
| 688 | + else: |
| 689 | + cmd = ['/usr/bin/open', '-a', self.name, url] |
| 690 | + proc = subprocess.run(cmd, stderr=subprocess.DEVNULL) |
| 691 | + return proc.returncode == 0 |
| 692 | + |
617 | 693 | class MacOSXOSAScript(BaseBrowser): |
618 | 694 | def __init__(self, name='default'): |
| 695 | + import warnings |
| 696 | + warnings._deprecated("webbrowser.MacOSXOSAScript", remove=(3, 17)) |
619 | 697 | super().__init__(name) |
620 | 698 |
|
621 | 699 | def open(self, url, new=0, autoraise=True): |
|
0 commit comments