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: 16 additions & 5 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,6 +329,7 @@ def close(self):
@unittest.skipUnless(sys.platform == "darwin", "macOS specific test")
@requires_subprocess()
class MacOSXOSAScriptTest(unittest.TestCase):
maxDiff = None
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change intentional?


def setUp(self):
# Ensure that 'BROWSER' is not set to 'open' or something else.
Expand Down Expand Up @@ -376,6 +380,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
Loading