Skip to content

Commit 1016d82

Browse files
[3.12] gh-148169: Fix webbrowser %action substitution bypass of dash-prefix check (GH-148170)
(cherry picked from commit d22922c) Co-authored-by: Stan Ulbrych <stan@python.org>
1 parent 82b53a6 commit 1016d82

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

Lib/test/test_webbrowser.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ 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)
114+
106115

107116
class EdgeCommandTest(CommandTestMixin, unittest.TestCase):
108117

Lib/webbrowser.py

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

269269
def open(self, url, new=0, autoraise=True):
270270
sys.audit("webbrowser.open", url)
271-
self._check_url(url)
272271
if new == 0:
273272
action = self.remote_action
274273
elif new == 1:
@@ -282,7 +281,9 @@ def open(self, url, new=0, autoraise=True):
282281
raise Error("Bad 'new' parameter to open(); " +
283282
"expected 0, 1, or 2, got %s" % new)
284283

285-
args = [arg.replace("%s", url).replace("%action", action)
284+
self._check_url(url.replace("%action", action))
285+
286+
args = [arg.replace("%action", action).replace("%s", url)
286287
for arg in self.remote_args]
287288
args = [arg for arg in args if arg]
288289
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)