Skip to content

Commit f7406de

Browse files
authored
[AI-6752] Sanitize guarddog_path argument (#23377)
* Fix guarddog_path argument injection in command execution Signed-off-by: sarah-witt <sarah.witt@datadoghq.com> * remove whitespace check and add changelog Signed-off-by: sarah-witt <sarah.witt@datadoghq.com> * fix style Signed-off-by: sarah-witt <sarah.witt@datadoghq.com> * remove unused constant Signed-off-by: sarah-witt <sarah.witt@datadoghq.com> * Update guarddog/changelog.d/23377.fixed Signed-off-by: sarah-witt <sarah.witt@datadoghq.com> --------- Signed-off-by: sarah-witt <sarah.witt@datadoghq.com>
1 parent 8145863 commit f7406de

4 files changed

Lines changed: 7 additions & 11 deletions

File tree

guarddog/changelog.d/23377.fixed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Sanitize guarddog_path argument

guarddog/datadog_checks/guarddog/check.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
from datadog_checks.base import AgentCheck, ConfigurationError
99
from datadog_checks.base.utils.time import get_current_datetime, get_timestamp
1010

11-
from . import constants
12-
1311

1412
class GuarddogCheck(AgentCheck):
1513
__NAMESPACE__ = "guarddog"
@@ -27,10 +25,10 @@ def __init__(self, name, init_config, instances):
2725
self.guarddog_path = str(init_config.get('guarddog_path')).strip()
2826
self.check_initializations.append(self.validate_config)
2927

30-
def get_guarddog_output(self, cmd_with_abs_path) -> subprocess.CompletedProcess:
28+
def get_guarddog_output(self, command_parts) -> subprocess.CompletedProcess:
3129
try:
32-
self.log.debug("Running command: %s", cmd_with_abs_path)
33-
cmd_output_with_abs_path = subprocess.run(cmd_with_abs_path.split(), capture_output=True, text=True)
30+
self.log.debug("Running command: %s", command_parts)
31+
cmd_output_with_abs_path = subprocess.run(command_parts, capture_output=True, text=True)
3432
return cmd_output_with_abs_path
3533
except FileNotFoundError as cmd_error:
3634
err_message = "GuardDog is not found at configured path."
@@ -70,11 +68,9 @@ def validate_config(self) -> None:
7068
def check(self, _):
7169
try:
7270
current_time = get_current_datetime()
73-
guarddog_command = constants.GUARDDOG_COMMAND.format(
74-
package_ecosystem=self.package_ecosystem,
75-
path=self.path,
71+
cmd_result = self.get_guarddog_output(
72+
[self.guarddog_path, self.package_ecosystem, "verify", self.path, "--output-format=json"]
7673
)
77-
cmd_result = self.get_guarddog_output(self.guarddog_path + " " + guarddog_command)
7874
if cmd_result.returncode != 0:
7975
cmd_result_err_message = f"GuardDog command failed: {cmd_result.stderr}"
8076
self.log.error(cmd_result_err_message)

guarddog/datadog_checks/guarddog/constants.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@
33
# Licensed under a 3-clause BSD style license (see LICENSE)
44
VALID_ECOSYSTEMS = ["pypi", "npm", "go", "github_action"]
55
DATE_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
6-
GUARDDOG_COMMAND = "{package_ecosystem} verify {path} --output-format=json"

guarddog/tests/test_unit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def test_get_guarddog_output(config, instance, mocker):
166166
expected_returncode = 0
167167
check.package_ecosystem = "pypi"
168168
check.path = dependency_file_path
169-
cmd = "guarddog pypi verify /tmp/dependency_file_path/requirements.txt --output-format=json"
169+
cmd = ["guarddog", "pypi", "verify", "/tmp/dependency_file_path/requirements.txt", "--output-format=json"]
170170

171171
mock_completed_process = Mock(stdout=expected_stdout, stderr=expected_stderr, returncode=expected_returncode)
172172

0 commit comments

Comments
 (0)