Skip to content

Commit 00fa6c7

Browse files
committed
gh-137586: Add MacOSX browser class using /usr/bin/open, deprecate MacOSXOSAScript
Add a new MacOSX class that opens URLs via subprocess.run(['/usr/bin/open', ...]) instead of piping AppleScript to osascript. For named browsers, /usr/bin/open -a <name> is used; for the default browser, /usr/bin/open <url> defers directly to the OS URL handler. MacOSXOSAScript is deprecated with a DeprecationWarning pointing users to MacOSX. register_standard_browsers() is updated to use MacOSX for all macOS registrations. osascript is a general-purpose scripting interpreter that is routinely blocked on managed endpoints due to its abuse potential, causing webbrowser.open() to fail silently. /usr/bin/open is Apple's purpose-built URL-opening primitive and carries no such restrictions. This also eliminates the PATH-injection vector in the existing os.popen("osascript", "w") call.
1 parent 8e1469c commit 00fa6c7

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

Lib/webbrowser.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -491,10 +491,10 @@ def register_standard_browsers():
491491
_tryorder = []
492492

493493
if sys.platform == 'darwin':
494-
register("MacOSX", None, MacOSXOSAScript('default'))
495-
register("chrome", None, MacOSXOSAScript('google chrome'))
496-
register("firefox", None, MacOSXOSAScript('firefox'))
497-
register("safari", None, MacOSXOSAScript('safari'))
494+
register("MacOSX", None, MacOSX('default'))
495+
register("chrome", None, MacOSX('google chrome'))
496+
register("firefox", None, MacOSX('firefox'))
497+
register("safari", None, MacOSX('safari'))
498498
# macOS can use below Unix support (but we prefer using the macOS
499499
# specific stuff)
500500

@@ -613,8 +613,27 @@ def open(self, url, new=0, autoraise=True):
613613
#
614614

615615
if sys.platform == 'darwin':
616+
class MacOSX(BaseBrowser):
617+
"""Launcher class for macOS browsers, using /usr/bin/open."""
618+
619+
def open(self, url, new=0, autoraise=True):
620+
sys.audit("webbrowser.open", url)
621+
self._check_url(url)
622+
if self.name == 'default':
623+
cmd = ['/usr/bin/open', url]
624+
else:
625+
cmd = ['/usr/bin/open', '-a', self.name, url]
626+
proc = subprocess.run(cmd, stderr=subprocess.DEVNULL)
627+
return proc.returncode == 0
628+
616629
class MacOSXOSAScript(BaseBrowser):
617630
def __init__(self, name='default'):
631+
import warnings
632+
warnings.warn(
633+
"MacOSXOSAScript is deprecated, use MacOSX instead.",
634+
DeprecationWarning,
635+
stacklevel=2,
636+
)
618637
super().__init__(name)
619638

620639
def open(self, url, new=0, autoraise=True):

0 commit comments

Comments
 (0)