Skip to content

Commit 7948f02

Browse files
committed
accept ALLOWED_COMMANDS as alias of ALLOW_COMMANDS
1 parent 96f52ed commit 7948f02

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,17 @@ pip install mcp-shell-server
7575

7676
```bash
7777
ALLOW_COMMANDS="ls,cat,echo" uvx mcp-shell-server
78+
# Or using the alias
79+
ALLOWED_COMMANDS="ls,cat,echo" uvx mcp-shell-server
7880
```
7981

80-
The `ALLOW_COMMANDS` environment variable specifies which commands are allowed to be executed. Commands can be separated by commas with optional spaces around them.
82+
The `ALLOW_COMMANDS` (or its alias `ALLOWED_COMMANDS` ) environment variable specifies which commands are allowed to be executed. Commands can be separated by commas with optional spaces around them.
8183

82-
Valid formats for ALLOW_COMMANDS:
84+
Valid formats for ALLOW_COMMANDS or ALLOWED_COMMANDS:
8385

8486
```bash
8587
ALLOW_COMMANDS="ls,cat,echo" # Basic format
86-
ALLOW_COMMANDS="ls ,echo, cat" # With spaces
88+
ALLOWED_COMMANDS="ls ,echo, cat" # With spaces (using alias)
8789
ALLOW_COMMANDS="ls, cat , echo" # Multiple spaces
8890
```
8991

src/mcp_shell_server/shell_executor.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ def __init__(self):
1818

1919
def _get_allowed_commands(self) -> set:
2020
"""
21-
Get the set of allowed commands from environment variable.
22-
23-
Returns:
24-
set: Set of allowed command names
21+
Get the set of allowed commands from environment variables.
22+
Checks both ALLOW_COMMANDS and ALLOWED_COMMANDS.
2523
"""
2624
allow_commands = os.environ.get("ALLOW_COMMANDS", "")
27-
return {cmd.strip() for cmd in allow_commands.split(",") if cmd.strip()}
25+
allowed_commands = os.environ.get("ALLOWED_COMMANDS", "")
26+
27+
# Combine and deduplicate commands from both environment variables
28+
commands = allow_commands + "," + allowed_commands
29+
return {cmd.strip() for cmd in commands.split(",") if cmd.strip()}
2830

2931
def _clean_command(self, command: List[str]) -> List[str]:
3032
"""

tests/test_shell_executor.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,39 @@ async def test_command_completes_within_timeout(executor, monkeypatch):
184184
assert result["error"] is None
185185
assert result["status"] == 0
186186
assert result["stdout"] == ""
187+
188+
189+
@pytest.mark.asyncio
190+
async def test_allowed_commands_alias(executor, monkeypatch):
191+
"""Test ALLOWED_COMMANDS alias functionality"""
192+
monkeypatch.setenv("ALLOWED_COMMANDS", "echo")
193+
result = await executor.execute(["echo", "hello"])
194+
assert result["stdout"].strip() == "hello"
195+
assert result["status"] == 0
196+
197+
198+
@pytest.mark.asyncio
199+
async def test_both_allow_commands_vars(executor, monkeypatch):
200+
"""Test both ALLOW_COMMANDS and ALLOWED_COMMANDS working together"""
201+
monkeypatch.setenv("ALLOW_COMMANDS", "echo")
202+
monkeypatch.setenv("ALLOWED_COMMANDS", "cat")
203+
204+
# Test command from ALLOW_COMMANDS
205+
result1 = await executor.execute(["echo", "hello"])
206+
assert result1["stdout"].strip() == "hello"
207+
assert result1["status"] == 0
208+
209+
# Test command from ALLOWED_COMMANDS
210+
result2 = await executor.execute(["cat"], stdin="world")
211+
assert result2["stdout"].strip() == "world"
212+
assert result2["status"] == 0
213+
214+
215+
@pytest.mark.asyncio
216+
async def test_allow_commands_precedence(executor, monkeypatch):
217+
"""Test that commands are combined from both environment variables"""
218+
monkeypatch.setenv("ALLOW_COMMANDS", "echo,ls")
219+
monkeypatch.setenv("ALLOWED_COMMANDS", "echo,cat")
220+
221+
allowed = executor.get_allowed_commands()
222+
assert set(allowed) == {"echo", "ls", "cat"}

0 commit comments

Comments
 (0)