Skip to content

Commit fef773a

Browse files
StanFromIrelandstratakis
authored andcommitted
Fix webbrowser `%action` substitution bypass of dash-prefix check
1 parent 7bc9eeb commit fef773a

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
@@ -99,6 +99,15 @@ def test_open_new_tab(self):
9999
options=[],
100100
arguments=[URL])
101101

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

103112
class EdgeCommandTest(CommandTestMixin, unittest.TestCase):
104113

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)