Skip to content

Commit 5886882

Browse files
Merge pull request #46 from plasma-umass/pr46
Add --unsafe mode, add barebone safe mode to LLDB/GDB.
2 parents 34fd7e3 + 6554aa1 commit 5886882

File tree

5 files changed

+53
-6
lines changed

5 files changed

+53
-6
lines changed

src/chatdbg/chatdbg_gdb.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
_SkippedFramesEntry,
1414
)
1515
from chatdbg.util.config import chatdbg_config
16+
from chatdbg.native_util.safety import command_is_safe
1617

1718
# The file produced by the panic handler if the Rust program is using the chatdbg crate.
1819
RUST_PANIC_LOG_FILENAME = "panic_log.txt"
@@ -262,7 +263,7 @@ def _prompt_stack(self):
262263
"""
263264
return None
264265

265-
def llm_debug(self, command: str) -> str:
266+
def llm_debug(self, command: str):
266267
"""
267268
{
268269
"name": "debug",
@@ -279,4 +280,6 @@ def llm_debug(self, command: str) -> str:
279280
}
280281
}
281282
"""
283+
if not chatdbg_config.unsafe and not command_is_safe(command):
284+
return command, f"Command `{command}` is not allowed."
282285
return command, self._run_one_command(command)

src/chatdbg/chatdbg_lldb.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
_SkippedFramesEntry,
1414
)
1515
from chatdbg.util.config import chatdbg_config
16+
from chatdbg.native_util.safety import command_is_safe
1617

1718
# The file produced by the panic handler if the Rust program is using the chatdbg crate.
1819
RUST_PANIC_LOG_FILENAME = "panic_log.txt"
@@ -290,7 +291,7 @@ def _prompt_stack(self):
290291
"""
291292
return None
292293

293-
def llm_debug(self, command: str) -> str:
294+
def llm_debug(self, command: str):
294295
"""
295296
{
296297
"name": "debug",
@@ -307,4 +308,6 @@ def llm_debug(self, command: str) -> str:
307308
}
308309
}
309310
"""
311+
if not chatdbg_config.unsafe and not command_is_safe(command):
312+
return command, f"Command `{command}` is not allowed."
310313
return command, self._run_one_command(command)

src/chatdbg/chatdbg_pdb.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,10 @@ def _getval(self, arg):
285285
Sandbox for evaluating expressions from the LLM.
286286
"""
287287
try:
288-
return sandbox_eval(arg, self.curframe.f_globals, self.curframe_locals)
288+
if chatdbg_config.unsafe:
289+
return super._getval(arg)
290+
else:
291+
return sandbox_eval(arg, self.curframe.f_globals, self.curframe_locals)
289292
except NameError as e:
290293
self.error(f"NameError: {e}")
291294
return None

src/chatdbg/native_util/safety.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import re
2+
3+
4+
# A very simple whitelist-based approach.
5+
# If ChatDBG wants to call other commands not listed here, they should be
6+
# evaluated and added if not possibly harmful.
7+
def command_is_safe(cmd: str) -> bool:
8+
cmd = cmd.strip()
9+
command_name = cmd.split()[0]
10+
11+
# Allowed unconditionally.
12+
if command_name in [
13+
"apropos",
14+
"bt",
15+
"down",
16+
"frame",
17+
"h",
18+
"help",
19+
"language",
20+
"l",
21+
"list",
22+
"source",
23+
"up",
24+
"version",
25+
]:
26+
return True
27+
28+
# Allowed conditionally.
29+
if command_name in ["p", "print"]:
30+
return re.fullmatch(r"[a-zA-Z0-9_ *]*", cmd) is not None
31+
32+
return False

src/chatdbg/util/config.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def _chatdbg_get_env(
2323
if type(default_value) == int:
2424
return int(v)
2525
elif type(default_value) == bool:
26-
return v.lower() == "true"
26+
return v.lower() == "true" or v.lower() == "1"
2727
else:
2828
return v
2929

@@ -85,18 +85,23 @@ class ChatDBGConfig(Configurable):
8585

8686
format = Unicode(
8787
_chatdbg_get_env("format", "md"),
88-
help="The output format (text or md or md:simple or jupyter).",
88+
help="The output format (text or md or md:simple or jupyter)",
8989
).tag(config=True)
9090

9191
instructions = Unicode(
9292
_chatdbg_get_env("instructions", ""),
93-
help="The file for the initial instructions to the LLM, or '' for the default (possibly-model specific) version.",
93+
help="The file for the initial instructions to the LLM, or '' for the default (possibly-model specific) version",
9494
).tag(config=True)
9595

9696
module_whitelist = Unicode(
9797
_chatdbg_get_env("module_whitelist", ""), help="The module whitelist file"
9898
).tag(config=True)
9999

100+
unsafe = Bool(
101+
_chatdbg_get_env("unsafe", False),
102+
help="Disable any protections against GPT running harmful code or commands",
103+
).tag(config=True)
104+
100105
_user_configurable = [
101106
debug,
102107
log,
@@ -105,6 +110,7 @@ class ChatDBGConfig(Configurable):
105110
no_stream,
106111
format,
107112
module_whitelist,
113+
unsafe,
108114
]
109115

110116
def _parser(self):

0 commit comments

Comments
 (0)