| 1 |
From: tomcruiseqi <[email protected]> |
| 2 |
Date: Wed, 25 Mar 2026 02:23:28 +0800 |
| 3 |
Subject: gh-143930: Reject leading dashes in webbrowser URLs (GH-143931) |
| 4 |
(GH-146364) |
| 5 |
|
| 6 |
(cherry picked from commit 82a24a4442312bdcfc4c799885e8b3e00990f02b) |
| 7 |
|
| 8 |
Co-authored-by: Seth Michael Larson <[email protected]> |
| 9 |
|
| 10 |
Origin: cpython, https://github.com/python/cpython/commit/ceac1efc66516ac387eef2c9a0ce671895b44f03 |
| 11 |
--- |
| 12 |
lib-python/3/test/test_webbrowser.py | 5 +++++ |
| 13 |
lib-python/3/webbrowser.py | 14 ++++++++++++++ |
| 14 |
2 files changed, 19 insertions(+) |
| 15 |
|
| 16 |
diff --git a/lib-python/3/test/test_webbrowser.py b/lib-python/3/test/test_webbrowser.py |
| 17 |
index 9d608d6..0ac985f 100644 |
| 18 |
--- a/lib-python/3/test/test_webbrowser.py |
| 19 |
+++ b/lib-python/3/test/test_webbrowser.py |
| 20 |
@@ -59,6 +59,11 @@ class GenericBrowserCommandTest(CommandTestMixin, unittest.TestCase): |
| 21 |
options=[], |
| 22 |
arguments=[URL]) |
| 23 |
|
| 24 |
+ def test_reject_dash_prefixes(self): |
| 25 |
+ browser = self.browser_class(name=CMD_NAME) |
| 26 |
+ with self.assertRaises(ValueError): |
| 27 |
+ browser.open(f"--key=val {URL}") |
| 28 |
+ |
| 29 |
|
| 30 |
class BackgroundBrowserCommandTest(CommandTestMixin, unittest.TestCase): |
| 31 |
|
| 32 |
diff --git a/lib-python/3/webbrowser.py b/lib-python/3/webbrowser.py |
| 33 |
index 5d72524..0fd0aeb 100755 |
| 34 |
--- a/lib-python/3/webbrowser.py |
| 35 |
+++ b/lib-python/3/webbrowser.py |
| 36 |
@@ -155,6 +155,12 @@ class BaseBrowser(object): |
| 37 |
def open_new_tab(self, url): |
| 38 |
return self.open(url, 2) |
| 39 |
|
| 40 |
+ @staticmethod |
| 41 |
+ def _check_url(url): |
| 42 |
+ """Ensures that the URL is safe to pass to subprocesses as a parameter""" |
| 43 |
+ if url and url.lstrip().startswith("-"): |
| 44 |
+ raise ValueError(f"Invalid URL: {url}") |
| 45 |
+ |
| 46 |
|
| 47 |
class GenericBrowser(BaseBrowser): |
| 48 |
"""Class for all browsers started with a command |
| 49 |
@@ -172,6 +178,7 @@ class GenericBrowser(BaseBrowser): |
| 50 |
|
| 51 |
def open(self, url, new=0, autoraise=True): |
| 52 |
sys.audit("webbrowser.open", url) |
| 53 |
+ self._check_url(url) |
| 54 |
cmdline = [self.name] + [arg.replace("%s", url) |
| 55 |
for arg in self.args] |
| 56 |
try: |
| 57 |
@@ -192,6 +199,7 @@ class BackgroundBrowser(GenericBrowser): |
| 58 |
cmdline = [self.name] + [arg.replace("%s", url) |
| 59 |
for arg in self.args] |
| 60 |
sys.audit("webbrowser.open", url) |
| 61 |
+ self._check_url(url) |
| 62 |
try: |
| 63 |
if sys.platform[:3] == 'win': |
| 64 |
p = subprocess.Popen(cmdline) |
| 65 |
@@ -257,6 +265,7 @@ class UnixBrowser(BaseBrowser): |
| 66 |
|
| 67 |
def open(self, url, new=0, autoraise=True): |
| 68 |
sys.audit("webbrowser.open", url) |
| 69 |
+ self._check_url(url) |
| 70 |
if new == 0: |
| 71 |
action = self.remote_action |
| 72 |
elif new == 1: |
| 73 |
@@ -358,6 +367,7 @@ class Konqueror(BaseBrowser): |
| 74 |
|
| 75 |
def open(self, url, new=0, autoraise=True): |
| 76 |
sys.audit("webbrowser.open", url) |
| 77 |
+ self._check_url(url) |
| 78 |
# XXX Currently I know no way to prevent KFM from opening a new win. |
| 79 |
if new == 2: |
| 80 |
action = "newTab" |
| 81 |
@@ -442,6 +452,7 @@ class Grail(BaseBrowser): |
| 82 |
|
| 83 |
def open(self, url, new=0, autoraise=True): |
| 84 |
sys.audit("webbrowser.open", url) |
| 85 |
+ self._check_url(url) |
| 86 |
if new: |
| 87 |
ok = self._remote("LOADNEW " + url) |
| 88 |
else: |
| 89 |
@@ -605,6 +616,7 @@ if sys.platform[:3] == "win": |
| 90 |
class WindowsDefault(BaseBrowser): |
| 91 |
def open(self, url, new=0, autoraise=True): |
| 92 |
sys.audit("webbrowser.open", url) |
| 93 |
+ self._check_url(url) |
| 94 |
try: |
| 95 |
os.startfile(url) |
| 96 |
except OSError: |
| 97 |
@@ -637,6 +649,7 @@ if sys.platform == 'darwin': |
| 98 |
|
| 99 |
def open(self, url, new=0, autoraise=True): |
| 100 |
sys.audit("webbrowser.open", url) |
| 101 |
+ self._check_url(url) |
| 102 |
assert "'" not in url |
| 103 |
# hack for local urls |
| 104 |
if not ':' in url: |
| 105 |
@@ -689,6 +702,7 @@ if sys.platform == 'darwin': |
| 106 |
|
| 107 |
def open(self, url, new=0, autoraise=True): |
| 108 |
sys.audit("webbrowser.open", url) |
| 109 |
+ self._check_url(url) |
| 110 |
if self.name == 'default': |
| 111 |
script = 'open location "%s"' % url.replace('"', '%22') # opens in default browser |
| 112 |
else: |