Skip to content
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

feat: whitelist some commands #190

Merged
merged 1 commit into from
Oct 23, 2024
Merged

Conversation

0xbrayo
Copy link
Collaborator

@0xbrayo 0xbrayo commented Oct 9, 2024

Important

Adds a whitelist for shell commands in execute_shell() to restrict execution and require confirmation for non-whitelisted commands.

  • Security:
    • Adds a whitelist for shell commands in execute_shell() to restrict execution to ls, stat, cd, cat, pwd, echo.
    • Uses regex to parse commands and checks against the whitelist.
    • Requires user confirmation for non-whitelisted commands if ask is true.
  • Misc:
    • Updates message formatting in execute_shell() to indicate if a command is whitelisted.

This description was created by Ellipsis for c3fe334. It will automatically update as commits are pushed.

@0xbrayo
Copy link
Collaborator Author

0xbrayo commented Oct 9, 2024

@ErikBjare Do I need to configure something on my end to fix these tests? I see one needs a OPENAI_KEY. They seem to be working on the other PRs though.

@ErikBjare
Copy link
Owner

ErikBjare commented Oct 10, 2024

Ah yes, since the keys are secret you don't have access to them when you trigger the job. Not sure what's the easiest way to work around this.

The logs contain Secret source: None

Weird that it worked for other PRs? It kinda shouldn't?

@ErikBjare
Copy link
Owner

@ErikBjare
Copy link
Owner

Anyone with collaborator access to this repository can use these secrets and variables for actions. They are not passed to workflows that are triggered by a pull request from a fork.

I added you as a collaborator (invite sent), but you might have to set the secrets in your forked repo.

@0xbrayo
Copy link
Collaborator Author

0xbrayo commented Oct 10, 2024

@ErikBjare Do I need to configure something on my end to fix these tests? I see one needs a OPENAI_KEY. They seem to be working on the other PRs though.

Sorry I meant it seems to be working on your PRs

@0xbrayo 0xbrayo marked this pull request as ready for review October 19, 2024 19:44
Copy link
Contributor

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Looks good to me! Reviewed everything up to 5442e46 in 14 seconds

More details
  • Looked at 37 lines of code in 1 files
  • Skipped 0 files when reviewing.
  • Skipped posting 1 drafted comments based on config settings.
1. gptme/tools/shell.py:280
  • Draft comment:
    There's a typo in the f-string formatting. The empty string should be replaced with double quotes to avoid syntax errors.
        msg = _format_block_smart(f'Ran {"whitelisted" if is_safe_cmd else ""} command', cmd, lang="bash") + "\n\n"
  • Reason this comment was not posted:
    Comment looked like it was already resolved.

Workflow ID: wflow_3KqKfOotNVh1Z3LB


You can customize Ellipsis with 👍 / 👎 feedback, review rules, user-specific overrides, quiet mode, and more.

@0xbrayo 0xbrayo force-pushed the whitelist branch 4 times, most recently from 2ae077c to c73de36 Compare October 20, 2024 18:05
@0xbrayo
Copy link
Collaborator Author

0xbrayo commented Oct 20, 2024

@ErikBjare I set the both openai and anthropic api keys in my repo secrets. But the errors are still there :(

@0xbrayo
Copy link
Collaborator Author

0xbrayo commented Oct 21, 2024

@ellipsis-dev review

Copy link
Contributor

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Looks good to me! Reviewed everything up to c3fe334 in 15 seconds

More details
  • Looked at 45 lines of code in 1 files
  • Skipped 0 files when reviewing.
  • Skipped posting 1 drafted comments based on config settings.
1. gptme/tools/shell.py:254
  • Draft comment:
    The regex used for command parsing does not account for control flow statements like if, for, while, etc. This could lead to security issues if such commands are executed without confirmation. Consider enhancing the regex or adding additional checks to handle these cases.
  • Reason this comment was not posted:
    Decided after close inspection that this draft comment was likely wrong and/or not actionable:
    The comment is relevant to the changes made in the diff, specifically the introduction of a regex to parse commands. The concern about control flow statements is valid, as they can introduce security risks if not handled properly. The comment provides a clear suggestion to enhance the regex or add checks, which is actionable.
    The comment assumes that control flow statements are a security risk without confirmation. It might be speculative unless there's evidence that these statements are indeed a risk in this context.
    Control flow statements can indeed be a security risk if they allow execution of unintended commands. The comment is providing a precautionary measure, which is reasonable given the context of executing shell commands.
    The comment is relevant and provides a valid concern about potential security issues with control flow statements. It suggests an actionable improvement to the code.

Workflow ID: wflow_5aYWERHs8G57XyS2


You can customize Ellipsis with 👍 / 👎 feedback, review rules, user-specific overrides, quiet mode, and more.

@0xbrayo
Copy link
Collaborator Author

0xbrayo commented Oct 21, 2024

@ErikBjare I think this is ready, do I merge? The list of whitelisted commands could be extended but that can be done later.

@0xbrayo 0xbrayo merged commit 7f1ba2b into ErikBjare:master Oct 23, 2024
4 of 7 checks passed
@ErikBjare
Copy link
Owner

Sure, but ask for a review first next time :)

yield Message("system", "Command not run")
return
#NOTE: This does not handle control flow words like if, for, while.
regex = r"(?:^|[|&;]|\|\||&&|\n)\s*([^\s|&;]+)"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested the regex extensively but I guess it's the most unpredictable bit here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +253 to +256
#NOTE: This does not handle control flow words like if, for, while.
regex = r"(?:^|[|&;]|\|\||&&|\n)\s*([^\s|&;]+)"

for match in re.finditer(regex, cmd):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should compile this regex

@0xbrayo 0xbrayo mentioned this pull request Oct 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants