Skip to content

Commit c0badc9

Browse files
tomcruiseqisethmlarson
authored andcommitted
Reject leading dashes in webbrowser URLs (pythonGH-143931) (pythonGH-146359) (cherry picked from commit 82a24a4) Backported from Python 3.10: ad4d5ba Co-authored-by: Seth Michael Larson <seth@python.org>
1 parent 9170c74 commit c0badc9

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

Lib/test/test_webbrowser.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ def test_open(self):
5353
options=[],
5454
arguments=[URL])
5555

56+
def test_reject_dash_prefixes(self):
57+
browser = self.browser_class(name=CMD_NAME)
58+
with self.assertRaises(ValueError):
59+
browser.open(f"--key=val {URL}")
60+
5661

5762
class BackgroundBrowserCommandTest(CommandTestMixin, unittest.TestCase):
5863

Lib/webbrowser.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ def open_new(self, url):
120120
def open_new_tab(self, url):
121121
return self.open(url, 2)
122122

123+
@staticmethod
124+
def _check_url(url):
125+
"""Ensures that the URL is safe to pass to subprocesses as a parameter"""
126+
if url and url.lstrip().startswith("-"):
127+
raise ValueError(f"Invalid URL: {url}")
128+
123129

124130
class GenericBrowser(BaseBrowser):
125131
"""Class for all browsers started with a command
@@ -136,6 +142,7 @@ def __init__(self, name):
136142
self.basename = os.path.basename(self.name)
137143

138144
def open(self, url, new=0, autoraise=True):
145+
self._check_url(url)
139146
cmdline = [self.name] + [arg.replace("%s", url)
140147
for arg in self.args]
141148
try:
@@ -153,6 +160,7 @@ class BackgroundBrowser(GenericBrowser):
153160
background."""
154161

155162
def open(self, url, new=0, autoraise=True):
163+
self._check_url(url)
156164
cmdline = [self.name] + [arg.replace("%s", url)
157165
for arg in self.args]
158166
try:
@@ -219,6 +227,7 @@ def _invoke(self, args, remote, autoraise):
219227
return not p.wait()
220228

221229
def open(self, url, new=0, autoraise=True):
230+
self._check_url(url)
222231
if new == 0:
223232
action = self.remote_action
224233
elif new == 1:
@@ -319,6 +328,7 @@ class Konqueror(BaseBrowser):
319328
"""
320329

321330
def open(self, url, new=0, autoraise=True):
331+
self._check_url(url)
322332
# XXX Currently I know no way to prevent KFM from opening a new win.
323333
if new == 2:
324334
action = "newTab"
@@ -402,6 +412,7 @@ def _remote(self, action):
402412
return 1
403413

404414
def open(self, url, new=0, autoraise=True):
415+
self._check_url(url)
405416
if new:
406417
ok = self._remote("LOADNEW " + url)
407418
else:
@@ -508,6 +519,7 @@ def register_X_browsers():
508519
if sys.platform[:3] == "win":
509520
class WindowsDefault(BaseBrowser):
510521
def open(self, url, new=0, autoraise=True):
522+
self._check_url(url)
511523
try:
512524
os.startfile(url)
513525
except OSError:
@@ -551,6 +563,7 @@ def __init__(self, name):
551563
self.name = name
552564

553565
def open(self, url, new=0, autoraise=True):
566+
self._check_url(url)
554567
assert "'" not in url
555568
# hack for local urls
556569
if not ':' in url:
@@ -588,6 +601,7 @@ def __init__(self, name):
588601
self._name = name
589602

590603
def open(self, url, new=0, autoraise=True):
604+
self._check_url(url)
591605
if self._name == 'default':
592606
script = 'open location "%s"' % url.replace('"', '%22') # opens in default browser
593607
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)