Skip to content

Commit f465482

Browse files
[3.11] gh-148169: Fix webbrowser %action substitution bypass of dash-prefix check (GH-148170) (#148520)
(cherry picked from commit d22922c)
1 parent 776d39f commit f465482

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

Lib/test/test_webbrowser.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,14 @@ def test_open_new_tab(self):
103103
options=[],
104104
arguments=[URL])
105105

106+
def test_reject_action_dash_prefixes(self):
107+
browser = self.browser_class(name=CMD_NAME)
108+
with self.assertRaises(ValueError):
109+
browser.open('%action--incognito')
110+
# new=1: action is "--new-window", so "%action" itself expands to
111+
# a dash-prefixed flag even with no dash in the original URL.
112+
with self.assertRaises(ValueError):
113+
browser.open('%action', new=1)
106114

107115
class MozillaCommandTest(CommandTestMixin, unittest.TestCase):
108116

Lib/webbrowser.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,6 @@ def _invoke(self, args, remote, autoraise, url=None):
265265

266266
def open(self, url, new=0, autoraise=True):
267267
sys.audit("webbrowser.open", url)
268-
self._check_url(url)
269268
if new == 0:
270269
action = self.remote_action
271270
elif new == 1:
@@ -279,7 +278,9 @@ def open(self, url, new=0, autoraise=True):
279278
raise Error("Bad 'new' parameter to open(); " +
280279
"expected 0, 1, or 2, got %s" % new)
281280

282-
args = [arg.replace("%s", url).replace("%action", action)
281+
self._check_url(url.replace("%action", action))
282+
283+
args = [arg.replace("%action", action).replace("%s", url)
283284
for arg in self.remote_args]
284285
args = [arg for arg in args if arg]
285286
success = self._invoke(args, True, autoraise, url)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
A bypass in :mod:`webbrowser` allowed URLs prefixed with ``%action`` to pass
2+
the dash-prefix safety check.

0 commit comments

Comments
 (0)