Skip to content

Commit 5b5617a

Browse files
StanFromIrelandmiss-islington
authored andcommitted
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 26105b0 commit 5b5617a

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
@@ -118,6 +118,15 @@ def test_open_bad_new_parameter(self):
118118
arguments=[URL],
119119
kw=dict(new=999))
120120

121+
def test_reject_action_dash_prefixes(self):
122+
browser = self.browser_class(name=CMD_NAME)
123+
with self.assertRaises(ValueError):
124+
browser.open('%action--incognito')
125+
# new=1: action is "--new-window", so "%action" itself expands to
126+
# a dash-prefixed flag even with no dash in the original URL.
127+
with self.assertRaises(ValueError):
128+
browser.open('%action', new=1)
129+
121130

122131
class EdgeCommandTest(CommandTestMixin, unittest.TestCase):
123132

Lib/webbrowser.py

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

276276
def open(self, url, new=0, autoraise=True):
277277
sys.audit("webbrowser.open", url)
278-
self._check_url(url)
279278
if new == 0:
280279
action = self.remote_action
281280
elif new == 1:
@@ -289,7 +288,9 @@ def open(self, url, new=0, autoraise=True):
289288
raise Error("Bad 'new' parameter to open(); "
290289
f"expected 0, 1, or 2, got {new}")
291290

292-
args = [arg.replace("%s", url).replace("%action", action)
291+
self._check_url(url.replace("%action", action))
292+
293+
args = [arg.replace("%action", action).replace("%s", url)
293294
for arg in self.remote_args]
294295
args = [arg for arg in args if arg]
295296
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)