| 1 |
From: Stan Ulbrych <[email protected]> |
| 2 |
Date: Mon, 13 Apr 2026 22:41:51 +0100 |
| 3 |
Subject: gh-148169: Fix webbrowser `%action` substitution bypass of |
| 4 |
dash-prefix check (GH-148170) (#148520) |
| 5 |
|
| 6 |
(cherry picked from commit d22922c8a7958353689dc4763dd72da2dea03fff) |
| 7 |
|
| 8 |
Origin: cpython, https://github.com/python/cpython/commit/f4654824ae0850ac87227fb270f9057477946769 |
| 9 |
--- |
| 10 |
lib-python/3/test/test_webbrowser.py | 8 ++++++++ |
| 11 |
lib-python/3/webbrowser.py | 5 +++-- |
| 12 |
2 files changed, 11 insertions(+), 2 deletions(-) |
| 13 |
|
| 14 |
diff --git a/lib-python/3/test/test_webbrowser.py b/lib-python/3/test/test_webbrowser.py |
| 15 |
index 522219b..ae86347 100644 |
| 16 |
--- a/lib-python/3/test/test_webbrowser.py |
| 17 |
+++ b/lib-python/3/test/test_webbrowser.py |
| 18 |
@@ -103,6 +103,14 @@ class ChromeCommandTest(CommandTestMixin, unittest.TestCase): |
| 19 |
options=[], |
| 20 |
arguments=[URL]) |
| 21 |
|
| 22 |
+ def test_reject_action_dash_prefixes(self): |
| 23 |
+ browser = self.browser_class(name=CMD_NAME) |
| 24 |
+ with self.assertRaises(ValueError): |
| 25 |
+ browser.open('%action--incognito') |
| 26 |
+ # new=1: action is "--new-window", so "%action" itself expands to |
| 27 |
+ # a dash-prefixed flag even with no dash in the original URL. |
| 28 |
+ with self.assertRaises(ValueError): |
| 29 |
+ browser.open('%action', new=1) |
| 30 |
|
| 31 |
class MozillaCommandTest(CommandTestMixin, unittest.TestCase): |
| 32 |
|
| 33 |
diff --git a/lib-python/3/webbrowser.py b/lib-python/3/webbrowser.py |
| 34 |
index 52ad205..c24748f 100755 |
| 35 |
--- a/lib-python/3/webbrowser.py |
| 36 |
+++ b/lib-python/3/webbrowser.py |
| 37 |
@@ -265,7 +265,6 @@ class UnixBrowser(BaseBrowser): |
| 38 |
|
| 39 |
def open(self, url, new=0, autoraise=True): |
| 40 |
sys.audit("webbrowser.open", url) |
| 41 |
- self._check_url(url) |
| 42 |
if new == 0: |
| 43 |
action = self.remote_action |
| 44 |
elif new == 1: |
| 45 |
@@ -279,7 +278,9 @@ class UnixBrowser(BaseBrowser): |
| 46 |
raise Error("Bad 'new' parameter to open(); " + |
| 47 |
"expected 0, 1, or 2, got %s" % new) |
| 48 |
|
| 49 |
- args = [arg.replace("%s", url).replace("%action", action) |
| 50 |
+ self._check_url(url.replace("%action", action)) |
| 51 |
+ |
| 52 |
+ args = [arg.replace("%action", action).replace("%s", url) |
| 53 |
for arg in self.remote_args] |
| 54 |
args = [arg for arg in args if arg] |
| 55 |
success = self._invoke(args, True, autoraise, url) |