-
Notifications
You must be signed in to change notification settings - Fork 3.2k
added some regex based checks for dangerous commands #38
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import re | ||
|
|
||
|
|
||
| def is_dangerous_command(command: str) -> bool: | ||
| """ | ||
| Check if the command matches any dangerous patterns. | ||
|
|
||
| Args: | ||
| command (str): The shell command to check. | ||
|
|
||
| Returns: | ||
| bool: True if the command is dangerous, False otherwise. | ||
| """ | ||
| dangerous_patterns = [ | ||
| r"\brm\b", # rm command | ||
| r"\bgit\s+push\b", # git push command | ||
| r"\bsudo\b", # sudo command | ||
| # Add more dangerous command patterns here | ||
| r"\bmv\b", # mv command | ||
| r"\bchmod\b", # chmod command | ||
| r"\bchown\b", # chown command | ||
| r"\bmkfs\b", # mkfs command | ||
| r"\bsystemctl\b", # systemctl command | ||
| r"\breboot\b", # reboot command | ||
| r"\bshutdown\b", # shutdown command | ||
| # Manipulating files in ~/ directly or dot files | ||
| r"^~/[^/]+$", # Files directly in home directory | ||
| r"/\.[^/]+$", # Dot files | ||
| ] | ||
| for pattern in dangerous_patterns: | ||
| if re.search(pattern, command): | ||
| return True | ||
| return False | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import pytest | ||
| from goose.utils.check_shell_command import is_dangerous_command | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "command", | ||
| [ | ||
| "rm -rf /", | ||
| "git push origin master", | ||
| "sudo reboot", | ||
| "mv /etc/passwd /tmp/", | ||
| "chmod 777 /etc/passwd", | ||
| "chown root:root /etc/passwd", | ||
| "mkfs -t ext4 /dev/sda1", | ||
| "systemctl stop nginx", | ||
| "reboot", | ||
| "shutdown now", | ||
| "echo hello > ~/.bashrc", | ||
| ], | ||
| ) | ||
| def test_dangerous_commands(command): | ||
| assert is_dangerous_command(command) | ||
|
Collaborator
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. nit: probably want to assert some of the inverse, with safe commands too
Collaborator
Author
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. that's what the second test does. |
||
|
|
||
|
|
||
| @pytest.mark.parametrize("command", ["ls -la", 'echo "Hello World"', "cp ~/folder/file.txt /tmp/"]) | ||
| def test_safe_commands(command): | ||
| assert not is_dangerous_command(command) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit: I think
rmon a single file might be fine, but I am scared ofrm -r- especially combined with the file path checks below. Wdyt?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.
better safe than sorry.
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.
A bit late, but I think it'd be nice to add some user flexibility here, possibly via config setting somewhere. eg: one might also not want to allow
curl,http, etc.