Skip to content

Commit 63f0de1

Browse files
committed
Try addressing PR comments
1 parent 73eac07 commit 63f0de1

2 files changed

Lines changed: 27 additions & 5 deletions

File tree

‎splunklib/searchcommands/environment.py‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff 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+
123136
splunk_home = path.abspath(path.join(getcwd(), environ.get("SPLUNK_HOME", "")))
124137
app_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

127140
splunklib_logger, logging_configuration = configure_logging("splunklib")
128141

‎tests/unit/searchcommands/test_builtin_options.py‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff 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)

0 commit comments

Comments
 (0)