-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
gh-137586: Replace 'osascript' with 'open' on macOS in webbrowser #146439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
secengjeff
wants to merge
26
commits into
python:main
Choose a base branch
from
secengjeff:gh-137586-macosx-open
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
00fa6c7
gh-137586: Add MacOSX browser class using /usr/bin/open, deprecate Ma…
secengjeff f697cd5
gh-137586: Add tests for MacOSX browser class and MacOSXOSAScript dep…
secengjeff 7781033
gh-137586: Document MacOSXOSAScript deprecation in webbrowser docs
secengjeff 6066221
gh-137586: Add NEWS entries for MacOSX webbrowser change
secengjeff d54293f
gh-137586: Fix NEWS entry class references with ! prefix to suppress …
secengjeff 080197e
gh-137586: Fix MacOSXOSAScriptTest for MacOSX registration change
secengjeff fdd6649
gh-137586: Use bundle IDs in MacOSX to prevent file injection via OS …
secengjeff 8e1eef4
gh-137586: Load AppKit before NSWorkspace lookup in _macos_default_br…
secengjeff e193626
gh-137586: Register chromium, opera, microsoft-edge in register_stand…
secengjeff 98dd1d8
gh-137586: Fix Microsoft Edge bundle ID on macOS (com.microsoft.edgemac)
secengjeff bdfc2e6
gh-137586: Replace _macos_default_browser_bundle_id with plistlib to …
secengjeff 614078f
gh-137586: Rename MacOSX to MacOS
secengjeff c77f4b8
gh-137586: Use frozendict for MacOS._BUNDLE_IDS
secengjeff c725927
gh-137586: Update webbrowser.rst for MacOS class, fix version directives
secengjeff 1316dbf
gh-137586: Revert frozendict for MacOS._BUNDLE_IDS pending frozendict…
secengjeff 612d276
gh-137586: Fix MacOSXOSAScript mangled to MacOSOSAScript in test rename
secengjeff 386af23
Update Misc/NEWS.d/next/Library/2026-03-26-01-42-20.gh-issue-137586.K…
secengjeff e4e89f1
Update Misc/NEWS.d/next/Security/2026-03-26-01-42-15.gh-issue-137586.…
secengjeff b513903
Update Doc/library/webbrowser.rst
secengjeff aacbceb
Update Doc/library/webbrowser.rst
secengjeff a6d76d6
gh-137586: Fix MacOS plist fallback, builtins.open shadow, add Brave
secengjeff 5a4fdad
Merge branch 'main' into gh-137586-macosx-open
secengjeff de9ec77
gh-137586: Address hugovk review comments
secengjeff 6ddf67f
lazy import plistlib
gpshead f874d67
gh-137586: Address hugovk post-approval concerns
secengjeff 3b390bb
gh-137586: Fix Sphinx ref.class warnings in whatsnew/3.15.rst
secengjeff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| """Interfaces for launching and remotely controlling web browsers.""" | ||
| # Maintained by Georg Brandl. | ||
|
|
||
| import builtins # because we override open | ||
| import os | ||
| lazy import plistlib | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| import shlex | ||
| import shutil | ||
| import sys | ||
|
|
@@ -492,10 +493,15 @@ def register_standard_browsers(): | |
| _tryorder = [] | ||
|
|
||
| if sys.platform == 'darwin': | ||
| register("MacOSX", None, MacOSXOSAScript('default')) | ||
|
gpshead marked this conversation as resolved.
|
||
| register("chrome", None, MacOSXOSAScript('google chrome')) | ||
| register("firefox", None, MacOSXOSAScript('firefox')) | ||
| register("safari", None, MacOSXOSAScript('safari')) | ||
| register("MacOS", None, MacOS('default')) | ||
| register("MacOSX", None, MacOS('default')) # backward compat alias | ||
| register("chrome", None, MacOS('google chrome')) | ||
| register("chromium", None, MacOS('chromium')) | ||
| register("firefox", None, MacOS('firefox')) | ||
| register("safari", None, MacOS('safari')) | ||
| register("opera", None, MacOS('opera')) | ||
| register("microsoft-edge", None, MacOS('microsoft edge')) | ||
| register("brave", None, MacOS('brave browser')) | ||
| # macOS can use below Unix support (but we prefer using the macOS | ||
| # specific stuff) | ||
|
|
||
|
|
@@ -614,8 +620,80 @@ def open(self, url, new=0, autoraise=True): | |
| # | ||
|
|
||
| if sys.platform == 'darwin': | ||
| def _macos_default_browser_bundle_id(): | ||
|
gpshead marked this conversation as resolved.
|
||
| """Return the bundle ID of the default web browser. | ||
|
|
||
| Reads the LaunchServices preferences file that macOS maintains | ||
| when the user sets a default browser. Returns 'com.apple.Safari' | ||
| if the file is absent or no https handler is recorded, because on | ||
| a fresh macOS installation Safari is the default browser and the | ||
| LaunchServices plist is not written until the user explicitly | ||
| changes their default browser. | ||
| """ | ||
| plist = os.path.expanduser( | ||
| '~/Library/Preferences/com.apple.LaunchServices/' | ||
| 'com.apple.launchservices.secure.plist' | ||
| ) | ||
| try: | ||
| with builtins.open(plist, 'rb') as f: | ||
| data = plistlib.load(f) | ||
|
gpshead marked this conversation as resolved.
|
||
| for handler in data.get('LSHandlers', []): | ||
| if handler.get('LSHandlerURLScheme') == 'https': | ||
| return (handler.get('LSHandlerRoleAll') | ||
| or handler.get('LSHandlerRoleViewer')) | ||
| except (OSError, KeyError, ValueError): | ||
| pass | ||
| return 'com.apple.Safari' | ||
|
|
||
| class MacOS(BaseBrowser): | ||
| """Launcher class for macOS browsers, using /usr/bin/open. | ||
|
|
||
| For http/https URLs with the default browser, /usr/bin/open is called | ||
| directly; macOS routes these to the registered browser. | ||
|
|
||
| For all other URL schemes (e.g. file://) and for named browsers, | ||
| /usr/bin/open -b <bundle-id> is used so that the URL is always passed | ||
| to a browser application rather than dispatched by the OS file handler. | ||
| This prevents file injection attacks where a file:// URL pointing to an | ||
| executable bundle could otherwise be launched by the OS. | ||
|
|
||
| Named browsers with known bundle IDs use -b; unknown names fall back | ||
| to -a. | ||
| """ | ||
|
|
||
| _BUNDLE_IDS = { | ||
| 'google chrome': 'com.google.Chrome', | ||
| 'firefox': 'org.mozilla.firefox', | ||
| 'safari': 'com.apple.Safari', | ||
| 'chromium': 'org.chromium.Chromium', | ||
| 'opera': 'com.operasoftware.Opera', | ||
|
hugovk marked this conversation as resolved.
|
||
| 'microsoft edge': 'com.microsoft.edgemac', | ||
| 'brave browser': 'com.brave.Browser', | ||
| } | ||
|
|
||
| def open(self, url, new=0, autoraise=True): | ||
| sys.audit("webbrowser.open", url) | ||
| self._check_url(url) | ||
| if self.name == 'default': | ||
| proto, sep, _ = url.partition(':') | ||
| if sep and proto.lower() in {'http', 'https'}: | ||
| cmd = ['/usr/bin/open', url] | ||
| else: | ||
| bundle_id = _macos_default_browser_bundle_id() | ||
| cmd = ['/usr/bin/open', '-b', bundle_id, url] | ||
| else: | ||
| bundle_id = self._BUNDLE_IDS.get(self.name.lower()) | ||
| if bundle_id: | ||
| cmd = ['/usr/bin/open', '-b', bundle_id, url] | ||
| else: | ||
| cmd = ['/usr/bin/open', '-a', self.name, url] | ||
| proc = subprocess.run(cmd, stderr=subprocess.DEVNULL) | ||
| return proc.returncode == 0 | ||
|
|
||
| class MacOSXOSAScript(BaseBrowser): | ||
| def __init__(self, name='default'): | ||
| import warnings | ||
| warnings._deprecated("webbrowser.MacOSXOSAScript", remove=(3, 17)) | ||
| super().__init__(name) | ||
|
|
||
| def open(self, url, new=0, autoraise=True): | ||
|
|
||
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Library/2026-03-26-01-42-20.gh-issue-137586.KmHRwR.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Add :class:`!MacOS` to :mod:`webbrowser` for macOS, which opens URLs via | ||
| ``/usr/bin/open`` instead of piping AppleScript to ``osascript``. | ||
| Deprecate :class:`!MacOSXOSAScript` in favour of :class:`!MacOS`. |
4 changes: 4 additions & 0 deletions
4
Misc/NEWS.d/next/Security/2026-03-26-01-42-15.gh-issue-137586.j3SkOm.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Fix a PATH-injection vulnerability in :mod:`webbrowser` on macOS where | ||
| ``osascript`` was invoked without an absolute path. The new :class:`!MacOS` | ||
| class uses ``/usr/bin/open`` directly, eliminating the dependency on | ||
| ``osascript`` entirely. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please list alphabetically, after
typing