@@ -158,6 +158,12 @@ def open_new(self, url):
158158 def open_new_tab (self , url ):
159159 return self .open (url , 2 )
160160
161+ @staticmethod
162+ def _check_url (url ):
163+ """Ensures that the URL is safe to pass to subprocesses as a parameter"""
164+ if url and url .lstrip ().startswith ("-" ):
165+ raise ValueError (f"Invalid URL: { url } " )
166+
161167
162168class GenericBrowser (BaseBrowser ):
163169 """Class for all browsers started with a command
@@ -175,6 +181,7 @@ def __init__(self, name):
175181
176182 def open (self , url , new = 0 , autoraise = True ):
177183 sys .audit ("webbrowser.open" , url )
184+ self ._check_url (url )
178185 cmdline = [self .name ] + [arg .replace ("%s" , url )
179186 for arg in self .args ]
180187 try :
@@ -195,6 +202,7 @@ def open(self, url, new=0, autoraise=True):
195202 cmdline = [self .name ] + [arg .replace ("%s" , url )
196203 for arg in self .args ]
197204 sys .audit ("webbrowser.open" , url )
205+ self ._check_url (url )
198206 try :
199207 if sys .platform [:3 ] == 'win' :
200208 p = subprocess .Popen (cmdline )
@@ -260,6 +268,7 @@ def _invoke(self, args, remote, autoraise, url=None):
260268
261269 def open (self , url , new = 0 , autoraise = True ):
262270 sys .audit ("webbrowser.open" , url )
271+ self ._check_url (url )
263272 if new == 0 :
264273 action = self .remote_action
265274 elif new == 1 :
@@ -350,6 +359,7 @@ class Konqueror(BaseBrowser):
350359
351360 def open (self , url , new = 0 , autoraise = True ):
352361 sys .audit ("webbrowser.open" , url )
362+ self ._check_url (url )
353363 # XXX Currently I know no way to prevent KFM from opening a new win.
354364 if new == 2 :
355365 action = "newTab"
@@ -554,6 +564,7 @@ def register_standard_browsers():
554564 class WindowsDefault (BaseBrowser ):
555565 def open (self , url , new = 0 , autoraise = True ):
556566 sys .audit ("webbrowser.open" , url )
567+ self ._check_url (url )
557568 try :
558569 os .startfile (url )
559570 except OSError :
@@ -638,6 +649,7 @@ def _name(self, val):
638649
639650 def open (self , url , new = 0 , autoraise = True ):
640651 sys .audit ("webbrowser.open" , url )
652+ self ._check_url (url )
641653 if self .name == 'default' :
642654 script = 'open location "%s"' % url .replace ('"' , '%22' ) # opens in default browser
643655 else :
0 commit comments