-
Notifications
You must be signed in to change notification settings - Fork 21
Feature/improved dangerous command safeguard #47
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
Feature/improved dangerous command safeguard #47
Conversation
Added manual command re-entry as a second-layer safety check for destructive commands like 'rm', 'delete', and 'shutdown'. Now, after initial confirmation, users must re-type the command exactly before execution.
- Implement manual reentry safeguard for dangerous commands - Remove CONFIRM prefix handling - Improve dangerous command detection patterns - Add better error handling and user feedback - Ensure proper validation before executing destructive commands
|
Hi @Kirti-Rathi , I've implemented all the necessary changes. The manual safeguard is now enforced after the user confirms execution, and before the actual execution of any dangerous command. It now includes an additional verification step requiring the user to re-type the exact command as a second-layer check, as requested. You can test this by triggering a dangerous command like: cpp Initial confirmation (yes/no) Exact re-entry of the command The changes have been made in ai_terminal_assistant.py, where the execution flow is handled. Also, I’ve made sure my branch is up-to-date with main. Please review it when you get a chance. Let me know if any further adjustments are needed! Thanks! *also could you please guide me to resolve through the the conflicts in the code, if possible. |
Kirti-Rathi
left a comment
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’ve added a detailed review, but I’d strongly recommend closing this PR and the associated branches. Please create a fresh branch and apply only the necessary modifications, as done in PR #10 and refer to it for the scope and structure of changes expected.
Also, make sure to your fork is up-to-date with main.
This PR includes a number of unnecessary changes, which made reviewing a fairly straightforward issue more time consuming than needed.
| def get_current_os(): | ||
| return platform.system() | ||
|
|
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.
This function is already defined in format_utils.py. Remove this
| from .node import Node | ||
| from .data_gatherer import DataGatherer | ||
| from .format_utils import format_text, reset_format, get_current_os, get_os_specific_examples | ||
| from .format_utils import format_text, reset_format |
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.
| from .format_utils import format_text, reset_format | |
| from .format_utils import format_text, reset_format, get_current_os, get_os_specific_examples |
Keep it as it was, importing functions from format_utils only
| return "" | ||
| if user_input.startswith('!'): | ||
| return self.run_direct_command(user_input[1:]) | ||
| additional_data = self.gather_additional_data(user_input) |
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.
Keep this line, it's a required arguement in command_executor
| Current OS: {get_current_os()} | ||
| Current OS specific examples: {get_os_specific_examples()} | ||
| Current OS: {platform.system()} |
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.
Don't change these, the functions are well-defined in format_utils and are required.
Keep it as it was
| Translate the user input into a SINGLE shell command according to the operating system. | ||
| Translate the user input into a SINGLE shell command. |
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.
No changes required here as well. keep the original line.
| """, additional_data=additional_data).strip() | ||
| """, additional_data=self.gather_additional_data(user_input)).strip() |
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.
Use the additional data variable defined above only
""", additional_data=additional_data).strip()
| if self.is_dangerous_command(command): | ||
| print(format_text('yellow', bold=True) + | ||
| f"\n⚠️ Warning: The command '{command}' may be destructive." + | ||
| reset_format()) |
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.
Please don't change the identification method of dangerous commads.
The api response prefixed with "CONFIRM: " gives consistent results and has been found to be more accurate.
Some destructive commands escapes the regex method used in is_dangerous_command function, which causes inconsistency in behaviour and faulty deletions.
| choice = questionary.confirm(f"Do you want to run the command '{command}'?").ask() | ||
| if choice: | ||
| if command.startswith("CONFIRM:"): | ||
| confirmation = questionary.confirm(f"Warning: This command may be destructive. Are you sure you want to run '{command[8:]}'?").ask() | ||
| if not confirmation: | ||
| return format_text('red') + "Command execution aborted." + reset_format() | ||
| command = command[8:] | ||
| formatted_command = format_text('cyan') + f"Command: {command}" + reset_format() | ||
| print(formatted_command) | ||
| self.command_history.append(command) | ||
| if len(self.command_history) > 10: | ||
| self.command_history.pop(0) | ||
| if command.startswith("cd "): | ||
| path = command.split(" ", 1)[1] | ||
| os.chdir(os.path.expanduser(path)) | ||
| result = f"Changed directory to {os.getcwd()}" | ||
| exit_code = 0 | ||
| else: | ||
| stdout, stderr, exit_code = self.execute_command_with_live_output(command) | ||
| result = "" | ||
| if exit_code != 0: | ||
| debug_suggestion = self.debug_error(command, stderr, exit_code) | ||
| result += format_text('yellow') + f"\n\nDebugging Suggestion:\n{debug_suggestion}" + reset_format() | ||
| return result.strip() | ||
| else: | ||
| print(format_text('red') + "Command cancelled!" + reset_format()) | ||
| return "" |
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.
Please don't change the logic here
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.
Thank you so much for the clarification. I’ll get back to you as soon as possible with the necessary corrections. @Kirti-Rathi
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.
Feel free to reach out in case you've doubts
Hey @Kirti-Rathi ,
I've implemented all the required changes as discussed:
✅ The manual safeguard now includes an additional confirmation step:
After the user confirms execution of a dangerous command, they’re also required to retype the exact command as a second-layer verification.
✅ This ensures the user’s input doesn't rely on simply typing CONFIRM, and adds stronger protection before execution.
✅ Try running a dangerous command like: delete spare_file.txt — you’ll now see both confirmation prompts working as intended.
✅ The main logic for this is updated in ai_terminal_assistant.py.
🔄 Also made sure my branch is up-to-date with main.
Let me know if anything else needs refining!