Skip to content

Commit c5767a7

Browse files
[3.10] gh-148169: Fix webbrowser %action substitution bypass of dash-prefix check (GH-148170) (#148521)
(cherry picked from commit d22922c)
1 parent 80f0bc1 commit c5767a7

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
@@ -101,6 +101,14 @@ def test_open_new_tab(self):
101101
options=[],
102102
arguments=[URL])
103103

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

105113
class MozillaCommandTest(CommandTestMixin, unittest.TestCase):
106114

Lib/webbrowser.py

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

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

281-
args = [arg.replace("%s", url).replace("%action", action)
280+
self._check_url(url.replace("%action", action))
281+
282+
args = [arg.replace("%action", action).replace("%s", url)
282283
for arg in self.remote_args]
283284
args = [arg for arg in args if arg]
284285
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)