Skip to content
Open
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
21 changes: 15 additions & 6 deletions Lib/test/test_webbrowser.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ def _test(self, meth, *, args=[URL], kw={}, options, arguments):
popen_args.pop(popen_args.index(option))
self.assertEqual(popen_args, arguments)

def test_reject_dash_prefixes(self):
browser = self.browser_class(name=CMD_NAME)
with self.assertRaisesRegex(
ValueError,
r"^Invalid URL \(leading dash disallowed\): '--key=val http.*'$"
):
browser.open(f"--key=val {URL}")


class GenericBrowserCommandTest(CommandTestMixin, unittest.TestCase):

Expand All @@ -67,11 +75,6 @@ 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 Expand Up @@ -326,7 +329,6 @@ def close(self):
@unittest.skipUnless(sys.platform == "darwin", "macOS specific test")
@requires_subprocess()
class MacOSXOSAScriptTest(unittest.TestCase):

def setUp(self):
# Ensure that 'BROWSER' is not set to 'open' or something else.
# See: https://github.com/python/cpython/issues/131254.
Expand Down Expand Up @@ -376,6 +378,13 @@ def test_explicit_browser(self):
self.assertIn('tell application "safari"', script)
self.assertIn('open location "https://python.org"', script)

def test_reject_dash_prefixes(self):
with self.assertRaisesRegex(
ValueError,
r"^Invalid URL \(leading dash disallowed\): '--key=val http.*'$"
):
self.browser.open(f"--key=val {URL}")


class BrowserRegistrationTest(unittest.TestCase):

Expand Down
2 changes: 1 addition & 1 deletion Lib/webbrowser.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def open_new_tab(self, url):
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}")
raise ValueError(f"Invalid URL (leading dash disallowed): {url!r}")


class GenericBrowser(BaseBrowser):
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Reject leading dashes in URLs passed to :func:`webbrowser.open`
Reject leading dashes in URLs passed to :func:`webbrowser.open`.
Loading