Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Lib/test/test_webbrowser.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ def test_open(self):
options=[],
arguments=[URL])

def test_reject_dash_prefixes(self):
browser = self.browser_class(name=CMD_NAME)
with self.assertRaises(ValueError):
browser.open(f"--key=val {URL}")


class BackgroundBrowserCommandTest(CommandTestMixin, unittest.TestCase):

Expand Down
14 changes: 14 additions & 0 deletions Lib/webbrowser.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ def open_new(self, url):
def open_new_tab(self, url):
return self.open(url, 2)

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


class GenericBrowser(BaseBrowser):
"""Class for all browsers started with a command
Expand All @@ -172,6 +178,7 @@ def __init__(self, name):

def open(self, url, new=0, autoraise=True):
sys.audit("webbrowser.open", url)
self._check_url(url)
cmdline = [self.name] + [arg.replace("%s", url)
for arg in self.args]
try:
Expand All @@ -192,6 +199,7 @@ def open(self, url, new=0, autoraise=True):
cmdline = [self.name] + [arg.replace("%s", url)
for arg in self.args]
sys.audit("webbrowser.open", url)
self._check_url(url)
try:
if sys.platform[:3] == 'win':
p = subprocess.Popen(cmdline)
Expand Down Expand Up @@ -257,6 +265,7 @@ def _invoke(self, args, remote, autoraise, url=None):

def open(self, url, new=0, autoraise=True):
sys.audit("webbrowser.open", url)
self._check_url(url)
if new == 0:
action = self.remote_action
elif new == 1:
Expand Down Expand Up @@ -358,6 +367,7 @@ class Konqueror(BaseBrowser):

def open(self, url, new=0, autoraise=True):
sys.audit("webbrowser.open", url)
self._check_url(url)
# XXX Currently I know no way to prevent KFM from opening a new win.
if new == 2:
action = "newTab"
Expand Down Expand Up @@ -442,6 +452,7 @@ def _remote(self, action):

def open(self, url, new=0, autoraise=True):
sys.audit("webbrowser.open", url)
self._check_url(url)
if new:
ok = self._remote("LOADNEW " + url)
else:
Expand Down Expand Up @@ -605,6 +616,7 @@ def register_standard_browsers():
class WindowsDefault(BaseBrowser):
def open(self, url, new=0, autoraise=True):
sys.audit("webbrowser.open", url)
self._check_url(url)
try:
os.startfile(url)
except OSError:
Expand Down Expand Up @@ -637,6 +649,7 @@ def __init__(self, name):

def open(self, url, new=0, autoraise=True):
sys.audit("webbrowser.open", url)
self._check_url(url)
assert "'" not in url
# hack for local urls
if not ':' in url:
Expand Down Expand Up @@ -689,6 +702,7 @@ def _name(self, val):

def open(self, url, new=0, autoraise=True):
sys.audit("webbrowser.open", url)
self._check_url(url)
if self.name == 'default':
script = 'open location "%s"' % url.replace('"', '%22') # opens in default browser
else:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Reject leading dashes in URLs passed to :func:`webbrowser.open`
Loading