Skip to content

Commit cbba611

Browse files
[3.12] gh-143930: Reject leading dashes in webbrowser URLs (GH-146360)
(cherry picked from commit 82a24a4) Co-authored-by: Seth Michael Larson <seth@python.org>
1 parent 57cc1bd commit cbba611

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

Lib/test/test_webbrowser.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ def test_open(self):
5959
options=[],
6060
arguments=[URL])
6161

62+
def test_reject_dash_prefixes(self):
63+
browser = self.browser_class(name=CMD_NAME)
64+
with self.assertRaises(ValueError):
65+
browser.open(f"--key=val {URL}")
66+
6267

6368
class BackgroundBrowserCommandTest(CommandTestMixin, unittest.TestCase):
6469

Lib/webbrowser.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ def open_new(self, url):
158158
def open_new_tab(self, url):
159159
return self.open(url, 2)
160160

161+
@staticmethod
162+
def _check_url(url):
163+
"""Ensures that the URL is safe to pass to subprocesses as a parameter"""
164+
if url and url.lstrip().startswith("-"):
165+
raise ValueError(f"Invalid URL: {url}")
166+
161167

162168
class GenericBrowser(BaseBrowser):
163169
"""Class for all browsers started with a command
@@ -175,6 +181,7 @@ def __init__(self, name):
175181

176182
def open(self, url, new=0, autoraise=True):
177183
sys.audit("webbrowser.open", url)
184+
self._check_url(url)
178185
cmdline = [self.name] + [arg.replace("%s", url)
179186
for arg in self.args]
180187
try:
@@ -195,6 +202,7 @@ def open(self, url, new=0, autoraise=True):
195202
cmdline = [self.name] + [arg.replace("%s", url)
196203
for arg in self.args]
197204
sys.audit("webbrowser.open", url)
205+
self._check_url(url)
198206
try:
199207
if sys.platform[:3] == 'win':
200208
p = subprocess.Popen(cmdline)
@@ -260,6 +268,7 @@ def _invoke(self, args, remote, autoraise, url=None):
260268

261269
def open(self, url, new=0, autoraise=True):
262270
sys.audit("webbrowser.open", url)
271+
self._check_url(url)
263272
if new == 0:
264273
action = self.remote_action
265274
elif new == 1:
@@ -350,6 +359,7 @@ class Konqueror(BaseBrowser):
350359

351360
def open(self, url, new=0, autoraise=True):
352361
sys.audit("webbrowser.open", url)
362+
self._check_url(url)
353363
# XXX Currently I know no way to prevent KFM from opening a new win.
354364
if new == 2:
355365
action = "newTab"
@@ -554,6 +564,7 @@ def register_standard_browsers():
554564
class WindowsDefault(BaseBrowser):
555565
def open(self, url, new=0, autoraise=True):
556566
sys.audit("webbrowser.open", url)
567+
self._check_url(url)
557568
try:
558569
os.startfile(url)
559570
except OSError:
@@ -638,6 +649,7 @@ def _name(self, val):
638649

639650
def open(self, url, new=0, autoraise=True):
640651
sys.audit("webbrowser.open", url)
652+
self._check_url(url)
641653
if self.name == 'default':
642654
script = 'open location "%s"' % url.replace('"', '%22') # opens in default browser
643655
else:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Reject leading dashes in URLs passed to :func:`webbrowser.open`

0 commit comments

Comments
 (0)