File tree Expand file tree Collapse file tree
tests/unit/searchcommands Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -120,9 +120,22 @@ def configure_logging(logger_name, filename=None):
120120
121121_current_logging_configuration_file = None
122122
123+
124+ def _find_app_root (app_file : str ) -> str :
125+ """Return the app root directory for a search command script."""
126+ directory : str = path .abspath (path .dirname (app_file ))
127+ while True :
128+ parent , name = path .split (directory )
129+ if name == "bin" :
130+ return parent
131+ if parent == directory :
132+ return path .dirname (path .abspath (path .dirname (app_file )))
133+ directory = parent
134+
135+
123136splunk_home = path .abspath (path .join (getcwd (), environ .get ("SPLUNK_HOME" , "" )))
124137app_file = getattr (sys .modules ["__main__" ], "__file__" , sys .executable )
125- app_root = path . dirname ( path . abspath ( path . dirname ( app_file )) )
138+ app_root = _find_app_root ( app_file )
126139
127140splunklib_logger , logging_configuration = configure_logging ("splunklib" )
128141
Original file line number Diff line number Diff line change @@ -137,16 +137,25 @@ def test_logging_configuration(self):
137137 f"Expected ValueError, but logging_configuration={ command .logging_configuration } "
138138 )
139139
140- # logging_configuration raises a value error for an existing absolute path outside the app directory (RCE guard)
140+ inside_app_root_logging_configuration = os .path .join (
141+ environment .app_root , "default" , "logging.conf"
142+ )
143+ command .logging_configuration = inside_app_root_logging_configuration
144+ assert command .logging_configuration == inside_app_root_logging_configuration , (
145+ "logging_configuration should accept an absolute path inside the app directory"
146+ )
147+
141148 try :
142- command .logging_configuration = os .path .join ( package_directory , "__init__.py" )
149+ command .logging_configuration = os .path .realpath ( __file__ )
143150 except ValueError :
144151 pass
145152 except BaseException as e :
146- pytest .fail (f"Expected ValueError, but { type (e )} was raised" )
153+ pytest .fail (
154+ f"Expected ValueError for a path outside the app directory, but { type (e )} was raised"
155+ )
147156 else :
148157 pytest .fail (
149- f"Expected ValueError, but logging_configuration= { command .logging_configuration } "
158+ f"Expected ValueError for a path outside the app directory , but { command .logging_configuration = } "
150159 )
151160
152161 # logging_configuration raises a value error when a relative path traverses outside the app directory (RCE guard)
You can’t perform that action at this time.
0 commit comments