@@ -120,6 +120,12 @@ def open_new(self, url):
120120 def open_new_tab (self , url ):
121121 return self .open (url , 2 )
122122
123+ @staticmethod
124+ def _check_url (url ):
125+ """Ensures that the URL is safe to pass to subprocesses as a parameter"""
126+ if url and url .lstrip ().startswith ("-" ):
127+ raise ValueError (f"Invalid URL: { url } " )
128+
123129
124130class GenericBrowser (BaseBrowser ):
125131 """Class for all browsers started with a command
@@ -136,6 +142,7 @@ def __init__(self, name):
136142 self .basename = os .path .basename (self .name )
137143
138144 def open (self , url , new = 0 , autoraise = True ):
145+ self ._check_url (url )
139146 cmdline = [self .name ] + [arg .replace ("%s" , url )
140147 for arg in self .args ]
141148 try :
@@ -153,6 +160,7 @@ class BackgroundBrowser(GenericBrowser):
153160 background."""
154161
155162 def open (self , url , new = 0 , autoraise = True ):
163+ self ._check_url (url )
156164 cmdline = [self .name ] + [arg .replace ("%s" , url )
157165 for arg in self .args ]
158166 try :
@@ -219,6 +227,7 @@ def _invoke(self, args, remote, autoraise):
219227 return not p .wait ()
220228
221229 def open (self , url , new = 0 , autoraise = True ):
230+ self ._check_url (url )
222231 if new == 0 :
223232 action = self .remote_action
224233 elif new == 1 :
@@ -319,6 +328,7 @@ class Konqueror(BaseBrowser):
319328 """
320329
321330 def open (self , url , new = 0 , autoraise = True ):
331+ self ._check_url (url )
322332 # XXX Currently I know no way to prevent KFM from opening a new win.
323333 if new == 2 :
324334 action = "newTab"
@@ -402,6 +412,7 @@ def _remote(self, action):
402412 return 1
403413
404414 def open (self , url , new = 0 , autoraise = True ):
415+ self ._check_url (url )
405416 if new :
406417 ok = self ._remote ("LOADNEW " + url )
407418 else :
@@ -508,6 +519,7 @@ def register_X_browsers():
508519if sys .platform [:3 ] == "win" :
509520 class WindowsDefault (BaseBrowser ):
510521 def open (self , url , new = 0 , autoraise = True ):
522+ self ._check_url (url )
511523 try :
512524 os .startfile (url )
513525 except OSError :
@@ -551,6 +563,7 @@ def __init__(self, name):
551563 self .name = name
552564
553565 def open (self , url , new = 0 , autoraise = True ):
566+ self ._check_url (url )
554567 assert "'" not in url
555568 # hack for local urls
556569 if not ':' in url :
@@ -588,6 +601,7 @@ def __init__(self, name):
588601 self ._name = name
589602
590603 def open (self , url , new = 0 , autoraise = True ):
604+ self ._check_url (url )
591605 if self ._name == 'default' :
592606 script = 'open location "%s"' % url .replace ('"' , '%22' ) # opens in default browser
593607 else :
0 commit comments