Skip to content

Commit 2795a2b

Browse files
update (#9380)
* prevent file reads outside of the template clone path * black and bandit * flake - spaces before inline comment * re-black on bandit exclusion * black 22.3.0
1 parent b746782 commit 2795a2b

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

consoleme/lib/scm/git/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ async def clone(self, no_checkout=True, depth: Optional[int] = None):
2727
await sync_to_async(git.Git(self.tempdir).clone)(*args, **kwargs)
2828
self.repo = git.Repo(os.path.join(self.tempdir, self.repo_name))
2929
self.repo.config_writer().set_value("user", "name", "ConsoleMe").release()
30+
self.repo.config_writer().set_value("core", "symlinks", "false").release()
3031
if self.git_email:
3132
self.repo.config_writer().set_value(
3233
"user", "email", self.git_email

consoleme/lib/templated_resources/requests.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import io
22
import json
3+
import os
4+
import random
5+
import string
36
import time
47

58
from ruamel.yaml.comments import CommentedSeq
@@ -30,7 +33,10 @@ async def generate_honeybee_request_from_change_model_array(
3033
repositories_for_request = {}
3134
primary_principal = None
3235
t = int(time.time())
33-
generated_branch_name = f"{user}-{t}"
36+
suffix = "".join(
37+
random.choices(string.ascii_lowercase + string.digits, k=10) # nosec
38+
)
39+
generated_branch_name = f"{user}-{t}-{suffix}"
3440
policy_name = config.get(
3541
"generate_honeybee_request_from_change_model_array.policy_name",
3642
"self_service_generated",
@@ -78,10 +84,26 @@ async def generate_honeybee_request_from_change_model_array(
7884
main_branch_name = repositories_for_request[change.principal.repository_name][
7985
"main_branch_name"
8086
]
81-
git_client.checkout(
82-
f"origin/{main_branch_name}", change.principal.resource_identifier
87+
88+
change_file_path = os.path.abspath(
89+
f"{repo.working_dir}/{change.principal.resource_identifier}"
8390
)
84-
change_file_path = f"{repo.working_dir}/{change.principal.resource_identifier}"
91+
clone_wd_path = os.path.abspath(repo.working_dir)
92+
if os.path.commonprefix((clone_wd_path, change_file_path)) != clone_wd_path:
93+
log.exception(
94+
f"User attempted to reference a file outside of the repository: {change_file_path} is not within {clone_wd_path}"
95+
)
96+
raise ValueError("Unable to raise change request for this resource")
97+
98+
try:
99+
git_client.checkout(
100+
f"origin/{main_branch_name}", "--", change.principal.resource_identifier
101+
)
102+
except Exception:
103+
log.exception(
104+
f"Unable to checkout {main_branch_name} for {change.principal.resource_identifier}"
105+
)
106+
raise ValueError("Unable to raise change request for this resource")
85107
with open(change_file_path, "r") as f:
86108
yaml_content = yaml.load(f)
87109

tests/handlers/v2/test_requests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,7 @@ def test_post_honeybee_request_dry_run(self, mock_git, mock_repo):
718718
- '*'
719719
Sid: admin"""
720720
with patch("builtins.open", mock_open(read_data=template_data)):
721+
mock_repo.return_value.working_dir = "/tmp"
721722
response = self.fetch(
722723
"/api/v2/request",
723724
method="POST",

0 commit comments

Comments
 (0)