@@ -154,6 +154,12 @@ def open_new(self, url):
154154 def open_new_tab (self , url ):
155155 return self .open (url , 2 )
156156
157+ @staticmethod
158+ def _check_url (url ):
159+ """Ensures that the URL is safe to pass to subprocesses as a parameter"""
160+ if url and url .lstrip ().startswith ("-" ):
161+ raise ValueError (f"Invalid URL: { url } " )
162+
157163
158164class GenericBrowser (BaseBrowser ):
159165 """Class for all browsers started with a command
@@ -171,6 +177,7 @@ def __init__(self, name):
171177
172178 def open (self , url , new = 0 , autoraise = True ):
173179 sys .audit ("webbrowser.open" , url )
180+ self ._check_url (url )
174181 cmdline = [self .name ] + [arg .replace ("%s" , url )
175182 for arg in self .args ]
176183 try :
@@ -191,6 +198,7 @@ def open(self, url, new=0, autoraise=True):
191198 cmdline = [self .name ] + [arg .replace ("%s" , url )
192199 for arg in self .args ]
193200 sys .audit ("webbrowser.open" , url )
201+ self ._check_url (url )
194202 try :
195203 if sys .platform [:3 ] == 'win' :
196204 p = subprocess .Popen (cmdline )
@@ -256,6 +264,7 @@ def _invoke(self, args, remote, autoraise, url=None):
256264
257265 def open (self , url , new = 0 , autoraise = True ):
258266 sys .audit ("webbrowser.open" , url )
267+ self ._check_url (url )
259268 if new == 0 :
260269 action = self .remote_action
261270 elif new == 1 :
@@ -357,6 +366,7 @@ class Konqueror(BaseBrowser):
357366
358367 def open (self , url , new = 0 , autoraise = True ):
359368 sys .audit ("webbrowser.open" , url )
369+ self ._check_url (url )
360370 # XXX Currently I know no way to prevent KFM from opening a new win.
361371 if new == 2 :
362372 action = "newTab"
@@ -603,6 +613,7 @@ def register_standard_browsers():
603613 class WindowsDefault (BaseBrowser ):
604614 def open (self , url , new = 0 , autoraise = True ):
605615 sys .audit ("webbrowser.open" , url )
616+ self ._check_url (url )
606617 try :
607618 os .startfile (url )
608619 except OSError :
@@ -633,6 +644,7 @@ def __init__(self, name):
633644
634645 def open (self , url , new = 0 , autoraise = True ):
635646 sys .audit ("webbrowser.open" , url )
647+ self ._check_url (url )
636648 assert "'" not in url
637649 # hack for local urls
638650 if not ':' in url :
0 commit comments