-
Notifications
You must be signed in to change notification settings - Fork 82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: shell true semgrep #760
Changes from 12 commits
4cdc02b
90ca669
91e3e1e
ad3b590
178c076
bc88a09
a8a21f1
8577639
83c3e9e
d131f01
1ddb04a
0733b91
0b808b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import re | ||
|
||
|
||
class CommandSanitizer: | ||
""" | ||
Takes a list of arguments and verifies that each argument is alphanumeric or "-" or "_" and of type string | ||
Trows a TypeError if any of these conditions is violated. | ||
""" | ||
def __init__(self, args) -> None: | ||
if not args: | ||
raise TypeError("Arguments cannot be empty") | ||
|
||
for arg in args: | ||
if not isinstance(arg, str): | ||
raise TypeError(f"arguments must be strings, not {type(arg)} of {str(arg)}") | ||
if re.search(r"[^a-zA-Z0-9-_]", arg): | ||
raise TypeError(f"argument contains invalid characters: {arg}") | ||
|
||
self._arguments = args | ||
|
||
@property | ||
def arguments(self): | ||
return self._arguments |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
|
||
from dataall.base import db | ||
from dataall.base.aws.sts import SessionHelper | ||
from dataall.base.utils.shell_utils import CommandSanitizer | ||
from dataall.core.environment.services.environment_service import EnvironmentService | ||
from dataall.modules.datapipelines.db.datapipelines_repositories import DatapipelinesRepository | ||
|
||
|
@@ -71,6 +72,10 @@ def __init__(self, target_uri): | |
'COMMITID=$(aws codecommit get-branch --repository-name ${REPO_NAME} --branch-name main --query branch.commitId --output text)', | ||
'aws codecommit put-file --repository-name ${REPO_NAME} --branch-name main --file-content file://app.py --file-path app.py --parent-commit-id ${COMMITID} --cli-binary-format raw-in-base64-out', | ||
] | ||
CommandSanitizer(args=[self.pipeline.repo]) | ||
|
||
# This command is too complex to be executed as a list of commands. We need to run it with shell=True | ||
# However, the input arguments have be sanitized with the CommandSanitizer | ||
|
||
process = subprocess.run( # nosemgrep | ||
"; ".join(update_cmds), # nosemgrep | ||
|
@@ -99,6 +104,11 @@ def initialize_repo(self): | |
|
||
logger.info(f"Running Commands: {'; '.join(cmd_init)}") | ||
|
||
CommandSanitizer(args=[self.pipeline.repo, self.pipeline.SamlGroupName]) | ||
|
||
# This command is too complex to be executed as a list of commands. We need to run it with shell=True | ||
# However, the input arguments have be sanitized with the CommandSanitizer | ||
|
||
process = subprocess.run( # nosemgrep | ||
'; '.join(cmd_init), # nosemgrep | ||
text=True, # nosemgrep | ||
|
@@ -201,6 +211,9 @@ def git_push_repo(self): | |
|
||
logger.info(f"Running Commands: {'; '.join(git_cmds)}") | ||
|
||
# This command does not include any customer upstream input | ||
# no sanitization is needed and shell=true does not impose a risk | ||
|
||
process = subprocess.run( # nosemgrep | ||
'; '.join(git_cmds), # nosemgrep | ||
text=True, # nosemgrep | ||
|
@@ -215,23 +228,17 @@ def git_push_repo(self): | |
@staticmethod | ||
def clean_up_repo(path): | ||
if path: | ||
precmd = [ | ||
'deactivate;', | ||
'rm', | ||
'-rf', | ||
f"{path}" | ||
] | ||
|
||
cmd = ['rm', '-rf', f"{path}"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tested in AWS and working fine from my end |
||
cwd = os.path.dirname(os.path.abspath(__file__)) | ||
logger.info(f"Running command : \n {' '.join(precmd)}") | ||
|
||
process = subprocess.run( # nosemgrep | ||
' '.join(precmd), # nosemgrep | ||
text=True, # nosemgrep | ||
shell=True, # nosec # nosemgrep | ||
encoding='utf-8', # nosemgrep | ||
capture_output=True, # nosemgrep | ||
cwd=cwd # nosemgrep | ||
logger.info(f"Running command : \n {' '.join(cmd)}") | ||
|
||
process = subprocess.run( | ||
cmd, | ||
text=True, | ||
shell=False, | ||
encoding='utf-8', | ||
capture_output=True, | ||
cwd=cwd | ||
) | ||
|
||
if process.returncode == 0: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am working on this item, after plotting the error logs I think the issue is on the url to get the ECS-EC2 credentials. I will use f-string to create the curl command since we can obtain it as environment variable. I am now testing it in AWS
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@noah-paige I tested in AWS and this way of defining the commands works :)