Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ You can run `interpreter -y` or set `interpreter.auto_run = True` to bypass this
- Watch Open Interpreter like a self-driving car, and be prepared to end the process by closing your terminal.
- Consider running Open Interpreter in a restricted environment like Google Colab or Replit. These environments are more isolated, reducing the risks of executing arbitrary code.

There is **experimental** support for a [safe mode](./docs/SAFE_MODE.md) to help mitigate some risks.

## How Does it Work?

Open Interpreter equips a [function-calling language model](https://platform.openai.com/docs/guides/gpt/function-calling) with an `exec()` function, which accepts a `language` (like "Python" or "JavaScript") and `code` to run.
Expand Down
61 changes: 61 additions & 0 deletions docs/SAFE_MODE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Safe Mode

**⚠️ Safe mode is experimental and does not provide any guarantees of safety or security.**

Open Interpreter is working on providing an experimental safety toolkit to help you feel more confident running the code generated by Open Interpreter.


Install Open Interpreter with the safety toolkit dependencies as part of the bundle:

```shell
pip install open-interpreter[safe]
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We can leverage the tool.poetry.extras configuration to allow folks to optionally install semgrep and other safety tools via the [safe] extra.

Copy link
Collaborator

Choose a reason for hiding this comment

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

YES. I love this idea.

I think it would be great to make a slimmed down version of OI called [core] (wouldn't even include rich or any terminal interface), [safe] for semgrep, perhaps docker, etc, then eventually [all] if we build features beyond safe mode with dependencies.

```

Alternatively, you can install the safety toolkit dependencies separately in your virtual environment:

```shell
pip install semgrep
```

## Features

- **No Auto Run**: Safe mode disables the ability to automatically execute code
- **Code Scanning**: Scan generated code for vulnerabilities with [`semgrep`](https://semgrep.dev/)

## Enabling Safe Mode

You can enable safe mode by passing the `--safe` flag when invoking `interpreter` or by configuring `safe_mode` in your [config file](https://github.com/KillianLucas/open-interpreter#configuration).

The safe mode setting has three options:

- `off`: disables the safety toolkit (_default_)
- `ask`: prompts you to confirm that you want to scan code
- `auto`: automatically scans code

### Example Config:

```yaml
model: gpt-4
temperature: 0
debug_mode: false
safe_mode: ask
```

## Roadmap

Some upcoming features that enable even more safety:

- [Execute code in containers](https://github.com/KillianLucas/open-interpreter/pull/459)

## Tips & Tricks

You can adjust the `system_message` in your [config file](https://github.com/KillianLucas/open-interpreter#configuration) to include instructions for the model to scan packages with [`guarddog`]() before installing them.

```yaml
model: gpt-4
debug_mode: false
safe_mode: ask
system_message: |
# normal system message here
BEFORE INSTALLING ANY PACKAGES WITH pip OR npm YOU MUST SCAN THEM WITH `guarddog` FIRST. Run `guarddog pypi scan $package` for pip packages and `guarddog npm scan $package` for npm packages. `guarddog` only accepts one package name at a time.
```
6 changes: 4 additions & 2 deletions interpreter/terminal_interface/terminal_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from ..utils.display_markdown_message import display_markdown_message
from ..utils.truncate_output import truncate_output
from ..utils.scan_code import scan_code
from ..utils.check_for_package import check_for_package


def terminal_interface(interpreter, message):
Expand All @@ -17,8 +18,9 @@ def terminal_interface(interpreter, message):
"**Open Interpreter** will require approval before running code."
]

if interpreter.safe_mode != "off":
interpreter_intro_message.append(f"**Safe Mode**: {interpreter.safe_mode}\n\n>Note: **Safe Mode** requires `semgrep` (`pip install semgrep`)")
if interpreter.safe_mode == "ask" or interpreter.safe_mode == "auto":
if not check_for_package("semgrep"):
interpreter_intro_message.append(f"**Safe Mode**: {interpreter.safe_mode}\n\n>Note: **Safe Mode** requires `semgrep` (`pip install semgrep`)")
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Now we only need to show the semgrep message if it isn't already in the user's current environment. If semgrep is available and safe_mode is enabled, semgrep will be automatically loaded.

else:
interpreter_intro_message.append(
"Use `interpreter -y` to bypass this."
Expand Down
19 changes: 19 additions & 0 deletions interpreter/utils/check_for_package.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import importlib.util
import sys

#borrowed from: https://stackoverflow.com/a/1051266/656011
def check_for_package(package):
if package in sys.modules:
return True
elif (spec := importlib.util.find_spec(package)) is not None:
try:
module = importlib.util.module_from_spec(spec)

sys.modules[package] = module
spec.loader.exec_module(module)

return True
except ImportError:
return False
else:
return False
5 changes: 4 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,6 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry.scripts]
interpreter = "interpreter:cli"

[tool.poetry.extras]
safe = ["semgrep"]