-
Notifications
You must be signed in to change notification settings - Fork 0
refactor(git): Enhance PR creation and commit handling #2
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
Changes from all commits
be374b9
00c0b3b
af97718
d1437d0
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 | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,19 +1,42 @@ | ||||||||||||||||||||
import subprocess | ||||||||||||||||||||
from query_ollama import query_ollama | ||||||||||||||||||||
from extract_bash import extract_bash_commands_no_line_split | ||||||||||||||||||||
from app.query_bedrock import query_bedrock | ||||||||||||||||||||
from app.extract_bash import extract_bash_commands_no_line_split | ||||||||||||||||||||
|
||||||||||||||||||||
def make_pull_request(): | ||||||||||||||||||||
def get_pr_diff(): | ||||||||||||||||||||
try: | ||||||||||||||||||||
diff = subprocess.check_output(["git", "diff", "origin/main...HEAD"]).decode() | ||||||||||||||||||||
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.
Improper Neutralization of Special Elements used in an OS CommandThe vulnerability, 'Improper Neutralization of Special Elements used in an OS Command', often referred to as 'OS command injection', occurs when an application fails to properly sanitize input that is passed to system commands. In the given code snippet, the application is executing a Git command using user-supplied input without validating or encoding it to ensure it is safe. This makes it possible for an attacker to inject malicious commands. How this could be exploited: Read more: ⚡ Here's how you might fix this potential vulnerability The modified code adds exception handling for the subprocess calls. If a subprocess call fails, the CalledProcessError exception is caught and an error message is printed. The function then returns None to indicate that an error occurred. This prevents the program from crashing if a subprocess call fails. The subprocess.run() calls now include the check=True argument, which causes an exception to be raised if the command returns a non-zero exit status. Please note that AI auto-fixes are currently experimental Add exception handling for subprocess calls
Suggested change
Add exception handling for subprocess calls try:
current_branch = subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"]).decode().strip()
except subprocess.CalledProcessError as e:
print(f"Error: {str(e)}")
return None Add exception handling for subprocess calls try:
subprocess.run(["git", "push", "--set-upstream", "origin", current_branch], check=True)
except subprocess.CalledProcessError as e:
print(f"Error: {str(e)}")
return None Add exception handling for subprocess calls try:
result = subprocess.run(["gh", "pr", "create", "--title", pr_title, "--body", pr_description], check=True)
except subprocess.CalledProcessError as e:
print(f"Error: {str(e)}")
return None Add exception handling for subprocess calls try:
result = subprocess.run(["gh", "pr", "edit", "--title", pr_title, "--body", pr_description], check=True)
except subprocess.CalledProcessError as e:
print(f"Error: {str(e)}")
return None Powered by nullify.ai Reply with |
||||||||||||||||||||
return diff | ||||||||||||||||||||
except subprocess.CalledProcessError: | ||||||||||||||||||||
print("Error: Unable to get the diff. Make sure you're in a git repository.") | ||||||||||||||||||||
return None | ||||||||||||||||||||
|
||||||||||||||||||||
def main(): | ||||||||||||||||||||
# Push the branch to remote | ||||||||||||||||||||
subprocess.run(["git", "push"]) | ||||||||||||||||||||
# Get the current branch name | ||||||||||||||||||||
current_branch = subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"]).decode().strip() | ||||||||||||||||||||
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.
Improper Neutralization of Special Elements used in an OS CommandThe vulnerability, 'Improper Neutralization of Special Elements used in an OS Command', occurs when user input or variable data isn't properly sanitized before being used in a system command. This can lead to executing unintended commands or passing invalid data to the system. In the provided code snippet, the output of a Git command is being fetched without ensuring that it doesn't contain malicious modifications. Although the current usage context in the snippet itself might seem benign, using unsanitized outputs further in the application can introduce serious security risks. How this could be exploited: Read more: Reply with 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.
Improper Neutralization of Special Elements used in an OS CommandThis code vulnerability, known as 'Improper Neutralization of Special Elements used in an OS Command', occurs when input data is not properly sanitized before being used in operating system commands. In this snippet, the code is using the Python 'subprocess' module to execute a 'git' command, which retrieves the current Git branch. However, if the output of the command includes special characters or unintended commands, it could potentially lead to the execution of arbitrary commands on the operating system. How this could be exploited: Read more: Reply with |
||||||||||||||||||||
|
||||||||||||||||||||
# Set the upstream to origin and push the current branch | ||||||||||||||||||||
subprocess.run(["git", "push", "--set-upstream", "origin", current_branch]) | ||||||||||||||||||||
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.
Improper Neutralization of Special Elements used in an OS CommandThe vulnerability 'Improper Neutralization of Special Elements used in an OS Command' occurs when an application does not properly sanitize input that is passed to the operating system. This can lead to command injection, where an attacker can control the command that is executed on the operating system. In the provided code snippet, the variable 'current_branch' is being used to dynamically create the command that is passed to the 'subprocess.run' method. If 'current_branch' comes from an unreliable source, or if it's manipulable by an external user without proper validation and sanitization, it could lead to a security risk. How this could be exploited: Read more: Reply with 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.
Improper Neutralization of Special Elements used in an OS CommandThe vulnerability 'Improper Neutralization of Special Elements used in an OS Command' arises when input data isn't correctly sanitized before being passed to an operating system command. In simpler terms, if the data that a user or another part of the application provides contains special characters or command operators, these might be interpreted by the operating system as instructions, rather than plain data. In the provided code snippet, the variable 'current_branch' is included directly in a system command that utilizes the 'subprocess.run' function to execute a 'git push' command. If 'current_branch' contains any special characters or unintended command sequences, these will be executed by the shell, leading to potential malicious operations. How this could be exploited: Read more: Reply with |
||||||||||||||||||||
|
||||||||||||||||||||
pr_diff = get_pr_diff() | ||||||||||||||||||||
if not pr_diff: | ||||||||||||||||||||
return | ||||||||||||||||||||
|
||||||||||||||||||||
# Generate PR description using Ollama | ||||||||||||||||||||
# Generate PR title and description using Ollama | ||||||||||||||||||||
prompt_title = "Generate a concise and informative pull request title for a feature branch." | ||||||||||||||||||||
pr_title = query_ollama(prompt_title) | ||||||||||||||||||||
# Generate PR title and description using Bedrock | ||||||||||||||||||||
prompt_title = f"""Generate a concise and informative pull request title based on the following diff: | ||||||||||||||||||||
|
||||||||||||||||||||
{pr_diff} | ||||||||||||||||||||
|
||||||||||||||||||||
Respond with only the title, enclosed in triple backticks (```). For example: | ||||||||||||||||||||
``` | ||||||||||||||||||||
feat(user-auth): Implement JWT-based authentication | ||||||||||||||||||||
``` | ||||||||||||||||||||
""" | ||||||||||||||||||||
pr_title = query_bedrock(prompt_title) | ||||||||||||||||||||
pr_title = extract_bash_commands_no_line_split(pr_title)[0] | ||||||||||||||||||||
|
||||||||||||||||||||
prompt_body = "Generate a concise and informative pull request description for a feature branch. Include key changes and their impact." | ||||||||||||||||||||
pr_description = query_ollama(prompt_body) | ||||||||||||||||||||
prompt_body = f"Generate a concise and informative pull request description based on the following diff. Include key changes and their impact:\n\n{pr_diff}" | ||||||||||||||||||||
pr_description = query_bedrock(prompt_body) | ||||||||||||||||||||
pr_description = extract_bash_commands_no_line_split(pr_description)[0] | ||||||||||||||||||||
|
||||||||||||||||||||
# Create pull request using GitHub CLI | ||||||||||||||||||||
|
@@ -30,7 +53,15 @@ def make_pull_request(): | |||||||||||||||||||
return | ||||||||||||||||||||
|
||||||||||||||||||||
# If gh is installed, proceed with creating the pull request | ||||||||||||||||||||
subprocess.run(["gh", "pr", "create", "--title", pr_title, "--body", pr_description]) | ||||||||||||||||||||
|
||||||||||||||||||||
result = subprocess.run(["gh", "pr", "create", "--title", pr_title, "--body", pr_description]) | ||||||||||||||||||||
j2nullify marked this conversation as resolved.
Show resolved
Hide resolved
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.
Improper Neutralization of Special Elements used in an OS CommandThe vulnerability identified is 'Improper Neutralization of Special Elements used in an OS Command,' often referred to as command injection. This occurs when input data containing special characters or commands is fed directly into system-level command execution without proper sanitization. In the provided code example, variables 'pr_title' and 'pr_description' are included directly in a command that creates a pull request via the GitHub CLI ('gh'). If these variables are taken from user inputs and not properly sanitized, they may include malicious commands that could be executed on the operating system. How this could be exploited: Read more: Reply with |
||||||||||||||||||||
if result.returncode != 0: | ||||||||||||||||||||
result = subprocess.run(["gh", "pr", "edit", "--title", pr_title, "--body", pr_description]) | ||||||||||||||||||||
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.
Improper Neutralization of Special Elements used in an OS CommandThe vulnerability identified, known as 'Improper Neutralization of Special Elements used in an OS Command' or 'Command Injection', occurs when user-supplied input (variables like pr_title or pr_description) is not properly sanitized before being included in operating system commands. Here, the code directly incorporates inputs pr_title and pr_description into the OS command to edit a pull request. If these inputs aren't properly validated or sanitized, they could be crafted to execute unintended additional commands alongside the intended ones. How this could be exploited: Read more: Reply with
Comment on lines
+57
to
+59
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.
Improper Neutralization of Special Elements used in an OS CommandThe identified vulnerability 'Improper Neutralization of Special Elements used in an OS Command' occurs when input data isn't properly sanitized before being used in an operating system command. This can allow attackers to inject unintended commands to be executed by the system. In the provided code, 'pr_title' and 'pr_description' are included as parameters to a command line operation without ensuring they are safe to employ. This implies that if the pull request title or description contains malicious command sequences, they might be executed. How this could be exploited: Read more: ⚡ Here's how you might fix this potential vulnerability The modified code mitigates this vulnerability by escaping any special characters in the pr_title and pr_description using the shlex.quote function. This ensures that these inputs are treated as literal strings by the shell and not interpreted as part of the command. Please note that AI auto-fixes are currently experimental Escape special characters in pr_title and pr_description
Suggested change
Powered by nullify.ai Reply with |
||||||||||||||||||||
|
||||||||||||||||||||
if result.returncode == 0: | ||||||||||||||||||||
print("✅ Pull request created successfully!") | ||||||||||||||||||||
else: | ||||||||||||||||||||
print("❌ Failed to create pull request. Please try again.") | ||||||||||||||||||||
|
||||||||||||||||||||
if __name__ == "__main__": | ||||||||||||||||||||
make_pull_request() | ||||||||||||||||||||
main() |
Uh oh!
There was an error while loading. Please reload this page.